Arithmetic operators are special symbols used to perform mathematical operations such as addition, subtraction, multiplication, and division. The symbols that perform these operations are called operators, while the values or objects they act upon are called operands.
Let's look at the example above. In the expression , the numbers 5 and 3 are the operands, while + is the operator. In this case, + is the arithmetic operator that performs addition.
All the Arithmetic Operators
Python provides seven primary arithmetic operators to handle everything from basic sums to complex powers:
- Addition
(+): Adds two operands together. - Subtraction
(-): Subtracts the right operand from the left operand. - Multiplication
(*): Multiplies two operands. - Division
(/): Divides the left operand by the right operand (always returns a decimal/float). - Floor Division
(//): Divides the left operand by the right operand and "chops off" the decimal, returning only the whole number. - Modulus
(%): Divides the left operand by the right operand and returns only the remainder. - Exponentiation
(**): Raises the left operand to the power of the right operand (e.g., 2 ** 3 is 2 cubed).
Operands Definition:
Operands are simply the values or variables that an operator acts upon. In the expression 5 + 10, the numbers 5 and 10 are the operands, while the + is the operator.
The Addition Operator (+)
To perform addition in Python, simply place the + operator between two numbers (the operands). For example, will calculate the sum, returning a new integer of 15
Mathematical Representation:
Addend & Sum
Addend: A number that is added to another number.
Sum: The result of an addition operation.
How it works in code:
The Subtraction Operator (-)
The subtraction operator calculates the difference between two numbers. It subtracts the right operand (the subtrahend) from the left operand (the minuend).
Mathematical Representation:
Subtrahend & Minuend
Minuend: The number from which another number is subtracted.
Subtrahend: The number that is subtracted from the minuend.
How it works in code:
The Multiplication Operator (*)
In Python, multiplication is represented by the asterisk (*). It calculates the product of two numbers, effectively performing repeated addition.
Mathematical Representation:
Multiplicand, Multiplier & Product
Multiplicand: The number that is being multiplied.
Multiplier: The number by which the multiplicand is multiplied.
Product: The result of a multiplication operation.
How it works in code:
The Division Operator (/)
The standard division operator calculates the quotient of two numbers. In Python, this operator always returns a decimal (a float), even if the result is a perfect whole number.
Mathematical Representation:
Dividend, Divisor & Quotient
Dividend: The number that is being divided.
Divisor: The number by which the dividend is divided.
Quotient: The result obtained after performing the division.
How it works in code:
The Floor Division Operator (//)
Floor division divides two numbers but "chops off" the decimal part, rounding the result down to the nearest whole number. This is the go-to operator when you want to ensure your result remains an integer.
Mathematical Representation:
How it works in code:
The Modulus Operator (%)
The modulus operator is the partner to floor division. Instead of giving you the quotient, it returns the remainder left over after the division is complete.
Mathematical Representation:
Remainder
Remainder: The value left over after one number is divided by another and the division is not exact.
How it works in code:
The Exponentiation Operator (**)
The exponentiation operator raises the left operand (the base) to the power of the right operand (the exponent). It is a clean and efficient way to calculate squares, cubes, or any higher power without needing to import a math library.
Mathematical Representation:
Base, Exponent
Base: The number that is raised to a power.
Exponent (or Power): The number that specifies how many times the base is multiplied by itself.
How it works in code:
Operator Precedence
Operator precedence is simply the priority given to each operator, which determines the order in which they are executed. This rule is very important in order to output a consistent result for the same mathematical expression. Without these rules, a single calculation could be interpreted in multiple ways, leading to bugs and errors.
The Order of Operations
When an expression contains multiple arithmetic operators, Python does not evaluate them randomly. Instead, it follows a predefined order of operations to determine which calculations are performed first.
This order is commonly remembered using the acronyms PEMDAS (Parentheses, Exponents, Multiplication, Division, Addition, Subtraction) or BODMAS (Brackets, Orders, Division, Multiplication, Addition, Subtraction).
Python evaluates arithmetic expressions according to the following hierarchy:
- Parentheses
()– Expressions enclosed in parentheses are evaluated first. - Exponentiation
**– Powers and exponents are evaluated next. - Multiplication, Division, Floor Division, and Modulus
*,/,//,%– These operators have the same precedence and are evaluated from left to right. - Addition and Subtraction
+,-– These operators are evaluated last, also from left to right.
Summary
- Arithmetic Operators are symbols (like
+,-,*) used to perform mathematical operations between values called operands. - The Seven Operators: Python has seven primary arithmetic operators: Addition (
+), Subtraction (-), Multiplication (*), Division (/), Floor Division (//), Modulus (%), and Exponentiation (**). - Division Nuance: Standard division (
/) always returns a decimal (float), while Floor Division (//) returns a whole number by "chopping off" the remainder. - Operator Precedence: Python follows the PEMDAS / BODMAS rule to determine which part of a calculation to execute first.
