Python Basics-Conditional statements (judging)

Source: Internet
Author: User

Python conditional statements

Conditional statements in Python are implemented in the same way as conditional statements in other languages, either If...else or If...elif...else. They are implemented by one or more execution results (true) or False (false) to determine the code block to execute.

The following diagram briefly describes the process of execution:

  

Specifies that any value other than 0 or non-null (NULL) in Python is true, specifying any 0 or null (NULL) value as False

In code, Python executes the IF judgment statement or if the syntax of the IF is:

If you need to determine the condition:   #这里需要注意的是一定不要忘记 ': '    code block 1else:    Executing code block 2

PS: When the condition after the if is judged to be true (true) (not 0 or non-null) execute code block 1 code (here The code can be multiple lines, with indentation range), when the condition after the if is determined to be false (false) (0 or empty) Execute code block 2 code (here The code can also be multiple lines, Use indentation to differentiate between ranges)

The following shows an example of an IF condition statement:

Name = (' Brian ') #定义一个name的变量 with a value of Brianusername = input (' Please input your name: ') #使用input的方法让用户输入自己的名字if username = = Name:      #使用if条件语句, the condition here is whether the user input is equal to the name variable we define    print (' Input Correct ') #如果是相等的会打印输出 Enter the correct prompt for the else:                       # Else is an optional statement, and if the user enters a name variable that is not equal to our definition,    print (' input error ')    #如果不相等就会打印输出 Enter the wrong hint # The result of the execution is: when we enter Brian. The print is input correct#            when we enter something else. The print is input error

The IF conditional statement can also be used in the comparison operator (< > = = = <= >==) to indicate its relationship

The specific way to achieve this is:

If condition 1:    Execute code 1 ...    elif condition judgment 2:    execute code 2 ...    elif condition 3:    execute code 3    ... else:    execute code 4, and then    ......

Demo Example:

num =     if num = =:            # Determines the value of num    print (' boss ')        elif num = =:    print (' user ') elif num = = 11:
   
    print (' worker ') Elif num < 0:           # value is less than zero output    print (' ERROR ') Else:    print (' Roadman ')     # Output When none of the conditions are true
   

The If condition judgment can also be used in conjunction with the logical operator, and used when judging the two conditions at the same time to determine the success of the judgment, and or in conjunction with the use when a condition to determine the success of the judgment success

Demo Example:

Number =                 #定义number的值if number >= 0 and number <=:       #判断是否大于等于0与是否小于等于100    print (' yes ')                        # The above judgments are true print Yeselse:                                   #否则 print    (' no ')                         #打印nonumber_one =               #定义number_one的值if Number_one < 1 or number _one >:    #判断是否小于1或者大于100    print (' yes ')                           #上面的条件有一个为真打印yeselse:                                       #否则    print (' no ')                             # Print Nonumber_two = 168                #定义number_two的值if (number_two >= 0 and Number_two <=) or (Number_two >= and num Ber_two <=):    #判断是否大于等于0与小于等于100, or greater than or equal to 100 and less than or equal to    #判断是否在0 ~100 or 100~200 between    print (' Yes ')                #条件一个成立打印yeselse:                           #否则    print (' no ')                 #打印no

If...elif...else statements are used extensively in Python, and can be used in conjunction with other operators

 

  

Python Basics-Conditional statements (judging)

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.