hello everyone!

I'm Xiao Huang. Nice to meet you again!

Refuse hydrology, start with me!!!!

Today's update is:

- 05 TIANCHI_Python_ Operators and bitwise operations.
- Previous searches: Program design learning notes - Contents

Created on: February 10, 2021

Software: Python 3, pychar

- First put a mind map to roughly know the specific functions and objectives of the operating system, and then describe them one by one.

# 1. Python operator:

## 1.1 what are operators:

# What are operators and operands? 4 + 5 = 9 4 And 5 are called operands,+ Called operator.

## 1.2 types of Python operators:

- Python operators are divided into seven categories: arithmetic operators, comparison (relational) operators, assignment operators, logical operators, bit operators, member operators and identity operators.

### Python operator 1.2 arithmetic:

- for instance:

a = 21 b = 10 c = 0 c = a + b print("1 - c The value of is:", c) c = a - b print("2 - c The value of is:", c) c = a * b print("3 - c The value of is:", c) c = a / b print("4 - c The value of is:", c) c = a % b print("5 - c The value of is:", c) # Modify variables a, b, c a = 2 b = 3 c = a ** b print("6 - c The value of is:", c) a = 10 b = 5 c = a // b print("7 - c The value of is:", c) # The value of 1 - c is 31 # The value of 2 - c is: 11 # The value of 3 - c is: 210 # The value of 4 - c is: 2.1 # The value of 5 - c is: 1 # The value of 6 - c is 8 # The value of 7 - c is: 2

### 1.2.2 Python comparison (relational) operator:

- for instance:

a = 21 b = 10 c = 0 if (a == b): print("1 - a be equal to b") else: print("1 - a Not equal to b") if (a != b): print("2 - a Not equal to b") else: print("2 - a be equal to b") if (a < b): print("3 - a less than b") else: print("3 - a Greater than or equal to b") if (a > b): print("4 - a greater than b") else: print("4 - a Less than or equal to b") # Modify the values of variables a and b a = 5 b = 20 if (a <= b): print("5 - a Less than or equal to b") else: print("5 - a greater than b") if (b >= a): print("6 - b Greater than or equal to a") else: print("6 - b less than a") # 1 - a is not equal to b # 2 - a is not equal to b # 3 - a is greater than or equal to b # 4 - a greater than b # 5 - a less than or equal to b # 6 - b is greater than or equal to a

### 1.2.3 Python assignment operator:

- for instance:

a = 21 b = 10 c = 0 c = a + b print("1 - c The value of is:", c) c += a print("2 - c The value of is:", c) c *= a print("3 - c The value of is:", c) c /= a print("4 - c The value of is:", c) c = 2 c %= a print("5 - c The value of is:", c) c **= a print("6 - c The value of is:", c) c //= a print("7 - c The value of is:", c) # The value of 1 - c is 31 # The value of 2 - c is: 52 # The value of 3 - c is 1092 # The value of 4 - c is 52.0 # The value of 5 - c is: 2 # The value of 6 - c is 2097152 # The value of 7 - c is: 99864

### 1.2.4 Python logical operators:

- for instance:

a = 10 b = 20 if (a and b): print("1 - variable a and b All for true") else: print("1 - variable a and b One is not true") if (a or b): print("2 - variable a and b All for true，Or one of the variables is true") else: print("2 - variable a and b Not for true") # Modify the value of variable a a = 0 if (a and b): print("3 - variable a and b All for true") else: print("3 - variable a and b One is not true") if (a or b): print("4 - variable a and b All for true，Or one of the variables is true") else: print("4 - variable a and b Not for true") if not (a and b): print("5 - variable a and b All for false，Or one of the variables is false") else: print("5 - variable a and b All for true") # 1 - both variables a and b are true # 2 - both variables a and b are true, or one of them is true # 3 - one of the variables a and b is not true # 4 - both variables a and b are true, or one of them is true # 5 - both variables a and b are false, or one of them is false

### 1.2.5 Python bitwise operators:

