A Beginner's Python Tutorial/Loops, Conditionals

(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
The following are examples of a type of loop, called the while loop:

How does this program work? Let's go through it in English (this is called pseudocode):
 * Code Example 1 – The 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 2 – Plain-language while loop

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

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:

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}
 * Code Example 4 – while loop form

An example:

Remember, to make a program, you open IDLE, click 'File > New Window', type your program in the new window, then press F5 to run.
 * Code Example 5 – <tt>while</tt> loop example

Boolean Expressions (Boolean... what?!?)
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:

Don't 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
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

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
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:

'a' is not greater than five, therefore what is under 'else' is done.
 * Code Example 7 - the else statement

'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:

The 'if' statement, along with 'else' and 'elif' follow this form:
 * Code Example 8 - The elif statement

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

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 ;)).
 * 1) You can have as many or as few elif statements as you need
 * 2) anywhere from zero to the sky.
 * 3) You can have at most one else statement
 * 4) and only after all other ifs and elifs.

Indentation
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:

Notice the three levels of indents there:
 * Code Example 10 - Indentation

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.