Python Operators

Operators in Python are special symbols used to perform operations on values or variables. Arithmetic, Comparison, Logical, and Assignment operators are the most commonly used. However, there are other types as well.

This tutorial covers the different types of operators used in python along with examples to give you an idea of how and when to use them.

Tree diagram of operators in python
  • Arithmetic Operators
  • Comparison Operators
  • Logical Operators
  • Assignment Operators
  • Identity Operators
  • Membership Operators
  • Bitwise Operators
  • Operator Precedence

Arithmetic operators are used for mathematical computations like addition, subtraction, multiplication, etc. They generally operate on numerical values and return a numerical value. They’re also referred to as mathematical operators.

OperatorDescriptionSyntax
+Addition – Adds two operands.x+y
Subtraction – Subtracts the right operand from the the left operand.x-y
*Multiplication – Multiplies two operands.x*y
/Division – Divides the left operand by the right one. Results in a float value.x/y
//Integer Division – Divides the left operand by the right one. Results in an integer value.x//y
%Modulus – Gives the remainder on dividing the left operand by the right one.x%y
**Exponent – Raises the left operand to the power of the right one.x**y
Arithmetic Operators in Python

Example:

x = 20
y = 10
# Addition Operator
print("x+y =", x+y)
# Subtraction Operator
print("x-y =", x-y)
# Multiplication Operator
print("x*y =", x*y)
# Division Operator
print("x/y =", x/y)
# Integer Division Operator
print("x//y =", x//y)
# Modulus Operator (Gives the remainder on division)
print("x%y =", x%y)
# Exponent Operator
print("x**y =", x%y)

The above code gives the following output on execution:

x+y = 30
x-y = 10
x*y = 200
x/y = 2.0
x//y = 2
x%y = 0
x**y = 10240000000000

Comparison operators are used for comparing two values. As an output, they return a boolean value, either True or False.

OperatorDescriptionSyntax
==Equal to – True if both operands are equalx==y
!=Not equal to – True if operands are not equalx!=y
>Greater than – True if the left operand is greater than the right operand.x>y
<Less than – True if the left operand is less than the right operand.x<y
>=Greater than equal to – True if the left operand is greater than or equal to the right one.x>=y
Comparison Operators in Python.

Example:

x = 20
y = 10
# Equal-To Operator
print("x==y =", x==y)
# Not-Equal-To Operator
print("x!=y =", x!=y)
# Greater-Than Operator
print("x&gt;y =", x&gt;y)
# Less-Than Operator
print("x&lt;y =", x&lt;y)
# Greater-Than-Equal-To Operator
print("x&gt;=y =", x&gt;=y)
# Less-Than-Equal-To Operator
print("x&lt;=y =", x&lt;=y)

The above code gives the following output on execution:

x==y = False
x=!y = True
x&gt;y = True
x&lt;y = False
x&gt;=y = True
x&lt;=y = False

These operators are used to perform logical and, or, and not operations. They operate on boolean values and return a boolean value.

OperatorDescriptionSyntax
andLogical AND – True if both the operands are truex and y
orLogical OR – True if either of the operands are truex or y
notNOT – Boolean compliment of the operandnot x
Logical Operators in Python

Example:

x = True
y = False 
# Logical and
print("x and y =", x and y)
# Logical or
print("x or y =", x or y)
# not operator
print("not x =", not x)
print("not y =", not y)

The above code gives the following output on execution:

x and y = False
x or y = True
not x = False
not y = True

In python, the “equal to” symbol, = is used as the assignment operator. It’s used to assign values to variables. For example, x = 2 will assign the value 2 to the variable x.
The assignment operator can be compounded with different arithmetic or bitwise operators, for example, x += 2 is the same as x = x + 2

Identity operators are special operators in python which are used to compare two operands based on the memory location they refer to. is and is not are the two identity operators used in python.

OperatorDescriptionSyntax
isTrue if the two operands refer to the same memory locationx is y
is notTrue if the two operands do not refer to the same memory locationx is not y
Identity Operators in Python.

Example 1:

# With Integer Type: Integers are premitive data types
x = 2
y = 2

print("x is y =", x is y)
print("x is not y =", x is not y)

# Memory location assigned to x and y
print("Memory location of x =", id(x))
print("Memory location of y =", id(y))

The above code gives the following output on execution:

x is y = True
x is not y = False
Memory location of x = 94583763778912
Memory location of y = 94583763778912

In the above example, we can see that for primitive data types such as Integer, variables with the same values have been assigned to the same memory location. However, this is not the case with reference data types like lists. Even if they store the same values, they’re not allocated to the same location. See example #2 below.

Example #2:

# With lists: Lists are reference data types
x_ls = [1,2,3]
y_ls = [1,2,3]

print("x_ls is y_ls =", x_ls is y_ls)
print("x_ls is not y_ls =", x_ls is not y_ls)

# Memory location assigned to x and y
print("Memory location of x_ls =", id(x_ls))
print("Memory location of y_ls =", id(y_ls))

The above code gives the following output on execution:

x_ls is y_ls = False
x_ls is not y_ls = True
Memory location of x_ls = 140378530740416
Memory location of y_ls = 140378530742080

Membership operators are also special operators in python which are used to determine whether a value or variable is present in a sequence (String, list, tuple, set, and dictionary). in and not in are the membership operators used in python.

OperatorDescriptionSyntax
inTrue if the value or variable is present in the sequencex in y
not in True if the value or variable is not present in the sequencex not in y
Membership Operators in Python

Example:

x = 2
y = [1, 2, 3]

# in operator
print("x in y =", x in y)
# not in operator
print("x not in y =", x not in y)

The above code gives the following output on execution:

x in y = True
x not in y = False

Bitwise operators operate on the binary values of the operands bit-by-bit. For example, a Bitwise AND operation on 9(0000 1001 in binary) and 3(0000 0011 in binary), that is, 9 & 3 results in 1(0000 0001). Whereas, a Bitwise Or operation, 9 | 3 results in 11(0000 1011).

Example: Assuming x=9 and y=3, the following table illustrates examples for each of the Bitwise operator.

OperatorNameExample
&Bitwise ANDx&y = 1(0000 0001)
|Bitwise ORx|y = 11(0000 1011)
~Bitwise NOT~x = -10(1111 0110)
^Bitwise XORx^y = 10(0000 1010)
>>Bitwise Right Shiftx>>2 = 2(0000 0010)
<<Bitwise Left Shiftx<<2 = 36(0010 0100)

Example:

# x = 9 which is 0001 0001 in binary
x = 9
# y = 3 which is 0000 0011 in binary
y = 3

# Bitwise AND
print("x&amp;y = ", x&amp;y)
# Bitwise OR
print("x|y = ", x|y)
# Bitwise Not
print("~x = ", ~x)
# Bitwise XOR
print("x^y = ", x^y)
# Bitwise Right Shift
print("x&gt;&gt;2 = ", x&gt;&gt;2)
# Bitwise Left Shift
print("x&lt;&lt;2 = ", x&lt;&lt;2)

The above code gives the following output on execution:

x&amp;y =  1
x|y =  11
~x =  -10
x^y =  10
x&gt;&gt;2 =  2
x&lt;&lt;2 =  36

For more on Bitwise operators refer to Python’s wiki on Bitwise operators.

Operator precedence refers to the order in which operators are executed in expressions. The following table lists the precedence of different operators in python in descending order.

OperatorDescription
()Parenthesis have the highest precedence.
x[index], x[index:index], x.attribute, x(args..)Subscription, Slicing, Attributes, Function call
**Exponent
+x, -x, ~xUnary plus, Unary minus, Bitwise Not
*, /, //, %Multiplication, Division, Floor Division, Modulus
+, –Addition, Subtraction
<<, >>Bitwise shift operations
&Bitwise AND
^Bitwise XOR
|Bitwise OR
in, not in, is, is not, <, <=, >, >=, !=, ==Membership, Identity, Comparison operators
notLogical NOT
andLogical AND
orLogical OR
Operator Precedence in Python

Parenthesis, (), have the highest precedence during expression evaluations. Operators having the same precedence are evaluated left to right. For more info, refer to python docs on operator precedence.

Example:

x =  5 + 9 * (4 - 2) / 3**2
print(x)

The above code gives the following output on execution:

7.0

In the above example, the expression inside the parenthesis (4 – 2) is evaluated first, followed by the exponent, 3**2this reduces the above expression to 5 + 9 * 2 / 9. Since multiplication and division have the same precedence, the expression 9 * 2 / 9 is evaluated left to right resulting in 2.0. Now, the expression is reduced to 5 + 2.0 which results in 7.0.


In this tutorial, we covered the different types of operators used in python. If you found this article useful do give it a share! For more such articles subscribe to us.

If you’re a beginner looking to start your data science journey and learn python, check out our Python for Data Science Series.