Home > Backend Development > Python Tutorial > What statement is used to implement the selection structure in Python?

What statement is used to implement the selection structure in Python?

angryTom
Release: 2020-02-11 10:44:26
Original
5844 people have browsed it

What statement is used to implement the selection structure in Python?

What statement is used to implement the selection structure in Python

The selection structure statement in Python is if elif else.

Python uses indentation instead of the braces {} commonly used in Java/C/C/C# to distinguish code blocks. In addition, Python needs to use a colon at the end of a column containing a selection keyword.

The following uses three examples to demonstrate the usage of the selection structure in Python

1. Only if example

a=1
if(a<0):
    print("a<0")#执行不到
    print("a<0")#执行不到
print("a=1")#执行到了
Copy after login

2.If-else example

a=1
if a<0 :#注意此处没有(a<0)的括号也行
    print("a<0")#执行不到
    print("a<0")#执行不到
else:
    print("a>=0")
Copy after login

3, if-elif-else example

a=1
if a<0 :
    print("a<0")#执行不到
    print("a<0")#执行不到
elif a<1:#注意不是else if
    print("0<a<1")#执行不到
else:
    print("a>=1")
Copy after login

Recommended: Python tutorial

The above is the detailed content of What statement is used to implement the selection structure 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