## Arithmetic comparison

### (1) Arithmetic operator: + - * / / /%**

#### +

var1 = 7 var2 = 90 res = var1 + var2 print(res)

#### -

var1 = 7 var2 = 90 res = var1 - var2 print(res)

#### *

var1 = 7 var2 = 10 res = var1 * var2 print(res)

#### / (the result is always decimal)

var1 = 10 var2 = 5 res = var1 / var2 print(res , type(res))

#### //Floor except

Divisor ÷ divisor = quotient

Note: if the divisor or divisor is a decimal, after the normal result is obtained, the value followed by. 0 becomes a decimal

var1 = 10.0 var2 = 3.0 # var2 = 3.0 res = var1 // var2 print(res)

#### % remainder

var1 = 7 var2 = 4 res = var1 % var2 res = -7 % 4 # -3 + 4 = 1 res = 7 % -4 # 3 + (-4) = -1 res = -7 % -4 # -3 (divisor and divisor are both negative, normal result plus minus sign) res = 81 % 7 # 4 res = 81 % -7 # -3 res = -81 % 7 # 3 res = -81 % -7 # -4 print(res)

#### **Power operation

res = 2 ** 3 print(res)

### (2) Comparison operators: >, <, > =, > =, = ==

The result of the comparison operator is either True or False. There are only two values

res = 10 > 5 res = 10 >= 10 # ==This symbol is used to compare whether the values on both sides of = = are the same res = 5 == 9 res = 5 != 9 print(res)

### (3) Assignment operator: = + = - = * = / = / / / =% =**=

#### =The assignment operator assigns the value on the right to the variable on the left

a = 5 <= 3 print(a) var1 = 10 var2 = 5

#### +=

"""var1 = var1 + var2""" # var1 += var2 # print(var1)

#### -=

"""var1 = var1 - var2""" # var1 -= var2 # print(var1)

#### %=

"""var1 = var1 % var2""" var1 %= var2 print(var1)

### (4) Member operators: in and not in (for container data)

#### ① When judging a string, it must be a continuous fragment

strvar = "It's going to rain today,Hurry home and collect your clothes" res = "this" in strvar res = "weather" in strvar res = "hurry back" in strvar print(res)

#### ② For lists, tuples, collections

container = ["Shenyang Zhao","Zhao Wanli","Zhao Shichao"] container = ("Shenyang Zhao","Zhao Wanli","Zhao Shichao") container = {"Shenyang Zhao","Zhao Wanli","Zhao Shichao"} # res = "Zhao Shenyang" in container # res = "Zhao Wanli" not in container res = "Zhao Shichao 1223232" not in container print(res)

#### ③ For the dictionary (the key of the dictionary is judged, not the value)

container = {"zsy":"Shenyang Zhao","zwl":"Zhao Wanli","zsc":"Zhao Shichao"} res = "Shenyang Zhao" in container # False res = "zsy" in container print(res)

### (5) Identity operators is and is not (detect whether the two data are the same value in memory)

#### ① Integer - 5 ~ positive infinity

var1 = 100 var2 = 100 print(var1 is var2)

#### ② Floating point nonnegative number

var1 = -9.1 var2 = -9.1 print(var1 is var2)

#### ③ bool is the same

var1 = True var2 = True print(var1 is var2)

#### ④ complex is different between real number and imaginary number (except for imaginary number)

var1 = 6-8j var2 = 6-8j var1 = -10j var2 = -10j print(var1 is var2)

#### ⑥ Container: the same string and empty tuple can be the same. All the remaining containers are different

container1 = () container2 = () print(container1 is not container2) container1 = "you" container2 = "you" print(container1 is not container2) container1 = [1,23,3] container2 = [1,23,3] print(container1 is not container2)

### (6) Logical operator: and or not

#### ① And logic and

One truth is true, one false is false

res = True and True # True res = True and False # False res = False and True # False res = False and False # False print(res)

#### ② or logic

One true is true, and all false is false

res = True or True # True res = False or True # True res = True or False # True res = False or False # False print(res)

#### ③ not logical

res = not True res = not False print(res)

#### ④ Logic short circuit

No matter whether the following expression is True or False, the final result cannot be changed. Then, short circuit directly and the following code will not be executed;

(1) True or print("program executed ~ 1111")

