 # Operators in Python

Print   11 min read
02 Sep 2022
Intermediate
1.85K Views

Operators are foundation of any programming language and same applicable for Python. Operators are special symbol which help to carry out mathematical and logical computations. The values on which operators operate are know as operands.

For Example
```>>> 2 + 6
>>>8
```

Here ‘+’ is an arithmetic operator. In above example 2 and 6 are the operands and ‘+’ is the operator.

## Python Arithmetic operators

Arithmetic operators are used to perform mathematical operations such as addition, subtraction, multiplication etc. Arithmetic operators available in Python are : ‘+’ , ‘-’, ‘/’, ‘*’, ‘%’, ‘//’, ‘**’

Operator
Meaning
Syntax
Add two operands or just a unary plus operator
x + y
- Subtraction
Subtracts right hand side operator from left side or unary minus
x - y
* Multiplication
Multiplies operands
x * y
/ Division(float)
Division (float): divides the first operand by the second
x/y
// Division(floor)
Floor division - division that results into whole number
x//y
% Modulus
Modulus : Gives the remainder when first operand is divided by other
x % y
** Exponent
left value raised to the power of right
x**y

### Examples of Arithmetic Operator

```x = 10
y = 5

# Addition of values
add = x + y
# Subtraction of values
sub = x - y
# Multiplication of values
mult = x * y
# Division(float) of values
div_float = x / y
# Division(floor) of values
div_floor = x // y
# Modulo of both values
modu = x % y

# print results
print(“Subtraction result :- ”, sub)
print(“Multiplication result :- ”,mult)
print(“Division float result :- ”, div_float)
print(“Division floor result :- ”, div_floor)
print(“Modulo result :- ”, modu)
```
Output
```Addition result :- 15
Subtraction result :- 5
Multiplication result :- 50
Division float result :- 2.0
Division floor result :- 2
Modulo result :- 0
```

## Python Comparison operators

Comparison operators are also called relational operators as they decides the relation between two operands. It returns either a “True” or “False” according to condition. Arithmetic operators available in Python are : ‘<’ , ‘>’, ‘==’, ‘!=’, ‘<=’, ‘>=’

Operator
Meaning
Syntax
> Greater than
Returns True if Left operand is greater than right
x > y
< Less than
Return True if Left operand is less than right.
x < y
== Equal to
Returns True if both the operands are equal
x == y
!= Not Equal to
(<>)Returns True if left operand is not equal to right operand.
x != y
<= Less than Equal to
Returns True if left operand is less or equal to the right operand.
x <= y
> = Greater than Equal to
Returns True if left operand is greater or equal to the right operand.
x >= y

### Examples of Comparison Operator

```
x = 20
y = 30

# x > y is False
print(x > y)

# x < y is True
print(x < y)

# x == y is False
print(x == y)

# x != y is True
print(x != y)

# x >= y is False
print(x >= y)

# x <= y is True
print(x <= y)
```
Output
```False
True
False
True
False
True
```

## Python Logical operators

Logical operators performs operations like Logical AND, Logical OR and Logical NOT. Logical operators available in Python are : ‘AND’ , ‘OR’, ‘NOT’. Logical Operators are also used to combine multiple conditions.

Operator
Meaning
Syntax
AND
Returns True if both the operands are True.
X AND Y
OR
Returns True if any one operand is True either left or right.
X OR Y
NOT
Complements the Operand will return True if operand is false
NOT X

```
X = True
Y = False

# AND operation
print(X AND Y)
# OR operation
print(X OR Y)
# NOT Operation
print(NOT Y)
```
Output
```
False
True
True
```

## Python Bitwise operators

Bitwise operators works on bits and perform bit by bit operation on operands. Bitwise operators available in Python are : ‘&’ , ‘|’, ‘~’, ‘^’, ‘>>’, ‘<<’.

Operator
Meaning
Syntax
& Bitwise AND
Sets bit if it’s present in both operands
x & y
| Bitwise OR
Set bit if present in any of operands.
x | y
~ Binary Ones Complement
Inverts all the bits
~x
^ X-OR operator
the bit if it is set in one operand but not both.
x ^ y
>> Bitwise Right Shift
Shift right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall off
x>>
<< Bitwise Left shift
Shift left by pushing zeros in from the right and let the leftmost bits fall off
x<<

### Examples of Bitwise Operator

```x = 10
y = 5

# Bitwise AND operation
print(x & y)
# Bitwise OR operation
print(x | y)
# Bitwise complement
print(~x)
# Bitwise XOR operator
print(x ^ y)
# Bitwise Right shift operator
print(x>> 2)
# Bitwise Left shift operator
print(x<< 2)
```
Output
```0
15
-11
15
2
40
```

## Python Assignment operators

Assignment operators are used assign a value to operand. Assignment operators available in Python are : ‘=’, ‘+=’ , ‘-=’, ‘/=’, ‘*=’, ‘%=’, ‘//=’, ‘**=’, ‘&=’, ‘|=’, ‘^=’, ‘>>=’, ‘<<=’.

