Curriculum
Arithmetic operators in Python are used to perform mathematical operations on numbers. It is common for programming languages to provide data types for storing numbers since this is needed to compute math. The same thing applies to the Python language. Python supports the following arithmetic operators:
Operator | Description | Example |
---|---|---|
+ |
Addition | 5 + 3 = 8 |
- |
Subtraction | 10 - 4 = 6 |
* |
Multiplication | 6 * 2 = 12 |
/ |
Division (floating-point) | 7 / 2 = 3.5 |
% |
Modulus (remainder) | 7 % 2 = 1 |
// |
Floor division | 7 // 2 = 3 |
** |
Exponentiation (power) | 2 ** 3 = 8 |
These operators allow mathematical operations in Python and are commonly used in various applications, including data processing, calculations, and programming logic.
Take some time to study the content in this table and feel free to return to it to refresh your memory. The easiest way to keep these in memory is to look for ways to practice them. Ultimately, this would happen if you are consistent with practicing Python every day or every other day.
W discuss each one in brief below.
+
)The +
operator is used to add two numbers.
a = 10
b = 5
sum_result = a + b
print("Sum:", sum_result)
Output:
Sum: 15
-
)The -
operator subtracts one number from another.
x = 20
y = 8
difference = x - y
print("Difference:", difference)
Output:
Difference: 12
*
)The *
operator multiplies two numbers.
p = 7
q = 3
product = p * q
print("Product:", product)
Output:
Product: 21
/
)The /
operator performs floating-point division, meaning the result will always be a decimal (even if the numbers divide evenly).
m = 10
n = 4
division = m / n
print("Division:", division)
Output:
Division: 2.5
%
)The %
operator returns the remainder when one number is divided by another. This is useful for checking divisibility and working with even/odd numbers.
num1 = 17
num2 = 5
remainder = num1 % num2
print("Remainder:", remainder)
Output:
Remainder: 2
//
)The //
operator performs integer (floor) division, which discards the decimal part and returns only the whole number.
num1 = 17
num2 = 5
floor_division = num1 // num2
print("Floor Division:", floor_division)
Output:
Floor Division: 3
**
)The **
operator raises a number to the power of another number.
base = 2
exponent = 3
power_result = base ** exponent
print("Exponentiation:", power_result)
Output:
Exponentiation: 8
Python follows operator precedence, meaning some operations are performed before others in an expression. The order of precedence is:
**
(Exponentiation)*
, /
, //
, %
(Multiplication, Division, Floor Division, Modulus)+
, -
(Addition, Subtraction)result = 2 + 3 * 4 ** 2 // 5 - 1
print("Result:", result)
Step-by-step evaluation:
4 ** 2 = 16
3 * 16 = 48
48 // 5 = 9
2 + 9 = 11
11 - 1 = 10
Final Output:
Result: 10
Here is a summary of what we have covered in this section
+
, -
, *
, /
, %
, //
, and **
.+
adds, -
subtracts, *
multiplies, and /
performs floating-point division.%
returns the remainder, //
performs floor division, and **
calculates exponents.In the next lesson, 3.2 Comparison Operators (==, !=, <, >, <=, >=), we will explore how to compare values using relational operators and apply them in conditional statements.
Not a member yet? Register now
Are you a member? Login now