Back to Python Mastery
BeginnerTeenagers

Working with Lists and Loops

A 'list' is a collection of items in a particular order. Think of it as a shopping list. You can store numbers, strings, or a mix of different types.

Here's how you create a list of numbers:
my_numbers = [1, 2, 3, 4, 5]

And a list of strings:
fruits = ["apple", "banana", "cherry"]

To go through each item in a list, we use a 'for loop'. A for loop lets you execute a block of code for every single item in a collection.

for fruit in fruits:
print(f"I like to eat {fruit}s.")

This loop will take each item from the 'fruits' list one by one, assign it to the variable 'fruit', and then run the print statement. The output would be:
"I like to eat apples."
"I like to eat bananas."
"I like to eat cherries."

Loops are incredibly powerful for automating repetitive tasks.

Sum a List

Write a function that takes a list of numbers and returns their sum.