Arithmetic Operations

Arithmetic Operations

There are a number of operations in mathematics we can use to calculate values using numbers. These are arithmetic operations. The arithmetic operations available to use in Python are listed below:

OperatorDescriptionExampleResult Returned
+Addition7+714
-Subtraction70-169
*Multiplication7*749
/Floating-point division7 / 23.5
//Integer or Truncated Division7 // 23
%Modulus or Remainder of Integer Division7 % 31
**Exponentiation7 ** 249

These examples are performing the operations on literal values as in the objects themselves not variables. Operations can be performed on both literal values and variables. Her are more examples:

Addition and Subtraction

>>> a = 6
>>> a + 4
10

Addition and subtraction works as expected. Note the use of both literal values and variables. And we can include as many operands as possible. An operand is any object being operated on by an operation.

>>> 7+10-2+11-14+a
18

Multiplication

Multiplication is similar. For example:

>>> 10*7*15*5
5250

Division

Division is a different story since there are different styles. There is floating point division: /. And division with truncation: //. Truncation is just removing everything after the decimal point. Truncating is essentially just rounding down to the nearest integer in the case of positive numbers and rounding up in the case of negatives.

Floating Point Division

Floating point division is division that will return the answer of division including if it is a floating point (a decimal number). For example:

>>> 7.5 / 2
3.75

Division With Truncation

Truncation will remove everything after the decimal point. It is essentially rounding or removing any remainder. For example:

>>> 7.5 // 2
3.0

Modulus

Modulus or mod for short simply returns the remainder of division. For example:

>>> 7.5 % 2
1.5

Exponentiation

Exponentiation is raising a number by an exponent. The number being raised, or the base, goes on the left. And the exponent is on the right. For example:

>>> 3 ** 2
9

Assignment Operators

Assignment operators are operators that allow us to both conduct an arithmetic operation on a variable and assign the result to the variable with a single operation. In other words, the arithmetic operation and assignment operation are combined into one step.

The assignment operators found in Python are listed below

OperatorDescription
=Assigns value to variable
+=Adds value and assigns result
-=Subtracts by value and assigns result
*=Multiplies by value and assigns result
/=Divides by value and assigns result
**=Raises by power and assigns result
%=Computes modulus between value and assigns result
//Floor divides by value and assigns result

Not every operator will be given an example for the sake of brevity, but the concept is the same for all. Let's use multiplication for our example. Consider:

>>> myNumber = 7
>>> myNumber = myNumber * 7
>>> myNumber
49

Here myNumber is assigned to 7. Then it is assigned to itself times 7 which is 49. And when returned it is now assigned to 49. Where it is being assigned to itself times 7 can be combined into a single operation with an assignment operator.

>>> myNumber = 7
>>> myNumber *= 7
>>> myNumber
49

The result of this code block is the same, however the multiplication and reassignment of myNumber is combined into the *= operator.


This section described Python's arithmetic operations. To see more examples of these operations in use check the appendix. The next section will cover logical operations.

Further Reading

Arithmetic Operations