Lecture 3 Control
- Question
- In what case is the parent not global?
- What is the difference between a statement and an expression?
-
None Value
- Print doesn’t include quotes in string
print(print(1), print(2)) ''' Evaluates to: 1 2 None None '''
- A function that does not explicitly return a value will return None
- None represents nothing in Python
- interpreter will not display None as the value of an expression
- “print” will display None
- Pure Functions & Non-Pure Functions
- Pure functions
- just return values
- Non-Pure Functions
- have side effects
- Print is a non-pure function
- Python displays the output “-2”
- Nested Expressions with Print
- prints 1 and 2 as a side effect
- passes in None, into the outside print statement
- Print will separate things in commas with a space
- Multiple Environments
- Life cycle of a User-Define Function
- Def statement
- Name, formal parameter, body, return expression
- What happens
- A new function is created
- name bound to that function in the current frame
- Call expression
- operator, function, operand
- What happens
- Operator and operands evaluated
- Function called on arguments
- Calling/Applying a Function
- A new frame is created
- Parameters bound to arguments
- Body is executed in that new environment
- Def statement
- Multiple Environments in One Diagram
- Green arrow: code that was previously evaluated
- Red arrow: next line that will be executed
- An environment is a sequence of frames
- global frame alone
- local, then global frame
- Every expression is evaluated in the context of an environment
- square is defined in the global frame, so its parent is in the global frame
- Names have different meanings in Different Environments
- A call expression and the body of the function being called are evaluated in different environments
- Misc Python Features
- Can use call expressions as operators
- from operator import mul, add,
- Multiple return values
- return x, y
- quotient, remainder = divide_exact(2023, 10)
- Doctest
- an example case in the docstring showing how program works
- Default arguments
- def divide_exact(n, d=10)
- you are able to only pass in one argument, d will equal 10
- you are able to override by putting in 2 arguments
- def divide_exact(n, d=10)
- Conditional Statements
- Statement
- executed by the interpreter to perform an action
- Compound statements
- The first header determines a statement’s type
- header of a clause “controls” the suit that follows
- Execution rule
- execute the first statement
- unless otherwise directed, execute the rest
- Execution rule for conditional statements
- evaluate the header’s expression
- If it is a true value, execute the suite and skip the remaining clauses
- Boolean Contexts
- False values: False, 0, ‘ ’, None
- True values: Anything else (True)
- One suite per conditional statement
- if, elif, else is all part of one conditional statement
- Control Expressions
- Evaluate the expression “left” and “right
- evaluate the subexpression left
- If it evaluates to a false-y value, then the expression evaluates to
- greedy, it was everything to be true
- Otherwise, the expression evaluates to
- Examples
- True and 5 → 5
- Evaluate left or right
- Evaluate the subexpression
- If it evaluates to a truth value, v, the expression evaluates to v
- Returns the first falsey value, or the last truthy value
- Otherwise, the expression evaluates to
- Evaluate the subexpression
- Evaluates not expression
- Evaluate the subexpression
- If it evaluates to a truth-y value, the expression evaluates to False
- Otherwise, the expression evaluates to True
- Examples
- not True → False
- not (None or 0) → True
- Iteration
- While statements
- Evaluate the header’s expression
- If it is a true value, execute the whole suite, then return to step 1
- Summary
- Pure and non pure functions
- print displays a value as a side effect
- Multiple environments can exist in a diagram
- Fllordiv, truediv, and mod are used in a boolean context
- using while statements for iteration