Python, part 5
IMPORTANT
NOTE: For many of the examples in this and
subsequent Python note pages, I've also written Python
programs which can be copied and run--check with your TAs.
The programs will have the program name in this font, with
an underline. e.g. alpha
We will look here at controlling the
flow of execution--for example,
we might want to process data values differently. Suppose we want
to
do something very simple--we will ask the user to type in a number,
and then print out whether that number was a negative number (<0)
or nonnegative--i.e. greater than or equal to 0.
number = input("please
type in an integer: ")
if number < 0:
print
"this number is negative"
else:
print
"this number is nonnegative"
simpleif
The "if" statement requires a condition (e.g. number < 0) that Python
can evaluate as being true or false. Here, whether number < 0
is true or
false depends on the number you typed in. Note the colon after
the
condition. This is followed by one or more statements that you
want
executed if the condition is true. Note that these statements are
indented--this tells Python
that this is what is called a BLOCK
of
statements in which the statements go together. Following this
block
is an OPTIONAL else:
the else statements will be executed if the
condition was false. If you simply want to do something when the
condition is true and do nothing special if the condition is false, then
simply omit the else; For example:
number = input("please type in a
number: ")
if number >= 100:
print "this number is greater than or equal to 100"
What also works--if you do not have multiple statements you want
executed--would have been (for the first example):
if number < 0: print
"this number is negative"
else: print "this number is
nonnegative"
You'll see that even though this latter version works OK, the first is
actually, with its indentation, easier to read.
CONDITION OPERATORS: < <=
> >= less than, less than or
equal to, greater than, greater than or equal to.
ALSO: == is equal to--note that a single = here
is NOT valid.
!= means is not equal to.
COMPOUND CONDITIONS:
if number > 0
and number < 100: this is true only
if both pieces
are true--i.e. number is greater than 0 and number is ALSO less
than 100.
if number > 0 or
number < 100: this is true if either part is true--
it is false only if both parts are false. You need to think about
what
you are doing and what it is you want to ask. In this case--is
there
any number for which the condition could be false? No--so in this
case why bother asking? if
number < 0 or number > 100: makes
sense--if we want to find any values that are less than zero or greate
than 100, but want to omit numbers from 0 to 100, inclusive.
There are
ways to get even more "compounded" conditions, so to speak--where
we might even mix ands and ors.
NESTED CONDITIONALS. Consider the following:
number = input("please type in a number")
if number < 0:
print "this number is negative"
if
number < -100:
print "this number is VERY negative!"
else:
print
"this number is nonnegative"
simpleif2
Note that the if number < -100:
is part of the first block--and note
how we have indented further. "this number is VERY negative!"
will only print if the first condition (number < 0) is true, and only
if the condition number < -100 is ALSO true. You should also
note
that for Python, the indentation is very important. See what
happens:
number = input("please type in a
number")
if number < 0:
print "this number is negative"
if number < -100:
print "this number is VERY negative!"
else:
print "this number is nonnegative"
simpleif2a
Python, because of the indentation, associates the else with the
if number < -100: condition--not with the if number < 0:
condition. Consequently--if you had typed in the value 68, there
would have been no output at all! (why?). If you type in the value
-555 the output would be:
this number is negative
this number is VERY negative!
And if you type in the value -15, the output would be
this number is negative
this number is nonnegative
In this case, Python is happy with the syntax--but you have made
a semantic error. python is happy--the human is not!
Suppose, to continue here, we want to output only a single statement--
a number is either negative, VERY negative, or nonnegative.
number = input("please type in a number")
if number < 0:
if number < -100:
print "this number is VERY negative!
else:
print "this number is negative"
else:
print "this number is nonnegative"
simpleif3
(There are also other ways of structuring this whole thing). We
can
build some complex stuff this way.