Relative Reassignment Operators
Refer to the following code snippet to answer the questions below.
def change_number(number: int, modifier: int) -> int: idx: int = 0 end: int = 3 while idx < end: number __ modifier idx += 1 return number print(change_number(16, 3))1.1. What would be the output if we ran this code and
__was replaced with+=?1.2. What would be the output if we ran this code and
__was replaced with-=?1.3. What would be the output if we ran this code and
__was replaced with*=?What would you put in the blank spaces (marked with
_’s) in the code snippet below to make this awhileloop that prints the powers of three (1, 3, 9, …) less than 1000? (Your answer should fit in the blanks)idx: int = _ end: int = 1000 while idx < end: print(idx) idx __ _
SHOW SOLUTIONS
Answers:
1.1.
251.2.
71.3.
432Here is the completed code snippet:
idx: int = 1 end: int = 1000 while idx < end: print(idx) idx *= 3
Lists and while loops
How do you modify an element located at a specific location within a
list? Give an example.The following questions will refer to the list below:
python my_list: list[int] = list()2.1. Write line(s) of code that would add the number8,0,3, and-1to the list.2.2. Write line(s) of code that removes
3from the list.2.3. Write line(s) of code that assigns the variable
dogto the element at the second index.2.4. Write line(s) of code that prints the amount of items in the list.
2.5. Change the value
8to0.2.6. We now have a function,
sumthat adds the elements of my_list and returns this amount. Write a line of code that instantiates alist[int]with the first value returned from callingsumonmy_list.The following questions will refer to the
animalslist below. Commas are used in place of new lines for the sample outputs.animals: list[str] = ["dog", "cat", "mouse", "bird", "whale"]3.1. What will print from the following code:
for x in range(1, len(animals)): print(animals[x])cat, mouse, bird, whaledog, cat, mouse, bird1, 2, 3, 4An error will occur when this code is run.
3.2. What will print from the following code:
for x in range(1, len(animals) - 1): print(x)1, 2, 3, 4cat, mouse, bird1, 2, 3An error will occur when this code is run.
3.3. What will print from the following code:
for animal in animals: print(animal)dog, cat, mouse, bird, whaledog, cat, mouse, bird0, 1, 2, 3, 4An error will occur when this code is run.
3.4. What will print from the following code:
for x in animals: print(animals[x])0, 1, 2, 3, 4dog, cat, mouse, bird, whale1, 2, 3, 4An error will occur when this code is run.
3.5. What will print from the following code:
x: int = 0 while x < len(animals): print(x) x += 20, 1, 2, 3, 4dog, cat, mouse, bird, whale0, 2, 4An error will occur when this code is run.
3.6. What will print from the following code:
x: int = len(animals) - 1 while x >= 0: print(animals[x]) x -= 14, 3, 2, 1, 0whale, bird, mouse, cat, dogbird, mouse, cat, dogAn error will occur when this code is run.
3.7. What will print from the following code:
for x in range(1, len(animals) - 1, 2): print(x)1, 3, 4cat, mouse, bird1, 3An error will occur when this code is run.
3.8. What will print from the following code:
x: int = len(animals) - 1 while x >= 0: print(animals[x]) print(x) x -= 24, 2, 0dog, 0, mouse, 2, whale, 4whale, 4, mouse, 2, dog, 0An error will occur when this code is run.
SHOW SOLUTIONS
Lists in Python are ordered, 0-indexed collections of values, which means each element has a specific index, starting from 0 and going to the length of the list minus one. You can access elements in a list using these indices via subscription notation, which means using the index within square brackets
[]next to the name of the list (list_name[index]). Then you can modify this element using the assignment operator=with the new value you want to assign to that place in the list to the right of the assignment operator (list_name[index] = new_value).For example:
my_list: list[str] = [“bark”, “meow”, “tweet”]In this example, if we wanted to change “meow”, which is at index 1, to “moo”, we would use the square brackets for subscription notation, assigning a new value at that index like this:
my_list[1] = “moo”List Manipulations
my_list: list[int] = list() # 2.1. Add the numbers 8, 0, 3, and -1 to the list. my_list.append(8) my_list.append(0) my_list.append(3) my_list.append(-1) # 2.2. Remove the number 3 from the list. my_list.pop(2) # 2.3. Assign the element at the second index to a variable named 'dog'. dog = my_list[2] # 2.4. Print the number of items in the list. print(len(my_list)) # 2.5. Change the value 8 to 0. my_list[0] = 0 # 2.6. Instantiate a list[int] with the first value returned from calling sum on my_list. summed_list = [sum(my_list)]The example list
animalsis repeated below for convenience.animals: list[str] = ["dog", "cat", "mouse", "bird", "whale"]3.1. (a)
cat, mouse, bird, whale3.2. (c)
1, 2, 33.3. (a)
dog, cat, mouse, bird, whale3.4. (d) An error will occur when this code is run. (An error will occur because we are attempting to use the string values of the list as indices in subscription notation, which will not work.)
3.5. (c)
0, 2, 43.6. (b)
whale, bird, mouse, cat, dog3.7. (c)
1, 33.8. (c)
whale, 4, mouse, 2, dog, 0
Sets
Sets in Python are ordered collections of data with values that aren’t necessarily unique (T/F).
Syntax Practice: How would you declare a set named
classesof the strings"110","210", and"311"? After this line, add two lines that add makes the conditionclasses == {"110", "311", "455"}beTrue.
SHOW SOLUTIONS
False, sets in Python both have unique values and are unordered.
Answer code:
classes: set[str] = {"110", "210", "311"} classes.add("455") classes.remove("210")
Dictionaries and for loops
Refer to the following code snippet to answer these questions:
py stats: list[int] = [7, 8, 9] index: int = 0 total: int = 100 while index < len(stats): total -= stats[index] index += 11.1. Rewrite the following code snippet with the same functionality using a
for ... inloop.1.2. Rewrite the following code snippet with the same functionality using a
for ... in range(...)loop.Dictionaries in Python can have duplicate keys. (T/F)
What act as your indices in a dict? How can you tell what the type of these indices are? How can you tell what the type of the values are?
Given the following code, answer the following question:
tarheels_numbers: dict[str, int] = {"Wilson": 8, "Trimble": 7, "Veesaar": 13, "Evans": 0, "Bogavac": 44}Coach Hubert Davis needs your help, Luka Bogavac is exhausted and needs to be substituted out for Freshman Derek Dixon who wears the jersey number 3. However, Hubert Davis can’t remember how to add key-value pairs to a dictionary or how to remove them. How would you write two lines of code to substitute Derek Dixon for Luka Bogavac? (Make sure there’s still only five players in the dictionary at the end!)
The following questions will refer to the dictionary below:
my_dict: dict[int, str] = {}5.1. Write line(s) of code that would add the following key-value pairs to the dictionary:
8: 'eight'0: 'zero'3: 'three'-1: 'negative one'
5.2. Write line(s) of code that removes value
three.5.3. Write line(s) of code that assign the value associated with the key
0inmy_dictto a variable calledcat.5.4. Write line(s) of code that print the number of keys in
my_dict.5.5. Write line(s) of code that print the number of values in
my_dict.5.6. Change the value associated with the key
8to'zero'.5.7. Suppose we have a function
sum_dict_keysthat sums the keys ofmy_dictand returns this amount. Write a line of code that instantiates adict[str, int]containing a single value, which is the result of callingsum_dict_keys(my_dict)and a key of “returned_amount”.The following questions will refer to the dictionary below:
my_dict: dict[int, str] = {10: "dog", 20: "cat", 100: "mouse", 50: "bird", 5: "whale"}6.1. What will print from the following code:
for x in my_dict: print(my_dict[x])dog, cat, mouse, bird, whaledog, cat, mouse, birdIndexOutOfRangeAn error will occur when this code is run.
6.2. What will print from the following code:
for x in my_dict: print(x)dog, 10, cat, 20, mouse, 100, bird, 50, whale, 5dog, cat, mouse, bird, whale10, 20, 100, 50, 5An error will occur when this code is run.
The following questions will refer to the dictionary below:
tarheels_numbers: dict[str, int] = {"Wilson": 8, "Trimble": 7, "Veesaar": 13, "Evans": 0, "Bogavac": 44}7.1. What will print from the following code:
for x in range(0, len(tarheels_numbers)): print(tarheels_numbers[x])0, 1, 2, 3, 4Wilson, Trimble, Veesaar, Evans, Bogavac8, 7, 13, 0, 44An error will occur when this code is run.
7.2. What will print from the following code:
for x in range(0, len(tarheels_numbers)): print(x)0, 1, 2, 3, 4Wilson, Trimble, Veesaar, Evans, Bogavac8, 7, 13, 0, 44An error will occur when this code is run.
7.3. What will print from the following code:
for x in tarheels_numbers: print(tarheels_numbers[x])0, 1, 2, 3, 4Wilson, Trimble, Veesaar, Evans, Bogavac8, 7, 13, 0, 44An error will occur when this code is run.
7.4. What will print from the following code:
for x in tarheels_numbers: print(x)0, 1, 2, 3, 4Wilson, Trimble, Veesaar, Evans, Bogavac8, 7, 13, 0, 44An error will occur when this code is run.
7.5. What will print from the following code:
x: int = 0 while x < int(len(tarheels_numbers) / 2): print(x) x += 10, 1Wilson, Trimble8, 7An error will occur when this code is run.
Create a new dictionary called
my_dictionarywithstrkeys andfloatvalues and initialize it as an empty dictionary.Using the following dictionary,
msg: dict[str, int] = {"Hello": 1, "Yall": 2}, access the value stored under key “Yall”.Using the following dictionary,
msg: dict[str, int] = {"Hello": 1, "Yall": 2}, increase the value stored under key “Yall” by 3.Using the following dictionary,
ids: dict[int, str] = {100: "Alyssa", 200: "Carmine"}, remove the value “Alyssa”, stored at key 100.Using the following dictionary,
ids: dict[int, str] = {100: "Alyssa", 200: "Carmine"}, write a line of code to get the number of key/value pairs in the dictionary.Using the following dictionary,
inventory: dict[str, int] = {"pens": 10, "notebooks": 5, "erasers": 8}, add a new key-value pair"markers": 15.Using the following dictionary,
prices: dict[str, float] = {"bread": 2.99, "milk": 1.99, "eggs": 3.49}, update the value of"milk"to2.50.Using the dictionary
scores: dict[str, int] = {"Alice": 85, "Bob": 90, "Charlie": 88}, print out all the keys in the dictionary.Using the dictionary
fruit_count: dict[str, int] = {"apples": 5, "bananas": 8}, iterate over the key-value pairs and print them in the format “key: value”.
SHOW SOLUTIONS
Original code copied for reference:
stats: list[int] = [7, 8, 9] index: int = 0 total: int = 100 while index < len(stats): total -= stats[index] index += 11.1. With a
for ... inloop:stats: list[int] = [7, 8, 9] total: int = 100 for elem in stats: total -= elem1.2. With a
for ... in range(...)loop.stats: list[int] = [7, 8, 9] total: int = 100 for index in range(0, len(stats)): total -= stats[index]False. Python dictionaries cannot have duplicate keys. Each key in a dictionary must be unique. If you attempt to create a dictionary with duplicate keys, the latest key-value pair will overwrite the previous one. For example:
my_dict = {"a": 1} my_dict["a"] = 2 print(my_dict) # Output: {'a': 2}In a Python dictionary, the keys act as the “indices.” Unlike lists, where numerical indices are used to access elements, dictionaries use keys to access values and these keys can be of any type. You can see the type of both the keys and the values from the type declaration of the dictionary, as the first type within the square brackets
[]will be the key type and the second will be the value type. For example:tarheels_numbers: dict[str, int] = {"Wilson": 8, "Trimble": 7, "Veesaar": 13, "Evans": 0, "Bogavac": 44}In this example, the first type listed in the square brackets is
strand the second type isint, thus the keys are of typestrand the values are of typeint.Here’s how you can help Coach Davis (either order of these two lines is fine):
tarheels_numbers.pop("Bogavac") tarheels_numbers["Dixon"] = 3Dictionary Manipulations
my_dict: dict[int, str] = {} # 5.1. Add key-value pairs to the dictionary. my_dict[8] = 'eight' my_dict[0] = 'zero' my_dict[3] = 'three' my_dict[-1] = 'negative one' # 5.2. Remove the key-value pair where the value is 'three'. my_dict.pop(3) # recall that pop takes an index and in the case of dictionaries or indicies are essentially our keys # 5.3. Assign the value associated with the key 0 to a variable called 'cat'. cat = my_dict[0] # 5.4. Print the number of keys in the dictionary. print(len(my_dict)) # 5.5. Print the number of values in the dictionary. print(len(my_dict)) # 5.6. Change the value associated with the key 8 to 'zero'. my_dict[8] = 'zero' # 5.7. Instantiate a dict[str, int] with the key 'returned_amount' and the value from sum_dict_keys(my_dict). result_dict = {'returned_amount': sum_dict_keys(my_dict)}The following answers refer to the dictionary below:
tarheels_numbers: dict[str, int] = {"Wilson": 8, "Trimble": 7, "Veesaar": 13, "Evans": 0, "Bogavac": 44}6.1. (a)
dog, cat, mouse, bird, whale6.2. (c)10, 20, 100, 50, 5The following answers refer to the dictionary below:
tarheels_numbers: dict[str, int] = {"Wilson": 8, "Trimble": 7, "Veesaar": 13, "Evans": 0, "Bogavac": 44}7.1. (d) An error will occur when this code is run. (
rangeandwhileloops are generally incompatible with dictionaries as unlike lists, they are not indexed by sequential integers beginning at 0. In this caserangeproduces invalid keys since our keys are of typestr.)7.2. (a)
0, 1, 2, 3, 47.3. (c)
8, 7, 13, 0, 447.4. (b)
Wilson, Trimble, Veesaar, Evans, Bogavac7.5. (a)
0, 1. (rangeandwhileloops are generally incompatible with dictionaries as unlike lists, they are not indexed by sequential integers beginning at 0. In this case awhileloop that increments an indexing variablexwould produce invalid keys since our keys are of typestr, but we don’t use them as keys so no error occurs.)my_dictionary: dict[str, float] = {}ormy_dictionary: dict[str, float] = dict()msg["Yall"]msg["Yall"] += 3ormsg["Yall"] = 5ids.pop(100)len(ids)inventory["markers"] = 15prices["milk"] = 2.50Code below:
for x in scores: print(x)Code below:
for boo in fruit_count: ghost = fruit_count[boo] print(f"{boo}: {ghost}")
Unit Tests (Conceptual Practice Only)
Testing Syntax:
1.1. What key word do we use within a test function to ensure that a boolean expression is true?
1.2. What does each test function’s name have to begin with?
1.3. What does each test file’s name have to end with?
If a function passes all of its associated unit tests, then the function is implemented correctly for all possible inputs (True/False).
What is the difference between testing a use case versus an edge case of a function in general?
Testing an edge case is testing a function when it is given expected or normal inputs, while testing a use case is testing a function when it is given unexpected or incorrect inputs to a function, very often an “empty” input.
Testing an edge case is testing a function when it is given unexpected or incorrect inputs to a function, very often an “empty” input, while testing a use case is testing a function when it is given expected or normal inputs.
Testing an edge case is when you test what happens when a function is given an empty input, while testing a use case is when you test what happens when a function is given nonempty input.
None of the above.
Consider the following code snippet:
def fill_list(num: int, length: int) -> list[int]:
"""Fill a list with a single value."""
result: list[int] = []
i: int = 0
while i < length:
result.append[num]
i += 1
return result
list_A: list[int] = fill_list(4, 19)
list_B: list[int] = fill_list(55, -2)
list_C: list[int] = fill_list(1, 110)Which function calls would be considered a use case of this function (list the associated variable name e.g. list_A)? Which would be considered edge cases? If there are any edge cases, what result would you get in the associated variable(s)?
SOLUTIONS
Question 1 Answers:
1.1.
assert1.2.
test_1.3.
_test.pyThis is False, as unit tests themselves can be incorrect so all tests passing is no guarantee of correctness even for the inputs the unit tests are testing for. Even if the unit tests are correct, there can still be certain inputs that they do not test for and therefore the unit tests cannot assure you that a function will always work properly. Unit tests are a helpful tool that can work well when implemented over a wide range of test inputs, but they must be accompanied by thoughtful implementation of the original function.
- Testing an edge case is testing a function when it is given unexpected or incorrect inputs to a function, very often an “empty” input, while testing a use case is testing a function when it is given expected or normal inputs.
The reason it is not answer choice c is because while empty vs nonempty inputs is often a distinction between edge cases and use cases, this is not true in all cases (some functions could expect empty inputs, for example).
list_Aandlist_Cwould be use cases since this is how we would expect this function to be used andlist_Bwould be an edge case as this is essentially attempting to make a function call that would construct a list of negative length since ourlengthargument is -2. In this edge case the result would be an empty list since we would never enter thewhileloop. It is possible that this is the intended behavior, or that maybe the original programmer never thought anyone would give a negative argument forlength.