Functions and Math
Math Review: What is a Function?
function has three anatomical parts A set D of allowable inputs, called the domain A set R of allowable outputs, called the codomain. A rule f associating each element of the domain with some element in the codomain. functions are consistent: the same input yields teh same output every time. len is a mathematical function domain: sequences. codomain: integer Mrs wormwood says f(x) = 2*x + 5 domain = codomain = real numbers. f(x) = 1/x domain: all numbers except 0.
Many Python Functions aren't Math Functions
Mathematical functions must be consistent. Mathematical functions have no side-effect.
The Three Traits of Functions.
- Preconditions: What must be true for it to make sense for a function to be called? This includes the number and types of arguments, or anything else that should be true for a function to be called.
- Postconditions: This includes two major items.
- the return value
- any side-effects of a function
What is a "pure" function? This is a matheamtical function.
Boss Statements: Conditional Logic
Truthy and Falsy (go to :30 to skip
the ad) An object is
truthy, if, when cast to a boolean, it returns
True
. An object is
falsy, if, when cast to a boolean, it returns
False
. Let us do a quick review of the
situaion so we know what to expect.
Numbers
Strings
Lists and Tuples
We will use the term predicate for an expression or function that returns a truthy value.
We will learn about three new boss statements. Here they are.
if
This requires a predicate.elif
This requires a predicate. It must immediately follow anif
and its block.else
This is a stand-alone statement It must immediately follow anif
or anelif
and its block.
Getting information into a Program
Today, let us discuss the function input
and
sys.argv
.