W5 - Nested Loops Flashcards

1
Q

Write a for loop which loops through the list below and prints the type of each element. Write general code. Do not assume that you know how many elements are in the list.

mylist = [43, ‘happy’, 3 < 5, 11.11]

A

we COULD use the range function to generate the indices of the elements, and use the iterator variable to index into the list:

mylist = [43, ‘happy’, 3 < 5, 11.11]
for i in range(0, len(mylist)):
print(type(mylist[i]))

OR just use the elements themselves and type function

for i in mylist:
print(type(i))

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

does this work?
intlist = [1, 2, 3, 4,]
print(1 == intlist(1))

A

no, must use brackets

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

Use the following list of strings. Write code to print all of the characters from all of the strings on one line, with a dash (‘-‘) after each.

strlist = [‘I’, ‘love’, ‘coding!’]

A

for i in range(len(strlist)):
for j in strlist[i]:
print(j, end =”-“)
print()

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