Python02input Function's use operator arithmetic operator assignment operator comparison operator Boolean operator bit operator priority
effect : Used to accept user input
The return value is str
Use = Assignment variable storage
# Input function input present = input(' What gift do girls want ?') print(present, type(present))
# It is required to enter two integers from the keyboard , To calculate the sum of two integers a = input(' Please enter an addend :') a = int(a) # Store the converted results in a b = int(input(' Please enter another addend :')) print(type(a), type(b)) print(a+b)
# Add, subtract, multiply and divide print(1+1) # Perform addition operations print(1-1) # Perform subtraction print(2*4) # Perform multiplication print(1/2) # Perform Division print(11//2) # Division operation # Take over operations print(11%2) # Take over operations # Power operation print(2**2) # It means 2 Of 2 Power print(2**3) # It means 2 Of 3 Power
print(9//4) # 2 print(-9//-4) # 2 print(9//-4) # -3 print(-9//4) # -3 # Integers : One positive and one negative rounded down print(9 % -4) # -3 # The formula : remainder = Divisor - Divisor * merchant 9-(-4)*(-3)== -3 print(-9 % 4) # 3 # -9-4*(-3)== 3
# Assignment operator , The operation order is from right to left i = 3+4 print(i) # Support chain assignment a = b = c = 20 print(a, id(a)) print(b, id(b)) print(c, id(c)) # Support parameter assignment a = 20 a += 30 # amount to a = a+30=50 print(a) a -= 10 print(a) a *= 2 print(a) # int print(type(a)) a /= 3 print(a) print(type(a)) a //= 2 print(a) a %= 3 print(a) # Support series unpacking assignment a, b, c = 20, 30, 40 print(a, b, c) # Exchange the values of two variables a, b = 10, 20 print(" Before the exchange :", a, b) # In exchange for a, b = b, a print(" After the exchange :", a, b)
# Comparison operator , The result of the comparison operator is bool type a, b = 10, 20 print("a>b Do you ", a > b) # False print("a<b Do you ", a < b) # True print("a<=b Do you ", a <= b) # True print("a>=b Do you ", a >= b) # False print("a==b Do you ", a == b) # False print("a!=b Do you ", a != b) # True ''' One = Is assignment operator , Two == Is a comparison operator A variable has three parts : identification , type , value == Is it a value or an identifier that is compared ? Compare values The identity of the comparison object uses is ''' a = 10 b = 10 print(a == b) # True explain :a And b Of value equal print(a is b) # True explain :a And b Of id Identity equality lst1 = [11, 22, 33, 44] lst2 = [11, 22, 33, 44] print(lst1 == lst2) # value print(lst1 is lst2) # id print(id(lst1)) print(id(lst2)) print(a is not b) # False a Of id And b Of id It's not equal print(lst1 is not lst2) #True
# Boolean operator a, b = 1, 2 print('-------------and also ----------------------------') print(a == 1 and b == 2) # True True and True -->True print(a == 1 and b < 2) # False True and False -->False print(a != 1 and b == 2) # False False and True -->False print(a != 1 and b != 2) # False False and False -->False print('-------------or perhaps ------------------------------') print(a == 1 or b == 2) # True True or True -->True print(a == 1 or b < 2) # True True or False -->True print(a != 1 or b == 2) # True False or True -->True print(a != 1 or b != 2) # False False or False -->False print('-------------not Not Yes bool Negate type operands ----------------------') f = True f2 = False print(not f) print(not f2) print('-----------------in And not in------------------------------------') s = 'helloworld' print('w' in s) print('k' in s) print('w' not in s) print('k' not in s)
Convert data into binary for calculation
# An operator print(4 & 8) # And &, The corresponding digits are 1, As a result, the number is 1, Otherwise 0 print(4 | 8) # or |, The corresponding digits are 0, As a result, the number is 0, Otherwise 1 print(4 << 1) # Shift left << Move one bit to the left , Equivalent to times 2 print(4 << 2) # Shift left << Move two digits to the left , Equivalent to times 4 print(4 >> 1) # Shift right << Move one bit to the right , Equivalent to divided by 2 print(4 >> 2) # Shift right << Move two digits to the right , Equivalent to divided by 4
Power operation > Multiplication and division > Addition and subtraction > Shift operation > Bitwise AND > Press bit or > Comparison operator >and>or>=
Arithmetic operator > An operation > Comparison operator > Boolean operator > Assignment operator