What Are Arithmetic Operator ?
Arithmetic operators are fundamental tools in Python used to perform common mathematical
operations. They take numerical values (operands) and return a single numerical result.
Python supports seven arithmetic operators, covering basic calculations to advanced division
logic.
Here is a comprehensive breakdown of arithmetic operators in Python, complete with a
structured table, real-world use cases, and code explanations.
| Operator | Name | Syntax |
|---|---|---|
| + | Addition | a + b |
| - | Subtraction | a - b |
| * | Multiplication | a * b |
| / | Division | a / b |
| // | Floor Division | a // b |
| % | Modulus | a % b |
1. Addition Operator (+)
The addition operator (+) is used to add two or more numerical values together. It can also be used to concatenate strings (we already covered this in detail in our Strings module). Let's look at some examples :
# Storing the prices of two separate items
bread_price = 40
milk_price = 30
# Using the addition operator to calculate the total cost
total_bill = bread_price + milk_price
print("The total bill is:", total_bill)
# Output: The total bill is: 70
2. Subtraction Operator (-)
The subtraction operator (-) is used to deduct the right-hand numerical value from the left-hand numerical value to find the difference between them. Let's look at some examples :
# Storing the initial money available and the money spent
wallet_balance = 500
meal_cost = 120
# Using the subtraction operator to calculate remaining money
money_left = wallet_balance - meal_cost
print("Money remaining in wallet:", money_left)
# Output: Money remaining in wallet: 380
3. Multiplication Operator (*)
The multiplication operator (*) is used to multiply two or more numerical values together. It can also be used to replicate strings a specific number of times. Let's look at some examples :
# Storing the price of one notebook and the total quantity ordered
notebook_price = 60
quantity_ordered = 5
# Using the multiplication operator to find the grand total
grand_total = notebook_price * quantity_ordered
print("Total cost for notebooks:", grand_total)
# Output: Total cost for notebooks: 300
4. Standard Division Operator (/)
The standard division operator (/) is used to divide the left-hand operand by the right-hand operand. Crucial Rule: Standard division in Python always returns a floating-point (decimal) number, even if the numbers divide perfectly with no remainder. Let's look at some examples :
# Storing a bill amount and the number of friends splitting it
total_pizza_bill = 500
people_count = 2
# Using the standard division operator to split the bill
individual_share = total_pizza_bill / people_count
print("Each person must pay:", individual_share)
# Output: Each person must pay: 250.0
5. Floor Division Operator (//)
The floor division operator (//) is used to divide the left-hand operand by the right-hand operand and then chop off everything after the decimal point. It rounds the final answer down to the nearest whole integer. Let's look at some examples :
# Storing total items and how many items fit into one box
total_chocolates = 13
box_capacity = 4
# Using floor division to find how many complete boxes can be made
full_boxes_created = total_chocolates // box_capacity
print("Total complete boxes made:", full_boxes_created)
# Output: Total complete boxes made: 3
6. Modulus Operator (%)
The modulus operator (%) is used to divide the left-hand operand by the right-hand operand and return only the leftover remainder of that division. Let's look at some examples :
# Storing total items and box capacity
total_chocolates = 13
box_capacity = 4
# Using the modulus operator to find the remaining unboxed items
leftover_chocolates = total_chocolates % box_capacity
print("Chocolates left outside the boxes:", leftover_chocolates)
# Output: Chocolates left outside the boxes: 1