Arithmetic Operators in Python

At a basic level, computers only operate with numbers. Even within application programs in high-level languages, there are many numbers and operations internally. Fortunately, to get started it is enough to know the usual arithmetic. We will have a look at the various arithmetic operators in Python programming language.

Arithmetic Operations & Arithmetic Operators in Python

Before delving further into operations in Python, we must understand about operators and operands, What are operator and operands? This can be explained by a simple example:

10 + 5 = 15

In this expression, 10 and 5 are operands. The “+” sign is an operator. We process the operands along with the operators to obtain the end result value.

What operators are used to perform calculations in Python?

The Python language has a number of mathematical (arithmetic) operators for performing calculations in arithmetic expressions. The list of these operations in descending order of priority is as follows:

• ** – exponentiation
• –X – unary minus
• / , // – regular division, division with rounding down (same priority)
• % Is the remainder of the division operation
• * – multiplication
•  – subtraction

g = 10
h = 15
print (g + h)
Output:
25

g = 75
h = 15
print (g - h)
Output:
60

k = 100
l = 10
print (k * l)
Output:
1000

Division

When you do a direct division using (/) operator in Python 3, the quotient will always be a floating point number, even if you are using two integers:

m = 80
n = 5
print (m / n)
Output:
16.0

Using (//) operator we get an integral output.

m = 80
n = 5
print (m // n)
Output:
16

Modulus

The % operator is used for modulo division, and returns the remainder of the division, not the quotient. This is useful, for example, to find the factors of a number.

o = 85
p = 15
print (o% p)
Output:
10

Exponentiation

The “**” operator in Python is used to raise the number on the left of the operator to the power on the right. That is, in the expression 5 ** 3, the number 5 is raised to the third power. In mathematics, the expression 5³ is often used. That is, 5 is multiplied by itself three times. In Python, we get the same result (125) by doing 5 ** 3 or 5 * 5 * 5.

s = 5
t = 3
print (s ** t)
Output:
125

Let us look at an example involving these operations in the Python programming language,

print("Python program to demonstrate various Arithmetic Operations")
print(8 + 2)
print(8 - 2)
print(8 * 2)
print(8 / 2)
print(8.23 // 2)
print(8 ** 2)
print(8 % 2)
Output:
Python program to demonstrate various Arithmetic Operations
10
6
16
4.0
4.0
64
0

To understand the Python precedence of operators, let us look at an example:

number = 3 + 4 * 6 ** 2 + 7
print(number)
Output:
154

Here, at the beginning, exponentiation (6 ** 2) is performed as an operation with high priority, then the result is multiplied by 4 (36 * 4), then addition (3 + 144) and then addition (147 + 7) occurs again.

You can use parentheses to redefine the order of operations:

number = (3 + 4) * (6 ** 2 + 7)
print(number)
Output:
301

It should be noted that both integers and fractional numbers can take part in arithmetic operations. If an integer (int) and a floating point number (float) are involved in one operation, then the integer is cast to the float type.

Assignment Operators in Python

A number of special operations allow you to use to assign the result of an operation to the first operand:

• + = Assigning the result of addition
• – = Assigning the Subtraction Result
• * = Assigning the result of a multiplication
• / = Assigning the result from division
• // = Assigning the Result of an Integer Division
• ** = Assigning a power of a number
• % = Assigning the remainder of a division
#Examples of operations:
number = 10
number += 5
print(number)

number -= 3
print(number)

number *= 4
print(number)
Output:
15
12
48

We have gone through the various arithmetic operators in Python, assignment operators in Python along with various examples. In the next article we will explore the logical operators.