In this article I would like to show you how to perform simple mathematical operations with Python.
The procedure is quite simple and basically no different from the way you know it from your normal calculator.
In this example you will also learn how to use variables to store results and perform arithmetic operations more easily.
To get the most learning effect, I recommend that you type the examples by yourself and not just copy and paste them. This way you can process what you have learned better and retrieve it more easily in later situations.
2. Simple Mathematics with Python
As already mentioned it is very easy to perform mathematical operations with Python. We start with a simple addition of numbers.
First we call the Python interpreter:
$ python3 Python 3.6.8 (default, May 2 2019, 20:40:44) [GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux Type "help", "copyright", "credits" or "license" for more information. >>>
2.1 Addition, Subtraction, Multiplication, Division
Now we can do some calculations:
>>> 2+2 4 >>> 20 - 3 * 3 11 >>> (10 * 3) + 4 / 2 32.0 # divsion returns floating point number >>> ((7 * 3) / 3) * 4 28.0
That was easy, wasn’t it?
What we notice about the calculations is that some results are integers and others return comma numbers as results.
This is because programming languages store numbers in different data types.
2 + 2 = 4 returns an integer result. We also call it an
int. When calculating
(10 * 3) + 4 / 2 = 32.0 we get a
floating point number or simply
float as result. Whenever a formula contains a division, the result is a floating point number.
We can also calculate powers with Python, like by writing the following:
>>> 2 ** 3 # 2 to the power of 3 8 >>> 2 ** 7 # 2 to the power of 7 = 2 * 2 * 2 * 2 * 2 * 2 * 2 128
2.2 Calculating with Variables
We can also store the results of the calculations in variables by first writing the name of the variable followed by an
= sign and after that the value to be stored:
>>> height = 2 >>> width = 4
We can output the value we have assigned to the variable by simply writing the variable name:
>>> height 2 >>> width 4
And of course we can also calculate with these values by putting the variable names into the formula instead of the numbers:
>>> height * width # 2 * 4 8
Here are some more examples like the calculation of net and gross:
>>> tax = (19 / 100) >>> net = 10.9 >>> result = net * tax >>> result 2.071 >>> round(result,2) # round up to 2 decimal places 2.07
Of course, we can also overwrite variables by adding new values to them. I also shortened the comma number in the result to 2 digits after the comma by using the
round(result,2) function. The function expects at least 2 parameters. The first parameter is the number we want to shorten (in this case
2.071) and the second parameter is the number of digits to be represented after the comma (in this case
2). Here are further examples of the
>>> 1 / 3 0.3333333333333333 >>> onethird = 1 / 3 >>> onethird 0.3333333333333333 >>> round(onethird,2) 0.33 >>> round(onethird,1) 0.3
In the following example, we change the tax rate from 19% to 25%:
>>> tax = (25 / 100) >>> result = net * tax >>> round(result,2) 2.73
As we can see, the final price is now higher. Since we overwrote the variable
tax with a higher tax rate, the result
result = net * tax is higher than in the previous calculation.
2.3 Calculating with Negative Values
In Python, of course, we can also calculate with negative values. For this we simply write a minus before the variable name or the number:
>>> -4 -4 >>> negativenumber = -4 >>> negativenumber -4 >>> positivenumber = 5 >>> negativenumber * positivenumber -20 >>> positivenumber - negativenumber 9 # 5 - (-4) = 5 + 4 = 9
The results are as we expect them to be. If you multiply a negative number
-4 by a positive number
5 you get a negative number as a result, because minus times plus results in a negative number.
If you subtract a negative number
-4 from a positive number
5 then this corresponds to an addition because we have in the formula minus a negative number, which of course becomes a positive number, as in the example
5 - (-4) = 5 + 4 = 9.
2.4 Absolute Values
To get the absolute value of a number in Python, i.e. the unsigned value, you can use the function
abs() and write the desired number or variable in this function:
>>> abs(negativenumber) # abs(-4) 4 >>> abs(positivenumber) # abs(5) 5 >>> abs(negativenumber) * abs(positivenumber) # 4 * 5 20 >>> >>> abs(positivenumber) - abs(negativenumber) # 5 - 4 1
2.5 Modulo Operations
Another very important function in programming is the modulo function. The modulo function returns the remainder of a division.
>>> 5 % 3 # 5 / 3 = 1 Remainder 2 2 >>> 7 % 3 # 7 / 3 = 2 Remainder 1 1
3. Try it Yourself
Try it yourself online.
If it does not work with Chrome, then allow third party cookies or use a different browser
4. Typical Exam Questions
You have now learned how to solve the most basic mathematical problems in Python and you should be able to answer the following questions. These are also typical questions that are asked in exams.
- Why does the result of some calculations result in an
integerand other calculations in a
- What is an
- What is a
floating point numberor
- How does the modulo calculation work? What is the result of
9 % 3?
- How does the
- Why do I need the
abs()function? What is the result of
abs(-2) * abs(3) - 4 * 5?
In this tutorial I showed how to do simple mathematical calculations with Python. Here we have used different mathematical operations, which are listed in the following:
|a + b||Addition|
|a - b||Subtraction|
|a * b||Multiplication|
|a / b||Division|
|a % b||Modulo|
|a**b||Exponent, power calculation|