Python Arithmetic Operators
Learn how Python performs mathematical calculations using addition, subtraction, multiplication, division, floor division, modulus, and exponentiation.
Learning Objectives
By the end of this lesson, you should be able to:
- Understand the purpose of arithmetic operators in Python.
- Perform addition, subtraction, multiplication, and division.
-
Understand the difference between
/and//. - Use the modulus operator to find remainders.
-
Calculate powers using
**. - Understand operator precedence.
- Use arithmetic operators with variables and user input.
- Apply Python arithmetic to real-world problems.
What Are Arithmetic Operators?
Arithmetic operators are symbols that tell Python to perform mathematical operations on values.
Python supports all of the common mathematical operations you would expect from a calculator, along with several useful operations specifically designed for programming.
Python provides
+,
-,
*,
/,
//,
%,
and **.
print(10 + 3)
print(10 - 3)
print(10 * 3)
print(10 / 3)
print(10 // 3)
print(10 % 3)
print(2 ** 3)
13 7 30 3.3333333333333335 3 1 8
Addition +
The plus sign + is used to add two
or more numeric values.
first_number = 15
second_number = 8
result = first_number + second_number
print(result)
23
Addition can also be used with floating-point numbers.
price = 19.99
delivery = 4.50
total = price + delivery
print(total)
Subtraction -
The minus sign - subtracts one value
from another.
balance = 500
expense = 175
remaining = balance - expense
print(remaining)
325
Subtraction can produce negative numbers when the second value is larger than the first.
result = 10 - 25
print(result)
-15
Multiplication *
Python uses the asterisk * for
multiplication.
price = 250
quantity = 4
total = price * quantity
print(total)
1000
If one product costs Rs. 250 and you buy four products, multiplying the price by the quantity gives the total cost.
Division /
The forward slash / performs division.
total = 100
people = 4
share = total / people
print(share)
25.0
Even when the result looks like a whole
number, the / operator returns
a floating-point value.
result = 10 / 2
print(result)
print(type(result))
5.0 <class 'float'>
Floor Division //
Floor division uses two forward slashes:
//.
It divides two numbers and returns the result rounded down to the nearest whole number.
result = 10 // 3
print(result)
3
Compare this with normal division:
print(10 / 3)
print(10 // 3)
3.3333333333333335 3
Floor division means rounding toward negative infinity, not simply removing the decimal part.
For example,
-7 // 3 gives
-3.
Modulus %
The modulus operator % returns the
remainder after division.
result = 10 % 3
print(result)
1
Why is the answer 1?
Three fits into ten three complete times:
3 × 3 = 9
10 - 9 = 1
Therefore, the remainder is 1.
The modulus operator is commonly used to check whether a number is even or odd.
number = 20
print(number % 2)
0
If a number divided by 2 leaves a remainder of
0, the number is even.
Exponentiation **
Python uses two asterisks ** to
calculate powers.
result = 2 ** 3
print(result)
8
This means:
2 ** 3
# Same as:
# 2 × 2 × 2
You can also calculate squares and cubes.
number = 5
square = number ** 2
cube = number ** 3
print(square)
print(cube)
25 125
Operator Precedence
When an expression contains multiple operators, Python needs to know which operation should be performed first.
This order is called operator precedence.
Python generally follows this order for arithmetic:
- Parentheses
() - Exponentiation
** - Multiplication, division, floor division, and modulus
- Addition and subtraction
result = 2 + 3 * 4
print(result)
14
Python performs multiplication before addition:
2 + 3 * 4
# First:
3 * 4 = 12
# Then:
2 + 12 = 14
If an expression is difficult to read, use parentheses to make your intention obvious.
Using Parentheses
Parentheses allow you to explicitly control which operation Python performs first.
result = 2 + 3 * 4
print(result)
14
result = (2 + 3) * 4
print(result)
20
Use parentheses whenever they improve readability or make the intended order of calculation clearer.
Arithmetic with Variables
Arithmetic becomes much more useful when values are stored in variables.
length = 12
width = 5
area = length * width
print(area)
60
You can combine several arithmetic operations in a single expression.
price = 1000
discount = 100
tax = 50
final_price = price - discount + tax
print(final_price)
950
Arithmetic with User Input
You can combine arithmetic operators with
input() to build interactive programs.
Remember that input() returns text,
so numeric input should normally be converted
before performing calculations.
first = float(input("Enter first number: "))
second = float(input("Enter second number: "))
total = first + second
print("Total:", total)
Enter first number: 25 Enter second number: 15 Total: 40.0
Do not try to perform numeric arithmetic
directly on strings returned by
input().
age = int(input("Enter your age: "))
next_year = age + 1
print("Next year:", next_year)
Arithmetic in Real-World Programs
Shopping Bill
Arithmetic operators can calculate the total price of products.
item_price = 1200
quantity = 3
subtotal = item_price * quantity
print("Subtotal:", subtotal)
Subtotal: 3600
Rectangle Area
length = 20
width = 8
area = length * width
print("Area:", area)
Area: 160
Average of Numbers
math_marks = 80
english_marks = 75
computer_marks = 90
total = math_marks + english_marks + computer_marks
average = total / 3
print("Total:", total)
print("Average:", average)
Total: 245 Average: 81.66666666666667
Common Mistakes
1. Using the wrong division operator
Remember the difference:
print(7 / 2)
print(7 // 2)
3.5 3
2. Forgetting operator precedence
This:
result = 10 + 5 * 2
produces 20, not 30,
because multiplication happens first.
3. Forgetting to convert input
Input values are strings by default.
number = int(input("Enter a number: "))
result = number * 2
print(result)
4. Dividing by zero
Python cannot divide a number by zero.
result = 10 / 0
Attempting to divide by zero raises a
ZeroDivisionError.
Practice Exercises
Try these exercises yourself before looking at other solutions.
Exercise 1 — Basic Arithmetic
Create a program that calculates:
- 25 + 15
- 50 - 17
- 8 × 6
- 100 ÷ 4
Exercise 2 — Remainder
Use the modulus operator to find the remainder when 47 is divided by 5.
Exercise 3 — Square
Store the number 12 in a variable and calculate
its square using **.
Exercise 4 — Rectangle
Store the length and width of a rectangle in variables and calculate its area.
Exercise 5 — Shopping
A product costs Rs. 750 and a customer buys 4 units. Calculate the total price.
Exercise 6 — User Input
Ask the user for two numbers and display their sum, difference, product, and division result.
Mini Challenge — Simple Bill Calculator
Build a small Python program that asks the user for the price of a product and the quantity purchased.
Your program should calculate:
- Subtotal
- 10% discount
- Final price after discount
Use input(),
float(),
multiplication, subtraction,
and division or percentage arithmetic.
price = float(input("Enter product price: "))
quantity = int(input("Enter quantity: "))
subtotal = price * quantity
discount = subtotal * 0.10
final_price = subtotal - discount
print("Subtotal:", subtotal)
print("Discount:", discount)
print("Final price:", final_price)
Add a tax calculation after the discount and display the final amount the customer has to pay.
Quick Knowledge Check
1. Which operator is used for multiplication?
2. What is the result of 10 // 3?
3. Which operator returns the remainder?
4. What is the result of 2 ** 4?
5. What is the result of
2 + 3 * 4?
6. What does / normally return?
7. Which operation should you use to calculate
the square of 7?
Lesson Summary
Arithmetic operators are fundamental building blocks of Python programming. They allow programs to perform calculations, process data, and solve real-world problems.
+
Addition
10 + 3
-
Subtraction
10 - 3
*
Multiplication
10 * 3
/
Division
10 / 3
//
Floor division
10 // 3
%
Remainder
10 % 3
**
Exponentiation
2 ** 3
Learn the operators, understand their behavior, and then practice combining them with variables and user input. This is the foundation for building useful Python programs.
Ready to continue?
Complete this lesson once you have practiced the arithmetic operators and understand how Python evaluates expressions.