Lists and Loops

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.

How to print a list structure

my_list = [1, 2, 3, "a", "b", "c"] print("my_list is:", my_list)

my_list is: [1, 2, 3, 'a', 'b', 'c']

Prints each element of a list individually

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

List slicing

Looping through a partial list

Looping through a partial list again

Last updated

Was this helpful?