Sitemap
Gideon Oludeyi

Gideon Oludeyi

Passionate about learning and teaching

Python Basics: Arithmetic Operations

Basic math operations

4 min readJun 14, 2022

--

Press enter or click to view image in full size
Photo by Michal Matlon on Unsplash

In the last article we learned that data types classify the types of data represented on a computer, and define the operations that can be performed on them. We looked at the four primitive data types: str, int, float, and bool.

Get Gideon Oludeyi’s stories in your inbox

Join Medium for free to get updates from this writer.

We also used a few int and float operations in our Invoice example. These numeric data types (int and float) possess similar operations called arithmetic operations.

Operations

Addition

Perhaps the most basic arithmetic operation is addition: the sum of two numbers. Like other built-in operators, addition uses infix notation, where the operands are to the left and right of the operator.

2   + 2   = 4     # int   + int   = int
1 + 0.5 = 0.5 # int + float = float
2.5 + 3.5 = 6.0 # float + float = float

Subtraction

The difference of two numbers.

3   - 2   = 1     # int   - int   = int
5 - 2.5 = 2.5 # int - float = float
5.5 - 1.5 = 4.0 # float - float = float

Multiplication

The product of two numbers.

2   * 3   = 6     # int   * int   = int
4 * 2.5 = 10.0 # int * float = float
2.5 * 1.5 = 3.75 # float * float = float

Division

The quotient of two numbers.

4   / 2   = 2.0  # int   / int   = float
6 / 2.5 = 2.4 # int / float = float
7.5 / 3 = 2.5 # float / int = float
6.5 / 1.3 = 5.0 # float / float = float

Exponentiation

The left operand is raised to the power of the right operand.

3   ** 2   = 9      # int   ** int   = int
2 ** 1.4 = 2.63 # int ** float = float
3.5 ** 2 = 12.25 # float ** int = float
2.5 ** 1.5 = 3.95 # float ** float = float

Modulo

The remainder from the division of two numbers. The modulo operation produces the remainder, not the quotient.

15   % 4   = 3     # int   % int   = int
6 % 2.5 = 1.0 # int % float = float
15.5 % 4 = 3.5 # float % int = float
8.25 % 3.5 = 1.25 # float % float = float

Floor Division

This operation yields the integer portion of the quotient between two numbers. The decimal is ignored.

5   // 3   = 1    # int   // int   = int
5 // 3.0 = 1.0 # int // float = float
5.0 // 3 = 1.0 # float // int = float
5.0 // 3.0 = 1.0 # float // float = float

Order of Operations

When working with numbers, it is often necessary to compose several operations together. For example, the Fahrenheit to Celsius conversion formula,(x-32) * 5/9, makes use of subtraction, multiplication, and division. In Python, all operators have an order that define which expressions are evaluated first. This extends the BEDMAS or PEMDAS rule (Parentheses, Exponents, Multiplication, Division, Addition, and Subtraction).

Operator         Name        Precedence  
----------- ---------------- ------------
() Parenthesis 6
** Exponentiation 5
// Floor Division 4
* Multiplication 3
/ Division 3
% Modulus 2
+ Addition 1
- Subtraction 1

Using the precedence rule, the Fahrenheit to Celsius conversion formula is evaluated as follows

Press enter or click to view image in full size
Evaluating an expression with BEDMAS order of operations

Note that the order in which multiplication and division is evaluated does not matter.

Assignment Operators

A common pattern with these arithmetic operators is the reassigning of variables based on previous values. In other words, you might encounter a situation where you need to write x = x + 1, which increments the value of variable x.

Python, along with some other programming languages, provide a shortcut for performing these operations. In the case of x = x+1, we can rewrite it more compactly as x += 1. This shortcut is supported for all the arithmetic operators.

Press enter or click to view image in full size
Assignment Operator examples

Note that the variable you’re are reassigning must be the leftmost value in the expression. This means that x = 2 - x cannot be converted to an assignment operation.

Conclusion

We took a look at arithmetic operators that are common to all numeric Python data types, and the order in which they are evaluated in an expression. We also looked at how to simplify variable reassignment using assignment operations. Next time we’ll be taking closer look at the bool data type and Relational Operations.

--

--