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.

OperatorMeaningExample
+Adds two operandsx + y + 2
Subtracts two operandsx – y – 2
*Multiplies two operandsx * 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 operandx % y
//Returns the integer part of the quotient when the left operand is divided by the right operandx // y
**Raises the left operand to the power of the right operandx**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.

OperatorMeaningExample
>Returns True if the operand on the left is greater than the operand on the right, and False otherwisex > y
<Returns True if the operand on the left is less than the operand on the right, and False otherwisex < y
==Returns True if both operands are equal, and False otherwisex == y
!=Returns True if both operands are not equal, and False otherwisex != y
>=Returns True if the operand on the left is greater than or equal to the operand on the right, and False otherwisex >= y
<=Returns True if the operand on the left is less than or equal to the operand on the right, and False otherwisex <= 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.

OperatorMeaningExample
andReturns True if both operands are Truex and y
orReturns True if at least one operand is Truex or y
notReturns True if the operand is False, returns False if the operand is Truenot x

The truth table for and operator

aba and b
TrueTrueTrue
TrueFalseFalse
FalseTrueFalse
FalseFalseFalse

The truth table for or operator

aba or b
TrueTrueTrue
TrueFalseTrue
FalseTrueTrue
FalseFalseFalse

The truth table for not operator

anot a
TrueFalse
FalseTrue

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.

OperatorMeaningExample
&Bitwise ANDx & y = 0 (0000 0000)
|Bitwise ORx | y = 14 (0000 1110)
~Bitwise NOT~x = -11 (1111 0101)
^Bitwise XORx ^ y = 14 (0000 1110)
>>Bitwise right shiftx >> 2 = 2 (0000 0010)
<<Bitwise left shiftx << 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.

OperatorExampleEquivalent
=x = 5x = 5
+=x += 5x = x + 5
-=x -= 5x = x – 5
*=x *= 5x = x * 5
/=x /= 5x = x / 5
%=x %= 5x = x % 5
//=x //= 5x = x // 5
**=x **= 5x = x ** 5
&=x &= 5x = x & 5
|=x |= 5x = x | 5
^=x ^= 5x = x ^ 5
>>=x >>= 5x = x >> 5
<<=x <<= 5x = 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.

OperatorMeaningExample
isReturns 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 notReturns 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.

OperatorMeaningExample
inReturns True if the value/variable belongs to a string data type5 in x
not inReturns True if the value/variable does not belong to a string data type5 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.

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

Leave a Reply

Your email address will not be published. Required fields are marked *