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

OperatorNameExampleResult
+Addition5 + 38
-Subtraction10 - 46
*Multiplication6 * 742
/Division15 / 43.75
//Floor Division15 // 43
%Modulus (Remainder)15 % 43
**Exponentiation (Power)2 ** 38

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 

Integers (int) - Whole Numbers

#Defining the Variables 'a' and 'b'
a = 10
b = 3

#variables can be used directly to perform the operations
print(a + b)    # 13
print(a - b)    # 7  
print(a * b)    # 30
print(a // b)   # 3 (floor division gives integer)
print(a % b)    # 1 (remainder)
print(a ** b)   # 1000 (10 to power 3)

Floats (float) - Decimal Numbers

#Defining the Variables 'x' and 'y'
x = 10.5
y = 3.2

print(x + y)    # 13.7
print(x - y)    # 7.3
print(x * y)    # 33.6  
print(x / y)    # 3.28125 (normal division gives float)
print(x // y)   # 3.0 (floor division of floats)

Mixed Operations (int + float = float)

print(10 + 3.5)     # 13.5 (result becomes float)
print(20 / 4)       # 5.0 (division always gives float)
print(type(20/4))   # <class 'float'>

Practice Exercises

Compound Interest Calculator

# Calculate compound interest
principal = float(input("Enter principal amount: ₹"))        #Take the input and convert it into the float data-type
rate = float(input("Enter annual interest rate (%): "))  
time = float(input("Enter time period (years): "))

# Formula: A = P(1 + r/100)^t
amount = principal * ((1 + rate/100) ** time)
interest = amount - principal

print("Principal: ", principal)
print(f"Amount : ", amount )
print(f"Interest earned: ", interest )

Key Takeaways

  • Mathematical operators work the same in Python as in real math
  • Integer operations stay integers (except division), float operations give floats
  • Mixed operations (int + float) always result in floats
  • Division (/) always gives float, even for whole number results

Coming Up Next

Great job! You now understand how Python handles numbers and mathematical operations. In our next lesson, we'll explore Strings - Python's way of working with text. We'll learn how to manipulate words, create messages, format text, and discover why that "10" + "10" = "1010" example makes perfect sense!

You're becoming a real programmer!