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**

Table of Contents

**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**

- Arithmetic operators
- Relational / Comparison operators
- Logical operators
- Bitwise operators
- Assignment operators
- Identity operators
- Membership operators

**#1. Arithmetic operators in Python**

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

Operator | Explanation | Syntax |
---|---|---|

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

Operator | Explanation | Syntax |
---|---|---|

< | Less than operand returns True if the left operand is smaller than the right operand | a<b |

> | Greater than operand returns True if the left operand is larger than the right operand | a>b |

<= | Less than or equal to operand returns True if left operand is less than or equal to the right operand | a<=b |

>= | Greater than or equal to operand returns True if left operand is greater than or equal to the right operand | a>=b |

== | Equal to operand returns True if both operands are equal | a==b |

!= | Not equal to operand returns True if operands are not equal | a!=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.

Operator | Description | Syntax |
---|---|---|

and | AND operator returns True if both the operands are true | a and b |

or | OR operator returns True if one of the operands is true | a or b |

not | NOT operator returns True if operand is false | a 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.

Operator | Explanation | Syntax |
---|---|---|

& | Bitwise AND | a&b |

| | Bitwise OR | a|b |

~ | Bitwise NOT | a~b |

^ | Bitwise XOR | a^b |

>> | Bitwise right shift | a>>b |

<< | Bitwise left shift | a<<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**

**in Python**

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

Operator | Explanation | Syntax |
---|---|---|

= | Assign the value to the right of the expression to the left operand | c = a + b |

+= | Add AND operator used to Add right operand with left operand and then assign to left operand | a+=b a=a+b |

-= | Subtract AND operator used to Subtract right operand with left operand and then assign to left operand | a-=b a=a-b |

*= | Multiply AND operator used to Multiply right operand with left operand and then assign to left operand | a*=b a=a*b |

/= | Divide AND operator used to Divide left operand with right operand and then assign to left operand | a/=b a=a/b |

%= | Modulus AND operator used to Takes modulus using left and right operands and assigns results to left operands | a%=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 operand | a//=b a=a//b |

**= | Exponent AND operator used to Calculate the exponent value using the operands and assign the value to the left operand | a**=b a=a**b |

&= | Performs bitwise AND on operands and assign value to the left operand | a&=b a=a&b |

|= | Performs bitwise OR on operands and assign value to the left operand | a|=b a=a|b |

^= | Performs bitwise XOR on operands and assign value to the left operand | a^=b a=a^b |

>>= | Performs Bitwise right shift on operands and assign value to the left operand | a>>=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:**

- is
- 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:

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