Python Introductory basic Tutorial ten Python branching statements

Source: Internet
Author: User

If branch statement

The function of a branching statement is to implement certain functional statement blocks with selective execution under some condition control. The IF branch statement is when the if condition satisfies, if the statement block is executed, the syntax format is as follows:

If <CONDITION>: statements

Let's take a look at the code.

>>> sex = ' male ' >>> if sex = = ' Male ': print ' man! ' #此处有两次回车键Man!>>> if sex = = ' Female ': print ' Woman ' #此处有两次回车键 >>>
If_else statements

The statement block under the if statement is executed when the <condition> condition is met, and the statement block under theelse statement is in the < condition> conditions are not satisfied with the case of execution, the use of if_else statement needs to be noted is if the <condition> criteria after the colon, else statement without <condition> determinant expression, but with a colon. The statement blocks under if and else do not use curly braces around them.

If <condition>: statementselse:statements

Give an example to illustrate the use of if_else.

Sex = raw_input ("plz input your sex:") if sex = = ' male ' or sex = = ' m ': print ' man! ' Else:print ' woman! '

The results of the program run as follows:

plz input your Sex:femalewoman

Do a little exercise, enter math score (shaping), 0~60 print "No pass!", 70print "Just pass! ”,
80 print "Good", 80 print "wonderful!", 90 print "excellent!", 100 print "best!", pleaseuse if_else nested to complete.

#coding: Utf-8x = input ("plz input math record: ") if x >= 60:         if x >= 70:                if x >= 80:                        if x >= 90:                 print  "best!"           else:               print  "excellent!"        else:           print  "good!"     else:        print  "just pass! "ELSE:&NBSP;&NBSP;&Nbsp; print  "No pass!" 

The program runs the following results

>>> ========= RESTART =========plz input math record:27no pass!>>> ========= RESTART =========plz input Math Record:67just pass! >>> ========= RESTART =========plz input math record:77good!>>> ========= RESTART =========plz input ma Th record:80excellent!>>> ========= RESTART =========plz input Math record:98best!>>>
IF_ELIF_ELSE Multi-Branch statements

If the above program is not well written, it is likely that it will not be able to complete the classification of the results, such as the completion of if_else nested programs can be completed with the if_elif_elif_.....elif_else structure, the syntax structure is as follows:

If <condition1>: Statements_1elif <condition2>: Statements_2elif <condition3>: Statements_3 .... Elif <conditionn>: statements_nelse:statements_else

This structure is called a multi-branch structure, which is checked from the upper if to the lower elif to determine the conditional expression.
<Conditionx>, see that the condition satisfies the execution of the statements_xon the statement block below it, all conditions are not satisfied to execute else the statement block under statements_else. The entire structure is executed with only one block of statements. The procedure for classifying the results from the previous section can be changed to look like this.

If x >= 90:print "best!" elif x >= 80:print "excellent!" elif x >= 70:print "good!" elif x >= 60:print "Just pass! "Else:print" No pass! "

Small exercise: Write a complete procedure for judging gender. (type ' male ', ' male ', ' m ', ' m ', ' man ', ' men ' all representing males)


This article is from the "Python Training Zhipu Education" blog, so be sure to keep this source http://jeapedu.blog.51cto.com/7878264/1617078

Python Introductory basic Tutorial ten Python branching statements

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.