Like other programming languages such as C++ or Java, Python also offers a rich set of many operators for working with and comparing types of data or information.
The definition of an operator is simply a special symbol that tells the interpreter to perform a specific operation or action, usually on numbers (operands).
For example, when we add two values, e.g., (10 + 20 = 30). Here, 10 and 20 are numbers also called operands. The plus (+) sign is an arithmetic operator, and (10 + 20) is a numeric expression.
Operands and Expressions in Python
The operand in the expression can be a literal, variable, or any expression that has to be calculated. When the operand and operator combine to perform a certain operation, it forms an expression that is evaluated according to operators.
In Python, an expression is a statement that consists of any combination of operators, constants, variables, or functions. For example, the simplest expression is x + y. Here, x and y are the variables, and the plus (+) sign is an operator. When this expression is evaluated by the Python interpreter, it produces a single value.
Programmers extensively use operators in Python programs to perform some sort of calculation, comparison, or assignment on one or more values. For example, some common calculations might be finding the sum of two or more numbers, dividing two numbers or combining two strings, etc.
Moreover, an operator provides a shortcut to reduce the length of code so that we need to type less.
Types of Operators in Python
The Python language supports the following operators that are as follows:
- Arithmetic Operators
- Assignment Operators
- Comparison (Relational) Operators
- Logical Operators
- Bitwise Operators
- Special Operators
In Python, we can further classify the operators into two groups. They are:
- Unary operators
- Binary operators
Unary and Binary Operators in Python
Unary operators are those that require only a single operand to operate upon. Some of the unary operators used in Python are:
- + Unary plus
- – Unary minus
- ~ Bitwise NOT
Binary operators are those that require two operands to operate upon. Some of the binary operators used in Python are:
- Arithmetic operators
- Relational operators
- Assignment operators, etc.
Let us have a look at the first arithmetic operators, and in the further tutorials, we will discuss the remaining operators one by one.
Arithmetic (Mathematical) Operators in Python
Operators that are used to perform the most common mathematical calculations or arithmetic operations are called arithmetic operators in Python.
All arithmetic operators are binary operators because they do operations on two operands (two or more values). Python supports seven types of arithmetic operators. They are:
- Addition (+)
- Subtraction (-)
- Multiplication (*)
- Division (/)
- Modulus (%)
- Floor division (//)
- Exponential (**)
These operators are also called mathematical operators. Let’s have a look at all Python arithmetic operators one by one with examples.
Addition Operator (+)
This operator is the most commonly used operator in Python. We can use it to add two or more numbers of any type, or to join two objects when applied to two strings, lists, tuples, or dictionaries.
Example 1: Addition of Two Numbers
Let’s write a simple Python program to add two numbers using (+) operator and display its result.
# Declare variables x and y with values.
x = 20
y = 30
sum = x + y # Adding two numbers with + operator.
print("Sum: ", sum) # Displaying the result.
Output:
Sum: 50
In this example, we have calculated the sum of two numbers and stored the result into a variable, sum.
Type Conversion in Addition Calculation
Python automatically performs type conversion when working with mathematical operators. When we use addition or other mathematical operators, the Python interpreter automatically converts different values, such as an integer (a non-decimal numeric value) and a float (a decimal numeric value), to the appropriate type.
Example 2: Type Conversion in Addition of Two Numbers
x = 20 # an integer value.
y = 30.60 # a float value.
sum = x + y # Adding two numbers with + operator.
print("Sum of two numbers: ", sum) # Displaying the result.
Output:
Sum of two numbers: 50.6
As you observe the result, Python added two numbers (an integer and a float) together and gave back the result as a float type.
String Concatenation Using (+) Operator
The plus sign operator (+) also performs string concatenation. It joins two or more strings into a single string. For example, the expression “Python” + “ Language” gives the result “Python Language” string.
We can concatenate (or join) any number of strings or literals together using the plus sign (+) operator. Consider the following example code where we will concatenate two strings using the + operator.
Example 3: Concatenation of Two String Literals
firstName = "John"
lastName = "Michael"
fullName = firstName + lastName # Joining two strings with + operator.
print("Full name: ", fullName) # Displaying the result.
Output:
Full name: John Michael
List Concatenation Using (+) Operator
The plus sign (+) operator also allows us to concatenate two or more lists. Let’s take an example of it.
Example 4: Concatenate Two Lists
x = [1, 2, 3, 4]
y = [5, 6, 7, 8]
result = x + y # Adding the lists x and y.
print(result)
Output:
[1, 2, 3, 4, 5, 6, 7, 8]
Q. Does the code have any errors? If not, what will be the output?
x = ('Hello', 20)
y = (20.50, 'Hi')
z = (20, 89)
result = x + y + z
print(result)
Subtraction Operator (-)
The minus operator (-) subtracts one number from another. That is, it subtracts the right value from the left.
Example 5: Subtraction Between Two Numbers
x = 20
y = 30.60
sub = x - y
print("Subtraction: ", +sub)
Output:
Subtraction: -10.600000000000001
In the above code, the third statement subtracts 30.60 from 20, and the result, -30, is stored into the variable sub.
Multiplication Operator (*)
The multiplication operator multiplies the values on either side of the operator. When the values on either side of the expression are integer or float numbers, they are multiplied together.
Example 6: Multiplication of Two Numbers
x = 20
y = 20.20
# Two variables multiplied using multiplication operator.
result = x * y
print("Multiplication: ", result)
Output:
Multiplication: 404.0
When we apply the multiplication operator (*) to objects like a string, a list, a tuple, or a dictionary, it repeats the object a number of times pursuant to the number mentioned after the * operator. Consider the below example code.
Example 7: Using Multiplication Operator with String
text = 'Hello'
y = 2
result = text * y
print("Result: ", result)
Output:
Result: HelloHello
Try It Yourself
list = ['Python', 'Programming']
y = 2
result = list * y
print("Result: ", result)
Division Operator (/)
The division operator divides the left value by the right. The value on the left side is a dividend, and the value on the right side is a divisor. For example, the code 6/2 means 6 divided by 2 and returns the result of 3.0.
The division operator result is always a float value in Python. The right-side operand cannot be zero.
Example 8: Division Operation
x = 5 # Integer value.
y = 5 # Integer value
z = x / y
print("Division: ", z)
x = 2.4 # float value.
y = 1.2 # float value.
z = x / y
print("Division: ", z)
Output:
Division: 1.0 Division: 2.0
Modulus Operator (%)
The modulus operator divides the left-hand operand by the right-hand operand and returns the remainder. The sign of the result will be the sign of the quotient. The following example code is based on the modulus operator.
Example 9: Use of Modulus Operator
num1 = 23
num2 = 5
num = num1 % num2
print("Remainder after division: ", num)
x = 2.6
y = 1.2
z = x % y
print("Remainder after division: ", z)
Output:
Remainder after division: 3 Remainder after division: 0.20000000000000018
Exponential Operator (**)
This operator is the most powerful operator in Python. It is used to find the power of a value written on the left side of the operator. The left-side value is raised to the power of the right.
Example 10: Calculating Power of Value
num1 = 2
num2 = 4
exp = num1 ** num2
print("Result: ", exp)
x = 1.2
y = 3
exp = x ** y
print("Result: ", exp)
Output:
Result: 16 Result: 1.7279999999999998
Floor Division (//)
This operator returns the integer values obtained after applying the division operator. We mostly use it when we want to skip the value after the decimal part. For example:
Example 11:
num1 = 5
num2 = 2
num = num1 // num2
print("Result: ", num)
x = 12.2
y = 3.1
z = x // y
print("Result: ", z)
Output:
Result: 2 Result: 3.0
Example of All Arithmetic Operations Using Python Operators
Let’s write a Python program in which we will perform all mathematical or arithmetic operations by using all Python mathematical operators. Look at the following script code.
Example 12:
num1 = int(input("Enter the first number: "))
num2 = int(input("Enter the second number: "))
print("Addition (num1 + num2): ", (num1 + num2))
print("Subtraction (num1 - num2): ", (num1 - num2))
print("Multiplication (num1 * num2): ", (num1 * num2))
print("Division (num1 / num2): ", (num1 / num2))
print("Floor division (num1 // num2): ", (num1 // num2))
print("Modulus (num1 % num2): ", (num1 % num2))
print("Exponential (num1 ** num2): ", (num1 ** num2))
Output:
Enter the first number: 5 Enter the second number: 2 Addition (num1 + num2): 7 Subtraction (num1 - num2): 3 Multiplication (num1 * num2): 10 Division (num1 / num2): 2.5 Floor division (num1 // num2): 2 Modulus (num1 % num2): 1 Exponential (num1 ** num2): 25
In this tutorial, you have learned arithmetic operators in Python with the help of various example programs. I hope that you will have understood the basic points of all arithmetic operators and practiced all example programs. Stay tuned for our next tutorial, where you will learn assignment operators in Python.



