Support Online
Skip to main content

Math Operations with Python Operators

Introduction

numbers appear almost everywhere in programming.
Screen sizes, geographical locations, money amounts, game scores,
They are used to express the elapsed time or colors in a video through numerical codes.

In short, the software world is incomplete without numbers — that's why it's so important to learn mathematical operations correctly in Python.

To be able to perform mathematical operations effectively in programming,
It's an important skill to develop because you'll be working with numbers a lot.

Of course, a general understanding of mathematics can make you a better programmer, but it's not a prerequisite.
Even if you have little knowledge of mathematics, you should see it as a tool.
It helps you achieve what you want to do and improve your logical thinking skills.

In this section we will work with the two most common numeric data types in Python:
integers and decimal numbers (floats).

  • Integers (int): Numbers that do not have a decimal part and can be positive, negative or zero
    (for example: …, -1, 0, 1, 2 …).
  • Decimal numbers (float): Values containing decimal points, that is, representing real numbers
    (for example: 9.0 or -2.25).

In this guide, we will examine the mathematical operators that can be used with these numeric data types in Python.

Operators

Operator is a symbol or function that represents an operation.
For example, the + (plus) sign in mathematics is an operator that expresses the addition operation.

We will see some operators we are familiar with from mathematics in Python,
but in addition there are some operators that are specific to programming.

Below is a brief summary table of math-related operators in Python.
In this lesson, we will examine each of these operators in detail.

TransactionWhat returns
x + yReturns the sum of x and y.
x - yReturns the difference of x and y.
-xchanges the sign of x.
+xReturns the id (as is its value) of x.
x * yReturns the product of x and y.
x / yReturns the divide of x by y (as a decimal).
x // yReturns the result of **integer division (floor division) of x by y.
x % yReturns the remainder of x divided by y.
x ** yReturns y prime (x to the y) of x.

We will also examine compound assignment operators, which combine arithmetic operators with =.
For example, operators such as += and *= both perform and assign the result to the same variable**.


Addition and Subtraction

In Python, the addition (+) and subtraction (-) operators work the same way as in mathematics.
You can even perform these operations directly by using Python as a calculator.

Now let's do a few examples and go over them, first let's start with integers:

print(5+3)
Output
8

Instead of writing the integers directly into the print expression, we can define variables that represent these numbers:

x = 34
y = 56
print(x+y)
Output
90

Integers can be positive, negative or zero. It is also possible to add a negative number and a positive number. Now let's do an example:

a = -34
b = 16
print(a+b)
Output
-18

Addition works the same way with decimal numbers (float). Let's do an example for you to understand better:

a = 5.3
b = 6.2
print(a+b)
Output
11.5

Since we are adding two float numbers, Python returns the result as a float value.

The syntax of subtraction is the same as addition,
the only difference is that the operator is - instead of +:

c = 64.43
d = 32

print(c-d)
Output
32.43

Here we subtracted an integer (int) from a decimal number (float).
In Python, if there is at least one float in the operation, the result is returned as float type**.


Single Arithmetic Operations

unary mathematical expressions consist of only one element.
In Python, the + and - flags can be used alone to protect the identity (+) or change the sign (-) of a value.

Although not used very often, the sign + represents the identity (as well as the value) of a number.
So when used with a positive number it doesn't change the value:

i = 135

print(+i)
Output
135

When we use the + sign with a negative value, Python returns the same value as that number itself, so the result is still negative:

j = -456

print(+j)
Output
-456

When a negative number of signs + is used, the result is still the same negative value.

In contrast, the sign - reverses the sign of a number. So if you put - in front of a positive number, you get a negative value:

i = 135

print(-i)
Output
-135

Similarly, if you use the sign - with a negative number,
This time the sign changes and the result becomes a positive value:

j = -456

print(-j)
Output
456

Unary arithmetic operations are performed with the signs + and -.
The expression +i returns the same value as the number itself,
The expression -i returns the opposite sign of the number.


Multiplication and Division

Just like addition and subtraction,
multiplication and division operations are also very similar to mathematics in Python.

  • The sign * is used for multiplication.
  • The / sign is used for division.

Here's an example of multiplication with two decimal numbers (float):

i = 100.1
j = 10.1

print(i * j)
Output
1011.0099999999999

When you do a division operation in Python, result (quotient) always returns float (decimal number).
Even if you use two integers:

m = 90
n = 10

print(m / n)
Output
9.0

This is one of the biggest differences between Python 2 and Python 3.
In Python 3, the / operator always returns a decimal result.

For example, the result of 13 / 2 is 6.5.
However, in Python 2, the same expression would return 6, because it does integer division.

In Python 2, the / operator performs integer division (floor division).
That is, the result returns the largest integer that is less than or equal to the quotient value.

