Python conditional statements
Python conditional statements are blocks of code that are executed by the execution result of one or more statements (True or False).
The following figure can be used to briefly understand the execution of conditional statements:
The Python programming language specifies any non-zero and non-null values to be true, 0 or null to false.
In Python programming, an if statement is used to control the execution of a program. The basic form is:
if Conditions: Execute statement... else: Execute statement...
When the "judgment condition" is established (non-zero), the following statement is executed, and the execution content can be multi-line, and the indentation is used to distinguish the same range.
else is an optional statement. When you need to execute the content when the condition is not met, you can execute the related statement. The specific examples are as follows:
The output is:
luren # output results
The judgment condition of the if statement can be expressed by > (greater than), < (less than), == (equal to), >= (greater than or equal to), and <= (less than or equal to).
When the judgment condition is multiple values, the following form can be used:
if Critical conditions1: Execute statement1... elif Conditions2: execute statement2... elif Conditions3: Execute statement3... else: execute statement4...
Examples are as follows:
The output is:
roadman # Output
Because Python does not support switch statements, multiple conditional judgments can only be implemented with elif. If it is judged that multiple conditions need to be judged at the same time, you can use or (or) to indicate that one of the two conditions is true. The judgment condition is successful; when using and, it means that only two conditions are satisfied at the same time, the judgment condition is successful.
When if there are multiple conditions, parentheses can be used to distinguish the order of judgment. The judgments in parentheses are executed first. In addition, the priority of and and or is lower than the judgment symbols such as (greater than) and < (less than). That is, greater than and less than in the case of no parentheses will be compared with or with priority.
Simple statement group
You can also use the if condition to determine statements on the same line, as in the following example:
The above code execution output is as follows：
variable var Value100 Good bye!