Latest web development tutorials

Python3 controlled conditions

Python conditional statement is executed by one or more statements of results (True or False) to determine the execution of the code block.

The following figure can be a simple understanding of the execution of the conditional statement:


if statement

The general form of Python if statement is as follows:

if condition_1:
    statement_block_1
elif condition_2:
    statement_block_2
else:
    statement_block_3
  • If "condition_1" to True will perform "statement_block_1" block statement
  • If "condition_1" is False, the judge "condition_2"
  • If "condition_2" to True will perform "statement_block_2" block statement
  • If "condition_2" is False, will perform "statement_block_3" block statement

Pythonelif used instead of else if,so if statementkeywords:if - elif - else.

note:

  • 1, the back of each condition you want to use a colon (:), followed by a statement showing the condition after the block to be executed.
  • 2, using indentation to divide the block of statements indented the same number of statements together to form a block.
  • 3, there is no switch in Python - case statement.

Examples

The following is a simple instance if:

#!/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!")

Implementation of the above code, the output is:

1 - if 表达式条件为 true
100
Good bye!

From the results can be seen due to the variable var2 is zero, so if statement within the corresponding not executed.

The following example demonstrates the dog's age calculation to determine:

#!/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 键退出')

The above script is saved in dog.py file and execute the script:

$ python3 dog.py 
请输入你家狗狗的年龄: 1

相当于 14 岁的人。
点击 enter 键退出

Following is a common operation if the operator:

Operators description
< Less than
<= less than or equal to
> more than the
>= greater than or equal to
== Equal comparison objects are equal
!= not equal to

Examples

#!/usr/bin/python3

# 程序演示了 == 操作符
# 使用数字
print(5 == 6)
# 使用变量
x = 5
y = 8
print(x == y)

Examples of the above output:

False
False

high_low.py file demonstrates a digital comparison operation:

#!/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 scripts, sample output results are as follows:

$ python3 high_low.py 
数字猜谜游戏!
请输入你猜的数字:1
猜的数字小了...
请输入你猜的数字:9
猜的数字大了...
请输入你猜的数字:7
恭喜,你猜对了!

if nested

In nested if statements, you can put if ... elif ... else structure on another if ... elif ... else structure.

if 表达式1:
    语句
    if 表达式2:
        语句
    elif 表达式3:
        语句
    else
        语句
elif 表达式4:
    语句
else:
    语句

Examples

# !/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 to test_if.py file after execution output is:

$ python3 test.py 
输入一个数字:6
你输入的数字可以整除 2 和 3