Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use elif conditional statement in python

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article is to share with you about how to use elif conditional statements in python. The editor thinks it is very practical, so I share it with you. I hope you can get something after reading this article.

I. nesting of if statements

When If statements can be nested

If condition judgment:

Print ("Liu Jinyu programming")

If condition judgment:

Print ("programming creates cities")

If condition judgment:

Print ("blog http://ljy.kim")"

II. Else statement

If... [else... .] Statement

This contiguous statement, as a direct result, executes either the module content of if or the module content of else. The final result can only be one of them.

Simple multiple if statements

At the same level, if there are several if statement modules, it should be judged several times.

III. Elif statement

Elif statement module, which can appear multiple times.

Prerequisites must be used in conjunction with if.

Format:

If condition judgment:

Execute Code 1

Elif condition judgment:

Execute Code 2

Elif condition judgment:

Execute Code 3

... . [here the code can have elif statements multiple times]

Note conclusion: in the continuous use of if+elif statements, the judgment result should be executed at least once.

III. The difference between if and elif

The difference between If statement and elif statement

1. The situation of If statement:

There are N if judgment statements, conclusion: execute n judgments

2. The situation of elif statement:

There are n judgment statements. Conclusion: execute judgment at least once and at least n times.

The conclusion is that the efficiency of elif statement is higher than that of if statement.

4. Multiple elif statements

If... .elif... Elif... Many judgments... Else..

If there are N multiple judgments, then this statement executes at least N multiple judgments.

If all the conditions are not met, the else statement module will be executed

X=input ("condition 1:")

If x = "111":

Print ("Liu Jinyu programming")

Elif x = "222":

Print ("programming creates cities!")

Elif X-ray = "333":

Print ("Welcome to subscribe to my channel")

Elif Xerox = "444":

Print ("Let's not be superstitious")

Elif x = "555":

Print ("please don't cry!")

Else:

Print ("Welcome! thank you, Lao Liu!")

5. Summary and emphasis

1. Nesting use of if statements

2. Multiple uses of if statements

3. The use of elif statement

4. Understand the difference between if statement and elif sentence

This section knowledge source code 1:

X=input ("condition 1:") y=input ("condition 2:") z=input ("condition 3:") if Xuan = "111": print (" Liu Jinyu programming ") if Yee =" 222": print ("programming creates the city") if Zhuan = "333": print ("blog http://ljy.kim") else: print (" Thank you guys! ") If: print ("Let's not be superstitious!") If print ("Please don't cry!")

This section knowledge source code 2:

X=input ("condition 1:") if X-ray = "111": print (" Liu Jinyu programming ") elif X-ray =" 222": print ("programming creates the city!") Elif Xbox = "333": print ("Welcome to subscribe to my channel!") Elif: print ("Let's not be superstitious!") Elif print ("Please don't cry!") Else: print ("Welcome! thank you, Lao Liu!") The above is how to use elif conditional statements in python. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report