Python Numbers & Math Operations: From Calculator Logic to Code
Lesson Overview
Master Python's mathematical operations by understanding how computers handle numbers just like calculators. Learn integer vs float operations and build your own calculator program with practical examples
Lesson Content
Think About Your Daily Math Operations
Every day, you perform mathematical operations without thinking:
- Splitting restaurant bill: ₹450 ÷ 3 people = ₹150 each
- Finding discount: ₹1000 - (20% of ₹1000) = ₹800
- Compound interest: ₹10000 × (1.08)² = ₹11664
These same mathematical operations work in Python too! The computer becomes your super-powered calculator.
Python Mathematical Operators
Python uses operators - special symbols that tell the computer what mathematical operation to perform:
Basic Arithmetic Operators available in Python
Let's See These in Action:
# !Using Print function to output the calculated result
# !we are using integers direcly as opperends
# Basic operations
print(25 + 15) # Output: 40
print(50 - 20) # Output: 30
print(8 * 6) # Output: 48
print(100 / 4) # Output: 25.0
# Special operations
print(17 // 5) # Output: 3 (how many times 5 goes into 17)
print(17 % 5) # Output: 2 (what's left over)
print(3 ** 4) # Output: 81 (3 to the power of 4)Key Rules for Integer and Float Operations
In the above given example, Ever notice something strange about why 100 / 4 produces 25.0 (a float) rather than 25 (an integer), despite the fact that it divides perfectly with no fractional part?. Python's numerical operations follow some counterintuitive rules that might seem confusing at first, but understanding these patterns will make you a more effective programmer.
Key Operation Rules:
Integer operations stay integers (except division)
Float operations give floats
Mixed operations (int , float) always result in floats
💬 Comments (0)
Login to join the discussion
No comments yet. Be the first to share your thoughts!