# Bitwise non operation~ # ~Reverse all 0 and 1 in the complement of num (0 becomes 1 and 1 becomes 0). The sign bits of signed integers will also be reversed in the ~ operation. ~ 1 = 0 ~ 0 = 1 00 00 01 01 -> 5 ~ --- 11 11 10 10 -> -6 11 11 10 11 -> -5 ~ --- 00 00 01 00 -> 4 # Bitwise and operation& # It is 1 only when both corresponding bits are 1 1 & 1 = 1 1 & 0 = 0 0 & 1 = 0 0 & 0 = 0 0 00 01 01 -> 5 & 00 00 01 10 -> 6 --- 00 00 01 00 -> 4 # Bitwise OR operation| # As long as one of the two corresponding bits is 1, it is 1 1 | 1 = 1 1 | 0 = 1 0 | 1 = 1 0 | 0 = 0 00 00 01 01 -> 5 | 00 00 01 10 -> 6 --- 00 00 01 11 -> 7 # Bitwise exclusive or operation^ # It is 1 only when the two corresponding bits are different 1 ^ 1 = 0 1 ^ 0 = 1 0 ^ 1 = 1 0 ^ 0 = 0 00 00 01 01 -> 5 ^ 00 00 01 10 -> 6 --- 00 00 00 11 -> 3 # The nature of XOR operation: satisfying commutative law and associative law A: 00 00 11 00 B: 00 00 01 11 A^B: 00 00 10 11 B^A: 00 00 10 11 A^A: 00 00 00 00 A^0: 00 00 11 00 A^B^A: = A^A^B = B = 00 00 01 11 # Shift left by bit operation<< # Num < < i moves the binary representation of num by i bits to the left. 00 00 10 11 -> 11 11 << 3 --- 01 01 10 00 -> 88 # Shift right by bit > > # Num > > i shifts the binary representation of num by i bits to the right. 00 00 10 11 -> 11 11 >> 2 --- 00 00 00 10 -> 2 # Ternary operator x, y = 4, 5 if x < y: small = x else: small = y print(small) # 4 # Ternary operator a statement to complete the following conditional judgment and assignment operations. x, y = 4, 5 small = x if x < y else y print(small) # 4

- for instance:

a = 10 b = 20 list = [1, 2, 3, 4, 5] if (a in list): print("1 - variable a In the given list list in") else: print("1 - variable a Not in the given list list in") if (b not in list): print("2 - variable b Not in the given list list in") else: print("2 - variable b In the given list list in") # Modify the value of variable a a = 2 if (a in list): print("3 - variable a In the given list list in") else: print("3 - variable a Not in the given list list in") # 1 - variable a is not in the given list # 2 - variable b is not in the given list # 3 - variable a is in the given list

### 1.2.5 Python bitwise operators_ Integer set realized by bit operation:

- The binary representation of a number can be regarded as a set (0 means not in the set and 1 means in the set).
- For example, the set {1, 3, 4, 8} can be expressed as 01 00 01 10 10, and the corresponding bit operation can be regarded as an operation on the set.

# Operation of elements and Collections: a | (1<<i) -> hold i Insert into collection a & ~(1<<i) -> hold i Remove from collection a & (1<<i) -> judge i Whether it belongs to the set (zero does not belong to, non-zero belongs to) # Operations between collections: a repair -> ~a a hand over b -> a & b a and b -> a | b a difference b -> a & (~b)

### 1.2.7 Python identity operator:

- The Python identity operator compares the storage locations of two objects.

Note: the id() function is used to obtain the memory address of the object.

a = 20 b = 20 if (a is b): print("1 - a and b Have the same identification") else: print("1 - a and b No identical identity") if (id(a) == id(b)): print("2 - a and b Have the same identification") else: print("2 - a and b No identical identity") # Modify the value of variable b b = 30 if (a is b): print("3 - a and b Have the same identification") else: print("3 - a and b No identical identity") if (a is not b): print("4 - a and b No identical identity") else: print("4 - a and b Have the same identification") # 1 - a and b have the same identification # 2 - a and b have the same identification # 3 - a and b do not have the same identification # 4 - a and b do not have the same identification

## 1.3 Python operator priority:

- Unary operators are better than binary operators. Such as sign.
- First arithmetic operation, then shift operation, and finally bit operation. For example, 1 < < 3 + 2 & 7 is equivalent to (1 < < (3 + 2)) & 7
- Logical operation and final combination
- Pyhton3 does not support the < > operator. You can use it= Replace

a = 20 b = 10 c = 15 d = 5 e = 0 e = (a + b) * c / d #( 30 * 15 ) / 5 print ("(a + b) * c / d The calculation result is:", e) e = ((a + b) * c) / d # (30 * 15 ) / 5 print ("((a + b) * c) / d The calculation result is:", e) e = (a + b) * (c / d) # (30) * (15/5) print ("(a + b) * (c / d) The calculation result is:", e) e = a + (b * c) / d # 20 + (150/5) print ("a + (b * c) / d The calculation result is:", e)

