Python: Loops Flashcards

1
Q

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]

A

[i for i in my_list if i > 5]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What would be the output of the following code:

for i in range(3):
print(5)

A

5
5
5

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Which of these list comprehensions will create a list equal to desired_list?

desired_list = [-1, 0, 1, 2, 3]

A

[i-1 for i in range(5)]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

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)

A

1
1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What would be the output of the following code:

for i in range(3):
print(i)

A

0
1
2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

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

A

while i <= 10:

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What would be the output of the following code:

numbers = [2, 4, 6, 8]
for number in numbers:
print(“hello!”)

A

hello!
hello!
hello!
hello!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What would be the output of the following code:

drink_choices = [“coffee”, “tea”, “water”, “juice”, “soda”]
for drink in drink_choices:
print(drink)

A

coffee
tea
water
juice
soda

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

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)

A

1
1
3

How well did you know this?
1
Not at all
2
3
4
5
Perfectly