Loops and List Comprehensions Flashcards

1
Q

The for loop specifies what?

for planet in planets:
print(planet, end=‘ ‘)

A

The variable name to use (in this case, planet)

The set of values to loop over (in this case, planets)

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

What does the word in do?

A

Link two objects together

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

If you wanted to repeat a loop multiple times what would you use?

A

range()

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

While loops iterate until what?

A

Some condition is met

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

The argument of the while loop is evaluated as a what?

A

Boolean statement

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

The argument of a while loop is executed until the statement is what?

A

False

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

For loops allow you to do what?

A

Iterate through a sequence

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

Which one of these can be whatever you’d like it to be?

for item in seq:
print(item)

A

Variable name ‘item’

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

What happens if you change the print statement in a for loop?

A

It’ll return whatever you printed, the same # of times as the sequence

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

How would you convert this to a list?

range(0,9)

A

list(range(0,9))

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

What would this return?

list(range(4))

A

[0,1,2,3]

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

This allows you to save a bunch of writing when you’re trying to create a for loop to create a list

A

List comprehension

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

How would you comprehend this list?

up = [ ]

for beez in u:
up.append(beez**2)

A

[beez**2 for beez in u]

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

List comprehension is basically what?

A

a for loop but backwards

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