Operators in python and their precedence

Posted by robinstott on Fri, 19 Nov 2021 23:25:00 +0100

1. Common operators

(1) Arithmetic operator

① Standard operators

② Remainder operator

③ Power operator

(2) Assignment operator

(3) Comparison operator

(4) Boolean operator

(5) Bitwise operator

2. Arithmetic operator

(1) Standard operators: add (+), subtract (-), multiply (*), divide (/)

(2) Remainder operator:%

(3) Power operator:**

(4) Example:

print(1+1)  #Addition operator
print(1-1)  #Subtraction operator
print(2*4)   #Multiplication operator
print(1/2)   #division operator 
print(11//2) # integer division
print(11%2)   #Remainder operator
print(2**2)  #Represents the power of 2

Output results:

  (5) Arithmetic operators:

%Remainder (one positive and one negative formula)9%41
Remainder = divisor divisor * quotient



/ /Integer (one positive and one negative rounded down) >11//25
-9// 4-3
**exponentiation 2**38

3. Assignment operator (=)

(1) Execution sequence:

Right → left

(2) Support chain assignment


(3) Support parameter assignment

+=\ , -= ,+=,/=,//=,%=

(4) Support series unpacking assignment


(5) Example:

  ① Chain assignment:


Output result: it can be seen from the result that the addresses pointed to by a, b and c are the same, that is, the so-called chain assignment

  ② Support parameter assignment

a+=30  #Equivalent to a=a+30
a-=10  #Equivalent to a=a-10;
a*=2  #Equivalent to a=a*2
print(type(a))  #int type
print(type(a))   #Equivalent to converting from int type to float type when
a//=2 # is equivalent to a=a//2
print(type(a))   #At this time, it is also of type float
a%=2  #Equivalent to a=a%2

Output results:

  ③ Unpacking assignment is supported


Output results:

It is equivalent to assigning 40 to c, 30 to b and 20 to a. remember to correspond one by one when using, that is, the number on the left should be equal to the number on the right

4. Comparison operator

Compare the size, true and false of the results of variables or expressions



Comparison of object value s

(3)is,is not

Comparison of object IDS

(4) Examples

#Comparison operator. The result of the comparison operator is bool type
print('a>b Are you?',a>b)  #The result is false
print('a<b Are you?',a<b)  #The result is true
print('a<=b Are you?',a<=b)  #The result is true
print('a>=b Are you?',a>=b)  #The result is flash
print('a==b Are you?',a==b)  #The result is false
print('a!=b Are you?',a!=b)  #The result is true
'''One=Called assignment operator, a==It is called a comparison operator
  A variable consists of two parts: identification, type and value
  ==Are values or identifiers compared? Values are compared
  Identity usage of comparison object is
print('=================Object identifier is Examples of=============')
print(a==b)  #true, indicating that the value s of a and b are equal
print(a is b)  #true, indicating that the id IDs of a and b are equal

#Let's look at the next group of examples, or is
print(lst1==lst2)   #Value -- > is compared and the result is true
print(lst1 is lst2)   #id (i.e. id) is compared --- > the result is false
print(a is not b)  #The result is false, and the id of a is equal to that of b
print(lst1 is not lst2) #The result is true. The id of lst1 is not equal to that of lst2

  Output results:


5. Boolean operator

(1) Boolean operators: and, or, not, in, not in

(2) Boolean operator chart:

operatorArithmetic numberOperation resultremarks
andTrueTrueTrueWhen both operands are True, the operation result is True
orTrueTrueTrueAs long as one operand is True, the operation result is True
notTrueFalseIf the operand is True, the result is False
FalseTrueIf the operand is False, the result is True

(3) Example:

#Boolean operator
#and examples:
print('==========and ,also==================')
print(a==1 and b==2) #The result is: true -- > true and true -- > true
print(a==1 and b<2) #The result is: false -- > true and false -- > false
print(a!=1 and b==2) #The result is: false -- > false and true -- > false
print(a!=1 and b!=2) #The result is: true -- > false and false -- > true

print(a==1 or b==1)   #The result is true, -- > true or true -- > true
print(a==1 or b<2)    #The result is true, -- > true or false -- > true
print(a!=1 or b==2)    #The result is true, -- > flash or Flash -- > true
print(a!=1 or b!=2)    #The result is true, -- > flash or Flash -- > flash

print('=============not yes bool Negate type operands=========================')
f= True
print(not f)  #The result is false
print(not f2)   #The result is true

print('===========int And not in =============')
print('w' in s)  #The result is true
print('k 'in s)  #The result is flash
print('w'not in s)  #The result is flash
print('k'not in s)  #The result is true

Output results:


6. Bitwise operator (convert data to binary for calculation)

(1) Bit and&

The corresponding digits are all 1, and the result digit is 1, otherwise it is 0

(2) Bit or|

The corresponding digits are all 0, and the result digit is 0, otherwise it is 1

(3) Left shift operator<<

High overflow discard, low fill 0

(4) Shift right operator > >

Low overflow discard, high fill 0

(5) Examples:

① Bit or &: the green row represents the binary of 4 and the blue row represents the binary of 8. The result is 1 only when the green and blue of each column are 1. The result is the row of skin color. Because the result of each column is 0, the total result is 0

  ② Bit or | (the decimal system corresponding to the binary of 1100 is 12, so the result is 12)

  ③ Left shift operator < <:

  ④ Shift right operator > >:


#Bitwise operators:
print('========Bit or&============')
print(4 & 8)  #At the same time, the result is 1

print('==============Bit or|===============')
print(4|8)  #At the same time, it is 0, otherwise it is 1, and the output result is 12

print('==============Left shift=============')
print(4<<1) #Moving 1 bit to the left (moving one position) is equal to multiplying by 2

print(4<<2)  #Move 2 bits to the left (move two positions)
print('============Right shift============')
print(4>>1) #Moving 1 bit to the right (moving one position) is equal to dividing by 2#

Operation results:


7. Operator priority



Topics: Python