Python Arithmetic Operators

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:

OperatorDescriptionabExampleResult
+Addition52a + b7
-Subtraction52a - b3
*Multiplication52a * b10
/Division (Floating)52a / b2.5
//Floor Division52a // b2
%Modulus (Remainder)52a % b1
**Exponentiation52a ** b25

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)

Try it

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)

Try it

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)

Try it

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)

Try it

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)

Try it

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)

Try it

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)

Try it

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)

Try it

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)

Try it

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)

Try it

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.

Quiz #

Was this tutorial helpful ?