Python IF statements
The if statement of Python is similar to that of other languages. The if statement contains a logical expression using which data is compared, and a decision is made based on the result of the comparison.
The syntax of an if statement in Python programming language is:
if expression: statement(s)
If the boolean expression evaluates to true then the block of statement(s) inside the if statement will be executed. If boolean expression evaluates to false then the first set of code after the end of the if statement(s) will be executed.
Python programming language assumes any non-zero and non-null values as true and if it is either zero or null then it is assumed as false value.
#!/usr/bin/python var1 = 100 if var1: print "1 - Got a true expression value" print var1 var2 = 0 if var2: print "2 - Got a true expression value" print var2 print "Good bye!"
When the above code is executed, it produces following result:
1 - Got a true expression value 100 Good bye!