Day 3: Python Basics: Variables, Types, Operators
✅ Today you will learn
- Variables and naming rules
- Data types: int, float, str, bool, None
- Type conversion – the #1 cause of data bugs
- Arithmetic, comparison and logical operators
- f-strings for clean reports
🏢 In the real world
A CSV exported from a billing system stores prices as text like "1,299". Until you convert it to a number, totals and averages are impossible. Knowing types saves hours of debugging in real projects.
Variables: names for values
A variable is a label that points to a value. Python decides the type automatically.
product = "Wireless Earbuds" # str (text) price = 1499 # int (whole number) rating = 4.3 # float (decimal) in_stock = True # bool (True / False) discount_code = None # None (no value yet) print(product, price, rating, in_stock, discount_code) print(type(product), type(price), type(rating), type(in_stock), type(discount_code))
| Rule | Good | Bad |
|---|---|---|
| Use letters, digits, _ | total_sales | total-sales |
| Cannot start with a digit | sales_2024 | 2024_sales |
| Use snake_case and meaning | avg_order_value | x1 |
| Case-sensitive | Price ≠ price | — |
Data types you meet in every dataset
Type conversion
price_text = "1,299" # how many CSVs store numbers
price = int(price_text.replace(",", ""))
qty = int("3")
print(price * qty) # now maths works
print(float("4.5") + 0.5)
print(str(2024) + "-25")
print(bool(0), bool(15), bool(""), bool("Delhi"))age = "twenty"
try:
age = int(age)
except ValueError as e:
print("Cannot convert:", e)Operators
monthly_salary = 45000
print("Yearly :", monthly_salary * 12)
print("Per day (30) :", monthly_salary / 30)
print("Full weeks :", 45 // 7, "| left days:", 45 % 7)
print("Power :", 2 ** 10)
price, gst_rate = 1499, 18
print("Price + GST :", price + price * gst_rate / 100)marks, attendance = 72, 68 print(marks >= 40) # comparison -> bool print(marks >= 40 and attendance >= 75) # both needed print(marks >= 90 or attendance == 100) # any one print(not attendance >= 75) city = "Pune" print(city in ["Pune", "Mumbai", "Nagpur"]) # membership
| Operator | Meaning | Example → result |
|---|---|---|
| + - * / | Add, subtract, multiply, divide | 7 / 2 → 3.5 |
| // | Floor division | 7 // 2 → 3 |
| % | Remainder | 7 % 2 → 1 |
| ** | Power | 2 ** 3 → 8 |
| == != | Equal / not equal | 5 == 5 → True |
| > < >= <= | Compare | 3 > 5 → False |
| and or not | Logic | True and False → False |
| in | Is inside | "a" in "data" → True |
Strings for data work
city = " new delhi "
clean = city.strip().title()
print(clean) # remove spaces + proper case
print(clean.upper(), clean.lower())
print(clean.replace("New ", ""))
email = "riya.sharma@gmail.com"
print(email.split("@")[1]) # domain
print(len(email), email.endswith(".com"))
print("Order ID:", "BK" + str(100245))f-strings and rounding
name, orders, revenue = "Riya", 14, 28749.456
print(f"{name} placed {orders} orders worth ₹{revenue:,.2f}")
print(f"Average order value: ₹{revenue / orders:.0f}")
growth = 0.1834
print(f"Growth: {growth:.1%}")
print(round(28749.456, 1), round(28749.456, -2))0.1 + 0.2 gives 0.30000000000000004 because computers store decimals in binary. Always round money for display and compare floats with a tolerance.
print(0.1 + 0.2) print(round(0.1 + 0.2, 2) == 0.3) print(abs((0.1 + 0.2) - 0.3) < 1e-9)
Real example: a shopping bill
items = 3
price_each = 649.0 # Basmati rice 5 kg
discount = 0.10 # 10 % festive offer
gst = 0.05 # 5 % GST on rice
subtotal = items * price_each
after_discount = subtotal * (1 - discount)
total = after_discount * (1 + gst)
print(f"Subtotal : ₹{subtotal:,.2f}")
print(f"After discount : ₹{after_discount:,.2f}")
print(f"Total with GST : ₹{total:,.2f}")
print(f"You saved : ₹{subtotal - after_discount:,.2f}")🏅 Job skills unlocked today
Practice tasks
🏋️ Practice – do it yourself
"₹52,500". Convert it to an integer and print the yearly salary.Show solution
salary_text = "₹52,500"
salary = int(salary_text.replace("₹", "").replace(",", ""))
print("Yearly:", salary * 12)Show solution
price, mileage, km = 96.72, 15, 420
litres = km / mileage
print(f"Litres: {litres:.1f} | Cost: ₹{litres * price:,.2f}")Show solution
maths, science, english = 45, 31, 60 print(maths >= 33 and science >= 33 and english >= 33)
Quick quiz
🧠 Quick Quiz – test yourself
Interview corner
💼 Interview Corner
What is the difference between / and // in Python?
/ is true division and always returns a float (7/2 = 3.5). // is floor division and returns the largest whole number not greater than the result (7//2 = 3).
Why can 0.1 + 0.2 != 0.3 be True?
Floats are stored in binary and many decimals cannot be represented exactly, so tiny rounding errors appear. Use round(), math.isclose() or the decimal module for money.
Summary
- Variables hold values; Python finds the type itself.
- int, float, str, bool and None cover most data.
- Clean text then convert: int(x.replace(",", "")).
- f"{value:,.2f}" and f"{rate:.1%}" make reports readable.
Day 4: make decisions with if/else, repeat with loops and package logic into reusable functions.
