1:print('2>1')"."/> 1:print('2>1')".">

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

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

angryTom
angryTomOriginal
2020-02-11 10:44:265811browse

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")#执行到了

2.If-else example

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

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")

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!

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