The introduction of

This article introduces The branching structure of Flow control in Python. Flow control is to control the execution process of the program. The execution process of the program is divided into three structures: sequence structure, branch structure and loop structure. The code we wrote before belongs to sequence structure, branch structure requires if judgment, and loop structure requires while and for loop.

Branching structure

The branch structure is to execute the code corresponding to different branches according to whether the conditions are valid. For example, if it rains today, bring an umbrella. Similarly, the computer also has the corresponding mechanism to judge the conditions.

The condition is the criterion for judgment, and there are only two possible criteria for judgment, one is true and the other is not true. If it rains, bring an umbrella.

What are the conditions, the criteria for judging? Conditions can be arbitrary expressions. Since there are only two possible criteria for judgment, all expressions in if judgment will be automatically converted to Boolean values, which can be used as the criterion for judgment. Boolean values can also be divided into two situations:

The first is an explicit Boolean value

The result of the comparison operation is a Boolean value
3 < 4  # True

Use Boolean values directly
is_true = True
is_false= False
Copy the code

The second kind: implicit Boolean value

All values can be used as conditions, just remember:0.NoneEmpty forFalse, the rest areTrue
Copy the code

If branch structure syntax format

The syntax of an if statement is very simple and executes when a branch condition is met. If the if branch fails, the code after the if branch continues to execute.

In an if branch code block, only if is required, the number of elIfs can be arbitrary, and else can occur at most once.

Note: Python identifies a block of code with the same indentation, usually four Spaces, in accordance with the PEP8 specification of Python. The same block of code runs from top to bottom.

ifconditions1:  If condition 1 results in True, code 1 is executedcode1
elifconditions2:# If condition 2 is True, code 2 is executedcode2
elifconditions3:# If condition 3 is True, code 3 is executedcode3.else:  Execute code 4 if none of the above conditions is truecode4
Copy the code

An example of an if branch structure

Simplest if structure

# If a boy is older than 35, he is called uncle
age_of_boy = 36
if age_of_boy > 35:
    print('Hello, uncle, has your daughter married yet? ')
Copy the code

if… The else structure

# If a boy is older than 35, he is called uncle, otherwise he is called little brother
age_of_boy = 36
if age_of_boy > 35:
    print('Hello, uncle, has your daughter married yet? ')
else:
    print('Brother, make a friend! ')
Copy the code

if… elif… elif… . The else structure

# Check who you are for me? Print the relationship with me based on user input
-Serena: "Boyfriend, mom, dad," and "Whatever."
name = input('Please enter your name :')
if name == 'xu':
    print('Boyfriend')
elif name == 'mom':
    print('mother')
elif name == 'dad':
    print('daddy')
else:
    print('Who are you? ')
Copy the code

If branch structures are nested

If the price of celery is less than 2 yuan per catty, buy it, otherwise go home empty-handed.
vegetables = 'celery'
price = 1.5
if vegetables == 'celery':
    if price < 2:
        print(Shall we buy more jin? Celery is cheaper today. ')
    else:
        print('Celery is too expensive today, I'll buy it next time. ')
else:
    print('There's no celery. Maybe next time. ')
Copy the code

If branch structure exercise – login function

The user enters the user name and password. If both the user name and password are equal to the preset values, the login succeeds. Otherwise, the login fails.

username = 'Python'
password = '123'

name = input('Please enter user name :').strip()
password = input('Please enter password :').strip()
if name == 'Python' and password == '123':
    print('Login successful')
else:
    print('Username')
Copy the code

At the end of the article

If you think my writing is good, please scan the QR code below to follow my wechat official account for more Python knowledge