if...elif...else
The use of conditionals in Python is intuitive:
>>> for i in range(3):
... if i == 0:
... print 'zero'
... elif i == 1:
... print 'one'
... else:
... print 'other'
...
zero
one
other
“elif” means “else if”. Both elif
and else
clauses are optional. There can be more than one elif
but only one else
statement. Complex conditions can be created using the not
, and
and or
operators.
>>> for i in range(3):
... if i == 0 or (i == 1 and i + 1 == 2):
... print '0 or 1'
...
0 or 1
0 or 1