Lesson 5
Lesson 5 of 16
Lists
Lesson Progress: 0%
Code Example
# List of integers
nums1 = [1, 2, 3]
print(nums1)
nums2 = [5, 10, 15]
print(nums2)
# List of decimals
prices = [1.5, 2.0, 3.25]
print(prices)
heights = [4.2, 5.1, 6.0]
print(heights)
# List of words
pets = ["Cat", "Dog", "Bird"]
print(pets)
colors = ["Red", "Blue", "Green"]
print(colors)Instructions
▼ ← Click the triangle to hide or reveal instructions.Python Code Editor
Task Incomplete
Editor Input:
Editor Output:
[ ].pets or colors, you can use it later in your code.print() with the list's name to see everything you stored inside it on the screen.Code Example
# Case 1: List of short sentences (Example A)
quotes = ["Be kind", "Work hard", "Stay happy"]
print(quotes)
# Case 1: List of short sentences (Example B)
facts = ["The sky is blue", "Grass is green"]
print(facts)
# Case 2: List Indexing - Picking the first sentence
my_pets = ["I have a cat", "I have a dog", "I have a bird"]
print(my_pets[0])
# Case 2: List Indexing - Picking the last sentence
my_goals = ["Learn to code", "Play soccer", "Read a book"]
print(my_goals[2])Instructions
▼ ← Click the triangle to hide or reveal instructions.Python Code Editor
Task Incomplete
Editor Input:
Editor Output:
[ ] right after the list name.Code Example
# List of different data types
mix1 = [5, "Hello", 2.5]
print(mix1)
mix2 = ["Cat", 10, True]
print(mix2)
# List indexing (getting one item from the list)
items1 = [7, "Apple", 3.5]
print(items1[0])
items2 = ["Dog", 4, False]
print(items2[1])Instructions
▼ ← Click the triangle to hide or reveal instructions.Python Code Editor
Task Incomplete
Editor Input:
Editor Output:
[ ] to get one item from a list.[0] means the first item.print() shows the list or the item on the screen.Code Example
# Zip names with ages
names = ["Sam", "Alex"]
ages = [10, 12]
for name, age in zip(names, ages):
print(name, age)
# Zip colors with objects
colors = ["Red", "Blue"]
things = ["Car", "Ball"]
for color, thing in zip(colors, things):
print(color, thing)Instructions
▼ ← Click the triangle to hide or reveal instructions.Python Code Editor
Task Incomplete
Editor Input:
Editor Output:
zip() helper is like a zipper on a jacket that pulls two different lists together into pairs.name, age to hold the two pieces of information at the same time.