Python lesson 7
Basic Lists
Store many values in one place. Then grab any one you need!
Lists Hold Many Values
A list stores many items inside square brackets, separated by commas. Each item has an index. The first item is index 0. You can add new items to a list with .append()!
fruits = ["apple", "banana", "mango"]
print(fruits[0]) → apple
print(fruits[1]) → banana
fruits = ["apple", "banana", "mango"]
print(fruits[0])
print(fruits[1])
print(fruits[2])
Which One Is Number 1?
pets = ["cat", "dog", "fish"]
print(pets[1])
print(len(pets))
Access by Index
Click Run to create a list and pick out specific items by their index!
Hint: Click Run! colors[0] gets the first item, colors[2] gets the third item.
colors = ["red", "green", "blue"]
print(colors[0])
print(colors[2])
Add to a List
Run this code to see .append() add a new item. Then add another item of your own!
Hint: .append() adds a new item to the end of the list. Add another animal after "rabbit"!
animals = ["cat", "dog"]
animals.append("rabbit")
print(animals)
List Length
Run this code to count how many items are in a list. Then add more and run again!
Hint: len() counts the items in the list. Append a new snack and run again to see the count go up!
snacks = ["apple", "chips", "yogurt"]
print("Snacks:", snacks)
print("Count:", len(snacks))
What you can do on CodeIt
Run the example, change one part, and use the result to check your understanding before moving to the next lesson.