The general Python syntax for a simple if statement is as follows:
if condition:
Indented code blocks
If the condition is true, then the indented code blocks are implemented; if not, then the indented code blocks will be skipped.
The result of an if condition is a boolean type of value, and its legal value is either True or False. Bool is short for boolean in Python.
The simplest condition is to compare two variables. The following table lists the comparison symbols of all the usual arithmetic.
Text |
Math Symbols |
Python Symbols |
---|---|---|
Less than |
< |
< |
Greater than |
> |
> |
Less than or equal |
<= |
<= |
Greater than or equal |
>= |
>= |
Equals |
= |
== |
Not equal |
!= |
!= |
For example:
$var1 > 90
$var1 > $var2
($var1 - $var2)/($var3-$var4) > 1
And you can use and or or operators to combine two simple conditions. For example:
$var1 <90 and $var1>80
$var3 == 1 or $var4 == 1
Tip: All the system built-in variables can be compared with None. For example:
$var1 is None |
Same as " var1 == None", the returned result is True if the $var1 variable is not specified a value, otherwise, return False. |
$var1 is not None |
Same as " var1 != None", the returned result is True if $var1 has a value, otherwise, return False. |
Note: A single equal sign (=) is not used to check for equality in Python. Use the double equal sign (==) instead.