Operators are used to perform operations on variables and values.
Arithmetic operators
Assignment operators
Comparison operators
Logical operators
Identity operators
Membership operators
Bitwise operators
You can use arithmetic operators to do some common mathematical operations.
Operator | Name |
---|---|
+ | Addition |
- | Subtraction |
* | Multiplication |
/ | Division |
% | Modulus |
** | Exponentiation |
// | Floor division |
x = 5
y = 3
a = x + y
b = x - y
c = x * y
d = x / y
e = x % y
f = x ** y
g = x // y
print(a)
print(b)
print(c)
print(d)
print(e)
print(f)
print(g)
>>> %Run -c $EDITOR_CONTENT 8 2 15 1.666667 2 125 1 8 2 15 >>>
Assignment operators can used to assign values to variables.
Operator | Example | Same As |
---|---|---|
= | a = 6 | a =6 |
+= | a += 6 | a = a + 6 |
-= | a -= 6 | a = a - 6 |
*= | a *= 6 | a = a * 6 |
/= | a /= 6 | a = a / 6 |
%= | a %= 6 | a = a % 6 |
**= | a **= 6 | a = a ** 6 |
//= | a //= 6 | a = a // 6 |
&= | a &= 6 | a = a & 6 |
|= | a |= 6 | a = a | 6 |
^= | a ^= 6 | a = a ^ 6 |
>>= | a >>= 6 | a = a >> 6 |
<<= | a <<= 6 | a = a << 6 |
a = 6
a *= 6
print(a)
>>> %Run test.py 36 >>>
Comparison operators are used to compare two values.
Operator | Name |
---|---|
== | Equal |
!= | Not equal |
< | Less than |
> | Greater than |
>= | Greater than or equal to |
<= | Less than or equal to |
a = 6
b = 8
print(a>b)
>>> %Run test.py False >>>
Return False, beause the a is less than the b.
Logical operators are used to combine conditional statements.
Operator | Description |
---|---|
and | Returns True if both statements are true |
or | Returns True if one of the statements is true |
not | Reverse the result, returns False if the result is true |
a = 6
print(a > 2 and a < 8)
>>> %Run -c $EDITOR_CONTENT True >>>
Identity operators are used to compare the objects, not if they are equal, but if they are actually the same object, with the same memory location.
Operator | Description |
---|---|
is | Returns True if both variables are the same object |
is not | Returns True if both variables are not the same object |
a = ["hello", "welcome"]
b = ["hello", "welcome"]
c = a
print(a is c)
# returns True because z is the same object as x
print(a is b)
# returns False because x is not the same object as y, even if they have the same content
print(a == b)
# returns True because x is equal to y
>>> %Run -c $EDITOR_CONTENT True False True >>>
Membership operators are used to test if a sequence is presented in an object.
Operator | Description |
---|---|
in | Returns True if a sequence with the specified value is present in the object |
not in | Returns True if a sequence with the specified value is not present in the object |
a = ["hello", "welcome", "Goodmorning"]
print("welcome" in a)
>>> %Run -c $EDITOR_CONTENT True >>>
Bitwise operators are used to compare (binary) numbers.
Operator | Name | Description |
---|---|---|
& | AND | Sets each bit to 1 if both bits are 1 |
OR | Sets each bit to 1 if one of two bits is 1 | |
^ | XOR | Sets each bit to 1 if only one of two bits is 1 |
~ | NOT | Inverts all the bits |
<< | Zero fill left shift | Shift left by pushing zeros in from the right and let the leftmost bits fall off |
>> | Signed right shift | Shift right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall off |
num = 2
print(num & 1)
print(num | 1)
print(num << 1)
>>> %Run -c $EDITOR_CONTENT 0 3 4 >>>