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 )Output:
13Subtraction (-) #
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)Output:
7Multiplication (*) #
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)Output:
30Division (/) #
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)Output:
3.3333333333333335Floor 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)Output:
3Modulus (%) #
The modulus operator (% ) allows you to return the remainder of the division of two numbers:
a = 10
b = 3
result = a % b
print(result)Output:
1You 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.');Output:
101 is odd.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.")Output:
2024 is a leap year.Exponentiation (**) #
The exponentiation operator ( ** ) raises the first number to the power of the second number:
a = 10
b = 3
result = a ** b
print(result)Output:
1000You 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}")Output:
Total Amount: $1,102.50In 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.
Quiz #
Python Arithmetic Operators
Thank you for your feedback!