Loading Page Content...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
Python can be used as a powerful calculator.
You can use Python to:
Python provides seven basic arithmetic operators:
| Operator | Name | Example | Result |
|---|---|---|---|
+ | Addition | 10 + 5 | 15 |
- | Subtraction | 10 - 5 | 5 |
* | Multiplication | 10 * 5 | 50 |
/ | True Division | 10 / 5 | 2.0 |
// | Floor Division | 10 // 3 | 3 |
% | Modulo | 10 % 3 | 1 |
** | Exponentiation | 2 ** 3 | 8 |
Let's understand each operator step by step.
Imagine you have 23 laddus and want to distribute them equally into 4 boxes.
There are three useful operations here:
/Result:
This tells us the exact mathematical result.
//Result:
This tells us how many complete groups of 4 can be made.
%Result:
This tells us that 3 laddus remain after making four groups of 5.
So:
+)The + operator adds two or more numbers.
Output:
Another example:
Output:
-)The - operator subtracts one number from another.
Output:
Real-life example:
Output:
*)The * operator multiplies numbers.
Output:
This is useful for:
/)The / operator performs normal division.
Output:
An important Python rule:
/ operator returns a float.Even when the division is exact:
Output:
Notice that the result is 2.0, not 2.
//)The // operator performs floor division.
Example:
Output:
Why?
Because:
Floor division moves down to the next whole-number value:
Floor division can be surprising with negative numbers.
For example:
Output:
Why not -3?
Because floor division moves toward negative infinity.
Compare:
Output:
// does not simply remove the decimal part.
It performs floor division.
%)The % operator gives the remainder after division.
Example:
Output:
Because:
So:
Modulo is commonly used to check whether a number is even or odd.
If a number divided by 2 has remainder 0, it is even.
Output:
Output:
We can write a simple checker:
Output:
Modulo is also useful when something repeats in a cycle.
For example, imagine 4 slides in a presentation:
After Slide 3, we want to return to Slide 0.
We can use:
Output:
This idea is useful for:
**)The ** operator is used for powers.
For example:
Output:
Because:
More examples:
Output:
Let's see all seven operators in one program.
Output:
divmod() — Get Quotient and Remainder TogetherSometimes you need both:
Instead of writing:
you can use:
Output:
So:
returns:
You can think of it as:
Suppose a video is 285 minutes long.
Output:
So the video duration is:
Output:
This means:
What happens when we use many operators in one expression?
For example:
Will Python calculate:
or:
The answer is:
Python follows operator precedence.
This is similar to the PEMDAS/BODMAS rule you may have learned in mathematics.
For the operators covered in this lesson:
| Priority | Operation | Example |
|---|---|---|
| 1 | Parentheses | (2 + 3) |
| 2 | Exponentiation | 2 ** 3 |
| 3 | Multiplication / Division | *, /, //, % |
| 4 | Addition / Subtraction | +, - |
So Python generally evaluates:
Consider:
Python evaluates it in this order:
Therefore:
Output:
Instead of relying completely on precedence:
you can make the intended order clearer:
Both produce:
Use parentheses when they make a formula easier to understand.
Exponentiation works from right to left.
For example:
Python interprets it as:
First:
Then:
So:
Output:
But:
gives:
because:
If a formula contains multiple powers, use parentheses to make your intention obvious.
Let's calculate:
Python:
Output:
Python automatically follows the correct order.
Output:
Here we used:
You cannot divide a number by zero.
For example:
Python raises:
The same applies to:
and:
| Situation | Avoid | Prefer |
|---|---|---|
| Clear formulas | Very long unclear expressions | Use parentheses |
| Quotient + remainder | Calculate both separately when both are needed | divmod() |
| Even/odd checking | Convert number to string | number % 2 |
| Powers | Unnecessary conversion to float | ** |
| Division | Assume / returns int | Remember / returns float |
| Negative division | Assume // simply removes decimals | Remember it floors toward negative infinity |
8 / 4 in Python?-9 // 2?17 % 5?So the remainder is 2.
divmod(29, 6) return?For 29 ÷ 6:
10 + 5 * 2 ** 2?2 ** 3 ** 2?EMI = P × r × (1 + r)ⁿ ───────────────────── (1 + r)ⁿ - 1
def calculate_emi(principal, annual_rate, tenure_months):
monthly_rate = (annual_rate / 12) / 100
growth_factor = (1 + monthly_rate) ** tenure_months
emi = ( principal
/ (growth_factor - 1) )
total_payment = emi * tenure_months
total_interest = total_payment - principal
years, months = divmod(tenure_months, 12)
print("=" 50) print(" HOME LOAN EMI CALCULATOR") print("=" 50)
print(f"Loan Amount : ₹{principal:,.2f}") print(f"Interest Rate : {annual_rate:.2f}%") print(f"Tenure : {years} Years {months} Months") print("-" * 50) print(f"Monthly EMI : ₹{emi:,.2f}") print(f"Total Payment : ₹{total_payment:,.2f}") print(f"Total Interest : ₹{total_interest:,.2f}")
print("=" * 50)
calculate_emi( principal=45_00_000, annual_rate=8.75, tenure_months=240 )
annual_rate / 12 ↓ Division
principal * monthly_rate ↓ Multiplication
(1 + monthly_rate) ** tenure_months ↓ Exponentiation
growth_factor - 1 ↓ Subtraction
divmod(tenure_months, 12) ↓ Years + remaining months
calculate_emi( principal=20_00_000, annual_rate=8.5, tenure_months=120 )
calculate_emi( principal=10_00_000, annual_rate=9.5, tenure_months=60 )
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
| Previous Lesson | Next Lesson |
|---|---|
| Integer, Float, Complex | Math Module (math, random) |
Test your understanding of this lesson with 6 questions. Each question has one correct answer.