Loops and List Comprehensions Flashcards
The for loop specifies what?
for planet in planets:
print(planet, end=‘ ‘)
The variable name to use (in this case, planet)
The set of values to loop over (in this case, planets)
What does the word in do?
Link two objects together
If you wanted to repeat a loop multiple times what would you use?
range()
While loops iterate until what?
Some condition is met
The argument of the while loop is evaluated as a what?
Boolean statement
The argument of a while loop is executed until the statement is what?
False
For loops allow you to do what?
Iterate through a sequence
Which one of these can be whatever you’d like it to be?
for item in seq:
print(item)
Variable name ‘item’
What happens if you change the print statement in a for loop?
It’ll return whatever you printed, the same # of times as the sequence
How would you convert this to a list?
range(0,9)
list(range(0,9))
What would this return?
list(range(4))
[0,1,2,3]
This allows you to save a bunch of writing when you’re trying to create a for loop to create a list
List comprehension
How would you comprehend this list?
up = [ ]
for beez in u:
up.append(beez**2)
[beez**2 for beez in u]
List comprehension is basically what?
a for loop but backwards