ls_numbers = []
for n in [1, 2, 3]:
ls_numbers = [n] + ls_numbers
# Remember you can add a print to see what is happening after every loop!
# print(f"n = {n} in this loop")
# print("After this loop the list is:", ls_numbers)
print(ls_numbers)Review: Lists & Tuples
Module 2: Data Structures
Review of Session 2.1: Lists & Tuples
After running this cell, what will be the contents of ls_numbers?
After running this cell, what will be the contents of ls_numbers?
ls_numbers = []
for n in "one, two, three":
ls_numbers.append(n)
print(ls_numbers)After running this cell, what will be the contents of ls_numbers?
ls_numbers = []
for n in range(2, 11, 2):
if n == 6:
continue
ls_numbers.append(n)
print(ls_numbers)After running this cell, what will be the contents of ls_numbers?
ls_numbers = []
for n in range(10):
ls_numbers.clear()
ls_numbers.append(n)
print(ls_numbers)What will be the output of the following cell?
ls_boardgames = ["Monopoly", "Catan", "UNO"]
print(ls_boardgames[1][0])::: {.callout-tip collapse=“true”} ## Solution In case of doubt, think about it one pair of brackets at a time:
:::
# First brackets
output_1 = ls_boardgames[1]
print(output_1)
# Second brackets
output_2 = output_1[0]
print(output_2)Remember this works the same for tuples!
What will be the output of the following cell?
tp_videogames = ("Pokemon", "Candy Crush", "FIFA", "Outer Wilds")
print(tp_videogames[1:3][1])What will be the output of the following cell?
tp_superheroes = ("Batman", "Ironman", "Superman")
print(tp_superheroes[2[1:4]])Which line is going to fail?
tp_colors = ("Blue",)
tp_colors = ("Green",) + tp_colors
tp_colors += ("Red,")
tp_colors.append("Yellow")
print(tp_aliens)Consider the following code. What will be the output?
numbers = [0, 1, 2, 3]
numbers = numbers.append(4)
print(numbers)The answer is “None”. The append method modifies the list in-place and returns None. So, after this operation, the numbers list is modified to [0, 1, 2, 3, 4], and the result of numbers.append(4) is None.
If you want to print the modified list, you should do it in two steps:
numbers = [0, 1, 2, 3]
numbers.append(4)
print(numbers)After run this code, what will be the value of ls?
ls = ["Lucy", "Jack", "Alberto"]
ls.sort()
ls.insert(2, "Ben")
print(ls)After run this code, what will be the value of ls?
ls = ["Lucy", "Jack", "Alberto"]
ls.insert(2, "Ben")
ls.sort()
print(ls)After run this code, what will be the value of x?
ls = ["Lucy", "Jack", "Alberto"]
ls.insert(2, "Ben")
x = ls.sort()[0]
print(x)Tuples
Consider the following code. What will be the expected output?
tp_numbers = (4, 2, 3)
print(tp_numbers)Consider the following code. What will be the expected output?
tp_numbers = (4, 2, 3)
tp_numbers[0] = 0
print(tp_numbers[0])Consider the following code. What will be the expected output?
tp_numbers = (4, 2, 3)
print(2 * tp_numbers)Consider the following code. What will be the expected output?
ls_names = ["Raul", "Ana", "Oscar"]
tp_names = tuple(ls_names)
print(tp_names[2])Consider the following code. What will be the expected output?
tp_numbers = (5, 2)
tp_names = ("Raul", "Ana", "Oscar")
print(tp_numbers + tp_names)Consider the following code. What will be the expected output?
ls_names = ["Raul", "Ana", "Oscar"]
tp_names = ("Raul", "Ana", "Oscar")
print(ls_names + tp_names)