An operator is a special symbol in Python used to perform arithmetic or logical operations on values or variables. Python supports operators such as:
- Arithmetic operators
- Comparison operators
- Logical operators
- Bitwise operators
- Assignment operators
- Some other operators
1. Arithmetic operators
The table below shows arithmetic operators in Python.
Operator | Meaning | Example |
---|---|---|
+ | Adds two operands | x + y + 2 |
– | Subtracts two operands | x – y – 2 |
* | Multiplies two operands | x * y |
/ | Divides left operand by right operand (result is always a float) | x / y |
% | Returns the remainder when the left operand is divided by the right operand | x % y |
// | Returns the integer part of the quotient when the left operand is divided by the right operand | x // y |
** | Raises the left operand to the power of the right operand | x**y (means x to the power y) |
Example:
x = 9
y = 4
# Output: x + y = 13
print('x + y =', x+y)
# Output: x - y = 5
print('x - y =',x-y)
# Output: x * y = 36
print('x * y =', x*y)
# Output: x / y = 2.25
print('x / y =', x/y)
# Output: x % y = 1
print('x % y =', x%y)
# Output: x // y = 2
print('x // y =', x//y)
# Output: x ** y = 6561
print('x ** y =', x**y)
Result
x + y = 13
x - y = 5
x * y = 36
x / y = 2.25
x % y = 1
x // y = 2
x ** y = 6561
2. Comparison Operators
The table below shows the comparison operators in Python.
Operator | Meaning | Example |
---|---|---|
> | Returns True if the operand on the left is greater than the operand on the right, and False otherwise | x > y |
< | Returns True if the operand on the left is less than the operand on the right, and False otherwise | x < y |
== | Returns True if both operands are equal, and False otherwise | x == y |
!= | Returns True if both operands are not equal, and False otherwise | x != y |
>= | Returns True if the operand on the left is greater than or equal to the operand on the right, and False otherwise | x >= y |
<= | Returns True if the operand on the left is less than or equal to the operand on the right, and False otherwise | x <= y |
Example:
x = 13
y = 33
# Output: x > y is False
print('x(13) > y(33) is', x>y)
# Output: x < y is True
print('x(13) < y(33) is', x<y)
# Output: x == y is False
print('x(13) == y(33) is', x==y)
# Output: x != y is True
print('x(13) != y(33) is', x!=y)
# Output: x >= y is False
print('x(13) >= y(33) is', x>=y)
# Output: x <= y is True
print('x(13) <= y(33) is', x<=y)
Result
x(13) > y(33) is False
x(13) < y(33) is True
x(13) == y(33) is False
x(13) != y(33) is True
x(13) >= y(33) is False
x(13) <= y(33) is True
3. Logical Operators
The table below shows the logical operators in Python.
Operator | Meaning | Example |
---|---|---|
and | Returns True if both operands are True | x and y |
or | Returns True if at least one operand is True | x or y |
not | Returns True if the operand is False, returns False if the operand is True | not x |
The truth table for and operator
a | b | a and b |
---|---|---|
True | True | True |
True | False | False |
False | True | False |
False | False | False |
The truth table for or operator
a | b | a or b |
---|---|---|
True | True | True |
True | False | True |
False | True | True |
False | False | False |
The truth table for not operator
a | not a |
---|---|
True | False |
False | True |
Example:
x = True
y = False
print('x(True) and y(False) is', x and y)
print('x(True) or y(False) is', x or y)
print('not x(True) is', not x)
Result
x(True) and y(False) is False
x(True) or y(False) is True
not x(True) is False
4. Bitwise Operators
Bitwise operators perform operations on each bit of the operands. The operands will be automatically converted to a binary representation before the bitwise operators are applied.
Example: Let x = 10 (binary representation is 0000 1010) and y = 4 (binary representation is 0000 0100). The table below shows the bitwise operations performed on x and y.
Operator | Meaning | Example |
---|---|---|
& | Bitwise AND | x & y = 0 (0000 0000) |
| | Bitwise OR | x | y = 14 (0000 1110) |
~ | Bitwise NOT | ~x = -11 (1111 0101) |
^ | Bitwise XOR | x ^ y = 14 (0000 1110) |
>> | Bitwise right shift | x >> 2 = 2 (0000 0010) |
<< | Bitwise left shift | x << 2 = 40 (0010 1000) |
Example:
# Binary 0000 1010
x = 10
# Binary 0000 0100
y = 4
# Output 0 (0000 0000)
print("x & y = ", x & y)
# Output 14 (0000 1110)
print("x | y = ", x | y)
# Output -11 (1111 0101)
print("~x = ", ~x)
# Output 14 (0000 1110)
print("x ^ y = ", x ^ y)
# Output 2 (0000 0010)
print("x >> y = ", x >> 2)
# Output 40 (0010 1000)
print("x >> y = ", x << 2)
Result
x & y = 0
x | y = 14
~x = -11
x ^ y = 14
x >> y = 2
x >> y = 40
You can read more about bitwise operators in the article Bitwise Operators.
5. Assignment Operators
The table below shows the assignment operators in Python.
Operator | Example | Equivalent |
---|---|---|
= | x = 5 | x = 5 |
+= | x += 5 | x = x + 5 |
-= | x -= 5 | x = x – 5 |
*= | x *= 5 | x = x * 5 |
/= | x /= 5 | x = x / 5 |
%= | x %= 5 | x = x % 5 |
//= | x //= 5 | x = x // 5 |
**= | x **= 5 | x = x ** 5 |
&= | x &= 5 | x = x & 5 |
|= | x |= 5 | x = x | 5 |
^= | x ^= 5 | x = x ^ 5 |
>>= | x >>= 5 | x = x >> 5 |
<<= | x <<= 5 | x = x << 5 |
Example:
# Assign value for a và b
a = 10
print("a = ", a)
b = a
print("b = ", b)
# Add and assign value
b += a
print("b +=a then b = ", b)
# Subtract and assign value
b -= a
print("b -=a then b = ", b)
# multiply and assign
b *= a
print("b *=a then b = ", b)
# bitwise lishift operator
b <<= a
print("b <<=a then b = ", b)
Result
a = 10
b = 10
b +=a then b = 20
b -=a then b = 10
b *=a then b = 100
b <<=a then b = 102400
6. Other Operators
Python supports some special operators such as identity operator and membership operator.
Identity Operator in Python
The identity operator helps to check whether two values are stored in the same memory location or not.
Operator | Meaning | Example |
---|---|---|
is | Returns True if the operands have the same value and are stored in the same memory location (refer to the same object) | x is y |
is not | Returns True if the operands have different values or are stored in different memory locations (do not refer to the same object) | x is not y |
Example:
x1 = 5
y1 = 5
x2 = 'Hello'
y2 = 'Hello'
x3 = [1,2,3]
y3 = [1,2,3]
# Output: False
print(x1 is not y1)
# Output: True
print(x2 is y2)
# Output: False
print(x3 is y3)
Result
False
True
False
An object of the int class is created to store the value 5. This object is allocated a memory space to store the value 5. Variables x1 and y1 both reference this object. Similarly, for the str class, x2 and y2 variables are created.
However, x3 and y3 are lists. When each list is created, a separate memory block is allocated to store it. Therefore, x3 and y3 reference two different objects (two different memory blocks).
Membership operator in Python
The membership operator in Python is used to check whether a value or a variable belongs to a data type such as string, list, tuple, set, and dictionary.
Note: In a dictionary, the membership operator only checks for key, not for value.
Operator | Meaning | Example |
---|---|---|
in | Returns True if the value/variable belongs to a string data type | 5 in x |
not in | Returns True if the value/variable does not belong to a string data type | 5 not in x |
Example:
x = 'Hello world'
y = {1:'a',2:'b'}
# Output: True
print('H' in x)
# Output: True
print('hello' not in x)
# Output: True
print(1 in y)
# Output: False
print('a' in y)
Result
True
True
True
False
In the example above, ‘H’ belongs to x but ‘hello’ does not belong to x (Python is a language that distinguishes between uppercase and lowercase characters). With the dictionary y, 1 is the key, and ‘a’ is the value. Therefore, 1 in y
returns True while 'a' in y
returns False.