# Python Operators

Updated Aug 5, 2020 View by 1.5 K

In this tutorial, you will learn about Python Operators and their types. Also, we will discuss their operational functionalities with examples.

**Python Operators**

A **Python** **Operator **is a symbol that tells the interpreter to perform certain mathematical or logical manipulation. In simple terms, we can say Python operators are used to manipulating data and variables. In programming universe, operators represent computations and conditional resemblance.

Python operators can be classified as listed below.

**Python Arithmetic Operators**

As the name suggests, Arithmetic Operators are used in Arithmetic (Mathematics) operations.

Let’s assume following two variables:

- x = 5
- y = 2

Operator | Name & Meaning | Example & Result |
---|---|---|

+ | Addition: Add two operands or unary plus | x + y = 7 |

– | Subtraction: Subtract right operand from the left | x – y = 3 |

* | Multiplication: Multiplies two operands | x * y = 10 |

/ | Division: Divides left operand by the right one | x / y = 2.5 |

% | Modulus – Takes the remainder | x % y = 1 |

// | Floor division: Division that results into the whole number truncating digits after decimal point | x // y = 2 |

** | Exponent – left operand raised to the power of right | x**y (5^2) = 25 |

**Example demonstrating use of Python Arithmetic operator**

**Python Relational Operator or Comparison Operator**

Relational or Comparison Operators are used to compare the operands on the either side of them.

Operator | Meaning |
---|---|

> | Greater than: Eg. x > y will return true if x is greater than y |

< | Less than: Eg. x < y will return true if x is less than y |

== | Equal to: Eg. x == y will return true if x is equal to y |

!= | Not equal to: x != y will return true if x is not equal to y |

>= | Greater Than or Equal to: Eg. x >= y will return true if x is greater than or equal to y |

<= | Less Than or Equal to: Eg. x <= y will return true if x is less than or equal to y |

**Example demonstrating use of Python Logical operator**

**Python Assignment Operators**

As simple as it sounds assignment operators are used for assigning values to variables.

Operator | Example | Meaning |
---|---|---|

= | x = 1 | Assigns 1 to x |

+= | x += 1 | x = x + 1 |

-= | x -= 1 | x = x – 1 |

/= | x /= 1 | x = x / 1 |

%= | x %= 1 | x = x % 1 |

*= | x *= 1 | x = x * 1 |

**= | x **= 1 | x= x ** 1 |

//= | x //= 1 | x = x // 1 |

**Example demonstrating use of Python Assignment operator**

**Python Bitwise Operators**

Bitwise operators take binary digits as operands and perform bit by bit operations.

Following is the list of bitwise operators supported in Python.

let’s assume:

- a = 5 = 0101 (in binary)
- b = 7 = 0111 (in binary)

Now if we were to use bitwise operator AND (&), it would generate following output.

a&b = 0101 & 0111 = 0101

**Explanation:** Here bitwise operator performs bit by bit operation. So, if we observe from left most bit, following operation happens.

(1st bit) 0&0 = 0, (2nd bit) 1&1 = 1, (3rd bit) 0&1 = 0, (4th bit) 1&1 = 1

Hence the output: 0101

Here is the list of bitwise operators in Python.

Operator | Example | Operation |
---|---|---|

& (Binary AND) | a & b | 0101 & 0111 = 0101 |

| (Binary OR) | a | b | 0101 | 0111 = 0111 |

^ (Binary XOR) | a ^ b | 0101 ^ 0111 = 0010 |

~ (Binary NOT) | ~ a | ~5 = -6 (11111010) |

>> (Binary right shift) | a >> 1 | 0101>>1 = 0010 |

<< (Binary left shift) | b << 1 | 0111<<1 = 1110 |

**Python Membership Operators**

Python membership operator is used to check whether a variable is a member of a sequence such as String, List, Tuple and Dictionary.

Note: In the Python dictionary, we can check the membership of key only, not the value.

Here are the membership operators in Python.

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

in | x in y | True if x is the member of y |

not in | x not in y | True if x is not the member of y |

**Example demonstrating use of Python Membership operator**

**Python Identity Operators**

Python identity operators are used to check if the operands have identical memory location. In simple language, it compares the memory location of two objects and returns True if both objects have identical or same memory location.

Here are the identity operators in Python.

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

is | x is y | True if both variables point to same memory location |

is not | x is not y | True if both variable point separate memory location |

**Example demonstrating use of Python Identity operator**

So, these are the Python operators explained above with examples which are used in manipulating data either mathematically or logically.

Now that we have discussed Python operators, let’s learn about how is the precedence of operators in Python.

### Precedence of Python Operators

Here is the list of Python Operators in descending order, listing from higher precedence to lower precedence.

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

( ) | Parentheses |

** | Exponent (raise to the power) |

+, -, ~ | Unary plus, Unary minus and Bitwise NOT |

*, /, %, // | Multiplication, Division, Modulus and Floor Division |

+, – | Addition and Subtraction |

>>, << | Bitwise Right Shift and Bitwise Left Shift |

& | Bitwise AND |

^, | | Bitwise XOR and OR |

<=, <, >, >= | Comparison Operators |

==, != | Equality Operators |

=, %=, /=, //=, -=, +=, *=, **= | Assignment Operators |

is, is not | Identity Operators |

in, not in | Membership Operators |

not, or, and | Logical Operators |