Conditional statements

Python conditional statements are basically the same as other languages. The execution result of one or more statements (True or False) determines the code block to be executed.

The Python programming language specifies that any non-zero and non-empty (null) value is True, and 0 or null is False.

The execution flow chart is as follows:

c580c12fe76cd4b072e63fe86f9ddf8.png

1. The basic form of the if statement

In Python, the basic form of the if statement is as follows:

if 判断条件:
    执行语句……
else:
    执行语句……

As mentioned earlier, the Python language has strict indentation requirements, so you need to pay attention to the indentation here, and don’t write less colons: .

The judgment conditions of the if statement can be expressed by > (greater than), < (less than), == (equal to), >= (greater than or equal to), <= (less than or equal to). .

For example:

# -*-coding:utf-8-*-
results=59
if results>=60:
    print ('及格')
else :
    print ('不及格')

The output result is:

不及格

As mentioned above, non-zero values, non-empty strings, non-empty lists, etc., are judged as True, otherwise False. Therefore, it can also be written like this:

num = 6
if num :
    print('Hello Python')

2. The form of multiple judgment conditions of if statement

Sometimes, we cannot have only two judgment statements. More than one is needed. For example, in the above example, if the score is greater than 60, it is considered passing. Then we have to judge that the score is greater than 90, which is excellent. What about the score between 80 and 90 that is good?

At this time, multiple judgment conditions of the if statement need to be used.

Use pseudo code to express:

if 判断条件1:
    执行语句1……
elif 判断条件2:
    执行语句2……
elif 判断条件3:
    执行语句3……
else:
    执行语句4……

Example:

# -*-coding:utf-8-*-
results = 89
if results > 90:
    print('优秀')
elif results > 80:
    print('良好')
elif results > 60:
    print ('及格')
else :
    print ('不及格')

The output result:

良好

3. The if statement can judge multiple conditions at the same time

Python does not have a switch statement like Java, so multiple condition judgments can only be implemented using elif, but Sometimes when multiple conditions need to be judged at the same time, you can use or (or), which means that the judgment condition is successful when one of the two conditions is true; when using and (and), it means that the judgment condition is successful when only two conditions are true at the same time. Only then succeeded.

# -*-coding:utf-8-*-
java = 86
python = 68
if java > 80 and  python > 80:
    print('优秀')
else :
    print('不优秀')
if ( java >= 80  and java < 90 )  or ( python >= 80 and python < 90):
    print('良好')

Output result:

不优秀
良好

Note: If there are multiple conditions, you can use brackets to distinguish the order of judgments. The judgments in brackets are executed first. In addition, and and or have low priority. Judgment symbols such as > (greater than) and < (less than), that is, greater than and less than, without parentheses, will take precedence over and or.

Continuing Learning
||
submitReset Code