Lists let you store multiple items in a single variable. For loops let you process each item automatically. Together, they're one of Python's most powerful features!
A list is a collection of items in square brackets [], separated by commas:
fruits = ["apple", "banana", "orange"]
numbers = [10, 20, 30, 40]
mixed = ["Vidar", 22, True, 3.14]# empty list
empty = []
# list of strings
colors = ["red", "green", "blue"]
# list of numbers
scores = [95, 87, 92, 100]
# list of mixed types (works, but avoid it)
stuff = ["text", 42, True]fruits = ["apple", "banana", "orange"]
print(fruits)
# output: ['apple', 'banana', 'orange']
# in an f-string
print(f"Fruits: {fruits}")
# output: Fruits: ['apple', 'banana', 'orange']A for loop runs code for each item in a list:
fruits = ["apple", "banana", "orange"]
for fruit in fruits:
print(fruit)Output:
apple
banana
orangeHow it works:
fruitfor item in list_name:
# this code runs for each item
print(item)Key parts:
for - starts the loopitem - variable name (you choose this!)in - keywordlist_name - the list to loop through: - colon (required!)Use descriptive names:
# good - clear what we're looping through
for color in colors:
print(color)
for score in scores:
print(score)
# bad - unclear
for x in colors:
print(x)The loop variable (fruit, score, etc.) holds the current item:
fruits = ["apple", "banana", "orange"]
for fruit in fruits:
print(f"I like {fruit}")Output:
I like apple
I like banana
I like orangenumbers = [10, 20, 30, 40, 50]
for num in numbers:
doubled = num * 2
print(f"{num} doubled is {doubled}")Output:
10 doubled is 20
20 doubled is 40
30 doubled is 60
40 doubled is 80
50 doubled is 100Use a variable to build up a total:
scores = [95, 87, 92, 88]
total = 0
for score in scores:
total = total + score
print(f"Total: {total}")
# output: Total: 362How it works:
total = 0total = 0 + 95 → total = 95total = 95 + 87 → total = 182total = 182 + 92 → total = 274total = 274 + 88 → total = 362fruits = ["apple", "banana", "orange", "grape"]
count = 0
for fruit in fruits:
count = count + 1
print(f"We have {count} fruits")
# output: We have 4 fruitsshopping_list = ["milk", "bread", "eggs", "butter"]
print("Shopping List:")
for item in shopping_list:
print(f"- {item.upper()}")scores = [85, 92, 78, 95, 88]
total = 0
for score in scores:
total = total + score
average = total / 5
print(f"Average score: {average}")names = ["Vidar", "bob", "charli"]
for name in names:
formatted = name.upper()
print(f"Hello, {formatted}!")products = ["laptop", "mouse", "keyboard", "monitor"]
print("Inventory Check:")
for product in products:
print(f"✓ {product} - in stock")Correct:
for num in [1, 2, 3]:
print(num) # indented - inside loop
print("---") # indented - inside loop
print("Done!") # not indented - runs once after loopWrong:
for num in [1, 2, 3]:
print(num) # eRROR! Must be indentedPrint each item:
for item in items:
print(item)Sum all numbers:
total = 0
for num in numbers:
total = total + numCount items:
count = 0
for item in items:
count = count + 1Transform each item:
for name in names:
upper_name = name.upper()
print(upper_name)Forgetting the colon:
# wrong
for item in items
print(item)
# right
for item in items:
print(item)Forgetting to indent:
# wrong
for item in items:
print(item)
# right
for item in items:
print(item)Using the list name instead of the loop variable:
# wrong
for fruit in fruits:
print(fruits) # Prints the whole list each time!
# right
for fruit in fruits:
print(fruit) # Prints each fruitYou now know the fundamentals of Python! Practice these concepts, and you'll be ready to build real programs. Consider exploring: