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

What is the selection statement in python?

藏色散人
Release: 2020-07-07 11:34:40
Original
9005 people have browsed it

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 表达式:
            语句块(执行代码)
Copy after login
if...else语句
    格式:
        if 表达式:
            语句块
        else:
            语句块
Copy after login
 if...elif...else多分支语句
    格式:
        if 表达式1:
            语句块1
        elif 表达式2:
            语句块2
        elif 表达式3:
            语句块3
        ...
        else:
            语句块n
Copy after login
# 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,"不符合!")
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template