The code below shows some ways of using python lists (known as arrays in many languages). Note how these lists are "sliced" from index i to j using my_list[i:j] notation.
print("Looping through a list...")for item in my_list:print("item is", item)
Looping through a list...
item is 1
item is 2
item is 3
item is a
item is b
item is c
Using range to loop through a list by index
print("A less great way to loop through a list...")for index inrange(len(my_list)): item = my_list[index]# accessing an element in a list! print("item is", item)
A less great way to loop through a list...item is1item is2item is3item is aitem is bitem is c
List slicing
Looping through a partial list
print("Slicing a list from beginning...")for item in my_list[:3]:print("item is", item)
item is1item is2item is3
Looping through a partial list again
print("Enumerating a list...")for i, item inenumerate(my_list):print("item number", i, "is", item)
Slicing a list to the end...item is1item is2item is3item is aitem is bitem is c
print("Another way to enumerate using a list 'method'...")for item in my_list: index = my_list.index(item)print("item", item, "has index", index)
Another way to enumerate using a list'method'...item 1 has index 0item 2 has index 1item 3 has index 2item a has index 3item b has index 4item c has index 5