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