In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the example analysis of process control and condition judgment in Python, which has a certain reference value, and interested friends can refer to it. I hope you will gain a lot after reading this article.
Initial recognition of conditional judgment (logical judgment) logical judgment and logical sentence
What is logical judgment? -> the judgment of whether something is right or not, that is, the judgment of true or false; in python, we use the bool type to judge true and false, right and wrong.
What is logical business? -> doing different things according to the results of logical judgment is the logical business that we need to do.
What is a conditional statement? -> for the judgment statement that the condition is satisfied, it is the conditional statement.
A logical statement is a combination of conditional statements and business statements.
If statement
The function of if statement: judge the authenticity of a condition, and execute the logical statement of if if the result of the condition is true (that is, True).
The usage of the if statement: an example is as follows
If bool_result: # syntax block do # business code block. Here you need to indent # > bool_result: judge whether the result is true or false, Boolean type. # > do: the business code block executed if bool_result is Treu. # > > if belongs to keyword No return value man = 'Neo'if man = =' Neo': print ('this man's name is:', man) # the result is as follows: the man's name is: Neoman = 'Neo'if man = =' Neo': print ('this man's real name is:', man.replace ('Neo') ('Jack')) # the execution result is as follows: # > this man's real name is: Jack's summary of logical judgment applications.
If you want to implement a complex functional program, logical judgment is essential. For the partners who have just come into contact with programming, it is difficult to carry out follow-up programming if they do not master good logic judgment, so it is necessary to summarize the definition of logic judgment again to help you better understand and apply it.
What is logical judgment? The so-called logical judgment is to judge the true and false result of the judgment sentence in a program, and the most basic criterion of logical judgment result is Boolean type. Boolean types have only two values: True and False, which correspond to 1 and 0 in Python.
Look at the following example:
Name = 'Xiao Ming' if name = 'Xiao Ming': name = 'Xiao Hong' print (name) # the execution result is as follows: # > Xiao Hong
Code interpretation: in the program, if name is equal to "Xiao Ming", the program will do the corresponding logic processing, changing name to "Xiao Hong". And print "Xiao Hong", if name is not equal to "Xiao Hong", then the program jumps out of if judgment to print name. The result of the output shows that the variable value of name becomes "Xiao Hong", which means that the judgment is True, so it goes to the code block under the if statement.
If... Else... Statement
With regard to the if statement, we know that it is to judge the authenticity of a condition. If the result of the condition is true (that is, True), then the logical statement of if is executed, which is the entry of the code block where the condition is true. Else is the opposite.
Else statement function: for the entry of a code block that executes another condition when the if condition is not met.
The usage of the else statement: an example is as follows
If bool_result: # syntax block do # business code block. Here, you need to pay attention to indenting else: elsedo # else grammar block, which needs to be indented; indentation level is consistent with do syntax block # > bool_result: judge whether the result is true or false, Boolean type. # > elsedo: the python code block corresponding to the else statement. # > else belongs to syntax and does not return a value # * sleep_time = '22:00' if sleep_time! = '22:00': print ('not yet', sleep_time, 'you can have another round of King's pesticides.') Else: print ('already', sleep_time, 'time to go to bed.') # > it's 22:00, it's time to go to bed. Elif statement
What is elif? -> elif (or if) multiple non-first judgments of conditions, each of which corresponds to a set of business codes.
The function of elif statement: the judgment statement of other conditions after the judgment of the first if statement is not satisfied.
The usage of the elif statement: an example is as follows
If bool_result: # Syntax Block do # Business Code Block Here we need to pay attention to indenting elif bool_result: elifdo # the grammar block corresponding to the current elif statement elif bool_result: elifdo # indentation level is the same as the do grammar block else: elsedo # else grammar block, need to indent The indentation level is consistent with the do syntax block # > elifdo: the python code block corresponding to the elif statement. # > elif belongs to syntax and does not return the value new_time= input ('Please enter time:') if new_time=='7 point': print (new_time,'it's time to get up.') Elif new_time=='8 point': print (new_time, 'time for breakfast') elif new_time=='12 point: print (new_time, 'time for lunch') elif new_time=='18 point': print (new_time, 'time for dinner') elif new_time=='22 point': print (new_time,' Time to go to bed') else: print ('what to do') the nesting of conditions for conditional judgment
In the program of conditional judgment, conditional judgment can also be a new conditional judgment statement, which forms a conditional nesting structure, as follows:
1. The program first determines whether condition 1 is true.
two。 If condition 1 is true, determine whether condition 2 is true
Condition 1 is true and condition 2 is true, execute code block 1
Condition 1 is true and condition 2 is false, execute code block 2
3. If condition 1 is false, determine whether condition 3 is true
Condition 1 is false and condition 3 is true, execute code block 3
Condition 1 is false and condition 3 is false, execute code block 4
Examples are as follows:
Sex = input ('please enter gender (man/woman):') age = int (input ('please enter age:') if sex = 'man': if age > 60: print (' old man') else: print ('young man') else: if age > 60: print (' old woman') else: print ('young woman')
First, the user enters the gender as man
Then, the user enters an age of 20
Conditional judgment sex = = man on line 3 and age > 60 on line 9
Execute the statement on line 12
Output young man
Summary of conditional statements
When a conditional statement satisfies a condition, the current conditional statement is exited.
In each conditional statement, there is only and must be one if statement, which can have 0 or more elif statements, 0 or 1 else statements.
Each conditional statement if must be the first conditional statement
A little exercise on conditional statements
Demand-the following is the standard of taxi fare in a city:
The starting price is 13 yuan, and the charge within 3 kilometers is 13 yuan.
More than 3 kilometers, the basic unit price is 2.3 yuan / km
For more than 10 kilometers, the basic unit price is plus 20%, that is, 2.76 yuan per kilometer.
Fuel surcharge 1 yuan per time
The taxi charging program is written according to this standard.
Examples are as follows:
Kilometer = float (input ('Please enter mileage:') # get mileage Fuel_oil_price = 1 # fuel surcharge start_price = 13 # starting price price = 0 # initialization fee if kilometer
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.