Lesson Notes
Lists are collections of objects
mylist = [] #empty list
myotherlist = [10,20,30,40]
myotherotherlist = [10,"Vanilla","Butterscotch", 4.5]
len()
is a function that returns a list length
mycolors = [ ‘Red’, ‘Orange’, ‘Yellow’, ‘Purple’, ‘Pink’]
list_size = len(mycolors)
print( list_size ) # prints 5
Objects in lists are accessible by their index, i.e. their location in the list
Each object is assigned an integer depending on the order in the list and the counting starts at 0.
lists are 0 indexed
To access a specific item in our list we can use the square bracket: [ ]
items[0] =>returns the first element in the list
Given the list: mylist = ['a','b','c']
a
is at index 0
print(mylist[0]) #prints 'a'
c
is at index 2
print(mylist[2]) #prints 'c'
EXAMPLE
print(mycolors[0])
print(mycolors[list_size-1])
print(mycolors[-1])
mycolors[2] = "Purple"
print(mycolors)
You and add new items to the end of the list with .append()
mylist.append(obj)
- addsobj
to the end of themylist
colors
print()
statementmycolors = ["Red", "Orange", "Yellow", "Green"]
print(mycolors)