Operators In Python: with Examples

operators in python

Hello Techies,

This blog is about “Operators in Python”. Let’s check the various operators in Python with their examples. Check my previous blog which is based on basics of Python syntax

What are operators in Python?

Operators are specific symbols in Python that perform arithmetic or logical calculations. Operand is the value operated by the operator.

For Example:

>>> 2+5
8

Here, there is the + operator that does the addition. There are 2 and 3 are the operands and 5 is the output of the operation.

Types of Operators in Python

  1. Arithmetic operators
  2. Relational / Comparison operators
  3. Logical operators
  4. Bitwise operators
  5. Assignment operators
  6. Identity operators
  7. Membership operators

#1. Arithmetic operators in Python

Arithmetic operators are used to performing mathematical operations such as addition, subtraction, multiplication, etc.

OperatorExplanationSyntax
+Addition operation adds two operands a+b
Subtraction operation subtracts two operands a-b
*Multiplication operation multiplies two operands a *b
/Division (float) operation divides the first operand by the second operand a/b
//Division (floor) operation divides the first operand by the second operand a//b
%Modulus operation returns the remainder when the first operand is divided by the second operand a%b
**Power operation Returns first raised to power second a**b

Example

a = 5
b = 4

# Addition 
add = a + b  
print(add)
  
# Subtraction
sub = a - b  
print(sub)
  
# Multiplication
mul = a * b 
print(mul) 
  
# Division(float) 
div1 = a / b  
print(div1)
  
# Division(floor)
div2 = a // b
print(div2) 
  
# Modulus
mod = a % b  
print(mod)
  
# Power 
power = a ** b 
print(power)

Output

9
1
20
1.25
1
1
625

#2. Relational / Comparison operators in Python

Relational operators are used to comparing the value of operands to create a logical value. The logical value is either true or false.

OperatorExplanationSyntax
<Less than operand returns True if the left operand is smaller than the right operanda<b
>Greater than operand returns True if the left operand is larger than the right operanda>b
<=Less than or equal to operand returns True if left operand is less than or equal to the right operanda<=b
>=Greater than or equal to operand returns True if left operand is greater than or equal to the right operanda>=b
==Equal to operand returns True if both operands are equala==b
!=Not equal to operand returns True if operands are not equala!=b

Example

a = 13
b = 33
  
# a < b is True
print(a < b) 
  
# a > b is False
print(a > b)  
  
# a <= b is True 
print(a <= b) 

# a >= b is False 
print(a >= b)
  
# a == b is False 
print(a == b) 
  
# a != b is True 
print(a != b) 

Output

True
False
True
False
False
True

#3. Logical operators in Python

Logical operators are used to connecting more relational operations to form a complex expression called logical expression. A value obtained by evaluating the logical expression is always logical, i.e. either True or False.

OperatorDescriptionSyntax
andAND operator returns True if both the operands are truea and b
orOR operator returns True if one of the operands is true
a or b
notNOT operator returns True if operand is falsea not b

Example

a = 5
b = 4
c = 8 
 
print(a>b and a>c) 
  
 
print(a>b or b<c) 
  
 
print(not(a>3)) 

Output

False
True
False

#4. Bitwise operators in Python

Bitwise operators are used to performing operations at the binary digit level. These operators are not commonly used and are only used in specialized applications where optimized use of storage is required.

OperatorExplanationSyntax
&Bitwise ANDa&b
|Bitwise ORa|b
~ Bitwise NOTa~b
^  Bitwise XORa^b
>>Bitwise right shifta>>b
<<Bitwise left shifta<<b

Example

a = 8
b = 4
  
# bitwise AND operation   
print(a & b) 
  
# bitwise OR operation 
print(a | b) 
  
# bitwise NOT operation  
print(~a) 
  
# bitwise XOR operation  
print(a ^ b) 
  
# bitwise right shift operation  
print(a >> 2) 
  
# bitwise left shift operation  
print(a << 2) 

Output

0
12
-9
12
2
32

#5. Assignment operators in Python

Assignment operators are used to performing arithmetic operations while assigning a value to a variable.

OperatorExplanationSyntax
=Assign the value to the right of the expression to the left operandc = a + b
+=Add AND operator used to Add right operand with left operand and then assign to left operanda+=b     a=a+b
-=Subtract AND operator used to Subtract right operand with left operand and then assign to left operanda-=b       a=a-b
*=Multiply AND operator used to Multiply right operand with left operand and then assign to left operanda*=b       a=a*b
/=Divide AND operator used to Divide left operand with right operand and then assign to left operanda/=b         a=a/b
%=Modulus AND operator used to Takes modulus using left and right operands and assigns results to left operandsa%=b   a=a%b
//=Divide(floor) AND operator used to Divide the left operand with the right operand and then assign the value (floor) to the left operanda//=b       a=a//b
**=Exponent AND operator used to Calculate the exponent value using the operands and assign the value to the left operanda**=b     a=a**b
&=Performs bitwise AND on operands and assign value to the left operanda&=b     a=a&b
|=Performs bitwise OR on operands and assign value to the left operanda|=b         a=a|b
^=Performs bitwise XOR on operands and assign value to the left operanda^=b       a=a^b
>>=Performs Bitwise right shift on operands and assign value to the left operanda>>=b     a=a>>b
<<=Performs Bitwise left shift on operands and assign value to the left operand a <<= b   a= a << b

Example

a = 5
b = 4

#Equal
c = a+b
print(c)

#Add AND
a+=b
print(a)

#Subtract AND
a = 5
a-=b
print(a);

#Multiple AND
a*=b
print(a);

#Divide AND
a = 5
a/=5
print(a);

#Modulus AND
a = 5
a%=4
print(a)

#Divide(floor) AND
a = 15
a//=10
print(a)

#Exponent AND
a = 5
a**=2
print(a)

Output

a = 5
b = 4

#Equal
9

#Add AND
9

#Subtract AND
1

#Multiple AND
20

#Divide AND
1.0

#Modulus AND
1

#Divide(floor) AND
1

#Exponent AND
25

#6. Identity operators in Python

The identity operators compare the memory location of two objects. Hence, it is possible to know whether two objects are the same or not.

Types Of Identity Operators:

  1. is
  2. is not

is :

  • This operator is used to compare whether two objects are the same or not.
  • It returns True if the memory location of two objects are the same else it returns False.

Example

a = 10
b = 10

print(a is b)

Output

True

is not:

  • This operator works in a reverse manner for is operator.
  • It returns True if memory location of two objects are not same and if they are same it returns False.

Example

a = 5
b = 5

print(a  is not b)

Output

False

Membership operators in Python

The membership operators are useful to test for membership in a sequence such as string, list, tuples, and dictionaries.

Types of membership operators:

  1. in
  2. not in

in:

  • in operator is used to find an element in the specified sequence.
  • It returns True if the element is found in the specified sequence else it returns False.
  • You can use this operator in lists, tuples, and dictionaries.

Example

str1 = "Welcome to TechPlusLifestyle"
print("to" in str1)

Output

True

not in:

  • not in operator works in a reverse manner for in operator.
  • It returns True if the element is not found in the specified sequence and if the element is found then it returns False.
  • You can use this operator in lists, tuples, and dictionaries.

Example

str1 = "Welcome to TechPlusLifeStyle"
print("too" not in str1)

Output

True

These are some operators in Python, I hope you understand the concept of Operators in Python. Comment down below if you have any doubts about the Operators in Python.

Leave a Comment