Knowledge Base‎ > ‎Programming‎ > ‎Python‎ > ‎Python Reference‎ > ‎

elif


Related:

 Examples

 
 
if age > 16:
print "You can drive!" elif age == 16: print "You just made it!" else: print "Nope, get a skateboard!"

 Description        

 
Elif is used with if - it allows for a second condition to be tested only but only when the first condition is false. It is a combination of if and else. It must be used after an if. You can use several elif's in a row if needed.

 Syntax

 
 
if condition:
    statement(s)
elif condition:
    statement(s)

 Parameters

 
condition - an expression which can be evaluated to either true or false

statement - the code to execute when the statement is true

 Interpreter

 Python 2.6.5 (r265:79359, Mar 24 2010, 01:32:55) 

[GCC 4.0.1 (Apple Inc. build 5493)] on darwin

Type "help", "copyright", "credits" or "license" for more information.

>>> age = 16

>>> if age > 16:

...   print "You can drive!"

... elif age == 16:

...   print "You just made it!"

... 

You just made it!

>>> 





Comments