A Beginner's Python Tutorial/Loops, Conditionals

From Wikibooks, open books for an open world
Jump to: navigation, search

Introduction[edit]

(Our final lesson before we get into interacting with human input. Can't wait, can you?)

Just imagine you needed a program to do something 20 times. What would you do? You could copy and paste the code 20 times, and have a virtually unreadable program. Or, you could tell the computer to repeat a bit of code between point A and point B, until the time comes that you need it to stop. Such a thing is called a loop.

The while loop[edit]

The following are examples of a type of loop, called the while loop:

Code Example 1 – The while loop
a = 0
while a < 10:
    a = a + 1
    print a

How does this program work? Let's go through it in English (this is called pseudocode):

Code Example 2 – Plain-language while loop
'a' now equals 0
As long as 'a' is less than 10, do the following:
    Make 'a' one larger than what it already is.
    print on-screen what 'a' is now worth.

What does this do? Let's go through what the computer would be 'thinking' when it is in the while loop:

Code Example 3 – while loop process
#JUST GLANCE OVER THIS QUICKLY
#(It looks fancy, but is really simple.)
Is 'a' less than 10? YES (it's 0)
Make 'a' one larger (now 1)
print on-screen what 'a' is (1)

Is 'a' less than 10? YES (it's 1)
Make 'a' one larger (now 2)
print on-screen what 'a' is (2)

Is 'a' less than 10? YES (it's 2)
Make 'a' one larger (now 3)
print on-screen what 'a' is (3)

Is 'a' less than 10? YES (it's 3)
Make 'a' one larger (now 4)
print on-screen what 'a' is (4)

Is 'a' less than 10? YES (it's 4)
Make 'a' one larger (now 5)
print on-screen what 'a' is (5)

Is 'a' less than 10? YES (it's 5)
Make 'a' one larger (now 6)
print on-screen what 'a' is (6)

Is 'a' less than 10? YES (it's 6)
Make 'a' one larger (now 7)
print on-screen what 'a' is (7)

Is 'a' less than 10? YES (are you still here?)
Make 'a' one larger (now 8)
print on-screen what 'a' is (8)

Is 'a' less than 10? YES (it's 8)
Make 'a' one larger (now 9)
print on-screen what 'a' is (9)

Is 'a' less than 10? YES (it's 9)
Make 'a' one larger (now 10)
print on-screen what 'a' is (10)

Is 'a' less than 10? NO (it's 10, therefore isn't less than 10)
Don't do the loop
There's no code left to do, so the program ends.

So in short, try to think of it that way when you write while loops. This is how you write them, by the way:

Code Example 4 – while loop form
while {condition that the loop continues}:
    {what to do in the loop}
    {have it indented, usually four spaces}
{the code here is not looped}
{because it isn't indented}

An example:

Code Example 5 – while loop example
#EXAMPLE
#Type this in and see what it does.
x = 10
while x != 0:
    print x
    x = x - 1
    print "Wow, we've counted x down, and now it equals", x
print "And now the loop has ended."

Remember, to make a program, you open IDLE, click 'File > New Window', type your program in the new window, then press F5 to run.

Boolean Expressions (Boolean... what?!?)[edit]

What do you type in the area marked {conditions that the loop continues}? The answer is a boolean expression.

What? A forgotten concept for the non-math people here. Never mind, boolean expression just means a question that can be answered with a TRUE or FALSE response. For example, if you wanted to say your age is the same as the person next to you, you would type:

My age == the age of the person next to me

And the statement would be TRUE. If you were younger than the person opposite, you'd say:

My age < the age of the person opposite me

And the statement would be TRUE. If, however, you were to say the following, and the person opposite of you was younger than you:

My age < the age of the person opposite me

The statement would be FALSE - the truth is that it is the other way around. This is how a loop thinks - if the expression is true, keep looping. If it is false, don't loop. With this in mind, lets have a look at the operators (symbols that represent an action) that are involved in boolean expressions:

Table 1 - Boolean operators
Expression Function
< less than
<= less than or equal to
> greater than
>= greater than or equal to
!= not equal to
<> not equal to (alternate, != preferred)
== equal to

Dont get '=' and '==' mixed up - the '=' operator makes what is on the left equal to what is on the right. the '==' operator says whether the thing on the left is the same as what is on the right, and returns true or false.

Conditional Statements[edit]

OK! We've (hopefully) covered 'while' loops. Now let's look at something a little different - conditionals.

Conditionals are where a section of code is only run if certain conditions are met. This is similar to the 'while' loop you just wrote, which only runs when x doesn't equal 0. However, Conditionals are only run once. The most common conditional in any program language, is the 'if' statement. Here is how it works:

Code Example 6 - if statement and example
'''
if {conditions to be met}:
    {do this}
    {and this}
    {and this}
{but this happens regardless}
{because it isn't indented}
'''
 
#EXAMPLE 1
y = 1
if y == 1:
    print "y still equals 1, I was just checking"
 
#EXAMPLE 2
print "We will show the even numbers up to 20"
n = 1
while n <= 20:
    n = n + 1
    if n % 2 == 0:    
           print n
print "there, done."

Example 2 there looks tricky. But all we have done is run an 'if' statement every time the 'while' loop runs. Remember that the % just means the remainder from a division - just checking that there is nothing left over if the number is divided by two - showing it is even. If it is even, it prints what 'n' is.

'else' and 'elif' - When it Ain't True[edit]

There are many ways you can use the 'if' statement, to deal with situations where your boolean expression ends up FALSE. They are 'else' and 'elif'.

'else' simply tells the computer what to do if the conditions of 'if' aren't met. For example, read the following:

Code Example 7 - the else statement
a = 1
if a > 5:
    print "This shouldn't happen."
else:
    print "This should happen."

'a' is not greater than five, therefore what is under 'else' is done.

'elif' is just a shortened way of saying 'else if'. When the 'if' statement fails to be true, 'elif' will do what is under it IF the conditions are met. For example:

Code Example 8 - The elif statement
z = 4
if z > 70:
    print "Something is very wrong"
elif z < 7:
    print "This is normal"

The 'if' statement, along with 'else' and 'elif' follow this form:

Code Example 9 - the complete if syntax
if {conditions}:
    {run this code}
elif {conditions}:
    {run this code}
elif {conditions}:
    {run this code}
else:
    {run this code}

#You can have as many or as few elif statements as you need
#anywhere from zero to the sky.
#You can have at most one else statement
#and only after all other ifs and elifs.

One of the most important points to remember is that you MUST have a colon (:) at the end of every line with an 'if', 'elif', 'else' or 'while' in it. I forgot that, and as a result a stack of people got stumped at this lesson (sorry ;)).

Indentation[edit]

One other point is that the code to be executed if the conditions are met, MUST BE INDENTED. That means that if you want to loop the next five lines with a 'while' loop, you must put a set number of spaces at the beginning of each of the next five lines. This is good programming practice in any language, but python requires that you do it. Here is an example of both of the above points:

Code Example 10 - Indentation
a = 10
while a > 0:
    print(a)
    if a > 5:
        print("Big number!")
    elif a % 2 != 0:
        print("This is an odd number")
        print("It isn't greater than five, either")
    else:
        print("this number isn't greater than 5")
        print("nor is it odd")
        print("feeling special?")
    a = a - 1
    print("we just made 'a' one less than what it was!")
    print("and unless a is not greater than 0, we'll do the loop again.")
print("well, it seems as if 'a' is now no bigger than 0!")
print("the loop is now over, and without furthur ado, so is this program!")

Notice the three levels of indents there:

Each line in the first level starts with no spaces. It is the main program, and will always execute. Each line in the second level starts with four spaces. When there is an 'if' or loop on the first level, everything on the second level after that will be looped/'ifed', until a new line starts back on the first level again. Each line in the third level starts with eight spaces. When there is an 'if' or loop on the second level, everything on the third level after that will be looped/'ifed', until a new line starts back on the second level again. This goes on infinitely, until the person writing the program has an internal brain explosion, and cannot understand anything he/she has written. There is another loop, called the 'for' loop, but we will cover that in a later lesson, after we have learnt about lists.