Python IF condition judgment

Source: Internet
Author: User

The IF statement is used for the execution of the control program, in the basic form:
If judgment condition: EXECUTE statement ... Else: Execute statement ...

Where the "judging condition" is established (not 0), the following statements are executed, and the execution content can be multiple lines, in order to be indented to differentiate the same range.

Else is an optional statement that executes the relevant statement when it needs to be executed when the condition is not true:

' Python ' if ' Python ':         #  to determine whether the variable is ' python '    print(" Welcome to learn python  ")              #  and output welcome message Else:    print(name)                #  output variable name when condition is not valid

The judgment condition of the IF statement can be expressed by > (greater than), < (less than), = = (equals), >= (greater than or equal), <= (less than or equal).

When judging a condition as multiple values, you can use the following form:

num =-59ifNum >= 90:#determine the value of Num    Print('Excellent')elifNum >= 80:    Print("Good")elifNum >= 60:    Print("Pass")Else:    Print("inferior lattice")

If it is judged that multiple conditions need to be judged at the same time, the or (or) can be used to indicate that two conditions have a successful condition, and when using and (with), it means that only two conditions have been established and the condition is successful.

num = 9ifNum >= 0 andNum <= 10:#determine if the value is between 0~10    Print(True) Num= 10ifNum < 0ornum > 10:#determine if the value is less than 0 or greater than    Print(True)Else:    Print(False) Num= 13#determine if the value is between 0~5 or 10~15if(Num >= 0 andNum <= 5)or(Num >= 10 andNum <= 15):    Print(True)Else:    Print(False)

Python IF condition judgment

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.