Getting started with Python: if statement usage
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. For more information, see
The if statement in Python is 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:
?
1 2 |
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:
?
1 2 3 4 5 6 7 8 9 10 11 12 |
#! /Usr/bin/python Var1 = 1, 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, the following results are generated:
?
1 2 3 |
1-Got a true expression value 100 Good bye! |