The basics of loops in python Flashcards

1
Q

Which of these Python data types can NOT be iterated through using for loops?

tuple
list
string
int

A

int

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

Which TWO of the following statements about for loops in Python are TRUE?

A

They may have an associated else block

They can iterate over the elements in tuples, lists, and dictionaries

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

ven the following code, what is the type of x which is printed out in each iteration?

my_list = [[‘tiger’, ‘lion’, ‘leopard’], [‘camel’, ‘llama’, ‘alpaca’], [‘zebra’, ‘donkey’, ‘wildebeest’]]

for x in my_list:
print(x)

A

A list of strings

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

Given a variable my_dict which is a dictionary, consider you use it in a for loop in this manner:

for x in my_dict:
print (x)

What are the contents printed out?

A

The keys in the dictionary

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

What is the correct value of x given the assignment shown?

x = list(range(-17, -7, 2))

A

[-17, -15, -13, -11, -9]

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

Which of the following function calls will generate the list below?
[10, 7, 4, 1, -2]

A

list(range(10, -3, -3)

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