Operators are symbols that are used to perform operations on values and variables. For example,

`print (6 + 5) # 11`

Here, `+`

is an operator that adds **6** and **5**.

## Types of Python Operators

The different types of operators used in Python are:

- Arithmetic Operators
- Comparison Operators
- Logical Operators
- Bitwise Operators
- Special Operators

Let's learn about them in detail.

## Arithmetic Operators

Arithmetic operators perform arithmetic operations like addition, multiplication, subtraction etc.

List of Arithmetic operators:

```
Addition: +
Subtraction: -
Multiplication: *
Division: /
Floor division: //
Remainder: %
Exponent: **
```

### Example: Python Arithmetic Operator

```
x = 7
# addition
result = x + 5
print('Result from addition:', result)
# subtraction
result = x - 5
print('Result from subtraction:', result)
# multiplication
result = x * 10
print('Result from multiplication:', result)
# division
result = x / 10
print('Result from division:', result)
# floor division
quotient = x // 2
print('Quotient:', quotient)
# modulus division
remainder = x % 2
print('Remainder:', remainder)
# exponent
result = x ** 2
print('Exponent:', result)
```

**Output**

Result from addition: 12 Result from subtraction: 2 Result from multiplication: 70 Result from division: 0.7 Quotient: 3 Remainder: 1 Exponent: 49

## Comparison Operators

Comparison operators compare values between operands and return `True`

or `False`

. For example,
`>`

, `<`

, `==`

etc.

Some of the comparison operators:

Operator | Example | Meaning |

> | x > y | x greater than y |

< | x < y | x less than y |

== | x == y | x is equal to y |

!= | x != y | x is not equal to y |

>= | x >= y | x is greater than or equal to y |

<= | x <= y | x is less than or equal to y |

### Example: Python Comparison Operator

```
x = 5
y = 6
# greater than
print('x is greater than y:', x > y)
# less than
print('x is less than y:', x
```

**Output**

x is greater than y: False x is less than y: True x is less than or equal to y: True

## Logical Operators

Logical Operators perform logical operations and returns `True`

or `False`

.

Some of the logical operators are:

- and - returns
`True`

if both operands are true - or - returns
`True`

if one of the operands is true - not - returns
`True`

if both operands are false

### Example: Python Logical Operator

```
x = False
y = True
# and operation
result = x and y
print('x and y is', x and y)
# or operation
result = x or y
print('x or y is',x or y)
# not operation
result = not x
print('not x is',not x)
```

**Output**

x and y is False x or y is True not x is True

## Bitwise Operators

Bitwise Operators perform bitwise calculations on integers. It simply performs bit by bit comparison.

Some of the bitwise operators are:

Operator | Example | Meaning |

& | x & y | Bitwise AND |

| | x | y | Bitwise OR |

<< | x << y | Bitwise left shift |

>> | x >> y | Bitwise right shift |

~ | ~x | Bitwise NOT |

^ | x ^ y | Bitwise XOR(exclusive OR) |

### Example: Python Bitwise Operator

```
x = 5 # 5 = 0000 0101 in binary
y = 6 # 6 = 0000 0110 in binary
# Bitwise AND
print(x & y) # 4
# Bitwise OR
print(x | y) # 7
# Bitwise NOT
print(~x) # -6
# Bitwise left shift
print(x > y) # 0
# Bitwise XOR
print(x ^ y) # 3
```

## Special Operators

In Python, there are special types of operators like:

- identity operator
- membership operator

### Identity Operators

Identity operators are used to check if two values are located on the same part of the memory. The two identity operators are:

`is`

- returns`True`

if both variables are of same object`is not`

- returns`True`

if both variables are not the same object

### Example: Python Identity Operators

```
# integers
x1 = 7
y1 = 7
# strings
x2 = 'Python'
y2 = 'Python'
# lists
x3 = [5,6,7]
y3 = [5,6,7]
print(x1 is y1) # True
print(x2 is not y2) # False
print(x3 is y3) # False
```

Here,

`x1`

and`y1`

are both equal and identical integers. So,`x1 is y1`

returns`True`

`x2`

and`y2`

are both equal and identical strings. So,`x2 is not y2`

returns`False`

`x3`

and`y3`

are both equal but not identical lists. So,`x3 is y3`

returns`False`

**Note:** The interpreter locates lists separately in memory. So they are not
identical.

### Membership Operators

Membership operators check whether the given value is in the sequence(list, string, set, tuple and dictionary).

Membership operators are:

`in`

- returns`True`

if value is in the sequence`not in`

- returns`False`

if value is not in the sequence

### Example: Python Membership Operator

```
# membership operator with string
x = 'Python'
print('P' in x) # True
print('P' not in x) # False
# membership operator with dictionaries
y = {1:'a',2:'b'}
print(1 in y) # True
# membership operator with lists
z = ['a', 'b']
print('a' in z) # True
# membership operator with sets
s = {5, 6, 7}
print(6 not in s) # False
```

Here, in the dictionary, the membership operator checks whether the key(not the value) is found or not.

**Recommended Reading: ** Python Boolean