top of page

Python - Arithmetic Operators


Arithmetic Operators


Introduction

In this article, we will learn to use arithmetic operations (e.g., addition, subtraction, multiplication, division) and operations on strings (e.g., concatenation).


Arithmetic operators

Python allows performing arithmetic operations in a simple way. In the examples below, we place the operations within the print statement to display their results on the screen. In further articles, we will learn how to store the results of operations in the computer's memory.


Most of the arithmetic operators should already be familiar to you, for example, from using a calculator. However, some of them may be new.


Addition (+)

Let's start with addition. Suppose we want to calculate 5 + 7 and see the result on the screen. We can do it as follows:


print(5 + 7)

In that case, the program will output:


12

The operation was performed first, and only after it, was the result printed out. Notice that mathematical expressions are not enclosed in quotation marks, unlike text. For example:


print("5 + 7")

will not calculate the result, it simply displays the text:


5 + 7

Subtraction (-)

The result of the operation 5 - 7 is -2. For example:


print(5 - 7)

will display:


-2

Multiplication (*)

The result of operation 5 * 7 is 35. Thus, the statement:


print(5 * 7)

will display:


35

Exponentiation (**)

The result of the operation 2 ** 4 (which is two to the power of four) is 2 * 2 * 2 * 2 = 16. For example:


print(2 ** 4)

will display:


16

and


print(16 ** 0.5)

will display:


4

This is the first time a non-integer number has appeared in our examples. A period (dot) is used to separate the fractional part. Notice that when we raise a number to the power of 0.5, we actually calculate the square root of that number.


Division (/)

The result of 5 / 2 is 2.5. For example:


print(5 / 2)

will display:


2.5

Integer division (//)

We can also perform division with remainder. In that case, the result is an integer (the result of the division rounded down) - such an operation is called integer division. For example, 13 // 3 equals 4, and the remainder is 1 because 13 = 3 * 4 + 1.


print(13 // 3)

will display:


4

The result of the operation -10 // 3 is -4 (the result is rounded down). For example:


print(-10 // 3)

will display:

-
4

Remainder of division, modulo (%)

The operator used to calculate the remainder of integer division is %. For example:


print(13 % 3)

will display:


1

Order of operations

For equations with more than one operator, the order of execution follows the rules of mathematics. Exponentiation has the highest priority, followed by multiplication, division, and modulo. Addition and subtraction have the lowest priority. Operations with the same priority are executed from left to right. For example, as the result of the following instruction:


print(6 % 4 + 2**3 * 4 // 2 - 5)

the computer will display:

13

First, exponentiation 2**3 was calculated, which resulted in: 6 % 4 + 8 * 4 // 2 - 5. Next, modulo, multiplication, and division operations were performed: 2 + 16 - 5, and finally, addition and subtraction. The result is 13.


By using brackets, you can enforce a different order of execution of operations. The operation that is inside the brackets always has the highest priority (is executed first). If we have multiple brackets, the one with the largest nesting (i.e. the one with no other brackets inside) will be executed first.


For example, as a result of execution of instructions:


print(2 + 2 * 2)
print((2 + 2) * 2)
print((1 + 1) * ((7 + 3) // (1 + 2)))

on the screen we will see:


6
8
6

In the third example, the answer is 6 because after performing the calculations within the innermost brackets, we get 2 * (10 // 3) = 2 * 3 = 6.


Operations on words


Joining words, concatenation (+)

In programming, the operation of combining or joining words is called concatenation. In Python, to perform word concatenation, we use the + operator.


print("Code" + "foria")

will display:


Codeforia

Word multiplication (*)

We can use the multiplication * operator to repeat a text multiple times. One argument of this operator is the text, the other is a number that indicates how many times we want to repeat the text. The arguments can be given in any order. For example:


print(10 * "+")

or


print("+" * 10)

will display:


++++++++++
 

Train with us!

example lesson on codeforia

Programming is a crucial aspect of learning to code. You can practice it by solving tasks (micro-projects) available on our educational platform Codeforia. Solutions are automatically checked, and shortly after submitting your code, you'll find out whether it's correct or needs modification. We also invite you to join our regular online classes, where you can further develop your skills under the guidance of an experienced trainer.

Check out our available courses.

Kursy programowania dla dzieci i mÅ‚odzieży.

bottom of page