Decisions 5.1 If statement
5.1 If statement
so here is a short program to compute
the absolute value of a number:
n = input("Number? ")
if n < 0:
print "The absolute value of",n,"is",-n
else:
print "The absolute value of",n,"is",n
Here is the output from the two times that I ran this program:
Number? -34
The absolute value of -34 is 34
Number? 1
The absolute value of 1 is 1
So what does the computer do when 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",n,"is",-n. Otherwise python runs the line print "The
absolute value of",n,"is",n.
More formally Python looks at whether the expression n < 0 is true or false. A if statement is followed by a block
of statements that are run when the expression is true. Optionally after the if statement is a else statement. The
else statement is run if the expression is false.
There are several different tests that a 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
Another feature of the if command is the elif statement. It stands for else if and means if the original if statement
17
is false and then the elif part is true do that part. Here’s a example:
a = 0
while a < 10:
a = a + 1
if a > 5:
print a," > ",5
elif a <= 7:
print a," <= ",7
else:
print "Neither test was true"
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 elif a <= 7 is only tested when the if statement fail to be true. elif allows multiple tests to be done in a single if statement.
the absolute value of a number:
n = input("Number? ")
if n < 0:
print "The absolute value of",n,"is",-n
else:
print "The absolute value of",n,"is",n
Here is the output from the two times that I ran this program:
Number? -34
The absolute value of -34 is 34
Number? 1
The absolute value of 1 is 1
So what does the computer do when 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",n,"is",-n. Otherwise python runs the line print "The
absolute value of",n,"is",n.
More formally Python looks at whether the expression n < 0 is true or false. A if statement is followed by a block
of statements that are run when the expression is true. Optionally after the if statement is a else statement. The
else statement is run if the expression is false.
There are several different tests that a 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
Another feature of the if command is the elif statement. It stands for else if and means if the original if statement
17
is false and then the elif part is true do that part. Here’s a example:
a = 0
while a < 10:
a = a + 1
if a > 5:
print a," > ",5
elif a <= 7:
print a," <= ",7
else:
print "Neither test was true"
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 elif a <= 7 is only tested when the if statement fail to be true. elif allows multiple tests to be done in a single if statement.



