Summary: in this tutorial, you’ll learn how to use Python arithmetic operators to perform mathematical operations.
In Python, you use arithmetic operators to perform mathematical operations on numbers. The following table lists all arithmetic operators in Python:
| Operator | Description | a | b | Example | Result |
|---|---|---|---|---|---|
+ | Addition | 5 | 2 | a + b | 7 |
- | Subtraction | 5 | 2 | a - b | 3 |
* | Multiplication | 5 | 2 | a * b | 10 |
/ | Division (Floating) | 5 | 2 | a / b | 2.5 |
// | Floor Division | 5 | 2 | a // b | 2 |
% | Modulus (Remainder) | 5 | 2 | a % b | 1 |
** | Exponentiation | 5 | 2 | a ** b | 25 |
Addition (+) #
The addition operator (+) allows you to add two numbers. You can use it to calculate the total of numbers. For example:
a = 10
b = 3
result = a + b
print(result )Code language: Python (python)Output:
13Code language: Python (python)Subtraction (-) #
The subtraction operator (-) allows you to subtract the second number from the first one. You can use it to calculate the difference between two values. For example:
a = 10
b = 3
result = a - b
print(result)Code language: Python (python)Output:
7Code language: Python (python)Multiplication (*) #
The multiplication operator (*) allows you to multiply two numbers. You can use it to calculate areas, scaling numbers, and perform financial calculations:
a = 10
b = 3
result = a * b
print(result)Code language: Python (python)Output:
30Code language: Python (python)Division (/) #
The division operator (/) allows you to divide the first number by the second one and return a float. You can use it to calculate the averages and perform financial calculations. For example:
a = 10
b = 3
result = a / b
print(result)Code language: Python (python)Output:
3.3333333333333335Code language: Python (python)Floor Division (//) #
The floor division operator ( // ) allows you to perform integer division and return a number without the decimal part. For example:
a = 10
b = 3
result = a // b
print(result)Code language: Python (python)Output:
3Code language: Python (python)Modulus (%) #
The modulus operator (% ) allows you to return the remainder of the division of two numbers:
a = 10
b = 3
result = a % b
print(result)Code language: Python (python)Output:
1Code language: Python (python)You can use the modulus operator to check if a number is odd / even:
a = 101
if a % 2 == 1:
print(f'{a} is odd.');
else:
print(f'{a} is even.');Code language: Python (python)Output:
101 is odd.Code language: Python (python)You can also use the % operator to check whether a year is a leap year or not:
year = 2024
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
print(f"{year} is a leap year.")
else:
print(f"{year} is not a leap year.")Code language: Python (python)Output:
2024 is a leap year.Code language: Python (python)Exponentiation (**) #
The exponentiation operator ( ** ) raises the first number to the power of the second number:
a = 10
b = 3
result = a ** b
print(result)Code language: Python (python)Output:
1000Code language: Python (python)You can use the exponentiation operator (**) in scientific and financial calculations. For example, you can use the exponentiation operator to calculate the compound interest:
principal = 1000
interest_rate = 0.05
year = 2
amount = principal * (1 + interest_rate ) ** year
print(f"Total Amount: ${amount:,.2f}")Code language: Python (python)Output:
Total Amount: $1,102.50Code language: Python (python)In this example, if you have $1,000 with 5% interest rate, after 2 years, you’ll receive $1,102.50.
Summary #
- Use Python arithmetic operators to perform mathematical operations.