Starter Files
Lab Format
As in Hog Phase 1, we will be using the op
autograder again (and for all future assignments)! You will be able to get realtime feedback and work through your mistakes this way.
Review
Lists
Lists are Python data structures that can store multiple values. Each value can be any type and can even be another list! A list is written as a commma separated list of expressions within square brackets:
>>> list_of_nums = [1, 2, 3, 4] >>> list_of_bools = [True, True, False, False] >>> nested_lists = [1, [2, 3], [4, [5]]]
Each element in a list is assigned an index. Lists are zeroindexed, meaning their indices start at 0
and increase in sequential order. To retrieve an element from a list, use list indexing:
>>> lst = [6, 5, 4, 3, 2, 1] >>> lst[0] 6 >>> lst[3] 3
Often times we need to know how long a list is when we’re working with it. To find the length of a list, call the function len
on it:
>>> len([]) 0 >>> len([2, 4, 6, 8, 10]) 5
Tip: Recall that empty lists, []
, are falsey values. Therefore, you can use an if statement like the following if you only want to do operations on nonempty lists:
if lst: # Do stuff with the elements of list
This is equivalent to:
if len(lst) > 0: # Do stuff
You can also create a copy of some portion of the list using list slicing. To slice a list, use this syntax: lst[<start index>:<end index>]
. This expression evaluates to a new list containing the elements of lst
starting at and including the element at <start index>
up to but not including the element at end index
.
>>> lst = [True, False, True, True, False] >>> lst[1:4] [False, True, True] >>> lst[:3] # Start index defaults to 0 [True, False, True] >>> lst[3:] # End index defaults to len(lst) + 1 [True, False] >>> lst[:] # Creates a copy of the whole list [True, False, True, True, False]
List Comprehensions
List comprehensions are a compact and powerful way of creating new lists out of sequences. The general syntax for a list comprehension is the following:
[<expression> for <element> in <sequence> if <conditional>]
The syntax is designed to read like English: “Compute the expression for each element in the sequence if the conditional is true for that element.”
Let’s see it in action:
>>> [i**2 for i in [1, 2, 3, 4] if i % 2 == 0] [4, 16]
Here, for each element i
in [1, 2, 3, 4]
that satisfies i % 2 == 0
, we evaluate the expression i**2
and insert the resulting values into a new list. In other words, this list comprehension will create a new list that contains the square of each of the even elements of the original list.
If we were to write this using a for statement, it would look like this:
>>> lst = [] >>> for i in [1, 2, 3, 4]: ... if i % 2 == 0: ... lst += [i**2] >>> lst [4, 16]
Note: The if
clause in a list comprehension is optional. For example, you can just say:
>>> [i**2 for i in [1, 2, 3, 4]] [1, 4, 9, 16]
Doing the Assignment
Do the actual coding in the lab05.py
file with Atom! I’m only describing what the problems are below.
What Would Python Display? (WWPD) problems:
As mentioned previously, we will be using the op
autograder again. For these problems, type in what Python would display after “?” .
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”.
If a question will result in a function, put in the string “Function”.
To distinguish between printed logs and outputted strings, add single quotes around your string anwers:
>>> def returns1(): ... return '1' ... >>> print(1) 1 >>> returns1() '1'
Q1: WWPD: Lists
Use Op to test your knowledge with the following “What Would Python Display?” questions:
python3 op q 1 u
For all WWPD questions, type Function
if you believe the answer is <function...>
, Error
if it errors, and Nothing
if nothing is displayed.
>>> lst, hi = [1, 2, 3, 4], 'hello' >>> lst[1] ______ >>> hi[2] ______ >>> lst[1:3] # Put a space after each comma ______ >>> lst[0:4:1] # Put a space after each comma ______ >>> lst[0:4:2] # Put a space after each comma ______ >>> 2 in lst ______ >>> 5 in lst ______ >>> >>> 'hi' in hi ______ >>> 'hell' in hi ______ >>> hi[0:4] + str(lst[0]) ______ >>> for num in lst: ... print(num) ______ ______ ______ ______ >>> for letter in hi: ... print(letter) ______ ______ ______ ______ ______ >>> for i in range(1, 5): ... print(i) ______ ______ ______ ______ >>> for i in range(1,10,2): ... print(i) ______ ______ ______ ______ ______ >>> [0 for i in [1, 2, 3] if i > 2 ] ______ >>> [[e, e+1] for e in [1, 2, 3]] ______ >>> lst = [1, 3, 3] >>> pika = lst.append(7) >>> lst ______ >>> print(pika) ______ >>> pika = lst.insert(0, 0) >>> lst ______ >>> print(pika) ______ >>> pika = lst.remove(3) >>> lst ______ >>> print(pika) ______ >>> pika = lst.pop(2) >>> pika ______ >>> lst ______ >>> lst2 = [0,1,7] >>> lst2 == lst ______ >>> lst2 is lst ______ >>> lst3 = lst >>> lst3 == lst ______ >>> lst3 is lst ______ >>> d = {'a': 1, 'b': 2, 'c': 3} >>> d['a'] ______ >>> d[1] ______ >>> d['hi'] = 4 >>> d['hi'] ______ >>> d['hi'] = 'hi' >>> d['hi'] ______ >>> d[1] = 'hi' >>> d[1] ______ >>> d[1337] = [1, 2, 3] >>> d[1337].pop(0) ______
Coding Practice
Free Response Questions (FRQs)
Q1. If this not that
def if_this_not_that(): """Define a function which takes a list of integers `i_list` and an integer `this`. For each element in `i_list`, print the element if it is larger than `this`; otherwise, print the word "that". >>> original_list = [1, 2, 3, 4, 5] >>> if_this_not_that(original_list, 3) that that that 4 5 """ "*** YOUR CODE HERE ***" # Your code goes here
Q2. Factors List
def factors_list(): """Return a list containing all the numbers that divide `n` evenly, except for the number itself. Make sure the list is in ascending order. >>> factors_list(6) [1, 2, 3] >>> factors_list(8) [1, 2, 4] >>> factors_list(28) [1, 2, 4, 7, 14] """ all_factors = [] "*** YOUR CODE HERE ***"
You’re done! Upload your screenshot here!
Screenshot for completion
Upload files






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