Python branch structure

Source: Internet
Author: User

If statement

If statement the computer can do a lot of automated tasks, because it can do their own conditions to judge. For example, to enter user age, print different content according to age, in a Python program, you can use the IF statement:= 20ifAge >= 18:    Print 'your age is', AgePrint 'Adult'Print 'END'Note: The indentation rules for Python code. Code with the same indentation is treated as a block of code, above 3, 4 linesPrintStatements form a block of code (but do not include print on line 5th). Ififstatement to True, this block of code is executed. Indent please strictly follow Python's customary notation: 4 spaces, do not use tab, do not Mix tab and space, otherwise it is easy to cause syntax errors due to indentation. Note:ifstatement followed by an expression, and then started with: representing the code block. If you hit the code in a Python interactive environment, also pay special attention to indentation, and exit indentation requires more than one line of return:>>> age = 20>>>ifAge >= 18:...     Print 'your age is', age ...Print 'Adult'... your age is20Adult

if-ElsewhenifWhen the statement evaluates to True for the expression, it executes theifblock of code included:ifAge >= 18:    Print 'Adult'if we want to determine the age of 18 years, print out'teenager', what to do? method is to write one moreif:ifAge < 18:    Print 'teenager'or with notOperation:if  notAge >= 18:    Print 'teenager'careful classmates can find that these two conditions are either "or", or meet the conditions of 1, or meet the conditions of 2, therefore, can be fully used aif...Else ... Statements to unify them together:ifAge >= 18:    Print 'Adult'Else:    Print 'teenager'Useif...Else... Statement, we can execute the value of the conditional expression as True or False, respectively.ifcode block orElsecode block. Note:ElseThere is a ":" in the back.

if-elif-ElseSometimes, aif...Else ... It's not enough. For example, according to age: Condition 1: Age 18 or above: Adult condition 2:6 years old or above: Teenager Condition 3: Under 6 years of age: kid, we can use aifAge >= 18 Determines whether the condition 1 is met, if not, and then through aifJudge Age >= 6to determine if the condition 2 is met, otherwise, perform condition 3:ifAge >= 18:    Print 'Adult'Else:    ifAge >= 6:        Print 'teenager'    Else:        Print 'Kid'so write it out and we get a two-layer nestedif...Else ... Statement. There is no problem with this logic, but if you continue to add conditions, such as baby under age 3:ifAge >= 18:    Print 'Adult'Else:    ifAge >= 6:        Print 'teenager'    Else:        ifAge >= 3:            Print 'Kid'        Else:            Print 'Baby'This indentation will only get more and more and the code will become more and more ugly. To avoid nesting of nested structuresif...Else..., we can useif... Multiple elif ...Else ... Structure, write all the rules at once:ifAge >= 18:    Print 'Adult'elifAge >= 6:    Print 'teenager'elifAge >= 3:    Print 'Kid'Else:    Print 'Baby'elifmeans thatElse if. In this way, we write a series of conditional judgments that are very clear in structure. Special attention: This series of conditional judgment will be judged from top to bottom, if a certain judgment is True, after executing the corresponding code block, the subsequent condition judgment is ignored and no longer executed. Consider the following code: age= 8ifAge >= 6:    Print 'teenager'elifAge >= 18:    Print 'Adult'Else:    Print 'Kid'when age= 8 o'clock, the result is correct, but Age = 20when, why did not print out adult? What should I do if I want to fix it? 

Python branch structure

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.