Judgment and loop statements - logical operators

Posted by CurrierSites on Tue, 07 Dec 2021 05:13:38 +0100

1, Introduce

Through the comparison operator, we know that when comparing the size and equality of data, we can directly use >, <, = =,! =, > =<= To operate

However, there are many times not only the simple comparison size, but also: and, or, No

for instance:

  • Adult and female
  • Undergraduate or graduate
  • Not a student under the age of 18

In order to implement and, or, no, Python provides logical operators

  • and realize the functions of
  • Or implement or function
  • not implemented

And, or, not for everyone's understanding, I give them names, but in professional terms, they are called and, or, not

2, Logical operator

1. What are the logical operators

and

Realize the function of and, in popular terms, and

General usage: the result is True only when both sides are True

for example

100 > 50 and 90 < 200  # The result of 100 > 50 is True, and the result of 90 < 200 is True, which is equivalent to True and True, so the final result is True

or

Realize the function of or, which is popularly known as or

General usage: as long as there is one True on both sides, the result is True

for example

100 > 50 and 90 > 200  # The result of 100 > 50 is True, and the result of 90 > 200 is False, which is equivalent to True or False, so the final result is True

not

To realize the function of non, in popular terms, it is not

General usage: if the previous result is True, then not True is False. If the previous result is False, then not False is True

for example

not (100 > 50)  # The result is False. Originally, the result of 100 > 50 is True, but adding not in front is equivalent to antisense, so the final result is False
not (100 < 50)  # The result is True. Originally, the result of 100 < 50 is False, but not is added before it, so the final result is True

2. Logical operators are used with comparison operators

Demo Chinese meaning: example, the word demo is often used in development

demo1

Requirement: how to achieve the same user name and password at the same time?

Reference codes are as follows:

name = input("Please enter user name:")  # Get user name
password = input("Please input a password:")  # Get password

print("Is the user name and password the same?")
print(name == "dong4716138@163.com" and password == "123456")  # Whether the output user name and password are the same

demo2

Demand: how to realize that the boss or leader can open the door?

Reference codes are as follows:

role = "boss"  # Define role variables and store positions

print("Can you open the door?")
print(role == "boss" or role == "leader")  # Output results

demo3

Demand: how to realize the judgment of not less than 18 years old?

Reference codes are as follows:

age = 20

print("Over 18?")
print(age > 18)  # Method 1: directly use the comparison operator
print(not(age <= 18))  # Mode 2: comparison operator and logical operator are used

demo4

Demand: women aged between 18 and 50

Reference code:

age = 20
gender = "female sex"

print("Does it meet the requirements?")
print(18<=age<=50 and gender == "female sex")  # Mode 1: simple writing
print(age >= 18 and age <= 50 and gender == "female sex")  # Mode 2: general writing

demo5

Demand: women aged 18-50 or men aged 18-60

Reference codes are as follows:

age = 20
gender = "Male"

print("Does it meet the requirements?")
print((18<=age<=50 and gender == "female sex") or (18<=age<=60 and gender == "Male"))

3, Logical operators in special cases

When we generally use logical operators, the left and right results are True or False, and finally we get a True or False

However, sometimes the left and right sides of logical operators are not True or False, so we need to be careful

1. and

Look at the example below and guess what the result is

print(100 and 200)  # Output what?
print(100 and 100>50)  # Output what?
print(0 and 200)  # Output what?
print(0 and 100>50)  # Output what?

Operation results:

200
True
0
0

Why?

A: to understand this problem, you need two points

Point 1:

Python has Boolean types, that is, True and false   Use True for True   False is used to represent false, but not in some other programming languages. For example, there is no boolean type in C language. Therefore, in order to express True and false, C language invented a provision that as long as the number is not 0, whether it is positive or negative, it represents True, and as long as it is 0, it represents false.

Later, although Python invented True and False, it still retains the habit that developers in C language are used to, so it is also used in Python

  • Non-zero indicates   really
  • 0 means false

Point 2:

We know that and implements True on both sides, and the final result is True, otherwise it is False, but it has a hidden function,

If the expression on the left is not True, the right will not execute at all, and the value on the left will be regarded as the final result

If the expression on the left is True, the right will execute, and the value on the right will be regarded as the final result

The reason for this hidden function is that and is the function of and. Since the left is false, there is no need to look at the right. The result must be false, but the result on the left is regarded as the final result at this time

According to the above 2 points

print(100 and 200). Because 100 is not 0, the left side of and is true. Next, execute the right side, and take 200 as the final result

Print (100 and 100 > 50). Because 100 is not 0, it is True. Execute the right   100 > 50 and its result is regarded as the final result, so the final result is True

print(0 and 200) is false because the left side of and is 0, the right side will not be executed, and the result on the left is regarded as the final result, so the last is 0

Print (0 and 100 > 50), the reason is the same as the previous one, and the result is 0

2. or

With the additional experience of and above, it is much easier for us to look at or

The special rules for or are as follows:

  • If the left is true, the value on the left is taken as the final result
  • If the left and right are false, the value on the right is regarded as the final result

The reason why or has such a function is that since the left side is true, there is no need to look at the right side. The result must be true, but the result on the left side is regarded as the final result at this time

Let's have a try. Do you have it

print(100 or 200)  # Output what?
print(100 or 100>50)  # Output what?
print(0 or 200)  # Output what?
print(0 or 100>50)  # Output what?

The operation results are as follows:

100
100
200
True

I don't need to say why. If you really don't know why, then I think it's necessary for you to read the above knowledge again. Only when you know why is it true

4, Summary

 

Topics: python3