Updated March 29, 2023
Introduction to Arithmetic Operators Python
Similar to any other programming language, Arithmetic operators in python are nothing but the symbols/ elements used for representing a specific mathematical and logical operation that is to be performed on a value or a variable assigned with a value. The various operators used for Arithmetic operations in python are ‘+’ for addition, ‘-‘ for subtraction, ‘*’ for multiplication, ‘/’ for division, ‘%’ is modulus operator used for getting the remainder as a result of a division operation, ‘//’ for floor division and ‘**’ for exponent operator.
Different Arithmetic Operators in Python
Various arithmetic calculations like addition, subtraction, multiplication, division, floor division, modulus, exponent, etc., can be performed using arithmetic operators. Various methods like eval function, call functions, declare the variable and calculate, etc., can be used for arithmetic calculations in python.
The arithmetic operators in python are:
1. ‘+.’
The ‘+’ operator is used to perform addition. Two operands can be added using the ‘+’ operator.
Syntax:
x '+' y
Let’s take a simple example in which we will add two digits using the ‘+’ operator. The two digits are the operands.
x = 4
y = 5
print(x+y)
Output:
In the above example, x and y are the operands, ‘+’ is the operator, and 9 is the output.
2. ‘-’
The ‘-’ operator is used to perform subtraction. Two operands can be subtracted using the ‘-’ operator.
Syntax:
x '-' y
Let’s take a simple example in which we will subtract two digits using the ‘-’ operator. The two digits are the operands.
x = 5
y = 4
print(x-y)
Output:
In the above example, x and y are the operands, ‘-’ is the operator, and 1 is the output.
3. ‘*’
The ‘*’ operator is used to perform multiplication. Two operands can be multiplied using the ‘*’ operator.
Syntax:
x '*' y
Let’s take a simple example in which we will multiply two digits using the ‘*’ operator. The two digits are the operands.
x = 4
y = 5
print(x*y)
Output:
In the above example, x and y are the operands, ‘*’ is the operator and 20 is the output.
4. ‘/’
The ‘/’ operator is used to perform division. The left operand is divided by the right operand. Two operands can be divided using the ‘/’ operator.
Syntax:
x '/' y
Let’s take a simple example in which we will divide two digits using the ‘/’ operator. The two digits are the operands.
x = 4
y = 2
print(x/y)
Output:
In the above example, x and y are the operands, ‘/’ is the operator and 2 is the output.
5. ‘%’
The ‘%’ operator is used to find out the remainder of the division when the left operand is divided by the right operand. The remainder of the two operands can be found using the ‘%’ operator. The ‘%’ operator is called the modulus operator.
Syntax:
x '%' y
Let’s take a simple example in which we will find out the reminder of two digits when one digit is divided by the other using the ‘%’ operator. The two digits are the operands.
x = 4
y = 2
print(x%y)
Output:
In the above example, x and y are the operands, ‘%’ is the operator and 0 is the output.
6. ‘//’
The ‘//’ operator is called the floor division operator. The results of division operation are converted into the whole number by adjusting the number to the left in the number line using floor division.
Syntax:
x '//' y
Let’s take a simple example in which we will divide two digits and apply floor division ‘//’ operator. The two digits are the operands.
x = 5
y = 2
print(x//y)
Output:
In the above example, x and y are the operands, ‘//’ is the operator and 2 is the output. The operation’s actual output is 2.5, but as we have applied floor division, the number before the decimal point is adjusted to the left in the number line, and the output is 2.
7. ‘**’
The ‘**’ operator is called the exponent operator. One operand raised to the power of
another operand is the exponent ‘**’ operator’s functionality. Its left operator raised to the power of the right operator.
Syntax:
x '**' y
Let’s take a simple example in which we will divide two digits and apply floor division ‘//’ operator. The two digits are the operands.
x = 2
y = 2
print(x**y)
Output:
In the above example, x and y are the operands, ‘**’ is the operator and 4 is the output.
Example to Implement Arithmetic Operators in Python
Let’s take a simple example to understand all the arithmetic operators in a python program by including all of them in a single program below:
Code:
x = 15
y = 4
print('x + y =',x+y)
print('x - y =',x-y)
print('x * y =',x*y)
print('x / y =',x/y)
print('x // y =',x//y)
print('x ** y =',x**y)
After running the program, the snapshot of the above program’s output is shown below, verifying the output given in the program. The ‘#’ symbol at the beginning of the line in a program indicates comments.
The values for x and y operands are 15 and 4. The output of addition is 19, the output of subtraction is 11, the output of multiplication is 60, the output of division is 3.75, the output of floor division is just 3, and the exponent operator’s output is 50625.
Conclusion
This article has learned about different types of operands and operands in python, their functionality, and examples of how to use them.
Recommended Articles
This is a guide to Arithmetic Operators in Python. Here we discuss the introduction, Different Arithmetic Operators in Python, along with examples codes and their output. You may also look at the following articles to learn more –