- and has higher priority:

x = True y = False z = False if x or y and z: print("yes") else: print("no") # yes

## 1.4 Python operator supplement:

### 1.4.1 difference between is and = = in Python:

- Is determines whether the two objects are the same object (is indicates the object identifier, i.e. object identity), that is, whether the addresses viewed with the id() function are the same. If they are the same, it returns True. If they are different, it returns False. Is cannot be overloaded; When the contents of two basic types of data (or tuples) are the same, the id will be the same, but it does not mean that a will change with the change of b.
- ==Is to compare whether the contents (values) of two objects are equal. The internal call of this operator is__ eq__ () method. So a==b is equivalent to a__ eq__ (b) , so = can be overloaded.

# 1. When the values in the list, tuple and dictionary all refer to a and B, it always returns True, which is not affected by the value of a and B a=1000 b=1000 list1=[a,3,5] list2=[b,4,5] print(list1[0] is list2[0]) tuple1=(a,3,5) tuple2=(b,4,5) print(tuple1[0] is tuple2[0]) dict1={6:a,2:3,3:5} dict2={1:b,2:4,3:7} print(dict1[6] is dict2[1]) # True # True # True # 2. As long as the values of variables are the same, the identifiers are the same, and there is no restriction of - 5 ~ 256: a = 100000 b = 100000 if a is b: print('a and b Identifications are the same, and the identifications are:',id(a)) else: print('a and b Identifications are different,a Identified as:',id(a),'b Identified as:',id(b)) # The identifications of a and b are the same, and the identification is 20004272 # 3. Similarly, if it is negative, there is still no above limit: a = -100000 b = -100000 if a is b: print('a and b Identifications are the same, and the identifications are:',id(a)) else: print('a and b Different identification,a Identified as:',id(a),'b Identified as:',id(b)) # The identifications of a and b are the same, and the identification is 30357680 # 4. The list is the same. As long as the value of the list item is the same, the identification is the same. Examples are as follows: list1 = [10000,20000,30000] list2 = [10000,12000,15000] if list1[0] is list2[0]: print('list1[0] and list2[0] Identifications are the same, and the identifications are:',id(list1[0])) else: print('list1[0] and list2[0] Different identification,list1[0]Identified as:',id(list1[0]),'list2[0]Identified as:',id(list2[0])) # The identifications of list1[0] and list2[0] are the same: 10370480 # 5. The identification of tuple follows the variable name. The variable name and identification are different. The above example: tuple1 = (10000,20000,30000) tuple2 = (10000,12000,15000) if tuple1[0] is tuple2[0]: print('tuple1[0] and tuple2[0] Identifications are the same, and the identifications are:',id(tuple1[0])) else: print('tuple1[0] and tuple2[0] Different identification,tuple1[0] Identified as:',id(tuple1[0]),'tuple2[0]Identified as:',id(tuple2[0])) # tuple1[0] and tuple2[0] have the same identification: 10763696 # 6. the dictionary is the same as the list. As long as the values of the list items are the same, the identifications are the same. tuple1 = (10000,20000,30000) tuple2 = (10000,12000,15000) dict1 = {1:10000,2:20000,3:30000} dict2 = {1:10000,2:12000,3:15000} if dict1[1] is tuple2[1]: print('dict1[1] and tuple2[1] Identifications are the same, and the identifications are:',id(dict1[1])) else: print('dict1[1] and tuple2[1] Different identification,dict1[1] Identified as:',id(dict1[1]),'tuple2[1] Identified as:',id(dict2[1])) # The identifications of dict1[1] and tuple2[1] are different. The identifications of dict1[1] are 15285680 and tuple2[1] are 15285680

# Forecast for next period:

- 06 TIANCHI_Python_ character string

Friends passing by, if you think you can learn something, please give a compliment before you go. You are welcome to comment and correct your mistakes, and you are also welcome to comment, leave messages and private letters from the children with problems.

The attention of every little partner is the driving force for me to update my blog!!!

Please search wechat for [Xiaohuang] Article update, which will be read at the first time!

Grasp the present, look forward to the future, come on!

Due to the limited level, there will inevitably be some deficiencies in the writing. Please give us your advice!