INFO1110 Lecture Notes - Lecture 4: Order Of Merit
Document Summary
Use len to nd the length of a list. print(len(my_list)) A list can be indexed using square brackets []. Note: the rst element in the list is at index zero. list elements 5 3 8 6 index. What happens when we choose an index that doesn"t exist? print(my_list[4]) Recall, we can use a while loop to count. Why not count the indices of the list from. 0 up to one less than the length of the list. i = 0 while i < len(my_list): print(i) i = i + 1. Rather than printing out the index, let"s use it to index the list and print out the element at that index. i = 0 while i < len(my_list): elem = my_list[i] print(elem)