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

if


Related:

 Examples

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

 Description        

 
Provides a way to execute code depending on various conditions - for instance if a certain variables has a certain value. Any code immediately following and indented below the if statement is executed when the if statement condition is true. 

 Syntax

 
 
if 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.

>>> temp = 30

>>> temp > 25

True

>>> temp < 25

False

>>> if temp > 25:

...   print "it is hot!"

... 

it is hot!

>>> if temp < 25: 

...   print "it is not hot!"

... 

>>> 





Comments