In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
In this issue, the editor will bring you about the if process control of python. The article is rich in content and analyzed and described from a professional point of view. I hope you can get something after reading this article.
1. Process control and branch structure process control concept: process: process control of code execution: process control of code execution process control three major structures: (1) sequential structure: from top to bottom The code executes in turn (2) branch structure: a total of 4 (3) loop structures: while for branch structure (1) single branch (2) double branch (3) multi-branch (4) nest branch 2, if single branch syntax: if conditional expression: code block operation mechanism: if the conditional expression is true (true) The code block is executed, otherwise the code block is not executed. Note: the code block-- > starts with a colon and divides the same scope with indentation. This whole is called a code block. Scope-- > region of action # name1 = 'Xiaoguo' if name1 = = 'Xiaoguo': print ('my name is:', name) # result: # my name is: Xiaoguo # case # name2 = '' if name = = 'Xiaoguo': print ('my name:', name) # conditional expression is not valid and there is no running result. 3. If double branch structure syntax: if conditional expression: code block 1 else: code block 2 operation mechanism: if the conditional expression is true (true), execute code block 1; if the conditional expression is not true (false), execute code block 2 Note: the code block below if is called true interval else. Your code block is called false interval # example-money = 5if money > 5: print ('take a taxi home') else: print ('take a bus home') # run result: # take a bus home # example 2 username = input ('Please enter your user name:') password = input ('Please enter your user name:') Login password') if username==' Xiaoguo 'and password=='0000': print (' login successful Welcome to ~') else: print ('login failed, password or user name is wrong')
4 、 If's multi-branch structure syntax: if conditional expression 1: code block 1 elif conditional expression 2: code block 2 elif conditional expression 3: code block 3 else: code block 4 operation mechanism: default from top to bottom if condition Execute code block 1 if expression 1 is true Otherwise, execute down if a conditional expression in elif holds. The corresponding code block is executed. If none of the conditional expressions in if and elif holds, the code block corresponding to else is executed. Note: if a conditional expression is valid, the contents of the code block corresponding to the conditional expression are executed, and all the remaining conditional expressions and the code blocks corresponding to else are not executed. Elif can be 1 or more, and else can only be 1 Or there are no # cases 1 age = 20if age > 0 and age18 and age60: print ('old age') # results: # middle-aged # cases 2 print ('1: durian 2: coconut 3: peach 4: cantaloupe 5: mango') num= int (input ('please choose your favorite fruit:) if num==1:print (' you like durian') elif num==2:print ('you like coconut') ) elif num==2:print ('you like peaches') elif num==3:print ('you like apples') elif num==4:print ('you like cantaloupe') elif num==5:print ('you like mango') else: print ('you don't like fruit') # cases 2 print ('1: durian 2: coconut 3: peach 4: cantaloupe 5: mango') num= int ('please choose you Favorite fruit:') if num==1: print ('you like durian') elif num==2: print ('you like coconut') elif num==2: print ('you like peaches') elif num==3: print ('you like apples') elif num==4: print ('you like cantaloupe') elif num==5: print ('you like mango') else Print ('you don't like fruit')
5 、 Nest branch multi-layer if structure nested combination to form nest branch name = 'Xiaoguo' sex = 'male' age = 22occupation = 'student' grade = 'junior' if name = = 'Xiaoguo': if sex = = 'male': if age = = 22: if occupation = = 'student': if grade = = 'junior': Print ("my name is: {}" Gender {}:, age: {}, occupation: {}, length of service: {} ".format (name,sex,age,occupation,grade)) else: print (" ) else: print ("age error") else: print ("name error") # result: # my name is: Xiao Guo, gender male:, age: 22, occupation: student, seniority: junior, this is what the if process control of python shared by Xiaobian is like, if you happen to have similar doubts It may be understood with reference to the above analysis. If you want to know more about it, you are welcome to 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.
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.