🐍 Using Python as a Calculator
Performing Arithmetic Calculations Using Python
📘 What is Python as a Calculator?
Python can be used like a simple calculator to perform
addition, subtraction, multiplication, division,
exponentiation and other mathematical operations.
We can directly type mathematical expressions at the Python prompt and immediately obtain the result.
We can directly type mathematical expressions at the Python prompt and immediately obtain the result.
1️⃣ Basic Calculation
For example, to add two numbers:
>>> 10 + 20
30
Python evaluates the expression and displays the result.
2️⃣ Arithmetic Operators in Python
| Operator | Operation | Example | Result |
|---|---|---|---|
| + | Addition | 10 + 5 | 15 |
| - | Subtraction | 10 - 5 | 5 |
| * | Multiplication | 10 * 5 | 50 |
| / | Division | 10 / 5 | 2.0 |
| // | Floor Division | 10 // 3 | 3 |
| % | Modulus | 10 % 3 | 1 |
| ** | Exponentiation | 2 ** 3 | 8 |
3️⃣ Addition
>>> 25 + 15
40
4️⃣ Subtraction
>>> 50 - 18
32
5️⃣ Multiplication
>>> 12 * 8
96
6️⃣ Division
>>> 20 / 4
5.0
💡 Important: The
/ operator normally
returns a floating-point value.
7️⃣ Floor Division
>>> 20 // 6
3
Floor division returns the quotient after removing the fractional part for positive numbers.
8️⃣ Modulus Operator
>>> 20 % 6
2
The modulus operator % returns the remainder.
9️⃣ Exponentiation
>>> 2 ** 5
32
Mathematical meaning:
25 = 2 × 2 × 2 × 2 × 2 = 32
25 = 2 × 2 × 2 × 2 × 2 = 32
🔟 Order of Operations
Python follows the standard mathematical order of operations.
>>> 10 + 5 * 2
20
Multiplication is performed before addition.
>>> (10 + 5) * 2
30
Parentheses can be used to change the order of evaluation.
🧮 Complex Mathematical Expression
>>> (25 + 15) * 2 / 5
16.0
Python evaluates the complete mathematical expression automatically.
📦 Using Variables as a Calculator
a = 25
b = 15
sum = a + b
print(sum)
40
⌨️ Calculator Using User Input
a = float(input("Enter first number: "))
b = float(input("Enter second number: "))
print("Sum =", a + b)
print("Difference =", a - b)
print("Product =", a * b)
print("Division =", a / b)
Enter first number: 20
Enter second number: 5
Sum = 25.0
Difference = 15.0
Product = 100.0
Division = 4.0
Enter second number: 5
Sum = 25.0
Difference = 15.0
Product = 100.0
Division = 4.0
🎮 Interactive Python Calculator Concept
Click a button to see the result of a Python expression.
Result will appear here...
✅ Advantages of Using Python as a Calculator
- Very easy to perform mathematical calculations.
- Supports integers and floating-point numbers.
- Can evaluate complex expressions.
- Supports many mathematical operators.
- Calculations can be stored in variables.
- Useful for learning programming fundamentals.
- Can perform calculations using user input.
- Useful for scientific and engineering calculations.
⚠️ Limitations
- Python must be installed to use the Python interpreter.
- Very large symbolic calculations may require special libraries.
- Beginners must understand Python syntax.
- Division by zero produces an error.
🎯 Important Points for Examination
- Python can be used as a calculator.
+is used for addition.-is used for subtraction.*is used for multiplication./performs true division.//performs floor division.%returns the remainder.**is used for exponentiation.- Parentheses can be used to control the order of evaluation.
- Python follows operator precedence rules.
📌 Quick Summary
| Task | Python Expression |
|---|---|
| Addition | 10 + 5 |
| Subtraction | 10 - 5 |
| Multiplication | 10 * 5 |
| Division | 10 / 5 |
| Floor Division | 10 // 3 |
| Remainder | 10 % 3 |
| Power | 2 ** 3 |
No comments:
Post a Comment