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:

Type in a number: -14 The absolute value of -14 is: 14

Type in a number: 24 The absolute value of 24 is: 24

So what does the computer do when it sees this piece of code? First it prompts the user for a number with the statement " ". Next it reads the line " ". If  is less than zero Python runs the line " ". Otherwise it runs the line " ".

More formally Python looks at whether the expression  is true or false. An  statement is followed by an indented block of statements that are run when the expression is true. Optionally after the  statement is an   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:

Another feature of the  command is the   statement. It stands for else if and means if the original  statement is false but the   part is true, then do the   part. And if neither the  or   expressions are true, then do what's in the   block. Here's an example:

and the output:

1 <= 7 2 <= 7 3 <= 7 4 <= 7 5 <= 7 6 > 5 7 > 5 8 > 5 9 > 5 10 > 5

Notice how the  is only tested when the   statement fails to be true. There can be more than one  expression, allowing multiple tests to be done in a single   statement.

Examples
And the output

False False

High_low.py

Sample run:

Guess a number: 100 Too high Guess a number: 50 Too low Guess a number: 75 Too low Guess a number: 87 Too high Guess a number: 81 Too high Guess a number: 78 Just right

even.py

Sample runs:

Tell me a number: 3 3 is odd.

Tell me a number: 2 2 is even.

Tell me a number: 3.14159 3.14159 is very strange.

average1.py

Sample runs:

Enter 0 to exit the loop Enter a number: 3 Enter a number: 5 Enter a number: 0 The average was: 4.0

Enter 0 to exit the loop Enter a number: 1 Enter a number: 4 Enter a number: 3 Enter a number: 0 The average was: 2.66666666667

average2.py

Sample runs:

This program will take several numbers then average them How many numbers would you like to average: 2 Number 1 Enter a number: 3 Number 2 Enter a number: 5 The average was: 4.0

This program will take several numbers then average them How many numbers would you like to average: 3 Number 1 Enter a number: 1 Number 2 Enter a number: 4 Number 3 Enter a number: 3 The average was: 2.66666666667

Exercises

 * 1) 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.
 * 2) Write a program that asks for two numbers.  If the sum of the numbers is greater than 100, print "That is a big number."
 * 3) 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."