Lecture 3: Control
Pure Functions vs Non-Pure Functions
Pure Functions
- Functions that only return values.
Example
An example of a pure function is abs(x) and pow(b, e).
Non-Pure Functions
- Functions that have side effects.
Example
An example of a non-pure function is print(), which displays output and returns None.
Life Cycle of a User Defined Function
Definition Lifecycle
- A new function is created.
- A name is bound to the function within the current frame.
Call Expression Lifecycle
- Operator and operands evaluate.
- Function called on arguments.
Calling and Applying Lifecycle
- A new frame is created.
- Parameters bound to arguments.
- Body is executed in that new environment.
Naming Frames
- Frames are named with the $fN$ format, where $N$ is the frame identifier (ID).
Environment & Frames
- Environment is a sequence of frames.
- Global frame is the initial frame.
- Search for variables/names starts from the local frame, to parent, then n parent, and continues all the way to the global frame.
Miscellaneous Python Features
Infix Functions
- A Kotlin reference to using symbols to "call" functions.
Division
- Regular division will lead to floats.
- Floor division (
//) will round down regardless.- Floor division by 10 will truncate the last number.
- Modulo (
%) will provide the remainder.- Opposite of floor division: when you modulo by 10, it gives you the last number.
- Using floor and modulo results in answers in the format x remainder y.
Multiple Return Values
- Utilize tuples for returning multiple values.
Docstrings/Tests
- Use triple quotes, similar to
kdocin Kotlin. - Doc tests provide an interpreter allowing you to write expected outputs.
- Use
>>>to show input, then present the expected output on the subsequent line.
- Use
Default Arguments
Statements
- These are executed by the interpreter to perform a specific action.
Statement Headers
- The header determines the statement's type.
- Controls the subsequent suite.
- Suites are sequences of statements.
- Suites execute from top to bottom unless directed otherwise.
Execution Rule of If-Statements
- Evaluate the header expression.
- If true, execute the suite and skip the subsequent clauses.
Boolean Context
False Values
- Examples:
False,0,"",None, etc.
Truth Values
- Basically, any other value not listed as a False value.
While Statements
- If the condition evaluates to true, the suite is executed, and then the condition is checked again.
- If the condition is false, the loop exits.