Today you will learn to:
By the end, you will be able to write programs that store and manage multiple pieces of data at once.
A list is a collection of multiple values.
Lists are used when you want to store many similar items.
Examples:
colors = ["red", "green", "blue"]
Explanation:
Each element in a list has a position (index).
Important: Python starts counting from 0.
colors = ["red", "green", "blue"]
print(colors[0])
Output:
red
Positions:
colors[0] → red
colors[1] → green
colors[2] → blue
New elements can be added.
For that, use append().
colors = ["red", "green", "blue"]
colors.append("yellow")
Now the list contains:
["red", "green", "blue", "yellow"]
You can also change elements.
colors = ["red", "green", "blue"]
colors[1] = "purple"
New list:
["red", "purple", "blue"]
You can loop through a list using a loop.
colors = ["red", "green", "blue"]
for color in colors:
print(color)
Output:
red
green
blue
A dictionary stores data as key-value pairs.
That means:
key → value
Examples:
person = {"name":"Max", "age":20}
Explanation:
To get a value, use the key.
person = {"name":"Max", "age":20}
print(person["name"])
Output:
Max
More examples:
person["age"] → 20
You can also change values.
person = {"name":"Max", "age":20}
person["age"] = 21
Now the age is 21.
List example:
colors = ["red", "green", "blue"]
colors.append("yellow")
print(colors[0])
Dictionary example:
person = {"name":"Max", "age":20}
print(person["name"])
Create a list for a shopping list.
Example structure:
shopping = ["Bread", "Milk", "Apples"]
Then print all the items.
Tip:
You can use a for loop.
Create a dictionary for a pet.
Information:
Example structure:
pet = {
"name":"Bello",
"species":"Dog",
"age":5
}
Then print the values:
print(pet["name"])
print(pet["species"])
print(pet["age"])