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 Python branch structure

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains "how to use Python branch structure", the content of the article is simple and clear, easy to learn and understand, now please follow the editor's ideas slowly in depth, together to study and learn "how to use Python branch structure" bar!

The use of if statements

In Python, the if, elif, and else keywords are used to construct the branch structure. Keywords are words with special meanings, such as if and else are keywords specifically used to construct branch structures, and obviously you can't use them as variable names. The following example demonstrates how to construct a branch structure.

""

User authentication

Version: 0.1

Author: Luo Hao

""

Username = input ('Please enter user name:')

Password = input ('Please enter password:')

# if the user name is admin and the password is 123456, the authentication is successful or the authentication fails

If username = 'admin' and password =' 123456:

Print ('authentication successful!')

Else:

Print ('authentication failed!')

It should be noted that unlike Python Candace +, Java and other languages, Python does not use curly braces to construct code blocks but uses indentation to represent the hierarchical structure of the code. If the if condition is established, you need to execute multiple statements, as long as you keep multiple statements with the same indentation. In other words, if continuous code maintains the same indentation, then they belong to the same code block, which is equivalent to an execution as a whole. Indentation can use any number of spaces, but usually use 4 spaces. It is recommended that you do not use tab keys or set up your code editing tool to automatically turn tab keys into 4 spaces.

Of course, if you want to construct more branches, you can use if...elif...else... Structure or nested if...else... Structure, the following code demonstrates how to use a multi-branch structure to implement piecewise function evaluation.

""

Piecewise function evaluation

Version: 0.1

Author: Luo Hao

""

X = float (input ('x ='))

If x > 1:

Y = 3 * x-5

Elif x > =-1:

Y = x + 2

Else:

Y = 5 * x + 3

Print (fallowf ({x}) = {y}')

Of course, according to the needs of actual development, the branch structure can be nested. For example, to judge whether or not to pass the customs, you have to give a grade to your performance according to the number of treasures or props you get (such as lighting two or three stars), then we need to construct a new branch structure in if. Similarly, new branches can also be constructed in elif and else, which we call nested branch structure. In other words, the above code can also be written as follows.

""

Piecewise function evaluation

Version: 0.1

Author: Luo Hao

""

X = float (input ('x ='))

If x > 1:

Y = 3 * x-5

Else:

If x > =-1:

Y = x + 2

Else:

Y = 5 * x + 3

Print (fallowf ({x}) = {y}')

Explanation: you can feel and judge which of these two ways of writing is better. In the Zen of Python, there is a saying: "Flat is better than nested", the reason for advocating code "flattening" is that if there are many nesting levels of the nested structure, it will seriously affect the readability of the code, so do not use the nested structure when you can use the flat structure.

Some examples 1: the imperial unit inch and the metric unit centimeter interchange.

""

Interchange between inch and centimeter in imperial units

Version: 0.1

Author: Luo Hao

""

Value = float (input ('Please enter length:')

Unit = input ('Please enter units:')

If unit = 'in' or unit = =' inch':

Print ('% f inch =% f cm'% (value, value * 2.54))

Elif unit = 'cm' or unit = =' cm':

Print ('% f cm =% f inch'% (value, value / 2.54))

Else:

Print ('Please enter valid units')

Example 2: the score of the percentile system is converted to the grade system.

Requirements: if the input score is more than 90 points (including 90 points), the output Ascape score is 80 points-90 points (excluding 90 points), the output Bash score is 70 points-80 points (excluding 80 points), the output score is 60 points-70 points (excluding 70 points), and the output score is below 60 points for E.

""

Convert the score of the percentile system to the grade system.

Version: 0.1

Author: Luo Hao

""

Score = float (input ('Please enter grades:')

If score > = 90:

Grade ='A'

Elif score > = 80:

Grade ='B'

Elif score > = 70:

Grade ='C'

Elif score > = 60:

Grade ='D'

Else:

Grade ='E'

Print ('corresponding grades are:', grade)

Example 3: enter the length of three sides, and calculate the perimeter and area if you can form a triangle.

""

Judge whether the input side length can form a triangle, and if so, calculate the perimeter and area of the triangle.

Version: 0.1

Author: Luo Hao

""

A = float (input ('a ='))

B = float (input ('b ='))

C = float (input ('c ='))

If a + b > c and a + c > b and b + c > a:

Peri = a + b + c

Print (f 'perimeter: {peri})

Half = peri / 2

Area = (half * (half-a) * (half-b) * (half-c)) * * 0.5

Print (f 'area: {area}')

Else:

Print ('cannot form triangles')

Explanation: the above formula for calculating the area of a triangle by side length is called Helen's formula.

Thank you for your reading, the above is the content of "how to use the Python branch structure", after the study of this article, I believe you have a deeper understanding of how to use the Python branch structure, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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

  • How does html realize that the script never goes wrong

    This article will explain in detail how to achieve html script never go wrong, the editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article. Scripts never go wrong

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

    12
    Report