arithmetic operator
operator (computing) | descriptive |
---|---|
+ | Adding two numbers to two numbers, or string concatenation |
- | subtract two numbers |
* | Multiply two numbers, or return a string repeated several times. |
/ | Dividing two numbers results in a floating point number. |
% | Takes a modulus and returns the remainder of the division of two numbers |
// | Divide two numbers and return the integer part of the quotient. |
** | Power operation, return multiplication result |
print(1 + 2) # 3 print(2 - 1) # 1 print(2 * 3) # 6 print(3 / 2) # 1.5 print(6 % 5) # 1 print(8 // 2) # 4 print(3 ** 2) # 9
If you want to get the quotient and remainder at the same time, you can use divmod this way
The return value of this method is tuple (x//y, x%y).
print(divmod(10,3)) # (3,1)
Python also has some computational glitches because of the floating point precision issue
print(0.1+0.1+0.1-0.3) # 5.551115123125783e-17
To solve this problem, you can import the decimal module
from decimal import Decimal # Calculated:0.0 print(Decimal('0.1')+Decimal('0.1')+Decimal('0.1')-Decimal('0.3'))
relational operator (computing)
operator (computing) | descriptive |
---|---|
== | Compare objects for equality |
!= | Compare objects for inequality |
> | greater than, for example, a>b, will compare the size of a and b, a>b return True, otherwise return False |
< | less than, for example, a<b, will compare the size of a and b, a<b return True, otherwise return False |
>= | greater than or equal to, for example, a>=b, will compare the size of a and b, a>=b return True, otherwise return False |
<= | less than or equal to, for example, a<=b, will compare the size of a and b, a<=b return True, otherwise return False |
a = 10 b = 20 print(a == b) # False print(a != b) # True print(a > b) # False print(a < b) # True print(a >= b) # False print(a <= b) # True
1. The return value is a bool value
print(True == 1) # True print(False == 0) # True
== compare values (int and str are not directly comparable)
print(2.0 == 2) # True print('2' == 2) # False
String to string is comparing ASCII values.
# True print('abc' < 'xyz') # 97 98 99 < 120 121 122
Consecutive comparisons, python's interpretation mechanism
print(3 > 2 > 1) # True print(3 > 2 > 2) # False print((3 > 2) > 1) # False
assignment operator
operator (computing) | descriptive |
---|---|
= | Regular assignment operators, which assign the result of an operation to a variable. |
+= | Additive assignment operators, e.g. a+=b is equivalent to a=a+b. |
-= | Subtractive assignment operators, e.g. a-=b is equivalent to a=a-b |
*= | Multiplication assignment operator, e.g. a*=b is equivalent to a=a*b |
/= | The division assignment operator, e.g. a/=b is equivalent to a=a/b. |
//= | Integer division assignment operators, such as a//=b are equivalent to a=a//b. |
%= | Modulo assignment operators, e.g. a%=b is equivalent to a=a%b. |
**= | Power assignment operators, e.g. a**=b is equivalent to a=a*b. |
a = 1 # Assign the right side of the equal sign to the left side of the equal sign # a = a + 1 # Calculate on the right side of the equals sign and then assign to the left side of the equals sign # a += 1 # a = a + 1
logical operator
operator (computing) | descriptive |
---|---|
and | The and operation returns False if a is False, a and b returns False otherwise it returns the computed value of y. |
or | The or operation returns the value of a if a is not 0, and the value of b otherwise. |
not | non-operational, if a is True, return False, if a is False, return True |
a = 10 b = 20 print(a and b) # 20 print(a or b) # 10 print(not a) # False
The result is True if a and b are both True.
a or b, one of them is True, the result is True.
short-circuit (lazy) principle
False and True When the and operator is used, the first False is not followed.
True or False When the or operator is used, the first one that comes up is True, and it doesn't go any further.
membership operator
operator (computing) | descriptive |
---|---|
in | Returns True if the value is found in the specified sequence, False otherwise. |
not in | Returns True if the value is found in the specified sequence, False otherwise. |
list1 = [1, 2, 3, 4, 5] a = 1 if a in list1: print("a is one of the elements of list1.") else: print("a is not an element of list1.")
identity operator (computing)
operator (computing) | descriptive |
---|---|
is | Determine if two identifiers refer to a single object |
is not determines whether two identifiers refer to different objects. | |
Difference between is and the comparison operator == | |
is is used to determine if two variables are quoted from the same object (you can use id() to see this), and == is used to determine if the values of the variables are equal! |
a = [1, 2, 3] b = [1, 2, 3] # Memory addresses can be viewed via id() print(id(a)) # 2042443551304 print(id(b)) # 2042443551816 print(a is b) # False print(a == b) # True
a = 2 b = 2.0 # Memory addresses can be viewed via id() print(id(a)) # 140722443350320 print(id(b)) # 2336678499216 print(a is b) # False print(a == b) # True
trinomial operator (math.)
Representation of trinomial operators: True_statements if expression else False_statements
a = 1 b = 2 # a+b is not greater than 3 Execute the subsequent else statement b-a = 1 print(a+b if a+b>3 else b-a)
operator priority
operator (computing) | descriptive |
---|---|
** | Index (highest priority) |
*/%// | Multiplication, division, modulo and division by integers |
± | addition subtraction |
<= >= | comparison operator |
== != | equality operator |
= %= 、= 、、=-= += *= **= | assignment operator |
is is not | identity operator (computing) |
in not in | membership operator |
not or and | logical operator |
to this article on the common python operators and usage of the article is introduced to this, more related python operators content please search for my previous articles or continue to browse the following related articles I hope you will support me more in the future!