Operator
Meaning
Syntax
=
Assign right side value to left side operand
x = y + z
adds right side operand to left and assign result to left side.
z += x same as, z = z + x
-= subtract AND
subtracts right side operand from left and assign result to right side.
x -= y same as, x = x - y
*= multiply AND
Multiply right side operand from left and assign result to right side.
x *=y same as, x = x * y
/= division AND
Division right side operand from left and assign result to right side.
x /=y same as, x = x / y
//= Division(floor) AND
Division floor right side operand from left and assign result to right side.
x //= y same as, x = x // y
%= Modulus AND
It modules two operand and assign result to left operand.
x %= y, same as, x = x % y
**= Exponent AND
Performs exponential on operators and assign value to the left operand
x** =y same as, x = x ** y
&= Bitwise AND
Bitwise AND operation is performed on operand and assigned value to the left operand
x&=y, x=x&y
|= Bitwise OR
Bitwise OR operation is performed on operand and assigned value to the left operand
x|=y, x=x|y
^= Bitwise XOR
Bitwise XOR operation is performed on operand and assigned value to the left operand
x^=y, x=x^y
>>= Bitwise right-shift
Bitwise right-shift operation is performed on operand and assigned value to the left operand
x>>=y, x=x>>y
<<= Bitwise left-shift
Bitwise left-shift operation is performed on operand and assigned value to the left operand
x <<= y, x= x << y

### Examples of Assignment Operator

```
# Assignment
x = 5
print(“ Assignment :-”, x)

x += 3
print(“ Addition :-”, x)

#Subtraction AND
x -= 3
print(“ Subtraction :-”, x)

#Multiplication AND
x *= 3
print(“ Multiplication :-”, x)

#Division AND
x /= 3
print(“ Division :-”, x)

#Exponent AND
x **= 3
print(“ Exponent :-”, x)

#Division Floor
x = 125
x //= 3
print(“ Division floor :-”, x)

#Modulus AND
x %= 3
print(“Modulus :-”, x)

#Bitwise AND
x = 5
x &= 3
print(“ Bitwise AND :-”, x)

#Bitwise OR
x = 5
x |= 3
print(“ Bitwise OR :-”, x)

#Bitwise XOR
x = 5
x ^= 3
print(“ Bitwise XOR :-”, x)

#Bitwise right shift
x = 5
x>>= 3
print(“ Bitwise right shift :-”, x)

#Bitwise left shift
x = 5
x <<= 3
print(“ Bitwise left shift :-”, x)
```
Output
```
Assignment :- 5
Subtraction :- 5
Multiplication :- 15
Division :- 5.0
Exponent :- 125.0
Division floor :- 41
Modulus :- 2
Bitwise AND :- 1
Bitwise OR :- 7
Bitwise XOR :- 6
Bitwise right shift :- 0
Bitwise left shift :- 40
```

## Python Special operators

Identity Operator “is” and “is not” are identity operators in Python. Identity operators are used to check if objects resides in same memory locations. Values is same doesn’t mean they resides in same memory location

operator
Meaning
Syntax
is
Returns true if operands are identical
x is y
is not
Returns true if operands are not identical
x is not y

### Examples of Identity operators

```
x = 5
y = 5

a = “dotnettricks”
b = “dotnettricks”

d = [1,2,3]
e = [1,2,3]

print(x is not y)
print(a is b)
print(d is e) # False since lists are mutable
```
Output
```False
True
False
```

Membership Operator - Membership operators are used to test whether a value or variable is in present in sequence. ‘in’ and ‘not in’ are membership operators available in Python.

Operator
Meaning
Syntax
in
Returns true if specified value is present in sequence
x in y
not in
Returns true if specified value is not present in sequence
x not in y

### Examples of membership operators

```a = “dotnettricks”
b = {2: ‘a’, 3: ‘b’}
d = [1,2,3]

print("dot" in a)
print("net" not in a)
print(2 in b) #
print(3 in d)
```
Output
```True
False
True
True
```

## Operators Precedence in Python

Below is lists of all operators from highest precedence to lowest.

Sr. No,
Description
1.
** Exponent
2.
~+- Complement, unary plus, unary minus
3.
* / % //Multiply, Divide, Modulo, divide floor
4.
5.
>><<Right Left bitwise Shift
6.
&Bitwise AND
7.
^|Bitwise Exclusive OR , Regular OR
8.
<> <=>=Comparison Operators
9.
== != <>Equality Operator
10.
= %= /= //= -= += *= **=Assignment Operators
11.
is is not Identity Operator
12.
in not in Membership Operator
13.
not or and Logical Operators
##### Summary

Python has all the operators which are available in other languages. There are more ways and different syntax are available to implement those operators in Python and some operators might have different meanings in other programming languages and somewhat different in Python.

Learn to Crack Your Technical Interview