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




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


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. 


if condition:


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

statement - the code to execute when the statement is true


 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!