Personal tools
You are here: Home CDAT Tips and Tricks Python Tips Conditional statements.
Document Actions

Conditional statements.

by Renata McCoy last modified 2007-05-08 10:44

Goal: Learn about conditional statements in python.

 
The if and else statements provide an easy way to perform tests. For instance:
if x != y:
print 'x is not equal to y'
else:
print 'x and y are equal'
The indentation is required to isolate the if and else clauses, but the else clause is optional. Do nothing clauses can be created by using the pass statement.
if x != y:
pass
else:
print 'x and y are equal!'
Multiple test cases can be implemented using the elif clause.
if x == 'n':
print 'Answered no'
elif x == 'y':
print 'Answered yes'
else:
print 'invalid answer'
Boolean expressions can be formed by using or, and, and not keywords.
if x > y and z > x:
print 'z is the max value'
if not (x==z or y==z or x==y):
print 'There are no equal values'


Powered by Plone