(2) False ande print("program executed ~ 2222")

True or print("The program was executed ~ 1111") True or True => True True or False => True False and print("The program was executed ~ 2222") False and True => False False and False => False

#### Calculation rule:

First, brain compensation is used to calculate whether the Boolean value of the current expression is True or False. If there is a True or expression or a False and expression, it will be returned directly. The following code will not be executed. If there is no short-circuit effect, it will be returned directly to the latter

res = 5 and 6 # 6

'''

True and True => True

True and False => False

'''

Next, let's look at a piece of code

res = 5 or 6 # 5 res = 0 and 999 res = 0 or "abc" print(res)

#### Priority of logical operators:

Priority from high to low: () > not > and > or

Small exercise:

res = 5 or 6 and 7 # 5 or 7 => 5 res = (5 or 6) and 7 # 5 and 7 res = not (5 or 6) and 7 # not 5 and 7 => False and 7 => False res = 1<2 or 3>4 and 5<100 or 100<200 and not (700>800 or 1<-1)

not (False or False) => True res = 1<2 or 3>4 and 5<100 or 100<200 and not (700>800 or 1<-1) res = True or False and True or True and True res = True or False or True res = True or True => True print(res)

### (7) Bitwise operators: & ^ < > >~

#### ① & bitwise AND

var1 = 19 var2 = 15 res = var1 & var2 print(res)

#The result is this: """ 000 ... 10011 000 ... 01111 000 ... 00011 => 3 """

#### ② | bitwise OR

var1 = 19 var2 = 15 res = var1 |var2 print(res)

The result is this: """ 000 ... 10011 000 ... 01111 000 ... 11111 """

#### ③ ^ bitwise XOR

Different quantity = > true; otherwise, False is returned

var1 = 19 var2 = 15 res = var1 ^ var2 print(res)

The result is this: """ 000 ... 10011 000 ... 01111 000 ... 11100 """

#### ④ < < shift left (want to be in multiplication)

This number is multiplied by the n-th power of 2

res = 5 << 1 # 10 res = 5 << 2 # 20 res = 5 << 3 # 40 print(res)

The result is this:

"""

000 ... 101 => 5

000 .. 1010 => 10

000 ..10100 => 20

000 .101000 => 40

"""

#### ⑤ > > shift right (equivalent to Division)

This number is divided by 2 to the nth power

res = 5 >> 1 # 2 res = 5 >> 2 # 1 res = 5 >> 3 # 0

print(res)

The result is this:

"""

000 ... 101

000 ... 010 => 2

000 ... 001 => 1

000 ... 000 => 0

"""

#### ⑥ ~ bitwise non (for complement operation, bitwise inversion, including each bit)

-(n+1)

Exercise 1:

# res = ~22 res = ~19 print(res)

The result is this: ""“

Original code: 000 ten thousand and eleven

Inverse code: 000 ten thousand and eleven

Complement: 000 ten thousand and eleven

Complement: 000 ten thousand and eleven

Bitwise non: 111 01100

Give you the complement - > original code

Complement: 111 01100

Inverse code: 100 ten thousand and eleven

Original code: 100 10100 => -20

"""

Exercise 2

res = ~-19 print(res)

The result is this:

"""

Original code: 100 ten thousand and eleven

Inverse code: 111 01100

Complement: 111 01101

Complement: 111 01101

Bitwise non: 000 ten thousand and ten

Give you the complement - > original code (because it is an integer, the original inverse complement is the same)

000 ... 10010 => 19

"""

#### Summary:

Individual operators:

The highest operator is:**

Operator with the lowest priority:=

() can raise priority

Unary operators > binary operators (priority)

Unary operator: operate on a value at the same time ~-

Binary operator: at the same time, operate on a value + - * /

Operators of the same kind:

Arithmetic operators: multiply and divide > add and subtract

Logical operators: () > not > and > or

Bitwise operators: (< > >) > & > ^ >|

Overall sorting:

Arithmetic operators > bit operators > comparison operators > identity operators > member operators > logical operators

The assignment operator is used for closing

Little practice

res = 5+5 << 6 // 3 is 40 and False """ res = 10 << 2 is 40 and False res = 40 is 40 and False res = True and False res = False """ print(res) # Raise the next priority with parentheses res = (5+5) << (6//3) is 40 and False