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

### Code Example 1 – The while loop

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

### Code Example 2 – Plain-language while loop

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

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

An example:

### Code Example 5 – while loop 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.

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

### Code Example 7 - the else statement

'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

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

### 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 ;)).

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

### Code Example 10 - Indentation

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.