In this tutorial, we will learn about the Operators in Python, or its types, and how to use them with the help of examples.

Define Operators in Python

Operators are the symbols or words those perform special operators on operands.

Python provides the following set of operator

  • Arithmetic Operators
  • Relational Operators or Comparison Operators
  • Logical operators
  • Assignment operators
  • Special operators

Arithmetic operators

Arithmetic operators are use to perform mathematical operations like as addition, subtraction, multiplication and division.

OperatorOperation
+Addition
Subtraction
*Multiplication
/Division
//Floor Division
%Reminder
**Exponent
Arithmetic operators

The Example of Arithmetic operators

a = 81
b = 9

# addition
print ('Sum: ', a + b)  

# subtraction
print ('Subtraction: ', a - b)   

# multiplication
print ('Multiplication: ', a * b)  

# division
print ('Division: ', a / b) 

# floor division
print ('Floor Division: ', a // b)

# modulo
print ('Modulo: ', a % b)  

# a to the Exponent b
print ('Power: ', a ** b)
Example of Arithmetic operators

Let’s run the code

Example of Arithmetic operators
Example of Arithmetic operators

Relational Operators or Comparison Operators

This operators compares the values. It either returns True or False according to the condition is know as Relational Operators.

Chaining of relational operators is possible. In the chaining, if all comparisons returns True then only result is True. If atleast one comparison returns False then the result is False.

OperatorDescription
==Equal To
!=Not Equal To
>Greater Than
>=Greater Than or Equal To
<Less Than
<=Less Than or Equal To
Relational Operators or Comparison Operators

The Example of Relational Operators or Comparison Operators

a = 25

b = 35

# equal to operator
print('a == b =', a == b)

# not equal to operator
print('a != b =', a != b)

# greater than operator
print('a > b =', a > b)

# greater than or equal to operator
print('a >= b =', a >= b)

# less than operator
print('a < b =', a < b)

# less than or equal to operator
print('a <= b =', a <= b)
Example of Relational Operators or Comparison Operators

Let’s run the code

Example of Relational Operators or Comparison Operators
Example of Relational Operators or Comparison Operators

Logical operators

 This operators perform Logical AND, Logical OR and Logical NOT operations.

OperatorDescription
ANDTrue only if both the operands are true
ORTrue if at least one of the operands is true
NOTTrue if the operand is False and vice-versa.
Logical operators

The Example of Logical operators

a = True
b = False
   
# Print a and b is False 
print(a and b) 
   
# Print a or b is True 
print(a or b) 
   
# Print not a is False 
print(not a) 
Example of Logical operators

Let’s run the code

Example of Logical operators
Example of Logical operators

Bitwise Operators

These operators are applicable only for int and boolean types. By mistake if we are trying to apply for any other type then we will get Error.

OperatorDescription
&Bitwise AND
|Bitwise OR
Bitwise NOT
^Bitwise XOR
>>Bitwise right shift
<<Bitwise left shift
Bitwise Operators

The Example of Bitwise Operators

a = 81
b = 9
   
# Print bitwise AND operation   
print(a & b) 
   
# Print bitwise OR operation 
print(a | b) 
   
# Print bitwise NOT operation  
print(~a) 
   
# print bitwise XOR operation  
print(a ^ b) 
   
# print bitwise right shift operation  
print(a >> 2) 
   
# print bitwise left shift operation  
print(a << 2)
Example of Bitwise Operators

Let’s run the code

Example of Bitwise Operators
Example of Bitwise Operators

Assignment Operators

OperatorDescription
=Assignment Operator
+=Addition Assignment
-=Subtraction Assignment
*=Multiplication Assignment
/=Division Assignment
%=Remainder Assignment
**=Exponent Assignment
Assignment Operators

The Example Assignment Operators

# Assignment Operator
a = 20
# Addition Assignment
a += 18
print ("a += 18 : ", a)
# Subtraction Assignment
a -= 8
print ("a -= 8 : ", a)
# Multiplication Assignment
a *= 8
print ("a *= 8 : ", a)
# Division Assignment
a /= 8
print ("a /= 8 : ",a)
# Remainder Assignment
a %= 2
print ("a %= 2 : ", a)
# Exponent Assignment
a **= 5
print ("a **= 5 : ", a)
# Floor Division Assignment
a //= 15
print ("a //= 15 : ", a)
Example Assignment Operators

Let’s run the code

Example of Assignment Operators

If you have any queries regarding this article or if I have missed something on this topic, please feel free to add in the comment down below for the audience. See you guys in another article.

To know more about Python Wikipedia please click here .

Stay Connected Stay Safe, Thank you


Basic Engineer

Hey Readers! We have more than fifteen years of experience in Software Development, IoT, Telecom, Banking, Finance and Embedded domain. Currently we are actively working on Data Science, ML and AI with multiple market leaders worldwide. Happy Reading. Cheers!

0 Comments

Leave a Reply

Avatar placeholder

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