Python3 conditional control
Python conditional statements are code blocks that are executed based on the execution results (True or False) of one or more statements.
You can simply understand the execution process of conditional statements through the following figure:
if statement
The if statement in Python The general form is as follows:
if condition_1: statement_block_1 elif condition_2: statement_block_2 else: statement_block_3
If "condition_1" is True, the "statement_block_1" block statement will be executed
If "condition_1" is False , will judge "condition_2"
if "condition_2" If it is True, the "statement_block_2" block statement will be executed
If "condition_2" is False, the "statement_block_3" block statement will be executed
Used in Python elif replaces else if, so the keywords of the if statement are: if – elif – else.
Note:
1. Use a colon (:) after each condition to indicate that the next statement is to be executed after the condition is met. piece.
2. Use indentation to divide statement blocks. Statements with the same indentation number form a statement block together.
3. There is no switch – case statement in Python.
Example
The following is a simple if example:
#!/usr/bin/python3 var1 = 100 if var1: print ("1 - if 表达式条件为 true") print (var1) var2 = 0 if var2: print ("2 - if 表达式条件为 true") print (var2) print ("Good bye!")
Execute the above code, the output result is:
1 - if 表达式条件为 true 100 Good bye!
From As a result, you can see that since the variable var2 is 0, the corresponding statement in the if is not executed.
The following example demonstrates the calculation and judgment of the dog's age:
#!/usr/bin/python3 age = int(input("请输入你家狗狗的年龄: ")) print("") if age < 0: print("你是在逗我吧!") elif age == 1: print("相当于 14 岁的人。") elif age == 2: print("相当于 22 岁的人。") elif age > 2: human = 22 + (age -2)*5 print("对应人类年龄: ", human) ### 退出提示 input('点击 enter 键退出')
Save the above script in the dog.py file and execute the script:
$ python3 dog.py 请输入你家狗狗的年龄: 1 相当于 14 岁的人。 点击 enter 键退出
The following is the if Commonly used operation operators:
Operator | Description |
---|---|
< | Less than |
<= | Less than or equal to |
##>
| is greater than |
>=
| is greater than or equal to |
==
| Equal, whether the comparison objects are equal|
!=
| Not equal to
#!/usr/bin/python3
# 程序演示了 == 操作符
# 使用数字
print(5 == 6)
# 使用变量
x = 5
y = 8
print(x == y)
The output result of the above example:False FalseThe high_low.py file demonstrates the comparison operation of numbers:
#!/usr/bin/python3 # 该实例演示了数字猜谜游戏 number = 7 guess = -1 print("数字猜谜游戏!") while guess != number: guess = int(input("请输入你猜的数字:")) if guess == number: print("恭喜,你猜对了!") elif guess < number: print("猜的数字小了...") elif guess > number: print("猜的数字大了...")Execute the above script, the example output result is as follows:
$ python3 high_low.py 数字猜谜游戏! 请输入你猜的数字:1 猜的数字小了... 请输入你猜的数字:9 猜的数字大了... 请输入你猜的数字:7 恭喜,你猜对了!
if NestingIn the nested if statement, you can put if...elif. The ..else structure is placed inside another if...elif...else structure.
if 表达式1: 语句 if 表达式2: 语句 elif 表达式3: 语句 else 语句 elif 表达式4: 语句 else: 语句Example
# !/usr/bin/python3
num=int(input("输入一个数字:"))
if num%2==0:
if num%3==0:
print ("你输入的数字可以整除 2 和 3")
else:
print ("你输入的数字可以整除 2,但不能整除 3")
else:
if num%3==0:
print ("你输入的数字可以整除 3,但不能整除 2")
else:
print ("你输入的数字不能整除 2 和 3")
Save the above program into the test_if.py file. After execution, the output result is: $ python3 test.py 输入一个数字:6 你输入的数字可以整除 2 和 3