# Non-Programmer's Tutorial for Python 2.6/Decisions
### If statement

As always I believe I should start each chapter with a warm-up typing exercise, so here is a short program to compute the absolute value of a number:
Here is the output from the two times that I ran this program:
So what does the computer do when it sees this piece of code? First it prompts the user for a number with the statement "n = input("Number? ")". Next it reads the line "if n < 0:". If n is less than zero Python runs the line "print('The absolute value of', int(n), 'is: ', abs(-n))". Otherwise it runs the line "print('The absolute value of', int(n), 'is: ', abs(n))".
More formally Python looks at whether the expression n < 0 is true or false. An if statement is followed by an indented block of statements that are run when the expression is true. Optionally after the if statement is an else statement and another indented block of statements. This second block of statements is run if the expression is false.
There are a number of different tests that an expression can have. Here is a table of all of them:
| operator | function |
| < | less than |
| <= | less than or equal to |
| > | greater than |
| >= | greater than or equal to |
| == | equal |
| != | not equal |
| <> | another way to say not equal (old style, not recommended) |
Another feature of the if command is the elif statement. It stands for else if and means if the original if statement is false but the elif part is true, then do the elif part. And if neither the if or elif expressions are true, then do what's in the else block. Here's an example:
and the output:
Notice how the elif a <= 7 is only tested when the if statement fails to be true. There can be more than one elif expression, allowing multiple tests to be done in a single if statement.

### Examples

And the output
High_low.py
Sample run:
even.py
Sample runs:
average1.py
Sample runs:
average2.py
Sample runs:

### Exercises

* Modify the higher or lower program from this section to keep track of how many times the user has entered the wrong number. If it is more than 3 times, print "That must have been complicated." Note that the program does not have to quit asking for the number before it is guessed, it just has to print this after the number is guessed.
* Write a program that asks for two numbers. If the sum of the numbers is greater than 100, print "That is a big number."
* Write a program that asks the user their name, if they enter your name say "That is a nice name", if they enter "John Cleese" or "Michael Palin", tell them how you feel about them ;), otherwise tell them "You have a nice name."
Modify the higher or lower program from this section to keep track of how many times the user has entered the wrong number. If it is more than 3 times, print "That must have been complicated."
Write a program that asks for two numbers. If the sum of the numbers is greater than 100, print "That is a big number."
Write a program that asks the user their name, if they enter your name say "That is a nice name", if they enter "John Cleese" or "Michael Palin", tell them how you feel about them ;), otherwise tell them "You have a nice name."
| Previous: Count to 10 | Front Page | Next: Debugging |