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

What is the form of Python if else conditional statement

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article introduces the relevant knowledge of "what is the form of Python if else conditional statement". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

We all know that the code is executed sequentially, that is, the first statement is executed first, then articles 2 and 3. All the way to the last statement, which is called a sequential structure.

But for many cases, the code with sequential structure is far from enough, for example, a program is limited to adults, and children do not have permission to use it because they are not old enough. At this point, the program needs to judge whether the user is an adult or not, and give a hint.

In Python, you can use if else statements to judge conditions, and then execute different code according to different results, which is called selective structure or branch structure.

If else statements in Python can be subdivided into three forms, namely if statements, if else statements, and if elif else statements, whose syntax and execution flow are shown in Table 1.

There are three forms of if else branch statements:

If expression syntax format: code block if statement execution flow

If expression syntax format: code block 1 else: code block 2 if else statement execution flow

If expression syntax format 1: code block 1 elif expression 2: code block 2 elif expression 3: code block 3... / / other eli statements else: code block n if elif else statement execution flow

Of the above three forms, the second and the third form are interlinked, and if the elif block in the third form does not appear, it becomes the second form. In addition, neither elif nor else can be used alone, they must appear with if and be paired correctly.

A description of the syntax format:

An expression can be a single value or variable, or a complex statement made up of operators in any form, as long as it can get a value. Regardless of the type of result of an expression, if else can determine whether it is true (true or false).

A code block consists of several statements with the same amount of indentation.

If, elif, and else statements all end with a colon: don't forget.

Once an expression holds, Python executes the corresponding block of code that follows it; if none of the expressions holds, the block of code after else is executed; if there is no else part, nothing is executed.

The simplest form of execution is the first form-- there is only one if part. If the expression is true (true), the following block of code is executed; if the expression is not true (false), nothing is executed.

For the second form, block 1 immediately following if is executed if the expression is valid; if the expression is not valid, block 2 immediately following else is executed.

For the third form, Python determines whether the expression is valid from top to bottom, and once it encounters a valid expression, it executes the following block of statements; at this point, the rest of the code is no longer executed, regardless of whether the following expression is true or not. If none of the expressions hold, execute the block of code that follows the else.

To sum up, no matter how many branches there are, only one branch can be executed, or none can be executed, and multiple branches cannot be executed at the same time.

[example 1] use the first selection structure to determine whether the user meets the criteria:

Age = int (input ("Please enter your age:") if age < 18: print ("you are underage, it is recommended to use this software with your family!") Print ("if you have your parents' consent, please ignore the above tips.") # this statement does not belong to if's code block print ("software is in use.")

Run result 1:

Please enter your age: 16 ↙

You are underage, it is recommended to use the software accompanied by your family!

If you have obtained the consent of your parents, please ignore the above tips.

The software is in use.

Run result 2:

Please enter your age: 24 ↙

The software is in use.

As you can see from the run result, if the input age is less than 18, the statement block after if is executed; if the input age is greater than or equal to 18, the statement block after if is not executed. The statement blocks here are two print () statements indented by four spaces.

[example 2] improve the above code to exit the program if the age does not match:

Import sysage = int (input ("Please enter your age:") if age < 18: print ("warning: you are underage and cannot use the software!") Print ("minors should study hard, go to a good university and serve the motherland.") Sys.exit () else: print ("you are an adult, you can use this software.") Print ("time is precious, please don't waste too much time on this software.") Print ("Software is in use...")

Run result 1:

Please enter your age: 16 ↙

Warning: you are underage and cannot use this software!

Minors should study hard, go to a good university and serve the motherland.

Run result 2:

Please enter your age: 20 ↙

You are an adult and can use the software.

Time is precious, please don't waste too much time on this software.

The software is in use.

The exit () function of the sys module is used to exit the program.

[example 3] judge whether a person's figure is reasonable:

Height = float (input ("enter height (m):") weight = float (input ("input weight (kg):") bmi= weight / (height * height) # calculate the BMI index if bmi=18.5 and bmi=24.9 and bmi

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

Development

Wechat

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

12
Report