Types of operators in Python

This post is lesson 7 of 54 in the subject Python Programming Language

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.

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.

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.

The truth table for and operator

The truth table for or operator

The truth table for not operator

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.

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.

Example:

``````# Assign value for a và b
a = 10
print("a = ", a)
b = a
print("b = ", b)

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.

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.

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.

5/5 - (1 vote)
Previous and next lesson in subject<< Basic data types in PythonBasic Input/Output in Python >>
0 Shares