# Python Operators With Examples [Latest]

Sep 17, 2021

Python operators allow us to do common processing on variables. We will look into different types of python operators with examples and also operator precedence.

## Python Operators

Python Operators are the special symbols that can manipulate values of one or more operands.

## Python Operator Types

Python operators can be classified into several categories.

• Arithmetic Operators
• Logical Operators
• Comparison Operators
• Bitwise Operators
• Assignment Operators

## Python Arithmetic Operators

Operator Description Example
+ used to add two numbers sum = a + b
used for subtraction difference = a – b
* used to multiply two numbers. If a string and int is multiplied then the string is repeated the int times. mul = a*b

>>> “Hi”*5
‘HiHiHiHiHi’

/ used to divide two numbers div = b/a
% modulus operator, returns the remainder of division mod = a%b
** exponent operator

Output:

Python Arithmetic Operators

## Python Comparison Operators

Operator Description Example
== returns True if two operands are equal, otherwise False. flag = a == b
!= returns True if two operands are not equal, otherwise False. flag = a != b
> returns True if left operand is greater than the right operand, otherwise False. flag = a > b
< returns True if left operand is smaller than the right operand, otherwise False. flag = a < b
>= returns True if left operand is greater than or equal to the right operand, otherwise False. flag = a > b
<= returns True if left operand is smaller than or equal to the right operand, otherwise False. flag = a < b

Python Comparison Operators

## Python Bitwise Operators

Operator Description Example
& Binary AND Operator x = 10 & 7 = 2
| Binary OR Operator x = 10 | 7 = 15
^ Binary XOR Operator x = 10 ^ 7 = 13
~ Binary ONEs Compliment Operator x = ~10 = -11
<< Binary Left Shift operator x = 10<<1 = 20
>> Binary Right Shift Operator x = 10>>1 = 5

Python Bitwise Operators

## Python Logical Operators

Operator Description Example
and Logical AND Operator flag = exp1 and exp2
or Logical OR Operator flag = exp1 or exp2
not Logical NOT Operator flag = not(True) = False

Python Logical Operators

## Python Assignment Operators

Operator Description
+= a+=b is equivalent to a=a+b
*= a*=b is equivalent to a=a*b
/= a/=b is equivalent to a=a/b
%= a%=b is equivalent to a=a%b
**= a**=b is equivalent to a=a**b (exponent operator)
//= a//=b is equivalent to a=a//b (floor division)

Python Assignment Operators

## Python Operator Precedence

Precedence of python operators means the priority level of operators. This becomes vital when an expression has multiple operators in it. For example consider the following expression:

Now, what do you think the series of operation would be? We can add 2 and 3, then multiply the result by 4. Also, we can multiply 3 and 4 first, then add 2 with it. Here we can see that the operators’ precedence is important.

Below is a list of operators indicating the precedence level. It’s in descending order. That means the upper group has more precedence than that of the lower group.

1. Parenthesis – `()`
2. Exponentiation – `**`
3. Compliment, unary plus and minus – `~`, `+`, `-`
4. Multiply, Divide, modulo – `*`, `/`, `%`
5. Addition and Subtraction – `+`, `-`
6. Right and Left Shift – `>>`, `<<`
7. Bitwise AND – `&`
8. Bitwise OR and XOR – `|`, `^`
9. Comparison Operators – `==`, `!=`, `>`, `<`, `>=`, `<=`
10. Assignment Operator- `=`

Reference: Official Python Documentation