Lists
A list holds an ordered collection of values: fruits = ["apple", "banana"].
Access items by zero-based index (fruits[0]), and add with
.append().
Example: a list comprehension
numbers = [1, 2, 3, 4, 5]
doubled = [n * 2 for n in numbers]
print(doubled)
[2, 4, 6, 8, 10]
[n * 2 for n in numbers] is a list
comprehension — a compact way to build a new list by transforming
every item in an existing one. It's equivalent to writing a full
for loop that appends to an empty list one item at a time,
just shorter and, once the syntax is familiar, more readable at a glance.
It's a genuinely idiomatic Python pattern worth learning early, since
you'll see it constantly in real Python code.
numbers = [1, 2, 3, 4, 5]
doubled = [n * 2 for n in numbers]
print(doubled)
[2, 4, 6, 8, 10]