# Operators In Python: with Examples

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 Operatorsin 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.

Example

``````a = 5
b = 4

# 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 operatorsin Python

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

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 operatorsin 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.

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 operatorsin 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.

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.

Example

``````a = 5
b = 4

#Equal
c = a+b
print(c)

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

9

#Subtract AND
1

#Multiple AND
20

#Divide AND
1.0

#Modulus AND
1

#Divide(floor) AND
1

#Exponent AND
25``````

### #6. Identity operatorsin 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 operatorsin 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.