Skip to main content

Arithmetic operators

Note☆ Learning Objectives
  • Apply the addition (+), subtraction (-), multiplication (*), and division (/) operators in Python expressions.
  • Apply the exponentiation (**) operator to calculate powers.
  • Apply the modulo (%) and floor division (//) operators to solve programming problems.
  • Evaluate arithmetic expressions on variables of the different basic data types: int, float, string, bool
  • Evaluate Python expressions by applying the correct priority of operations.

Operators: Similarly to mathematics, the +, -, *, / are operators used to perform arithmetic operations on numbers. In Python, these operators can also be used on other data types but with caution. Complications may arise if you combine literals of different types. For example, 2 + "4" will lead to an error and 5 + True won’t crash but isn’t necessarily correct.

Combining data types

To use arithmetic operators correctly, you should first be aware of the different data types and their behaviour when used with arithemtic operators.

Strings

  • Strings can be combined (or concatenated) to other strings using the + operator
  • Strings should never be combined to other types or else the program crashes.

There is only one exception. Python allows a string to be “multiplied” n times (int) which creates a longer string repeating the original string. Eg:

base = "Hi"
print(base*4)
HiHiHiHi

Numbers

Arithmetic operations are deisigned for integers and floats. These types can also be safely used together. But, since integers have no fractional part, they will be automatically converted to floats by Python when combined to floats:

print( 3.5 + 5)
8.5
  • The result is always a float

You can also explicitly convert float to int if this is intended outcome.

print(int(3.5) + 5)
8
ImportantLoss of precision

This will truncate the decimal part of the float number, the resulting int is 3. Therefore, you must be careful when performing those conversion because this might result in loosing precision or loosing information. We say that floats are wider range than intor int are narrower than floats.

Booleans

Although extremely rare, arithmetic operations with the bool type are possible in Python.

If you perform an addition, such as: True + True, Python will convert the bool into integers 1 (or 0 for False), and still performs the operation (1+1 = 2). If it’s a more complex operation, for example False / True, the values will be converted to float, therefore the result will be 0.0 / 1.0 = 0.0. Conceptually, this makes no sense, therefore we’ll tend to avoid those situations.

In summary, when performing arithmetic operation, ensure all the variables are in the correct type, don’t mix strings with numeric variables, and avoid using booleans.

Table of operators

Here is an extensive table with all opertors applied to string, int and float:

Operator Name string with string int with int float with float int with float
+ Addition "H" + "i" → "Hi" 3 + 2 → 5 3.2 + 2.5 → 5.7 3 + 2.0 → 5.0
- Subtraction Error ❌ 3 - 2 → 1 3.2 - 2.5 → 0.7 3 - 2.0 → 1.0
* Multiplication Error ❌ 3 * 2 → 6 3.5 * 2.0 → 7.0 3 * 2.0 → 6.0
/ Division Error ❌ 3 / 2 → 1.5 3.0 / 2.0 → 1.5 3 / 2.0 → 1.5
// Floor Division Error ❌ 3 // 2 → 1 4.0 // 3.0 → 1.0 4 // 2.0 → 2.0
% Modulo Error ❌ 10 % 7 → 3 7.0 % 5.0 → 2.0 7 % 5.0 → 2.0
** Power Error ❌ 4 ** 2 → 16 4.0 ** 2.0 → 16.0 4 ** 2.0 → 16.0

Modulus %

The modulus operation returns the remainder of a long division (e.g. 7 % 5 = 2). It’s most commonly used on integers and rarely used on floats. If is an easy way to verify if a number is divisible by another number (remainder is 0).

NoteLONG DIVISION
  • To grasp this operator, it’s essential to recall what a long division remainder is. Consider the following division: \[ 7/5 = 1.4 = 1 + 2/5 \]

    • Quotient: 1
    • Remainder: 2
  • If performing this operation with two int, the result will be an int
  • If performing this operation with two float, the result will be a float (e.g.: 7.0 % 5.0 = 2.0)

  • If performing this operation with an int and a float, the int is automatically converted into the wider type float and the result is a float.

  • This operator doesn’t apply to string literals and will cause an error if used on strings.

Checking if a number is even/odd

This operator is useful to determine if a number is odd or even. If it even, the remainder of a division by 2 would be 0. Otherwise, if the number is odd, the remainder would be 1:

print("Odd numbers")

print(1 % 2)
print(3 % 2)
print(5 % 2)
print(51 % 2)

print("Even numbers")

print(0 % 2)
print(2 % 2)
print(4 % 2)
print(50 % 2)
Odd numbers
1
1
1
1
Even numbers
0
0
0
0

Floor division //

Divides the first operand by the second and rounds the result down to the nearest whole number. It’ll compute the division, but then discard the fractional part and the result is always an integer.

  • It’s useful when dealing with “countable” elements such as count of people, count of objects, number of pixel. These enties can be divided further, the result of the operation must be an integer.

  • You can also replace the use of the // by performing a normal division with /, then converting the result into an int: x // y is the same as int(x/y)

a = 4
b = 3

result = a//b

Is the

  • If two int are used in this operation, the result is and int
  • If any of the number is a float, the result will be a float and any integer will automatically be converted to the wider type float.
  • This operator doesn’t apply to string literals and will cause an error if used on strings.

🎯 Test Myself: Manually execute the operations of the program below and write the expected result. Check your answers by running the code.