In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "what is the use of Python branch structure". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what is the use of Python branch structure".
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, different from C++, Java and other programming 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 strongly recommended that you do not use tab keys to indent code, if you are used to doing so, you can set up a code editing tool to automatically change 1 tab key into 4 spaces, many code editing tools support this function.
Tip: there is a colon at the end of if and else: it is a colon entered by the English input method; the special characters entered in the program, such as', ", =, (,), are all entered in the English input method. Many beginners often don't pay attention to this, and as a result, they will encounter a lot of inexplicable error tips when running the code. It is strongly recommended that everyone turn on the English input method when writing the code (note that it is the English input mode of the English input method rather than the English input mode of the Chinese input method), which can avoid a lot of unnecessary trouble.
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
Example 1: Imperial units inches and metric units centimeters are interchangeable.
"
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 "what is the use of Python branch structure". After the study of this article, I believe you have a deeper understanding of what the use of Python branch structure is, 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.