Lists
Lists
A list is a data structure which holds any number of values, separated by commas and
surrounded by square brackets. For example, []
, [50]
, and [100, 200, 300, 400]
are all lists of numbers (although []
is an empty list).
Iteration
Iteration is the process of going through a collection of things one at a time. There are plenty of everyday situations where you iterate through collections of things. For example, you might iterate through a pile of dollar bills to count how much money you have, or you might iterate through all today's mail to find out whether you got a birthday card from your friend.
Here are some reasons you might iterate through a collection in Python:
- You're looking for something that's in the collection.
- You want to do something to each item in the collection.
- There's some action that you want to repeat multiple times.
In Python, something is iterable if it can contain multiple elements. For now,
we will be working with two iterables: lists and ranges. A list is any number of
items inside of a pair of brackets ([]
), separated by commas. For example,
[1, 3, 5, 7, 9]
. The range
function creates a list of numbers from 0 up to the
number provided. For example:
>>> for number in range(4):
... print(number)
0
1
2
3