Home  >  Article  >  Backend Development  >  What is the selection statement in python?

What is the selection statement in python?

藏色散人
藏色散人Original
2020-07-07 11:34:408951browse

Python's selection statements mainly have three forms, namely: 1. if statement; 2. "if...else" statement; 3. "if...elif...else" multi-branch statement .

What is the selection statement in python?

Python selection statement

There are three main forms of selection statements in Python, namely:

if语句
    格式:
        if 表达式:
            语句块(执行代码)
if...else语句
    格式:
        if 表达式:
            语句块
        else:
            语句块
 if...elif...else多分支语句
    格式:
        if 表达式1:
            语句块1
        elif 表达式2:
            语句块2
        elif 表达式3:
            语句块3
        ...
        else:
            语句块n
# if 实例
print("今有物不知其数,三三数之剩二,五五数之剩三,七七数之剩二,问几何?")
num = int(input("请输入你认为符合条件的数字:"))
if (num % 3 == 2) and (num % 5 == 3) and (num % 7 == 2):
    print(num,"符合三三数之剩二,五五数之剩三,七七数之剩二")
# if...else 实例
print("今有物不知其数,三三数之剩二,五五数之剩三,七七数之剩二,问几何?")
num = int(input("请输入你认为符合条件的数字:"))
if (num % 3 == 2) and (num % 5 == 3) and (num % 7 == 2):
    print(num,"符合三三数之剩二,五五数之剩三,七七数之剩二")
else:
    print(num,"不符合!")

Recommended: "python tutorial"

The above is the detailed content of What is the selection statement in python?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn