List of Arithmetic Operators
Operator | Description | Example (a = 10, b = 3) |
---|---|---|
+ | Addition | a + b = 13 |
- | Subtraction | a - b = 7 |
* | Multiplication | a * b = 30 |
/ | Division (float result) | a / b = 3.3333 |
// | Floor Division | a // b = 3 |
% | Modulus (Remainder) | a % b = 1 |
** | Exponentiation (Power) | a ** b = 1000 |
Real-Life Analogies for Arithmetic Operators in Python
Addition (+): Counting total items in your shopping cart.
Subtraction (-): Calculating how much money you have left after a purchase.
Multiplication (*): Determining the total cost when buying multiple items.
Division (/): Splitting a bill among friends.
Modulus (%): Finding out the remainder of a group when dividing people into teams.
Exponentiation (**): Calculating compound interest.
Arithmetic Operators with Examples in Python
Example 1: Addition (+)
Adds two numbers or variables.
a = 10 b = 3 result = a + b print(f"Addition: {result}") # Output: 13
Real-Life Example: Adding apples and oranges to calculate the total fruits.
apples = 5 oranges = 7 total_fruits = apples + oranges print(f"Total fruits: {total_fruits}") # Output: 12
Example 2. Subtraction (-)
Subtracts the second number from the first.
a = 10 b = 3 result = a - b print(f"Subtraction: {result}") # Output: 7
Real-Life Example: Calculating the remaining balance after spending.
wallet = 100 spent = 35 remaining = wallet - spent print(f"Remaining balance: {remaining}") # Output: 65
Example 3. Multiplication (*)
Multiplies two numbers.
a = 10 b = 3 result = a * b print(f"Multiplication: {result}") # Output: 30
Real-Life Example: Total cost when buying multiple items.
price_per_item = 15 quantity = 4 total_cost = price_per_item * quantity print(f"Total cost: {total_cost}") # Output: 60
Example 4. Division (/)
Divides the first number by the second and returns a float.
a = 10 b = 3 result = a / b print(f"Division: {result}") # Output: 3.3333333333333335
Real-Life Example: Splitting a bill equally.
total_bill = 120 people = 4 share_per_person = total_bill / people print(f"Each person pays: {share_per_person}") # Output: 30.0
Example 5. Floor Division (//)
Performs division but discards the fractional part.
a = 10 b = 3 result = a // b print(f"Floor Division: {result}") # Output: 3
Real-Life Example: Distributing chocolates evenly among kids.
chocolates = 22 kids = 5 chocolates_per_kid = chocolates // kids print(f"Each kid gets: {chocolates_per_kid}") # Output: 4
Example 6. Modulus (%)
Returns the remainder of the division.
a = 10 b = 3 result = a % b print(f"Modulus: {result}") # Output: 1
Real-Life Example: Finding leftover chocolates after distributing evenly.
chocolates = 22 kids = 5 remaining_chocolates = chocolates % kids print(f"Leftover chocolates: {remaining_chocolates}") # Output: 2
Example 7. Exponentiation (**)
Raises the first number to the power of the second.
a = 10 b = 3 result = a ** b print(f"Exponentiation: {result}") # Output: 1000
Real-Life Example: Calculating compound interest.
principal = 1000 rate = 0.05 years = 2 final_amount = principal * (1 + rate) ** years print(f"Final amount: {final_amount}") # Output: 1102.5
Key Points to Remember while using Arithmetic operators in python
Operator Precedence: Some operators have higher precedence than others. For example: ** > *, /, % > +, -
Use parentheses to ensure correct order of operations.
result = 2 + 3 * 2 # 2 + (3 * 2) = 8 result_with_parentheses = (2 + 3) * 2 # (2 + 3) * 2 = 10
Division by Zero: This causes an error.
print(10 / 0) # ZeroDivisionError
Type Compatibility: Arithmetic operations can only be performed on compatible types (e.g., integers or floats). Strings can use + for concatenation.
Real-Life Application
Imagine creating an invoice system:
Here’s an example:
unit_price = 50 quantity = 7 total_cost = unit_price * quantity print(f"Total cost: {total_cost}") boxes = 3 items_per_box = total_cost // boxes leftover = total_cost % boxes print(f"Each box gets {items_per_box}, with {leftover} items left.")