list

list is ordered, indexed, and flexible

a list of 4 integers

a list of 4 words.

a list of mixed words and numbers.

a list of mixed objects... not kidding... python is cool like that.

list is a basic data structure.

Whatever you put in, that's the order you get out.

create a list

             # To create a list, you can create it by [  ] notation

             []  # this is an empty list
             [1] # this is a list containing one element
             [2] # this is a list containing one element and the value is an integer (2)
             [1, 2] # this is a list containing two elements
             [1, 2, 'hello! awkward?'] # this list has two integers and a string!!!!
            

read list1.py

list uses zero-based index

first item in the list is at index 0, and the last item in the list is at index n-1 where n is the number of items in the list.

read an item from the list

             my_list = [100, 42, 'awkward!', 5]
             print 'my_list[2] is', my_list[2]   # print the value of my_list at index [2]
             print 'my_list[0] is',my_list[0]   # what gets print out?   :)
            

read list2.py

pause

             my_new_list = ['hello', 'world', '123', 123]    # create a new list
             print my_new_list[0]
             print my_new_list[4]
            

We learned how to create a list and how to read an item from a list. Do you think this is a valid Python code?

pause

             my_new_list = ['hello', 'world', '123', 123]    # create a new list
             print my_new_list[0]
             print my_new_list[4]
            

We learned how to create a list and how to read an item from a list. Do you think this is a valid Python code?
Yes. There is no synatx error. BUT...

OUT OF RANGE

The last index we can access is always n-1, which is 3 in this case.

list3.py

read that

list4.py

read that

try list5.py

Tired?

Take a break and come back! :)