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.
Table of Contents
- Arithmetic Operators
- Comparison Operators
- Logical Operators
- Assignment Operators
- Identity Operators
- Membership Operators
- Bitwise Operators
- Operator Precedence
Arithmetic Operators
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.
Operator | Description | Syntax |
---|---|---|
+ | 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 |
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
Comparison operators are used for comparing two values. As an output, they return a boolean value, either True
or False
.
Operator | Description | Syntax |
---|---|---|
== | Equal to – True if both operands are equal | x==y |
!= | Not equal to – True if operands are not equal | x!=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 |
Example:
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
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>y =", x>y) # Less-Than Operator print("x<y =", x<y) # Greater-Than-Equal-To Operator print("x>=y =", x>=y) # Less-Than-Equal-To Operator print("x<=y =", x<=y)
The above code gives the following output on execution:
x==y = False x=!y = True x>y = True x<y = False x>=y = True x<=y = False
Logical Operators
These operators are used to perform logical and
, or
, and not
operations. They operate on boolean values and return a boolean value.
Operator | Description | Syntax |
---|---|---|
and | Logical AND – True if both the operands are true | x and y |
or | Logical OR – True if either of the operands are true | x or y |
not | NOT – Boolean compliment of the operand | not x |
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
Assignment Operators
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
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.
Operator | Description | Syntax |
---|---|---|
is | True if the two operands refer to the same memory location | x is y |
is not | True if the two operands do not refer to the same memory location | x is not y |
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
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.
Operator | Description | Syntax |
---|---|---|
in | True if the value or variable is present in the sequence | x in y |
not in | True if the value or variable is not present in the sequence | x not in y |
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
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 operators.
Operator | Name | Example |
---|---|---|
& | Bitwise AND | x&y = 1(0000 00 01) |
| | Bitwise OR | x|y = 11(0000 1011) |
~ | Bitwise NOT | ~x = -10(1111 0110) |
^ | Bitwise XOR | x^y = 10(0000 1010) |
>> | Bitwise Right Shift | x>>2 = 2(0000 0010) |
<< | Bitwise Left Shift | x<<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&y = ", x&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>>2 = ", x>>2) # Bitwise Left Shift print("x<<2 = ", x<<2)
The above code gives the following output on execution:
x&y = 1 x|y = 11 ~x = -10 x^y = 10 x>>2 = 2 x<<2 = 36
For more on Bitwise operators refer to Python’s wiki on Bitwise operators.
Operator Precedence in Python
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.
Operator | Description |
---|---|
() | Parenthesis have the highest precedence. |
x[index], x[index:index], x.attribute, x(args..) | Subscription, Slicing, Attributes, Function call |
** | Exponent |
+x, -x, ~x | Unary 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 |
not | Logical NOT |
and | Logical AND |
or | Logical OR |
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.