For example, if you run the expression print(80 / 5) in Python 2,
the output is 16 and the decimal part is not shown.

You can use the // operator to do this in Python 3.
For example, 100 // 40 returns 2.

Integer division is very useful when you want to get the result as an integer.


Mod (Remainder) Transaction

The % operator is the mod (remainder) operator.
After division, it returns the remainder, not the division.

This operator is especially useful for checking whether a number is a multiple of another number.

Let's see how it works with an example:

m = 85
n = 15

print(m / n)
Output
10

Let's explain this process step by step:

  • When the number 85 is divided by 15, quotient is 5 and remainder is 10.
  • % (modulo) operator returns this remainder value.
  • So the result here is 10.

If you perform modulo (%) with two decimal floats,
Python also returns the remaining value as float type:

a = 42.0
b = 6.0

print(a % b)
Output
0.0

Since the number 42.0 is divisible by 6.0, there is no remainder.
Therefore, the operation result is returned as 0.0.


Exponentiation

In Python, the ** operator is used to raise the number on the left to the power on the right.
So in the expression 5 ** 3, the number 5 is 3. is increased to the power.

In mathematics we often see this as
In fact, this means 5 multiplied by itself 3 times.

In Python, both 5 ** 3 and 5 * 5 * 5 expressions return the same result, namely 125.

Now let's create variables and demonstrate the same process with an example:

x = 5.2
y = 7

print(x ** y)
Output
102807.1702528

Number 5.2 with the operator ** 7. When we increase it to the power,
Python performs the operation and returns a very large float value.


Operator Priority

In Python (just like in mathematics) operations are not left to right or right to left,
It is evaluated according to order of priority.

So some operators run before others.

Let's do an example to understand better:

a = 10 + 10 * 5

Even though we are reading this expression from left to right, Python will do the multiplication first.

So when we run the command print(u), we get the following result:

Output
60

The reason for this is to first perform 10 * 5 and obtain the result 50,
Then 10 is added to this result to return the value 60.

If we want to first perform 10 + 10 and then multiply the result by 5,
Just like in math, we can use brackets:

a = (10 + 10) * 5
Output
100

An easy way to remember process priority is
Using the abbreviation PEMDAS:

SequenceLetterMeaning
1PParentheses
2EExponent
3MMultiplication
4DDivision
5AAddition
6SSubtraction

The important thing is to remember the correct order when performing mathematical operations in Python.
thus getting the results you expect.


Assignment Operators

The most commonly used assignment operator is actually the sign =, which you already recognize.
The = operator assigns the value on the right to the variable on the left.

For example:

v = 23

While programming, you can operate on the current value of a variable.
compound assignment operators, which assign the result back to the same variable, are frequently used.

These operators are a combination of an arithmetic operator and the sign =.
For example, for addition, + and = are combined and += is obtained.

Let's see what this looks like:

a = 82
a += 3
print(a)
Output
85

First, we assigned the value 82 to the variable a.
Then, using the compound assignment operator +=, we added the number on the right to the value of the variable on the left. and we assigned the result back to the variable a.

Compound assignment operators are used quite often, especially in for loops where we want to repeat an operation multiple times.
This way we can easily update the value of a variable in each round of the loop:

for x in range(0, 5):
x *= 2
print(x)
Output
0
2
4
6
8

Thanks to this for loop, we have automated the process performed by the *= operator.
Variable x multiplied by 2 at each loop step
and the result obtained was assigned again to the variable x to be used in the next step.

Python has a compound assignment operator for every arithmetic operator we look at in this lesson:

y += 1 # önce topla, sonra sonucu ata
y -= 1 # önce çıkar, sonra sonucu ata
y *= 2 # önce çarp, sonra sonucu ata
y /= 3 # önce böl, sonra sonucu ata
y //= 5 # önce tam sayı bölmesi yap, sonra sonucu ata
y **= 2 # önce üssünü al, sonra sonucu ata
y %= 3 # önce kalanı bul, sonra sonucu ata

Compound assignment operators are very useful when you need to increase or decrease a value or when you want to automate some operations in your program.
These operators make the code both shorter and readable.


Result

In this lesson, you learned many different arithmetic operators that you can use with integer (int) and decimal (float) data types.
Now you can use addition, subtraction, multiplication, division, exponentiation, mode etc. in Python.
You can easily perform basic mathematical operations.

Additionally, thanks to compound assignment operators, you can increase or decrease variable values ​​step by step or automate a specific mathematical relationship.
This makes your code shorter and more readable.

As you've seen throughout the course, Python is not just a programming language, but also a powerful mathematical computing platform.

Click here to discover my other Python tutorials