# Python Operators

### What are operators?

An operator in a programming language is a symbol that tells the compiler or interpreter to perform a specific mathematical, relational, or logical operation and produce the final result.

The operators in python are divided into the following groups:

• Arithmetic Operators
• Assignment Operators
• Comparison Operators
• Logical Operators
• Identify Operators
• Membership Operators
• Bitwise Operators

Let’s look at all the operator groups and operators belonging to a particular group.

### Arithmetic Operators:

Arithmetic operators are used to performing the common mathematical operations on numbers.

Code:

## Python Code

``````a = 8
b = 4

# subtraction
sub = a-b
print(sub)

# Multiplication
mult = a*b
print(mult)

# Division
div = a/b
print(div)

# Modulus
mod = a % b
print(mod)

# Floor Divison
floor_div = a//b
print(floor_div)

# Power
pow = a**b
print(pow)
``````

Output:

12
4
32
2.0
0
2
4096

### Assignment Operators:

Assignment operators are used to assigning values to the variables.

Code:

## Python Code

``````a = 8
b = 4

# Equal to
a = b
print(a)

a += b
print(a)

a -= b
print(a)

# Subtract AND assignement
a *= b
print(a)

# Divide AND assignement
a /= b
print(a)

# Floor Divide AND assignement
a //= b
print(a)

# Modulus AND assignement
a %= b
print(a)

x = 4
y = 3
# Left Shift AND assignement
x <<= y
print(x)

# Right shift AND assignement
x >>= y
print(x)

# Exponent AND assignment
x **= y
print(x)
``````

Output:

4
8
4
16
4.0
1.0
1.0
32
4
64

### Comparison Operators :

Comparison operators are used to comparing two values.

Code:

## Python Code

``````a = 40
b = 34

#a == b is False
print(a == b)

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

#a > b is True
print(a > b)

# a < is False
print(a < b)

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

#a <= b is False
print(a <= b)
``````

Output:

False
True
True
False
True
False

Logical Operators :

Logical Operators are used to combining the conditional Statements.

NOTE: S1 and S2 are conditional Statements.

Code:

## Python Code

``````a = 45
b = 353

# Logical AND
#a < 50 (True) , b > 350 (True)
# True and True = True
print(a < 50 and b > 350)

# Logical OR
#a > 30 (True), b < 350 (False)
# True | False = True
print(a > 30 or b < 350)

# logical Not
#a > 50 (False)
# not False = True
print(not (a > 50))
``````

Output:

True
True
True

### Identify Operators :

They are used to check if two values (or variables) are located on the same part of the memory. Two variables that are equal do not imply that they are identical.

Code:

## Python Code

``````x = 20
y = 30
z = x

# Example 1 :

# x and y are differnt objects
# x is not y = True
print(x is not y)

# z and x are same objects
# x is z = True
print(x is z)
``````

Output:

True
True

### Membership Operators :

These are used to check whether a value or variable is in sequence data types like list, tuple, or strings.

NOTE: a is value or variable. L is the sequence

Code:

## Python Code

``````L = [10, 20, 30, 40, 50]
x = 30

# checking variable x is present in L or not
if x in L:
print("x is present in the List L")
else:
print("x is not present in the List L")

# checking value 60 present in L or not
if 60 in L:
print("60 is present in List L")
else:
print("60 is not present in List L")
``````

Output:

x is present in the List L
60 is not present in List L

### BitWise Operators :

Bitwise acts on the bits of the number.

```Let a = 45 (101101) b = 60(111100)
Let’s see how the Bitwise operators work on each bit.
```
```a&b (101100)  = 44
a|b (111101)   = 61
a^b (010010)  = 17
```

Code:

## Python Code

``````a = 45
b = 60

# Bitwise AND Operation
print("Bitwise AND operation on 45,60 yields :", a & b)

# Bitwise OR operation
print("\nBitwise OR operation on 45,60 yields  : ", a | b)

# Bitwise XOR operation
print("\nBitwise XOR operation on 45,60 yields : ", a ^ b)

# Bitwise NOT operation
print("\nAfter reversing all the bits in 45, then it becomes : ", ~a)

# Left shift
print("\nUpon left shifting bits of 45 by 3 places then it becomes :", a << 3)

# Right Shift
print("\nUpon Right shifting bits of 60 by 3 places then it becomes :", b >> 3)
``````

Output:

Bitwise AND operation on 45,60 yields: 44

Bitwise OR operation on 45,60 yields: 61

Bitwise XOR operation on 45,60 yields: 17

After reversing all the bits in 45, then it becomes: -46

Upon left shifting bits of 45 by 3 places then it becomes: 360

Upon Right shifting bits of 60 by 3 places then it becomes: 7