if...elif...else

The use of conditionals in Python is intuitive:

  1. >>> for i in range(3):
  2. ... if i == 0:
  3. ... print 'zero'
  4. ... elif i == 1:
  5. ... print 'one'
  6. ... else:
  7. ... print 'other'
  8. ...
  9. zero
  10. one
  11. 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.

  1. >>> for i in range(3):
  2. ... if i == 0 or (i == 1 and i + 1 == 2):
  3. ... print '0 or 1'
  4. ...
  5. 0 or 1
  6. 0 or 1