Python logical operators and python Operators

Source: Internet
Author: User

Python logical operators and python Operators

Logical operators are mainly used for logical judgment. logical operators and comparison operators are put together, and are also used for Condition Selection and loop.

Assume that variable a is 10 and variable B is 20:

Example1:

# AndYes. All conditions areTrue,The result isTrue; As long as one isFalse,All resultsFalse

Print (TrueAndTrue)

Print (FalseAndTrue)

Print (FalseAndFalse)

 

# OrYes or, if one of all the conditions isTrue,The result isTrue; All conditions are:FalseYesFalse

Print (TrueOrTrue)

Print (FalseOrTrue)

Print (FalseOrFalse)

 

# NotReturns the inverse of an operator.True, notAfterFalse;It turns out to beFalse, notAfterTrue

Print (NotTrue)

Print (NotFalse)

 

Result:

True

False

False

True

True

False

False

True

 

Example2:

Username = input ("Enter the User Name:")

Password = input ("Enter the password:")

IFUsername ="Admin"AndPassword ="123.com":

Print ("Logon successful")

Else:

Print ("Incorrect username or password")

 

Result:

Enter the User name: Admin

Enter the password 123.com

Logon successful

 

Tip: The result of the comparison operator is a Boolean value, which isTRueOr False

 

Demonstration 1:

#Enter Sandy's Chinese and math scores, and output the following judgment:

#Yes (True) Incorrect (False)

Chinese = int (input ("Enter the Chinese score:"))

Maths = int (input ("Enter the math score:"))

Print ("SandyAre all scores equal to or greater90Points:", (Chinese> = 90AndMaths> = 90 ))

Print ("SandyIs there a score equal to or greater90Points:", (Chinese> = 90OrMaths> = 90 ))

Result:

C: \ python \ python.exe C:/python/demo/file2.py

Enter Chinese score: 89

Enter the math score: 99

Sandy's score is greater than or equal to 90: False

Sandy's score is equal to or greater than 90: True

 

Process finished with exit code 0

 

Demo2: The user enters a year to determine whether it is a leap year.

Leap year: 1) divisible by 400Or2) can be divisible by 4, but cannot be divisible by 100

Year = int (input ("Enter a year:"))

If(Year % 400 = 0)Or(Year % 4 = 0AndYear % 100! = 0 ):

Print ("% DIt is a leap year."% Year)

Else:

Print ("% DIt's the year of the year."% Year)

Result:

C: \ python \ python.exe C:/python/demo/file2.py

Enter a year: 2020

2020 is a leap year

 

Process finished with exit code 0

 

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.