An operator is a symbol that performs an operation on values and variables. You can use the following operator types in Python:

- Arithmetic Operators
- Comparison Operators
- Assignment Operators
- Logical Operators
- Bitwise Operators
- Membership Operators
- Identity Operators

**Arithmetic Operators**

Arithmetic operators are used to perform arithmetic operations like addition, subtraction, multiplication, division, etc.

Operator | Description | Format | Example |
---|---|---|---|

+ | Addition | a + b | 3 + 5 = 8 |

– | Subtraction | a – b | 10 – 7 = 3 |

* | Multiplication | a * b | 3 * 4 = 12 |

/ | Division | a / b | 21 / 7 = 3 |

& | Modulus | a % b | 23 % 7 = 2 |

** | Exponent | a ** b | 2 * 4 = 16 |

// | Floor Division – Rounds down to nearest whole number | a // b | 9 // 2 = 4 -9 // 2 = -5 |

**Comparison Operators**

Comparison operators are used to compare two values and return a boolean result (true/false).

Operator | Description | Format | Example |
---|---|---|---|

== | Equality | a == b | 5 == 5 |

!= | Inequality | a != b | 3 != 5 |

> | Greater than | a > b | 5 > 3 |

< | Less tahn | a < b | 3 < 5 |

>= | Greater than or equal to | a >= b | 5 >= 3 |

<= | Less than or equal to | a <= b | 3 <= 5 |

In Python 2, both `!=`

and `<>`

are used as Inequality Operator (a != b) (a <> b).

In Python 3, `<>`

is removed, only `!=`

is used for inequality comparison.

**Assignment Operators**

Assignment operators are used to assign right-side expressions to left-side variables.

Operator | Description | Format | Equivalent to |
---|---|---|---|

= | Assignment | a = b | a = b |

+= | Addition and Assignment | a += b | a = a + b |

-= | Subtraction and Assignment | a -= b | a = a – b |

*= | Multiplication and Assignment | a *= b | a = a * b |

/= | Division and Assignment | a /= b | a = a / b |

%= | Modulus and Assignment | a %= b | a = a % b |

**= | Exponent and Assignment | a **= b | a = a ** b |

=// | Floor Division and Assignment | a //= b | a = a // b |

**Logical Operators**

There are 3 logical operators in Python: AND, OR and NOT. These operators are mainly used in conditional statements.

Operator | Description | Format | Example |
---|---|---|---|

AND | If both the expression are true, then returns TRUE | condition1 AND contidion2 | (6 > 5) AND (5 > 4) returns TRUE |

OR | If at least one of the expressions is true, then returns TRUE | condition1 OR contidion2 | (5 > 4) OR (4 > 5) returns TRUE |

NOT | If condition is true, returns FALSE If condition is false, returns TRUE | NOT(condition) | NOT(5 > 4) returns FALSE |

**Bitwise Operators**

Bitwise operators are used to perform operations on individual bits in a number.

Operator | Description | Format |
---|---|---|

& (AND) | If both bits are 1, resulting bit will be 1 | 1010 & 0101 = 1111 |

| (OR) | If either of the bits is 1, resulting bit will be 1 | 1010 | 1001 = 1011 |

^ (XOR) | If only one of the bits is 1, resulting bit will be 1 | 1010 ^ 1001 = 0011 |

~ (Bitwise Inversion) | This operator calculates ones’ complement of the number | ~1100 = -1101 |

<< (Left shift) | Each bit in a number is shifted to left | 0001101 << 2 = 0110100 |

(Right shift) | Each bit in a number is shifted to right | 0110100 >> 2 = 0001101 |

Bitwise Inversion of a number `x`

is calculated as `-(x+1)`

. For instance, bitwise inversion of 151 (10010111) is -152 (-10011000).

**Membership Operators**

Membership operators are used to check the appearance of a value within a sequence like lists, strings or tuples. There are two membership operators in Python: IN, NOT IN

For the examples below consider prime list is defined as:

```
primes = [2, 3, 5, 7, 11]
```

Operator | Description | Format | Example |
---|---|---|---|

IN | Returns TRUE if the value appears in the sequence | a in b | print(7 in primes) returns TRUE |

NOT IN | Returns TRUE if the value does not appear in the sequence | a not in b | print(4 not in primes) returns TRUE |

**Identity Operators**

Identity operators are used to check whether two objects refer to the same object. There are two identity operators in Python: IS, IS NOT

For the examples below consider the code below. Both `var1`

and `var2`

have the same content but they are distinct objects in memory. `var3`

, on the other hand, refers to the same object as var1.

```
var1 = ["Hello", "World"]
var2 = ["Hello", "World"]
var3 = var1
print(var1 is var2)
print(var1 is var3)
```

Operator | Description | Format | Example |
---|---|---|---|

IS | Returns TRUE if both operands refer to the same object | a is b | `var1` is `var2` returns FALSE`var1` is `var3` returns TRUE |

IS NOT | Returns TRUE if the operands are different objects | a is not b | `var1` is not `var2` returns TRUE |

You can also use identity operators to check the type of an object.

Since the type of `var1`

is list, following code prints TRUE.

```
print(type(var1) is list)
```