Week Five
This week we learn how to store multiple values using lists and how to access, modify, and check items inside them.
By the end of this lesson, your programs will be able to remember more than one thing at a time.
Presentation
Variables vs. Lists
Single Variables
A variable stores one value.
name = "Alex"If we want to store more than one name, this doesn’t work very well.
Lists
A list stores many values in one variable.
names = ["Alex", "Jordan", "Sam"]Creating Lists
Lists use square brackets [].
colors = ["red", "blue", "green"]An empty list:
items = []Lists keep items in order. The order matters.
Indexing Lists
Each item in a list has a position number called an index.
Indexes start at 0.
colors = ["red", "blue", "green"]
print(colors[0]) # red
print(colors[1]) # blue
print(colors[2]) # greenChanging Items
colors[1] = "yellow"
print(colors)Result:
['red', 'yellow', 'green']Using an index that doesn’t exist will cause an error.
List Length
Use len() to find how many items are in a list.
numbers = [3, 7, 2]
print(len(numbers))Adding Items to a List
You can add new colors to a list using append().
append()
append() adds an item to the end of a list.
colors = ["red", "blue"]
colors.append("green")
print(colors)
# ['red', 'blue', 'green']After running this code:
"green"is added to the end of the list- The list now has three colors
Using Input to Add a Color
You can also add a color using user input.
colors = ["red", "blue"]
new_color = input("Enter a color to add: ")
colors.append(new_color)
print(colors)append() always adds the item to the end of the list.
Comparing a List Item to a Value
You can compare a specific item in a list to a value using its index.
This lets your program make decisions based on what is stored in the list.
Example: Checking a List Item
colors = ["red", "blue", "green"]
if colors[0] == "red":
print("The first color is red!")In this example:
colors[0]means the first item- The program compares it to
"red" - If they match, the message prints
Using Input with List Indexes
You can compare user input to an item in a list.
answers = ["A", "B"]
choice = input("Choose A or B: ")
if choice == answers[0]:
print("You chose A!")
elif choice == answers[1]:
print("You chose B!")Comparing Numbers in a List
Lists can also store numbers.
scores = [85, 92]
if scores[1] >= 90:
print("High score!")Here:
scores[1]is92- The condition checks if it is greater than or equal to 90
Always make sure the index exists before comparing it.
Classroom Example
# Make a list of fruits
fruitlist = ["apple", "banana", "cherry"]
# Print the list out
print(fruitlist)
# Each item in the list has an "index" - which is it's position in the list
# In Python the index starts at 0, we acccess the item using square brackets and the position
# Examples:
# Print the first fruit in the list
# Print the second fruit in the list
# Print the third fruit in the list
# What happens if I try to access an item in position 4?
# How to add something at the end of the list?
fruitlist.append("orange")
# Print the entire list
print(fruitlist)
# Now try to acccess the item at position 4
# Print how many fruit are in the list
print(len(fruitlist))
# To use len() to access last item in the list
# print(fruitlist[len(fruitlist)-1])
# https://www.w3schools.com/python/python_lists_methods.asp