This article mainly introduces the usage of if Statements in the Python getting started tutorial. It is the basic knowledge of getting started with Python. if statements in Python are similar to other languages. The if statement contains a logical expression that uses the data for comparison and makes a decision based on the comparison result.
Syntax:
The if statement syntax in the Python programming language is:
If expression: statement (s)
If the Boolean expression returns true, the if statement block is executed. If the Boolean expression of the if statement is calculated as false, the first group of code is executed.
The Python programming language assumes that any non-zero and non-null values are true. If it is zero or null, it is assumed that the value is false.
Example:
#! /Usr/bin/pythonvar1 = 100if var1: print "1-Got a true expression value" print var1var2 = 0if var2: print "2-Got a true expression value" print var2print "Good bye! "
When the above code is executed, the following results are generated:
1-Got a true expression value100Good bye!