Hurry, only 4 days left to get one year of Unlimited learning for £249.99 £174.99. New subscribers only. T&Cs apply

# 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
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 = 5AmountOfApples = AmountOfApples - 1

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.

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

© Coventry University. CC BY-NC 4.0