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

else

Related:

 Examples

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

 Description        

 
Must be used along with an if statement. Provides a way to execute alternative code is the condition of the if statement is false.

Any code immediately following and indented below the else statement is executed when the if statement condition is false. 

 Syntax

 
 
if condition:
    statement(s)
else:
    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 = 14

>>> if age >= 16:

...     print "you can drive"

... else:

...     print "not yet!"

...     

not yet!


Comments