W5 - Nested Loops Flashcards
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]
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))
does this work?
intlist = [1, 2, 3, 4,]
print(1 == intlist(1))
no, must use brackets
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!’]
for i in range(len(strlist)):
for j in strlist[i]:
print(j, end =”-“)
print()