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

Our purpose is to transform access to education.

We offer a diverse selection of courses from leading universities and cultural institutions from around the world. These are delivered one step at a time, and are accessible on mobile, tablet and desktop, so you can fit learning around your life.

We believe learning should be an enjoyable, social experience, so our courses offer the opportunity to discuss what you’re learning with others as you go, helping you make fresh discoveries and form new ideas.
You can unlock new opportunities with unlimited access to hundreds of online short courses for a year by subscribing to our Unlimited package. Build your knowledge with top universities and organisations.

Learn more about how FutureLearn is transforming access to education