First Look at Lists¶
Like we said before, a list is a lot like a list that you already know about, like a grocery list:
Things To Buy
- apples
- oranges
- bread
- milk
But in Python we would write it like this:
things_to_buy = [ 'apples','oranges','bread','milk']
The brackets, [
and ]
are most often used to mean that something is a list.
There are a lot of neat things we can do with a list.
First, you can get a specific item from a list, using the []
with a number inside.
things_to_buy[1]
> oranges
Getting values out of a list like this is called “indexing”.
Like most programming languages, the first item in a list is 0, not 1, so if
you wanted to get apples
from the list, you would write things_to_get[0]
Another important thing about lists is you can iterate them, which means ‘do something repeatedly’. Here is how we would print out all of the items in the list:
Loops and lists could be very useful for our turtle programs. For instance, we could make a square with a different color on each side:
Or, we could change the angle that tina turns:
Here is a way that we could change two variables at once, using array indexes:
Now, write your own crazy program. You can copy and change the programs we’ve done previously.