Python: Loops Flashcards
Which of these list comprehensions will create a list equal to desired_list?
my_list = [5, 10, -2, 8, 20]
desired_list = [10, 8, 20]
[i for i in my_list if i > 10]
[i + 5 for i in my_list]
[i for i in my_list if i > 5]
[i for i in my_list]
[i for i in my_list if i > 5]
What would be the output of the following code:
for i in range(3):
print(5)
5
5
5
Which of these list comprehensions will create a list equal to desired_list?
desired_list = [-1, 0, 1, 2, 3]
[i-1 for i in range(5)]
What would be the output of the following code:
numbers = [1, 1, 2, 3]
for number in numbers:
if number % 2 == 0:
break
print(number)
1
1
What would be the output of the following code:
for i in range(3):
print(i)
0
1
2
Fill in the blank with the appropriate while condition in order to print the numbers 1 through 10 in order:
i = 1
___Blank___
print(i)
i += 1
while i <= 10:
What would be the output of the following code:
numbers = [2, 4, 6, 8]
for number in numbers:
print(“hello!”)
hello!
hello!
hello!
hello!
What would be the output of the following code:
drink_choices = [“coffee”, “tea”, “water”, “juice”, “soda”]
for drink in drink_choices:
print(drink)
coffee
tea
water
juice
soda
What would be the output of the following code:
numbers = [1, 1, 2, 3]
for number in numbers:
if number % 2 == 0:
continue
print(number)
1
1
3