Register to get access to free programming courses with interactive exercises

Arithmetic operations Python Basics

Counting sticks

At the most basic level, computers use only arithmetic operations and numbers. The basics of arithmetics are essential for now, and that is where we start.

For example, we can add two numbers in math, writing 3 + 4.

The same goes for programming, so let us observe a program that adds up two numbers:

3 + 4

Arithmetic in programming is virtually the same as school arithmetic.

The line of code 3 + 4 will make the interpreter add the numbers and calculate the result. The program will work, but it makes no sense. We are not giving the interpreter a command. We are just asking what three plus four is. In real life, you need to do more than tell the interpreter about the mathematical expression by itself.

For example, if you're creating an online store, you can't just ask the interpreter to calculate the cost of items in the shopping cart. You have to ask to calculate the cost AND show the price to the buyer.

We need to ask the interpreter to add 3 + 4 AND give the command to do something with the result. For example, print it:

# First, the amount is calculated
# Then it is passed to the print function
print(3 + 4)  # => 7

In addition, the following operations are available:

  • - — subtraction
  • * — multiplication
  • ** — exponentiation
  • / — division
  • // — integer division (to read more about it)
  • % — remainder of division (to read more about it)

Now let's print the result of division and then the result of exponentiation:

print(8 / 2)   # => 4.0 (Dividing two numbers produces the float data type)
print(3 ** 2)  # => 9
print(9 % 2)   # => 1

Let's break down the components of mathematical operations called in programming.

Operators and operands

The + and other operation signs are operators. They perform operations on values — operands. The operators themselves are usually one or more characters. On occasion, it can also be a word. Most operators are identical to those you'll have seen in math class:

print(8 + 2)

In this example, + is an operator, and the numbers 8 and 2 are operands. We have two operands for addition: one to the left and one to the right of the + sign.

Operations that require two operands are called binary. The program will end with a syntax error if at least one operand is missing, for example, 3 +.

Operations can be more than just binary. They can also be unary with one operand or ternary with three operands. Moreover, operators may look the same but denote different operations. The symbols + and - are not only used as operators. When it comes to negative numbers, the minus sign becomes part of the number:

print(-3)  # => -3

Above is an example of applying a unary operation to the number 3. The minus operator before the three tells the interpreter to take the number 3 and find the opposite -3. It can be confusing because -3 is both a number and an operator with an operand. But this is the structure programming languages have.

Commutative operations

"The sum does not change by changing the places of the summands" is one of the basics of arithmetic, also called the commutative law. A binary operation is commutative if you get the same result even if you swap the operands.

In this case, addition is a commutative operation:

2 + 3 = 3 + 2

But subtraction is not a commutative operation:

2 - 3 ≠ 3 - 2

In programming, this law applies just like it does in arithmetic. Most operations we encounter in real life are not commutative. Always pay attention to the order of things you work with.

Composition of operations

Let's look at an example:

print(2 * 4 * 5 * 10)

We can combine the operations to calculate increasingly complex compound expressions. Let us give you an idea of how to do calculations inside the interpreter and look at an example: 2 * 4 * 5 * 10.

Let us observe it step by step:

  1. First, calculate 2 * 4 and get the expression 8 * 5 * 10
  2. Then 8 * 5. The result is 40 * 10
  3. As a result, the last multiplication takes place, and the result is 400

Operations may contain different operators, which raises the question of their priority.

Priority of operations

Suppose we need to calculate this expression: 2 + 2 * 2. That's what we will write down:

print(2 + 2 * 2)  # => 6

You will know about what the order of operations is from school. It determines what order we should perform the operations. Multiplication and division have a higher priority than addition and subtraction, and exponentiation has a higher priority than all other arithmetic operations. For example: 2 ** 3 * 2 will calculate to 16.

But sometimes, we have to perform calculations in a non-standard order. In that case, we need to set the priority using parentheses. It was also the case at school, for example:

(2 + 2) * 2

Parentheses go with any operation. They can be nested into each other as many times as you need. Here are some examples:

print(3 ** (4 - 2))  # => 9
print(7 * 3 + (4 / 2) - (8 + (2 - 1)))  # => 14

The main thing is to pay attention to pairing, i.e., close the brackets in the correct order. It often causes errors not just for beginners but also for experienced programmers. For convenience's sake, do the opening and closing parentheses first, then write the inside part.

The editor on our site and most other code editors do this automatically:

  • You write (
  • The editor immediately adds )

It also applies to other paired characters, such as quotation marks. We will talk about them in future lessons.

Sometimes, an expression may be visually quite a lot to take in. So you can place brackets without affecting the priority:

# Before
print(8 / 2 + 5 - -3 / 2)  # => 10.5

# After
print(((8 / 2) + 5) - (-3 / 2))  # => 10.5

It is important to remember we write code for people. People will read the code, and machines will only execute it. From the computer's point of view, the code is either correct or incorrect. There is no more or less understandable code for them.

Sign up

Programming courses for beginners and experienced developers. Start training for free

  • 130 courses, 2000+ hours of theory
  • 1000 practical tasks in a browser
  • 360 000 students
By sending this form, you agree to our Personal Policy and Service Conditions

Our graduates work in companies:

Bookmate
Health Samurai
Dualboot
ABBYY