# Operators in Python

An operator in Python is a symbol which performs some mathematical operations between operands. For example, an assignment operator(=) is used to store values in variables, an addition operator(+) is used to add numbers.

Python provides us several groups of operators. These are as follows:

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

## Python Arithmetic Operators

Arithmetic operators are used to perform simple mathematical operations like addition, subtraction, multiplication, etc. Arithmetic operators are as follows:

## Example 1: Arithmetic Operators in Python

```# A program to demonstrate arithmetic operators in Python

a = 3
b = 2

print('a + b = ', a+b)

# Subtraction operator
print('a - b = ', a-b)

# Multiplication operator
print('a * b = ', a*b)

# Division operator
print('a / b = ', a/b)

# Modulus operator
print('a % b = ', a%b)

# Exponent operator
print('a ** b = ', a**b)

# Floor division operator
print('a // b = ', a//b)

```

### Output:

``````a + b =  5
a - b =  1
a * b =  6
a / b =  1.5
a % b =  1
a ** b =  9
a // b =  1``````

## Python Comparison Operators

Comparison operators are used to compare two operands. The result of comparison is always True or False.

## Example 2: Comparison Operators in Python

```# A sample program to demonstrate comparison operators in Python
a = 10
b = 20

# Equal operator
print('a == b is: ', a==b)

# Not equal operator
print('a != b is: ', a!=b)

# Less than operator
print('a < b is: ', a<b)

# Greater than operator
print('a > b is: ', a>b)

# Less than or equal operator
print('a <= b is: ',a<=b)

# Greater than or equal operator
print('a >= b is: ', a>=b)
```

### Output:

``````a == b is:  False
a != b is:  True
a < b is:  True
a > b is:  False
a <= b is:  True
a >= b is:  False``````

## Python Bitwise Operators

Bitwise operators are used to perform bitwise operations(operations on bits). This is done by converting a given number into its binary equivalent and then performing bit by bit operations.

## Example 3: Bitwise Operators in Python

```# A program to demonstrate bitwise operators in Python

a = 2 # Binary equivalent is: 1 0
b = 3 # Binary equivalent is: 1 1

# Bitwise AND
print('a & b is: ', a&b)

# Bitwise OR
print('a | b is: ', a|b)

# Bitwise NOT
print(' ~ a is: ', ~a)

# Bitwise XOR
print('a ^ b is: ', a^b)

# Left shift
print('a << 1 is: ', a<<1)  # Shifts each bit towards left by 1

# Right shift
print('a >> 1 is: ', a>>1) # Shifts each bit towards right by 1
```

### Output:

``````a & b is:  2
a | b is:  3
~ a is:  -3
a ^ b is:  1
a << 1 is:  4
a >> 1 is:  1``````

## Python Assignment Operators

Assignment operators are used to assign values to variables. Assignment is done using assignment operator ( = ). The assignment operator can also be used as a compound operator like +=, -=, etc.

``````# Simple assignment
a = 5  ``````
``````# Compound assignment
a += 5 # same as  a = a + 5
``````

## Python Logical Operators

Logical operators are used to perform logical operations on operands like logical AND, logical OR, etc. The result of the logical operation is always a Boolean(True or False) value.

## Example 5: Logical operators in Python

```# A program to demonstrate logical operators in Python

a = True
b = False

# Logical AND operator
print('a and b is: ', a and b)

# Logical OR operator
print('a or b is: ', a or b)

# Logical NOT operator
print('not a is: ', not a)
```

### Output:

``````a and b is:  False
a or b is:  True
not a is:  False``````

## Python Identity operators

Identity operators are used to check if two variables are equal or not. Here equal means not equal by value but whether both the variables have exactly the same object or not and also the same location in memory. If both the variables have the same address in the memory and also have the same object it simply means that they are equal otherwise not.

## Example 6: Identity operators in Python

```# A program to demonstrate identity operators in Python
a = [1, 2]
b = [1, 2]

print('a is b: ', a is b)
print('a is not b: ', a is not b)

print('id of a: ', id(a))
print('id of b: ', id(b))
```

### Output:

``````a is b:  False
a is not b:  True
id of a:  139632750588464
id of b:  139632750591024``````

In the above example, both a and b have exactly the same value but they are not equal. This is because, both the variables have a different identity(or a different location in memory).

## Membership Operators

Python membership operators are used to check the existence of a given variable in a given sequence(string, list, dictionary, etc). If the given variable exists in the sequence, the result is True, otherwise False. There are two membership operators which Python provides.

## Example 7: Membership operators in Python

```# A program to demonstrate membership operator in Python

a = 2
b = [1, 2, 3, 4, 5]

# The in operator
print('a in b is:  ', a in b)

# The not in operator
print('a not in b is:  ',a not in b)
```

### Output:

``````a in b is:   True
a not in b is:   False``````