Python: if/else Statements

By Eric Downing. Filed in Programming, Python, Scripting  |  
TOP del.icio.us digg

There comes a time when you are writing a program and you need to branch based on input to a program.
This is the format of the if/else statement in Python.

if <some true/false statement> :
  <some statements that run if the if statement is true>
else:
  <some other statements that run if the if statement are false>  

There may be times when you have more than one value set to test against. In that case you can write a many if/else
statements but you can chain the if/else statements with an elif. So the chain becomes if/elif/else.

if x < 5:
  <some statements that run if the if statement is true>
elif x > 5 and x < 10:
  <some statements that run if the elif statement is true>
else:
  <if none of the if checks are true, run the statements in the else case.>

Tags: ,

Leave a Reply