Numbers are extremely common in programming. They are used to represent things like screen size dimensions, geographic locations, money and points, the amount of time that passes in a video, positions of game avatars, and colours through assigning numeric codes.
Being able to effectively perform mathematical operations in programming is an important skill to develop because of how frequently you’ll be working with numbers. Try to think of maths as a tool to accomplish what you would like to achieve, and as a way to improve your logical thinking.
We'll be working with two of Python's most used numeric data types, integers and floats:
Integers are whole numbers that can be positive, negative, or 0 (…, -1, 0, 1, …).
Floats are real numbers, they contain a decimal point (as in 9.0 or -2.25).
This tutorial will go over operators that can be used with number data types in Python.
An operator is a symbol or function that indicates an operation. For example, in maths the plus sign or + is the operator that indicates addition.
In Python, we will see some familiar operators that are brought over from maths, but other operators we will use are specific to computer programming.
Here is a quick reference table of maths-related operators in Python. We’ll be covering all of the following operations in this section.
Operation | What it returns |
x + y | Sum of x and y |
x - y | Difference of x and y |
x * y | Product of x and y |
x / y | Quotient of x and y |
x // y | Quotient from floor division of x and y |
x % y | Remainder of x / y |
x ** y | x to the y power |
We’ll also be covering compound assignment operators, including += and *=, that combine an arithmetic operator with the = operator.
In Python, addition and subtraction operators perform just as they do in mathematics. In fact, you can use the Python programming language as a calculator.
Let’s look at some examples, starting with integers:
print(1 + 5)
6
Instead of passing integers directly into the print statement, we can initialise variables to stand for integer values:
a = 88 b = 103 print(a + b)
191
Because integers can be both positive and negative numbers (and 0 too), we can add a negative number with a positive number:
c = -36 d = 25 print(c + d)
-11
Addition will behave similarly with floats:
e = 5.5 f = 2.5 print(e + f)
8.0
Because we added two floats together, Python returned a float value with a decimal place.
The syntax for subtraction is the same as for addition, except you’ll change your operator from the plus sign (+) to the minus sign (-):
g = 75.67 h = 32 print(g - h)
43.67
Here, we subtracted an integer from a float. Python will return a float if at least one of the numbers involved in an equation is a float.
Like addition and subtraction, multiplication and division will look very similar to how they do in mathematics. The sign we’ll use in Python for multiplication is * and the sign we’ll use for division is /.
Here’s an example of doing multiplication in Python with two float values:
k = 100.1 l = 10.1 print(k * l)
1011.0099999999999
When you divide in Python 3, your quotient will always be returned as a float, even if you use two integers:
m = 80 n = 5 print(m / n)
16.0
In Python 3, you can use // to perform floor division. The expression 100 // 40 will return the value of 2. Floor division is useful when you need a quotient to be in whole numbers.
Write a program that demonstrates + or -.
Write a program that demonstrates all of the operators: + - / *.
Write a program that demonstrates all of the operators: // % **.
Thanks [name] from [whichclass][location], Mr McG will get your message…"
[message]"…and will get back to you ASAP via [email]. Catch you soon.
Have a great day!!
K McGuinness - 2018
No personal data will be shared with any third party. The only data processed via this website is anonymous browsing data to best manage your user experience.