Control
If statement
Alfonso will only wear a jacket outside if it is below 60 degrees or it is raining.
Write a function that takes in the current temperature and a boolean value telling
if it is raining and returns True if Alfonso will wear a jacket and False otherwise.
First, try solving this problem using an if statement.
1 | def wears_jacket_with_if(temp, raining): |
Note that we’ll either return True or False based on a single condition, whose
truthiness value will also be either True or False. Knowing this, try to write this
function using a single line.
1 | def wears_jacket(temp, raining): |
While loop
Q1: What is the result of evaluating the following code?
1 | def square(x): |
Infinite loop because x will always be greater than 0; the num / 0 is never executed.
Q2: Write a function that returns True if n is a prime number and False otherwise. After
you have a working solution, think about potential ways to make your solution more
efficient.
Hint: use the % operator: x % y returns the remainder of x when divided by y.
1 | from math import sqrt |
Environment diagram
An environment diagram keeps track of all the variables that have been defined
and the values they are bound to. We will be using this tool throughout the course
to understand complex programs involving several different objects and function
calls.
Remember that programs are simply a set of statements, or instructions, so drawing
diagrams that represent these programs also involve following sets of instructions!
Let’s dive in.
Assignment Statement
Assignment statements, such as x = 3, define variables in programs. To execute
one in an environment diagram, record the variable name and the value:
- Evaluate the expression on the right side of the = sign
- Write the variable name and the expression’s value in the current frame
Q1: Use these rules to draw a simple diagram for the assignment statements below.
1 | x = 10 % 4 |
def Statements
def statements create function objects and bind them to a name. To diagram def
statements, record the function name and bind the function object to the name.
It’s also important to write the parent frame of the function, which is where the
function is defined.
- Draw the function object to the right-hand-side of the frames, denoting the
intrinsic name of the function, its parameters, and the parent frame (e.g. func
square(x) [parent = Global]. - Write the function name in the current frame and draw an arrow from the
name to the function object.
Q2: Use these rules and the rules for assignment statements to draw a diagram for the
code below.
1 | def double(x): |
Call Expressions
Call expressions, such as square(2), apply functions to arguments. When executing
call expressions, we create a new frame in our diagram to keep track of local
variables:
- Evaluate the operator, which should evaluate to a function.
- Evaluate the operands from left to right.
- Draw a new frame, labelling it with the following: 1
- A unique index (f1, f2, f3, …)
- The intrinsic name of the function, which is the name of the function
object itself. For example, if the function object is func square(x)
[parent=Global], the intrinsic name is square. - The parent frame ([parent=Global])
- Bind the formal parameters to the argument values obtained in step 2 (e.g.
bind x to 3). - Evaluate the body of the function in this new frame until a return value is
obtained. Write down the return value in the frame.
If a function does not have a return value, it implicitly returns None. In that case,
the “Return value” box should contain None.
Q3: Let’s put it all together! Draw an environment diagram for the following code.
1 | def double(x): |
Q4: Draw the environment diagram that results from executing the code below. What
will be displayed when running the code (note this separately from the diagram)?
1 | from operator import add |
Print output: 0