Skip to content

Programs that calculate

This lesson presents new Python programs to perform small calculations such as the product of two numbers given by the user. To do this, we will see how to read numeric data and how to perform arithmetic operations. Along the way, this lesson also introduces the concept of comments. As an exercise, you are asked to make a variation of the program that calculates the sum of three numbers.

Product calculation

Here is a new Python program:

python
# Product calculator.

# reading input data
x = int(input('Enter a number: '))
y = int(input('Enter another number: '))

# product calculation
p = x * y

# writing output data
print('The product is', p)

It is a kind of very simple calculator that only reads two numbers and writes their product. As before, let's first see what it does: Click the ▶ button!

When you click the ▶ button, the Python program starts running. A window will open and ask you for a first number. Then another window will open and ask you for the second number. Finally, on the right window you will see the product of the two given numbers.

So, if I enter 12 as the first number and 5 as the second number, the computer informs me that their product is 60.

Run the program again by clicking the ▶ button and try a new test. For example, try multiplying two large numbers, such as 4377678463 by 817287427844233.

Wonderful, right? 😎

Program explanation

In order to understand what this program does, we first need to consider these new elements:

  • Programs contain comments: pieces of text that document some part of the program and are ignored by the computer. Proper use of comments makes understanding and maintaining programs easier for humans. In Python, comments start with the symbol # and end at the end of the line.

  • Blank lines also have no importance in Python programs, but just as when we write we separate ideas into paragraphs, we also leave blank lines to separate different parts of programs. This makes programs less cluttered and therefore clearer. The computer doesn't care about this, but programs are not only executed by machines, they are also read by humans who deserve our consideration.

  • The different colors you see in programs are only to facilitate understanding when reading. Like comments, these colors do not affect the meaning of the program. But unlike comments, which are written by programmers, the colors are automatically generated by the computer (and are not always the same across different systems).

Thus, for the computer, the really important fragment of the previous program is this:

python
x = int(input('Enter a number: '))
y = int(input('Enter another number: '))
p = x * y
print('The product is', p)

The program consists of four instructions. As you know, each instruction in Python is written on one line and the computer executes the instructions sequentially: one after another from top to bottom. It is important to emphasize that one instruction does not start until the previous one has finished.

To understand what this program does, here is an informal description of each instruction, in the order they are carried out:

  1. The first instruction, x = int(input('Enter a number: ')) contains an assignment with an input, which you already know is used to ask for data and store it in a variable. This time, however, since the data we want is numeric, we wrap the input inside an int(). When the computer executes this instruction, it first writes the text "Enter a number:" and then reads a text that the user types. This text is converted to an integer value (with int()) and stored in the variable x. For the computer, texts and numbers are different things, so it is necessary to convert texts to numbers to manipulate them. Just remember that, for now, this is the way to read integer data.

  2. The second instruction, y = int(input('Enter another number: ')) is a second instruction analogous to the previous one and makes the computer ask for the second number and store it in the variable y.

  3. The third instruction, p = x * y calculates the product of the values stored in the variables x and y and stores the result in a new variable called p through an assignment instruction. In Python (and in computing in general), the multiplication operator is written with an asterisk: *.

  4. The fourth and last line, print('The product is', p) is a write instruction: it indicates that the text 'The product is' followed by the value stored in the variable p should be printed.

In this program we can see that x and y correspond to the input data of our problem, and that p is the output data. There was no need to describe any algorithm to calculate p from x and y, since Python calculates products (with *) directly.

The basic arithmetic operations that can be applied between two integers are:

operationoperator
addition+
subtraction-
multiplication*
integer division//
remainder of integer division%
exponentiation**

As usual in mathematics, expressions such as x * y or E - (m * c**2) combine values, variables, and operators and can use parentheses to group operations. We will see more details about expressions and all these operations later.

Exercise

Now it's your turn! Using the previous program as a template, write a program that asks for three integers and prints their sum. For example, if the program reads 12, 20, and 3 it should print 35.

I hope you succeed! This time you need to read three numbers and calculate their sum. Don't forget to adjust the comments.

Go to the next lesson when you finish, we will do drawings! If you find it difficult, don't worry, you can reread this lesson and try the program we have seen.

Jordi Petit
Lliçons.jutge.org
© Universitat Politècnica de Catalunya, 2026

python-for-ai.potipoti.org