Detailed explanation of conditional statements that must be mastered in Python

Learn Python data science, play games, learn Japanese, and engage in programming all in one package.

The data applied in the whole set of self-study courses are the contents of the games of "Three Kingdoms" and "Dynasty Three Kingdoms Warriors" series.

Conditional statements in Python perform different calculations or actions depending on whether a specific Boolean constraint evaluates to true or false. Conditional statements are handled by the IF statement in Python.

The code demonstrated so far consists of sequential execution. But the reality is that often a program needs to skip some statements, execute a series of statements repeatedly, or choose between an alternate set of statements to execute.

This needs to be applied to control structures , which direct the execution order of statements in a program (called the program's flow of control).

insert image description here

if statement

insert image description here

if <布尔计算的表达式>:
    <执行的python语句>

# 举例
x = 1
y = 2

if x < y:                            # 真
    print('yes')
yes

if y < x:                            # 假
    print('yes')


if x:                                # 假
    print('yes')

if y:                                # 真
	print('yes')
yes

if x or y:                           # 真
    print('yes')
yes

if x and y:                          # 假
	print('yes')


if '無雙' in '三國無雙':                # 真
	print('yes')
yes

if '郭嘉' in ['王異', '夏侯惇', '夏侯淵']:  # 假
	print('yes')

Grouping Statements: Indentation and Blocks

Evaluate the multi-conditional selection case when the condition is true.

After passing Xiangyang, Zhuge Liang said that the first master attacked Cong, and Jingzhou could have it. The First Sovereign said: "I can't bear it." - "Three Kingdoms, Shu Shu, The First Sovereign Biography"
If Cao Cao attacked Jingzhou, Liu Bei had several options:
1. Take Cong and replace him
2. Evacuate to the south
3. Combine with Sun Quan to block Cao Cao
4. Surrender directly.

if <布尔计算的表达式>:
	<执行的python语句1>
	<执行的python语句2>
	<执行的python语句3>

Python follows a convention called the offside rule, and languages ​​that follow the offside rule define blocks by indentation. Indentation is used to define compound statements or blocks.

The compound if statement is shown below.

if <布尔计算的表达式>:
    <执行的python语句1>
    <执行的python语句2>
    ...
    <执行的python语句3>
<后续的语句>

if '孫尚香' in ['太史慈', '大喬', '丁奉']:
    print('1')
    print('2')
    print('...')
    print('n')
print('0')

insert image description here

else and elif clauses

else statement

Evaluates a condition with an else clause and executes a statement if it is true.

if <布尔计算的表达式>:
	<执行的python语句1>
else:
	<执行的python语句2>

x = 20
if x < 50:
    print('1111')
    print('2222')
else:
    print('2222')
    print('1111')
1111
2222


x = 120
if x < 50:
    print('1111')
    print('2222')
else:
    print('2222')
    print('1111')
2222
1111

elif statement

elif can specify any number of clauses, one and only one of which is true.

name = '孫権'
if name == '孫尚香':
    print('{}!'.format(name))
elif name == '太史慈':
    print('{}!'.format(name))
elif name == '孫権':
    print('{}!'.format(name))
elif name == '大喬':
    print('{}!'.format(name))
else:
    print("谁啊?")
    
孫権!

It is also easier to apply a dictionary to manipulate data.

names = {
    
    
    '孫権': '孫権!',
    '太史慈': '太史慈!',
    '大喬': '大喬!',
    '孫尚香': '孫尚香!'
}

print(names.get('孫権', "谁啊?"))
孫権!

print(names.get('小喬', "谁啊?"))
谁啊?

single line if statement

Short form of if.

if <布尔计算的表达式>:
	<执行的python语句>
# 等价于
if <布尔计算的表达式>: <执行的python语句>


if '三國無雙' in '真·三國無雙': print('真'); print('真'); print('真')

else and elif can also be used to append to single-line shorthands.

x = 2
if x == 1: print('魏'); print('魏'); print('魏')
elif x == 2: print('蜀'); print('蜀')
else: print('晋'); print('晋')

Ternary operator conditional expression

insert image description here

<执行的python语句1> if <布尔计算的表达式> else <执行的python语句2>

age = 27
s = '諸葛亮 未出山' if age < 21 else '諸葛亮 已出山'
s
'諸葛亮 已出山'

if else Standard statement.

if a > b:
    m = a
else:
    m = b

# 等价于
m = a if a > b else b

# <执行的python语句1> if <布尔计算的表达式> else <执行的python语句2>
x = y = 40
z = 1 + x if x > y else y + 2
z
42

# 如果<布尔计算的表达式>为真,<执行的python语句1>则返回并且<执行的python语句2>不执行。
z = (1 + x) if x > y else (y + 2)
z
42

# 如果<布尔计算的表达式>为假,<执行的python语句2>则返回并且<执行的python语句1>不执行。
x = y = 40
z = 1 + (x if x > y else y) + 2
z
43

pass statement

Without changing the program, pass is used as a placeholder and is an empty execution.

Usually we use pass as a placeholder for a function or conditional sub-statement, indicating that specific content can be filled in in the future.

if True:
    pass

print('呂布')

Guess you like

Origin blog.csdn.net/qq_20288327/article/details/124475858