<> Arithmetic operator
In the above , We've talked about Canada , reduce , ride , except , Take surplus , Take mold , Power operation total 7 Seed run . This kind of symbol is called “ operator ”, We talked about 7 All operators belong to “ Arithmetic operator ”.
operator function
+ number + number = Summation ; character string + character string = Merge strings in order before and after
- number - number = Calculate the difference
* number * number = quadrature ; character string * number = String repeated several times
/ number / number = Seeking quotient
% number % number = Take surplus ( Remainder of division )
// number // number = Take mold ( Integral part of quotient )
** number ** number = exponentiation ( Power of the last power of the first number ) a = 5 b = 3 print(a + b) # Output value : 8 print(a - b) # Output value : 2
print(a * b) # Output value : 15 print(a / b) # Output value : 1.6666666666666667 print(a % b) #
Output value : 2 print(a // b) # Output value : 1 print(a ** b) # Output value : 125
Except for arithmetic operators , And assignment operators , Comparison operator , Logical operators , member operator , Identity operator , Bitwise Operators .
<> Assignment Operators
below , Let's first understand that it's the same as arithmetic operators , Assignment operators also related to calculation . This kind of operators can operate at the same time of variable assignment .
operator function
= Assign the content after the equal sign to the variable before the equal sign ; for example :a=3 Is to 3 Assigned to variable a
+= Add the contents after the equal sign to the variables before the equal sign , for example :a+=3 Is equivalent to a=a+3
-= Reduce the contents after the equal sign to the variables before the equal sign , for example :a-=3 Is equivalent to a=a-3
*= Multiply the contents after the equal sign to the variables before the equal sign , for example :a*=3 Is equivalent to a=a*3
/= Take the contents before the equal sign as divisor , Contents after equal sign as divisor , And assign the result of division to the variable before the equal sign ; for example :a/=3 Is equivalent to a=a/3
%= Take the contents before the equal sign as divisor , Contents after equal sign as divisor , And assign the remainder result to the variable before the equal sign ; for example :a%=3 Is equivalent to a=a%3
//= Take the contents before the equal sign as divisor , Contents after equal sign as divisor , And assign the result of modulus to the variable before the equal sign ; for example :a//=3 Is equivalent to a=a//3
**= Calculating the power of the number of times after the number of equal signs before the number of equal signs , And assign the result to the variable before the equal sign ; for example :a**=3 Is equivalent to a=a**3 b = 3 a = 5 a += b print(a)
# Output value : 8 a = 5 a -= b print(a) # Output value : 2 a = 5 a *= b print(a) # Output value : 15 a = 5 a
/= b print(a) # Output value : 1.6666666666666667 a = 5 a %= b print(a) # Output value : 2 a = 5 a
//= b print(a) # Output value : 1 a = 5 a **= b print(a) # Output value : 125
<> Comparison operator
The comparison operator is used to compare the size relationship between two variables , The comparison result is Boolean .
operator function
== Judge whether the two variables are the same ; Return if two variables are the same True, Otherwise return False
!= Judge whether the two variables are different ; Return if two variables are different True, Otherwise return False
> x>y Judgment x Is it greater than y; if x greater than y, Then return True, Otherwise return False
< x<y Judgment x Is it less than y; if x less than y, Then return True, Otherwise return False
>= x>=y Judgment x Is greater than or equal to y; if x Greater than or equal to y, Then return True, Otherwise return False
<= x<=y Judgment x Less than or equal to y; if x Less than or equal to y, Then return True, Otherwise return False b = 3 a = 5 print(a == b) #
Output value : False print(a != b) # Output value : True print(a > b) # Output value : True print(a < b) #
Output value : False print(a >= b) # Output value : True print(a <= b) # Output value : False
<> Logical operators
Logical operators are operators used to connect simple logical statements to more complex logical statements , Including or (or), And (and) And non (not).
operator function
or x or y; if x or y One of them is True, The result is True; only x and y All are False The result is False
and x and y; if x and y All of them are True, The result is True; stay x and y One of them is False, The result is False
not not y; if y by True, Then the result is False; if y by False, Then the result is True a = True b = False print(a and b)
# Output value : False print(a or b) # Output value : True print(not b) # Output value : True
<> member operator
member operator (in) Is an operator used to determine whether a sequence contains an element , This sequence can be a string , aggregate , List or tuple, etc . If the sequence contains this element , The returned result is True, Otherwise return False.
a = [1, 2, 3, 4, 5] b = 3 c = 6 print(b in a) # Output value : True print(c in a) # Output value :
False
<> Identity operator
Identity operator (is) It is used to determine whether the variables are from the same object . If it is the same object , Then the result is True, Otherwise, the result is False.
a = 20 b = 10 print(a is b) # Output value : False print(a is not b) # Output value : True
<> Bitwise Operators ( Selected studies )
Bitwise operators evaluate numbers as binary , More efficient operation , But we seldom use it at the beginning of our study .
a = 10 # 0000 1010 b = 5 # 0000 0101 print(a & b) # Output value : 0 print(a | b) # Output value :
15(0000 1111) print(a ^ b) # Output value : 15(0000 1111) print(~a) # Output value : -11(1111 0101)
print(a << 2) # Output value : 40(0010 1000) print(a >> 2) # Output value : 2(0000 0010)
operator function
& Bitwise AND : If both variables are 1, Then the bit is 1; for example :1010&0101=0000
/ Bitwise AND : If two variables in a certain bit only one is 1, Then the bit is 1; for example :1010&0101=1111
^ Allosteric : If two variables have different values in a certain bit , Then the bit is 1; for example :1010&0101=1111
~ Reverse by bit : Reverses the value of a variable in all bits , Equivalent to calculation -x-1; for example :~00001010=11110101
<< Left shift :a<<2 soon a Move two bits to the left , Equivalent to times 4; for example :00001010<<2=00101000
>> Shift right :a>>2 soon a Move two bits to the right , Equivalent to divided by 4; for example :00001010>>2=00000010
Technology
Daily Recommendation