Starter Files
New lab format!
We’ve improved the WWPD part of the lab. Now, you can get real time feedback.
To run the WWPD part of the lab, use the following command in the terminal:
python3 wwpd.py
Then, write what Python would display, and hit enter to check your answer:
If it’s incorrect, the terminal shows Incorrect
. Keep trying until you get it right!
But, to check your FRQ questions, please still use:
python3 lab03.py
If the FRQ questions all pass the tests and you’ve completed all your WWPD questions, the terminal will output that you’ve completed everything. Screenshot that and upload it.
Review + Some extras
In Lecture 03, we learned a new datatype: None!
1. None, pure, and nonpure functions
Remember that None just means that nothing is returned; it is not displayed by the interpreter either.
Here are some examples:
>>> None >>> print(None) None >>> not None True >>> if None: ... print(1) ... else: ... print(2) ... 2
Next, we learned about pure functions and nonpure functions.
 Pure functions return values
 Nonpure functions have sideeffects and returns None
The difference between print and return is that printing is a sideeffect of the print() function; it actually returns None. return, on the other hand, allows values to be passed into and output out of functions.
Here are some examples:
>>> def square(x): ... return x**2 #Pure function because it returns a value ... >>> square(2) 4 #The return value >>> x = print(2) #print() is a nonpure function 2 #print(2) first gets evaluated so 2 gets displayed as output. This is NOT the return value; it is just a side effect of running print() >>> print(x) None #Now we can see that print(2) evaluated to None, then binded to x because it was a nonpure function
2. “Falsey” values, “Truthy” values, boolean expressions, and conditional statements
In Python, there are values that evaluate to False—”Falsey” values—and values that evaluate to Truth—”Truthy” values.
There will be more to come, but for now, just know the following:
 “Falseyy”: 0, None, “”, [], False
 “Truthy”: everything else
Here are some examples:
>>> bool(0) #bool() gives the boolean context of the input False >>> bool("") False >>> bool(1) True >>> bool(True) True >>> bool("CSS @ SAS") True
There are also many boolean expressions:
 and: Evaluates to the first false value. If none are false, evaluates to the last expression
 or: Evaluates to the first true value. If none are true, evaluates to the last expression
 not: Flips the boolean context of a value.
Also, remember shortcircuiting—when an expression returns the value and stops evaluating latter expressions.
Here are some examples:
>>> True and True and True True >>> True and True and False False >>> True and False and 1 / 0 False #This actually doesn't error because it stops being evaluated at the first False value! >>> False or False or False False >>> "Hello" or False or 1 / 0 True #This also doesn't error because it stops being evaluated at the first True value—"Hello" in this case! >>> not(0) True #0 holds a "Falsey" value >>> not(1) False #1 holds a "Truthy" value
Also remember these can be used in combination with conditional statements if, elif, and else:
""" if <conditional expression>: <suite of statements> elif <conditional expression>: <suite of statements> else: <suite of statements> Always starts with if clause Zero or more elif clauses Zero or one else clause, always at the end """ >>> if 0: #False ... print(1) ... elif not(True): #False ... print(2) ... elif "Yo": #True so stops here ... print(3) ... else: #Never gets evaluated because it stops in the preceding elif ... print(1 / 0) ... 3
3. Iteration
We use iteration when we want to repeat a process.
 while loop needs a stop condition and must increment towards that stop condition
 for loop requires a range (start, end, increment) to iterate through
Here are some examples:
>>> i = 0 >>> while i < 5: #The stop condition ... print(i) ... i = i + 1 #Increments i towards 5, the stop condition ... 0 1 2 3 4 >>> for i in range(0, 5): #Iterates from 0 (inclusive) to 5 (exclusive) ... print(i) ... 0 1 2 3 4
Doing the Assignment
Do the actual coding in the lab03.py
file with Atom! I’m only describing what the problems are below.
What Would Python Display? (WWPD) problems:
Remember that all the variables defined in the fill in the blank sections below are in the same frame! So, for instance, Q2 might use a variable defined in Q1.
If a question will result in an error, put in the string “Error”.
If a question will result in None, put in None.
If a question will result in no output, put in “Nothing”.
— Section 1 —
#Q1. #print(print(1)) #Q2a. #True and 1 / 0 and False #Q2b. #True or 1 / 0 or False #Q2c. #True and 0 #Q2d. #False or 1 #Q2e. #1 and 3 and 6 and 10 and 15 #Q2f. #0 or False or 2 or 1 / 0 #Q3 def how_big(x): if x > 10: print('huge') elif x > 5: return 'big' elif x > 0: print('small') else: print("nothin'") #Q3a. how_big(7) #Q3b. how_big(12) #Q3c. how_big(1) #Q3d. how_big(1)
Free Response Questions (FRQs)
#Q1 """Using the following definitions of if_function() and with_if_statement(), complete the YOUR CODE HERE segments to satisfy the doctests in if_function() and with_if_statement(). """ def if_function(condition, true_result, false_result): """Return true_result if condition is a true value, and false_result otherwise. >>> if_function(True, 2, 3) 2 >>> if_function(False, 2, 3) 3 >>> if_function(3==2, 3+2, 32) 1 >>> if_function(3>2, 3+2, 32) 5 """ if condition: return true_result else: return false_result def with_if_statement(): """ >>> result = with_if_statement() 2 >>> print(result) None """ if c(): return t() else: return f() def with_if_function(): """ >>> result = with_if_function() 1 2 >>> print(result) None """ return if_function(c(), t(), f()) def c(): "YOUR CODE HERE" def t(): "YOUR CODE HERE" def f(): "YOUR CODE HERE" #Q2 def a_plus_abs_b(a, b): """Return a+abs(b), but without calling abs. >>> a_plus_abs_b(2, 3) 5 >>> a_plus_abs_b(2, 3) 5 """ "YOUR CODE HERE" #Q3 def largest_factor(n): """Return the largest factor of n that is smaller than n. >>> largest_factor(15) # factors are 1, 3, 5 5 >>> largest_factor(80) # factors are 1, 2, 4, 5, 8, 10, 16, 20, 40 40 >>> largest_factor(13) # factor is 1 since 13 is prime 1 """ "YOUR CODE HERE"
You’re done! Upload your screenshot here!
Upload files






If you encounter any issues, please contact Leo, Eugene, or John through Wechat. Thanks!