Lessons

Explore Further

These are good links to check out after watching the videos on this page

Related Links

Decision Control

If Statement
else and elif
Nesting Statements
Comparison Operators
And and Or
Lists
For and While Loops

Exercise: If, Elif And Else

 

The exercises on this page were taken from Learn Python The Hard Way. They have been edited for our IDLE 2.6.6 development environment.

Type this in and make it work:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
people = 30
cars = 40
buses = 15


if cars > people:
    print "We should take the cars."
elif cars < people:
    print "We should not take the cars."
else:
    print "We can't decide."

if buses > cars:
    print "That's too many buses."
elif buses < cars:
    print "Maybe we could take the buses."
else:
    print "We still can't decide."

if people > buses:
    print "Alright, let's just take the buses."
else:
    print "Fine, let's stay home then."

What You Should See

>>> ================================ RESTART ================================
>>>
We should take the cars.
Maybe we could take the buses.
Alright, let's just take the buses.
>>>

Extra Credit

  1. Change the numbers of cars, people, and buses and then trace through each if-statement to see what will be printed.
  2. Try some more complex boolean expressions like cars > people and buses < cars.
  3. Above each line write a comment with an English description of what the line does.

Loops And Lists

 

We now will build some lists using some loops and print them out:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
the_count = [1, 2, 3, 4, 5]
fruits = ['apples', 'oranges', 'pears', 'apricots']
change = [1, 'pennies', 2, 'dimes', 3, 'quarters']

# this first kind of for-loop goes through a list
for number in the_count:
    print "This is count %d" % number

# same as above
for fruit in fruits:
    print "A fruit of type: %s" % fruit

# also we can go through mixed lists too
# notice we have to use %r since we don't know what's in it
for i in change:
    print "I got %r" % i

# we can also build lists, first start with an empty one
elements = []

# then use the range function to do 0 to 5 counts
for i in range(0, 6):
    print "Adding %d to the list." % i
    # append is a function that lists understand
    elements.append(i)

# now we can print them out too
for i in elements:
    print "Element was: %d" % i

What You Should See

>>> ================================ RESTART ================================
>>>
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
>>>

Extra Credit

  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?

Matrices

In the Python Shell, create a list called
mat:
>>> mat = [
...        [1, 2, 3],
...        [4, 5, 6],
...        [7, 8, 9],
...       ]

Try out the following statement:

>>> mat[1]

What do you see? Why?

Now try this:

>>> mat[1][0]
What's going on? Write a statement that returns the number 8 from the list
mat
.

Tuples

In addition to the list, Python has a data structure called a tuple. Try typing the following into the Python Shell:

>>> t = (1, 2)
>>> t
>>> t[0]
>>> t[1]
What do you notice? Tuples behave very much like lists. One important difference is that you can't append items to the end of a tuple. Try this:
>>> l = [1, 2]
>>> l
>>> l[0]
>>> l[2]
>>> l.append(3)
>>> l
>>> l[0]
>>> l[2]
What does it mean to append something to a list? Try appending something to a tuple:
>>> t.append(3)
What happens? Tuples are used when we know exactly how many items we want to store. For example, the width and height or the x and y positions of graphical objects are often stored in tuples.

Other Ways to Assign Tuples to Variables

You can assign a tuple to a variable without using parentheses. Try out the following in the Python Shell:

>>> a = 1, 2
>>> a
>>> a[0]
>>> a[1]

You can also assign a tuple to a tuple of variables. Try this:

>>> x, y = 39, 14
>>> x
>>> y