Skip main navigation

Variable operations

How arithmetic operators work in python.
© Coventry University. CC BY-NC 4.0

What use is storing data in variables if we don’t do anything with it?

In Python, and many other programming languages, we can use operators in order to do calculations on and manipulate different variables.

Arithmetic operators

These operators are most commonly used to operate on numerical variables. Most of them you may have seen before, the last three might be new to you.

Operator Name
+ Addition
Subtraction
* Multiplication
/ Division
% Modulus
** Exponentiation
// Floor Division

But how do we use these on our variables? Consider this example:

AmountOfApples = 5

We have five apples. Let’s say we ate one of the apples. How would we then subtract one from the amount? We would need to use the subtraction operator.

First we need to declare what variable we are going to change (AmountOfApples = ). Next we can write our operation, which would be to subtract 1 from AmountOfApples which is as simple as AmountOfApples - 1. Then we can put all this together to form:

AmountOfApples = 5
AmountOfApples = AmountOfApples - 1

Your task

Without searching online, what you think the last three operators do: % (Modulus), ** (Exponentiation) and // (Floor Division)?
Select one and provide an example of when you might use it.

Further reading

Tutorials Point. (n.d.). Python Arithmetic Operators Example. Web link

© Coventry University. CC BY-NC 4.0
This article is from the free online

Get ready for a Masters in Data Science and AI

Created by
FutureLearn - Learning For Life

Reach your personal and professional goals

Unlock access to hundreds of expert online courses and degrees from top universities and educators to gain accredited qualifications and professional CV-building certificates.

Join over 18 million learners to launch, switch or build upon your career, all at your own pace, across a wide range of topic areas.

Start Learning now