Related links
""" @author GroupiesM @date 2022/4/27 12:15 @introduction python Operator """
1. Arithmetic operator
1.1 Standard arithmetic operators [+、-、*、/、//] // To divide or divide , Rounding down
1.2 The remainder operator [%]
1.3 Power operator [**]
2. Assignment operator
Execution order : Right -> Left
Support chain assignment : a=b=c=20
Support parameter setting : +=、-=、*=、/=、//=、%=
Support series unpacking assignment : a,b,c=20,30,40
3. Comparison operator
[>、<、>=、<=、!=]
== -> object value Comparison
is,is not object id Comparison
4. Boolean operator
and And
or or
not Take the opposite
in ('xx','xx',...) In scope
not in ('xx','xx',...) Out of range
5. An operator
& Bit and : The corresponding digits are 1, As a result, the number is 1, Otherwise 0
| Bit or : The corresponding digits are 0, The result is 0, Otherwise 1
<< Left displacement : High overflow discard , Low complement 0
>> Right displacement : Low removal discard , High compensation 0
""" @author GroupiesM @date 2022/4/27 12:17 @introduction Common operators 01 1. Arithmetic operator 1.1 Standard arithmetic operators [+、-、*、/、//] // To divide or divide , Rounding down 1.2 The remainder operator [%] 1.3 Power operator [**] """
# 1.1 Standard arithmetic operators
print(1 + 1) # 2
print(1 - 1) # 0
print(2 * 3) # 6
print(1 / 2) # 0.5 except
print(11 // 2) # 5 to be divisible by
# 1.2 The remainder operator
print(11 % 2) # 1
# 1.3 Power operator
print(2 ** 4) # 16 2^4 = 2*2*2*2 = 16
# Complex situation
# to be divisible by
print(-9 // -2) # 4 (4.5 Rounding down =4)
print(-9 // 2) # -5 (-4.5 Rounding down =-5)
# remainder ( Divisor - Divisor * merchant )
print(9 % 4) # 1 9-4*2 = 9-8 = 1
print(9 % -4) # -3 9-(-4)*(-3) = 9 - (12) = -3
print(-9 % -4) # -1 -9-(-4)*2 = -9 + 8 = -1
""" @author GroupiesM @date 2022/4/27 13:29 @introduction Common operators 02 2. Assignment operator Execution order : Right -> Left Support chain assignment : a=b=c=20 Support parameter setting : +=、-=、*=、/=、//=、%= Support series unpacking assignment : a,b,c=20,30,40 """
a = 3 + 4
print(a) # 7
""" Support chain assignment : a=b=c=20 """
a = b = c = 3
""" Support series unpacking assignment """
print(a + b + c) # 9
a, b, c = 4, 5, 6
print(a + b + c) # 15
a = 1
a += 1
print(a) # 2
a *= 3
print(a) # 6
a /= 5
print(a) # 1.2
""" @author GroupiesM @date 2022/4/27 13:41 @introduction Common operators 03 3. Comparison operator [>、<、>=、<=、!=] == -> value Comparison is,is not Memory address comparison """
a, b = 10, 20
print('a>b?', a > b) # False
print('a<b?', a < b) # True
print('a>=b?', a >= b) # False
print('a<=b?', a <= b) # True
print('a!=b?', a != b) # True
print('a == b?', a == b) # False ( Values are different )
print('a is b?', a is b) # False ( Different memory addresses )
print('a is not b?', a is not b) # True
""" For basic types If the value is the same, the memory address will be the same """
a, b = 30, 30
print('a == b?', a == b) # True
print('a is b?', a is b) # True Same memory address
print('a is not b?', a is not b) # False
""" For reference types Same value , Reference types are not necessarily the same """
list1 = [11, 22, 33, 44]
list2 = [11, 22, 33, 44]
print(list1 == list2) # True
print(list1 is list2) # False Different memory addresses
print(id(list1), id(list2)) # 4370292096 4370298624
list2 = list1
print(list1 is list2) # True Same memory address
print(id(list1), id(list2)) # 4370292096 4370292096
""" @author GroupiesM @date 2022/4/27 13:52 @introduction Common operators 04 4. Boolean operator and And or or not Take the opposite in ('xx','xx',...) In scope not in ('xx','xx',...) Out of range """
a, b = 1, 2
t = a == 1
f = a > 1
print("-----and And -----")
print(t and t) # True
print(t and f) # False
print(f and f) # False
print("-----or or -----")
print(t or t) # True
print(t or f) # True
print(f or f) # False
print("-----not Take the opposite -----")
print(not t) # False
print(not f) # True
print("-----in-----")
''' integer '''
print(3 in (3, 4, 5)) # True
print(7 in (1, 2, 3)) # False
''' character string '''
print('p' in 'python') # True
print('p' in 'java') # False
''' list '''
print(5 in [5, 6, 7]) # True
print('5' in [5, 'hello', True]) # False
print("-----not in-----")
print(3 not in (3, 4, 5)) # False
print(7 not in (1, 2, 3)) # True
""" @author GroupiesM @date 2022/4/27 14:01 @introduction Common operators 05 5. An operator & Bit and : The corresponding digits are 1, As a result, the number is 1, Otherwise 0 | Bit or : The corresponding digits are 0, The result is 0, Otherwise 1 n<<m n Left displacement m position : High overflow discard , Low complement 0, Moving one bit to the left is equivalent to *2 n>>m n Right displacement m position : Low removal discard , High compensation 0, Moving one bit to the right is equivalent to /2 """
# 4 : 0000 1000
# 8 : 0001 0000
""" & Bit and : The corresponding digits are 1, As a result, the number is 1, Otherwise 0 0000 1000 0001 0000 --------- 0000 0000 """
print(4 & 8) # 0
""" | Bit or : The corresponding digits are 0, The result is 0, Otherwise 1 0000 1000 0001 0000 --------- 0001 1000 -> 12 """
print(4 | 8) # 12
""" n<<m n Left displacement m position : High overflow discard , Low complement 0 0000 0100 ---------- Move left 2 position 0 0000 1000 ---------- High abandonment , Low complement 0 0000 1000 -> 8 """
print(4 << 1) # 8
"""n>>m n Right displacement m position : Low removal discard , High compensation 0 0000 0100 ---------- Move right 2 position 00 0001 00 ---------- Low abandonment , High compensation 0 0000 0001 -> 1 """
print(4 >> 2) # 1
""" @author GroupiesM @date 2022/4/27 14:20 @introduction """
Operator priority :
() Brackets > 1. Arithmetic operation :1.3 Power operation > 1.1 Standard arithmetic operators [*、/、//、%] > 1.1 Standard arithmetic operators [+、-]
> 5. An operation : [<<、>>] > [&] > [|]
> 3. Comparison operator [>、<、>=、<=、!=]
> 4 Boolean operator [and] > 4 Boolean operator [or]
> 2 Assignment operator
22/06/27
M