Python Operators

Python Operators with Types and Examples:

Python Data Types were explained in detail along with their classification in our previous tutorial.

In this tutorial, we learn all about Python Operators along with their types. Simple examples pertaining to each type of Python operator are included in this tutorial.

This Python Training Tutorial Series will be a perfect guide for any beginner to enhance their knowledge on Python concepts.

Airthmetic, Comparision, Logical and Bitwise operators1 (2)

Watch the VIDEO Tutorials

Overview of Operators in Python (Part 1):

An In-Depth Look at Operators in Python (Part 2):

Python Operators

What are Python Operators?

Operators are used for carrying out operations on values and variables.

Python has 7 types of Operators as stated below:

  • Arithmetic Operator
  • Comparison operators
  • Logical operators
  • Bitwise operators
  • Assignment Operator
  • Identity operators
  • Membership operators

#1) Arithmetic Operators

Python programming language supports different kinds of arithmetic operators for both integer and floating point like addition, subtraction, multiplication, division and so on.

Example:

x = 15
y = 10
print('x + y =', x+y)

Output: x + y = 25

 print('x - y =', x-y) 

Output: x – y = 5

 print('x * y =', x*y) 

Output: x * y = 150

 print('x / y =', x/y) 

Output: x / y = 1.5

 print('x % y =', x%y) 

Output: x % y = 5

 print('x // y =', x//y) 

Output: x // y = 1

 print('x ** y =', x**y) 

Output: x ** y = 576650390625

Python arithmetic operator example

#2) Comparison Operators

Comparison operators are used for comparing values. It either returns True or False according to the condition.

Example:

x = 8
y = 15
('x>y is',x>y)

Output: x > y is False

 print('x< y is',  x<y) 

Output: x < y is True

 print('x == y is', x==y) 

Output: x == y is False

 print('x != y is', x!=y) 

Output: x != y is True

 print('x >= y is', x>=y) 

Output: x >= y is False

 print('x<= y is', x<=y) 

Output: x <= y is True

Python comparision operator example

#3) Logical Operators

Logical operators are used for performing AND, OR and NOT operations. It either returns True or False according to the condition.

Example:

a = True
b = False
print(‘a and b is’, a and b)

Output: a and b is False

 print(‘a or b is’, a or b) 

Output: a or b is True

 print(‘not a is’, not a) 

Output: not a is False

Python logical operator example

#4) Bitwise Operators

Bitwise operators operate on bits and perform bit by bit operation.

#5) Assignment Operator

An assignment operator is used to assign a value to a variable.

#6) Identity Operators

Python offers 2 types of identity operators i.e is and is not.

Both are used to compare if two values are located on the same part of the memory. Two variables that are equal does not imply that they are identical.

Example:

a1 = 3
b1 = 3
a2 = "Python"
b2 = "Python"

a3 = [4,5,6]
b3 = [4,5,6]

print(a1 is not b1)

Output: False

 print(a2 is b2) 

Output: True

 print(a3 is b3) 

Output: False

Python identity operator example

Here a3 and b3 are listed, interpreter allocates memory separately and even though they are equal, it returns False.

#7) Membership Operators

Python offers 2 types of membership operators i.e in and not in.

Both are used to test whether a value or variable is in a sequence.

Example:

a = “Python operators”
b = {1:'x',2:'y'}

print(“P” in a)

Output: True

 print(“python” not in a) 

Output: False

 print(1 in b) 

Output: True

 print('y' in b) 

Output: False

Python membership operators example

1 is key and ‘x’ is the value in dictionary b. Hence, ‘y’ in b returns False.

Hope you are clear about Python operators and their various types.

Watch out our upcoming to know more about Python Conditional Statements!!

PREV Tutorial | NEXT Tutorial

Was this helpful?

Thanks for your feedback!