Numbers and math in Python
Coding with Python
🕑 This lesson will take about 20 minutes
In the first lesson, you learned how to make a message display on the screen using the print() function. This example message (Hello world) was a string. A string is a sequence of characters which can include letters, punctuation and special characters, and numbers (in other words, text).
In this lesson, you will learn how to work with simple numbers and math expressions in Python. In Python and other programming languages, whole numbers (eg. 9, 36, 852, -8) are known as integers, and numbers with a decimal point (eg. 3.5, 2.66, 391.597) are known as floats or (floating point numbers).
Strings, integers and floats are all examples of data types. There are other data types such as Boolean that we will also look at later in this course, However, in this lesson, we are just going to focus on working with numbers (the integer and float data types).
Basic addition, subtraction, division, and multiplication are really easy in Python. If you're using the Python shell, just type in the expression (for example 3+5 or 7-3) and press the Enter key. Otherwise, if you’re typing code into a Python file, you will need either type these expressions into the print() function, for example, print(3+5) to display the results as output on the screen, or store them in variables (we will look at variables in the next lesson). For division, you use the forward slash key (for example 12/4) and for multiplication, you use the star key (for example 3*3).
If you wish to display just numbers or the result of a mathematical expression using the print() function, then you don’t need to use quotation marks. For example, print(3+5) would display the output of 8, whereas print("3+5") will display the text 3+5.
Arithmetic operators in Python
These are some of the basic operators used for arithmetic operations in Python:
+ is used for addition (eg. 3+5 would return 8)
- is used for subtraction (eg. 8-3 would return 5)
* is used for multiplication (eg. 3*5 would return 15)
/ is used for division (eg. 15/4 would return 3.75)
// is used for floor division (discards the fractional part eg. 15//4 would return 3)
% is used for modulus (returns the remainder from a division eg. 17%5 would return 2)
** is used for exponentiation (eg. 2**3 means 2 raised to the power of 3 which would return 8)
We can use the print() function to display numbers or the result of different math expressions. Check out the examples below. When we write a line of code like print(3+5), Python will first evaluate 3+5 and then display the result which is 8.
Sample code
You will notice that when doing division it will give the answer with a decimal place even if the answer is a whole number. Later on you will learn how to specify the number of decimal places for the result and how to round numbers.
Modulus
In Python, you can use the modulus feature to find the remainder when dividing a number by another number (that is, how much is left over). For example, 9 divided by 4 equals 2 with a remainder of 1. The modulus operator is the % symbol. For example, 9%4 would return 1 as a result. 8%4 would return 0 because there is no remainder when you divide 8 by 4.
Modulus can be very useful eg. if you want to check if a number is odd or even, or if you want to check if a year is a leap year. Check out the examples below.
Modulus sample code
Exponentiation
Exponentiation is easy in Python. Two stars (**) are used for the exponent operator. If you wanted to calculate 4 raised to the power of 3 (4³) you can type 4 followed by ** and 3. This works with both positive and negative numbers. Check out the examples below.
Exponentiation sample code
Floor division
When dividing numbers, it is quite likely that you will not get a whole number as a result. There are functions you can use in Python to round numbers. But what if you want the result from a division without the numbers after the decimal place? This is called floor division. Instead of using / to divide, you can use two // forward slashes. For example: 16//9 would give a result of 1 whereas 16/9 would give a result of 1.777777777…
Floor division sample code
Note that you can also shorten arithmetic in Python. For example, instead of writing x = x + 1, you can write x += 1. Likewise, instead of writing x = x - 3, you could write x -= 3, and so for multiplication and division.
Next lesson: Variables and data types