How to do basic math in Python

In this article we will show you how to do some basic math in python. We will work with numbers to show you how to do basic operations like adding and subtracting numbers. But also a little bit more complicated functions like dividing. Numbers are being used a lot in programming languages so it is a must that you can work with them. There are numbers everywhere, think for example about your account balance, the weather, grades or the number of words in this article. After reading this tutorial you will be able to use Python as a calculator and do simple calculations.

Related article
How to use Python functions with examples

How to add two numbers in Python?

To calculate the sum of two numbers in Python we can use the addition operator (+).

31 + 47

Which will result in 78.

How to subtract numbers in Python?

We can use the above example and change it a little so it will work for subtracting numbers as well. For subtracting numbers we use the minus sign ().

31 - 47

The above expression will return -16.

How to multiply numbers in Python?

For the multiplication of numbers we use the asterisk sign (*).

59 * 12

When you use the above code in your Python program the result of the expression will be 708.

In most programming languages the operators are the same, so if you can use them in one language, you can use them in almost all programming languages. You can group numbers or calculations by using parentheses.

advertisement
advertisement