You should now be able to do some programs that are much more interesting. If you have been keeping up, you should realize that now you can combine all the other things you have learned with if-statements
and Boolean expressions to make your programs do smart things.
However, programs also need to do repetitive things very quickly. We are going to use a for-loop
in this exercise to build and print various lists. When you do the exercise, you will start to figure out what they are. I won’t tell you right now. You have to figure it out.
Before you can use a for-loop
, you need a way to store the results of loops somewhere. The best way to do this is with lists. A list is exactly what the name says: a container of things that are organized in order from first to last. It’s not complicated; you just have to learn a new syntax. First, here’s how you make lists:
hairs = ['brown', 'blond', 'red']
eyes = ['brown', 'blue', 'green']
weights = [1, 2, 3, 4]
You start the list with the [
(left bracket) which “opens” the list. Then you put each item you want in the list separated by commas, similar to function arguments. Lastly, end the list with a ]
(right bracket) to indicate that it’s over. Python then takes this list and all its contents and assigns them to the variable.
Warning!
This is where things get tricky for people who can’t code. Your brain has been taught that the world is flat. Remember in the last exercise where you put if-statements
inside if-statements
? That probably made your brain hurt because most people do not ponder how to “nest” things inside things. In programming nested structures are all over the place. You will find functions that call other functions that have if-statements
that have lists with lists inside lists. If you see a structure like this that you can’t figure out, take out a pencil and paper and break it down manually bit by bit until you understand it.
We now will build some lists using some for-loops
and print them out:
ex32.py
1 the_count = [1, 2, 3, 4, 5]
2 fruits =['apples', 'oranges', 'pears', 'apricots']
3 change = [1, 'pennies', 2, 'dimes', 3, 'quarters']
4
5 # this first kind of for-loop goes through a list
6 for number in the_count:
7 print(f"This is count {number}")
8
9 # same as above
10 for fruit in fruits:
11 print(f"A fruit of type: {fruit}")
12
13 # also we can go through mixed lists too
14 # notice we have to use {} since we don't know what's in it
15 for i in change:
16 print(f"I got {i}")
17
18 # we can also build lists, first start with an empty one
19 elements = []
20
21 # then use the range function to do 0 to 5 counts
22 for i in range(0, 6):
23 print(f"Adding {i} to the list.")
24 # append is a function that lists understand
25 elements.append(i)
26
27 # now we can print them out too
28 for i in elements:
29 print(f"Element was: {i}")
Exercise 32 Session
$ python3.6 ex32.py
This is count 1
This is count 2
This is count 3
This is count 4
This is count 5
A fruit of type: apples
A fruit of type: oranges
A fruit of type: pears
A fruit of type: apricots
I got 1
I got pennies
I got 2
I got dimes
I got 3
I got quarters
Adding 0 to the list.
Adding 1 to the list.
Adding 2 to the list.
Adding 3 to the list.
Adding 4 to the list.
Adding 5 to the list.
Element was: 0
Element was: 1
Element was: 2
Element was: 3
Element was: 4
Element was: 5
1. Take a look at how you used range
. Look up the range
function to understand it.
2. Could you have avoided that for-loop
entirely on line 22 and just assigned range(0,6)
directly to elements
?
3. Find the Python documentation on lists and read about them. What other operations can you do to lists besides append
?
How do you make a 2-dimensional (2D) list? That’s a list in a list, like this: [[1,2,3],[4,5,6]]
.
Aren’t lists and arrays the same thing? That depends on the language and the implementation. In classic terms a list is very different from an array because of how they’re implemented. In Ruby, though, they call these arrays. In Python they call them lists. Just call these lists for now since that’s what Python calls them.
Why is a for-loop
able to use a variable that isn’t defined yet? The variable is defined by the for-loop
when it starts, initializing it to the current element of the loop iteration each time through.
Why does for i in range(1, 3):
only loop two times instead of three times? The range()
function only does numbers from the first to the last, not including the last. So it stops at two, not three, in the preceding. This turns out to be the most common way to do this kind of loop.
What does elements.append()
do? It simply appends to the end of the list. Open up the Python shell and try a few examples with a list you make. Any time you run into things like this, always try to play with them interactively in the Python shell.
18.219.22.169