Operators are fundamental part of a programming language as these are used for doing Mathematics on different entities. **Operators are special symbols which can be used for doing different kind of computations like arithmetic or logical computations.**

For example – You may want to add two numbers together, how to do this just use plus operation 10 + 23.

Python operators can be divided into 7 categories – **Arthimetic, Assignment, Comparison, Logical, Identity, Membership and Bitwise.**

These operators are applied to certain values and those values are called **Operands**.

Table of Contents

## Arithmetic Operators in Python

Operators which are used for mathematical operations like doing addition, subtraction, multiplication or division.**Below table lists all of Arithmetic Operators in Python with examples for each one.**

Operator Name | Operator Symbol | Description | Example |
---|---|---|---|

Addition | + | Adding values | 10 + 2 x + y |

Substraction | – | Subtracting values | 132 – 212 x – y |

Multiplication | * | Multiplying values | 163 * 4 x * y *z |

Division | / | Dividing values | 19/2 x/y |

Modulus | % | Remainder of left after dividing left operand on right | x % y 10 % 3 |

Floor Division | // | Division where resulting number is shifted to it’s closest lower whole number. Like 10//3 will be 3 only not 3.33 | 92//3 x//y |

Exponent | ** | Multiplying left operand with itself for as many times as right operand OR Left operand raised to the power of right | x**y 45**4 |

## Assignment Operators in Python

In Python, Assignment Operators are used for assigning values to variables. For example => **a = 5 here = equal symbol is an assignment operator.****Below table lists all of Assignment Operators in Python with examples for each one.**

Operator Name | Operator Symbol | Description | Example |
---|---|---|---|

Equal | = | Assigning value to a variable | x = 5 |

Increment and Assign | += | First increase value and then do assignment | x += 10 |

Decrement and Assign | -= | First Decrement value and then do assignment | x -= 40 |

Multiply and Assign | *= | First do multiplication and then do assignment | x *= 5 |

Divide and Assign | /= | First do division and then do assignment | x /= 38 |

Modulus and Assign | %= | First take modulus of operands and then do assignment | x %= 29 |

Floor Division and Assignment | //= | First do Floor Division and do assignment | x //= 24 |

Raise to Power and Assignment | **= | Firstly raise left operand to right power of right operand and then do assignment | x **= 26 |

And-percent and Assignment | &= | First do & operation(check for common elements inside operands) and then do assignment | x &= 29 |

OR Operator and Assignment | |= | First do logical OR operation on operands and then do assignment | x |= 10 |

Exclusive-OR and Assignment | ^= | First do exclusive OR operation on operands and then do assignment | x ^= 28 |

## Comparison Operators in Python

In Python Programming Language, **Comparison Operators** are used for comparing two values. For example – If we have two variables then to check which is greater that other **Comparison Operator** can be used.**Below is a table listing Comparison Operators in Python with some examples for each one.**

Operator Name | Operator Symbol | Description | Example |
---|---|---|---|

Greater than | > | Checks if left operand is larger than right operand. If it is then returns true false otherwise | x > y 10 > 28 |

Greater than and Equal to | >= | Checks if left operand is larger or equals to right operand. If it is then returns true false otherwise | x >= y 27 >= 17 |

Less than | < | Checks if left operand is smaller than right operand. If it is then returns true false otherwise | x < y 18 < 29 |

Less than and Equal to | <= | Checks if left operand is smaller or equals to right operand. If it is then returns true false otherwise | x <= y 38 <= 29 |

Equal to | == | Checks if both of operands are equal or not. If they are equal then returns true false otherwise | x == y 18 == 29 |

Not Equal to | != | Checks if both operands are not equal. Returns true if they’re not equal, false otherwise | x != y 17 != 28 |

## Logical Operators in Python

Python have three logical operators – **And, OR, Not**. These can be used for Control Flow in program based upon how variables evaluate in program.**Below is a table listing all of Logical Operators in Python with some example for each one.**

Operator Name | Operator Symbol | Description | Example |
---|---|---|---|

AND | and | True only if both operands are true, false otherwise. | x and y |

OR | or | True if either of operand is true, false otherwise | x or y |

NOT | not | Reverses Boolean value. If operand true change it to false, if its false change it to true | not x |

## Identity Operators in Python

Identity Operators are for checking whether two identities(say two variables) are located on same piece of memory.**Below is a diagram showing what does it mean to be on same piece of memory.**

Below is a table listing all identity operators in Python with some examples for each one.

Operator Name | Operator Symbol | Description | Example |
---|---|---|---|

is | is | Returns true if both of operands point to same memory location, false otherwise. | x is y |

is not | is not | Returns true if both of operands does not point to same memory location, true otherwise. | x is not y |

## Membership Operator in Python

In Python Programming Language, **Membership Operator** is used for checking whether some value is a member of sequence. For example – If we have a **list [ 19, 28, 21 ]** then to check whether **19 is there in list**. We can do **19 in list** membership test, where **in** is a Membership Operator.

If your not aware of **What are sequences in Python? Or What are other 5 data types expect Sequences?** then check out below article.

Below is a table listing all of Python’s Membership Operators with examples for each one.

Operator Name | Operator Symbol | Description | Example |
---|---|---|---|

in | in | Returns true if value is inside sequence, false otherwise | 10 in x |

not in | not in | Returns true if value is not inside sequence, false otherwise(If value is inside sequence) | 28 not in x |

## Bitwise Operators in Python

**These operators treat operands in binary format rather than normal format.**

Below is a table listing Python’s Bitwise Operators with some examples for each one.

Operator Name | Operator Symbol | Example |
---|---|---|

Bitwise AND | & | 13 & 3 Returns 1 |

Bitwise OR | | | 15 | 3 Returns 12 |

Bitwise XOR | ^ | 34 ^ 28 Returns 62 |

Bitwise Ones Complement | ~ | ~23 Returns -24 |

Bitwise Left Shift | << | 8 << 3 Returns 64 |

Bitwise Right Shift | >> | 24 >> 5 Returns 0 |

## No Comments

Leave a comment Cancel