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:

operatorexpressexampleresult
+plus1+12
-reduce1-10
*ride2*48
/except1/20.5
%Remainder (one positive and one negative formula)9%41
Remainder = divisor divisor * quotient

9%-4

9-(-4)*(-3)

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

3. Assignment operator (=)

(1) Execution sequence:

Right → left

(2) Support chain assignment

a=b=c=20

(3) Support parameter assignment

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

(4) Support series unpacking assignment

a,b,c=20,30,40

(5) Example:

  ① Chain assignment:

i=3+4
print(i)
a=b=c=20
print(a,id(a))
print(a,id(b))
print(c,id(c))

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

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

Output results:

  ③ Unpacking assignment is supported

a,b,c=20,30,40
print(a,b,c)

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

(1)>,K,>=,<=,!=

(2)==

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
a,b=10,20
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=============')
a=10
b=10
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
lst1=[11,22,33,44]
lst2=[11,22,33,44]
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(id(lst1))
print(id(lst2))
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
TrueFalseFalse
FalseTrue
FalseFalse
orTrueTrueTrueAs long as one operand is True, the operation result is True
TrueFalse
FalseTrue
FalseFalseFalse
notTrueFalseIf the operand is True, the result is False
FalseTrueIf the operand is False, the result is True

(3) Example:

#Boolean operator
a,b=1,2
#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('=============or,perhaps=============================')
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
f2=False
print(not f)  #The result is false
print(not f2)   #The result is true


print('===========int And not in =============')
s='helloworld'
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