दिन 3: Python Basics: Variables, Types, Operators
✅ आज आप सीखेंगे
- Variables और नाम रखने के नियम
- Data types: int, float, str, bool, None
- Type conversion – data bugs की सबसे बड़ी वजह
- Arithmetic, comparison और logical operators
- साफ़ reports के लिए f-strings
🏢 असली दुनिया में
Billing system से export हुई CSV में prices "1,299" जैसे text में होते हैं। जब तक इसे number में न बदलें, total और average निकल ही नहीं सकते। Types की समझ असली projects में घंटों की debugging बचाती है।
Variables: values के नाम
Variable एक label है जो किसी value की ओर इशारा करता है। Python type अपने आप तय करता है।
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))
| नियम | सही | ग़लत |
|---|---|---|
| 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 | — |
हर dataset में मिलने वाले data types
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 | मतलब | 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 |
Data के काम की strings
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 और 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 का उत्तर 0.30000000000000004 आता है क्योंकि computer decimals को binary में रखता है। पैसे दिखाते समय हमेशा round करें और floats की तुलना थोड़ी छूट के साथ करें।
print(0.1 + 0.2) print(round(0.1 + 0.2, 2) == 0.3) print(abs((0.1 + 0.2) - 0.3) < 1e-9)
असली example: 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
Practice tasks
🏋️ Practice – ख़ुद करें
"₹52,500" है। इसे integer में बदलें और yearly salary print करें।Solution देखें
salary_text = "₹52,500"
salary = int(salary_text.replace("₹", "").replace(",", ""))
print("Yearly:", salary * 12)Solution देखें
price, mileage, km = 96.72, 15, 420
litres = km / mileage
print(f"Litres: {litres:.1f} | Cost: ₹{litres * price:,.2f}")Solution देखें
maths, science, english = 45, 31, 60 print(maths >= 33 and science >= 33 and english >= 33)
Quick quiz
🧠 Quick Quiz – ख़ुद को जाँचें
Interview corner
💼 Interview Corner
Python में / और // में क्या फ़र्क है?
/ true division है और हमेशा float देता है (7/2 = 3.5)। // floor division है और result से बड़ी न होने वाली सबसे बड़ी पूर्ण संख्या देता है (7//2 = 3)।
0.1 + 0.2 != 0.3 True क्यों हो सकता है?
Floats binary में रहते हैं और कई decimals ठीक-ठीक नहीं बनते, इसलिए छोटी rounding errors आती हैं। पैसे के लिए round(), math.isclose() या decimal module इस्तेमाल करें।
सारांश
- Variables values रखते हैं; Python type ख़ुद पहचानता है।
- int, float, str, bool और None ज़्यादातर data cover करते हैं।
- Text साफ़ करें फिर बदलें: int(x.replace(",", ""))।
- f"{value:,.2f}" और f"{rate:.1%}" reports को पढ़ने लायक बनाते हैं।
Day 4: if/else से फ़ैसले, loops से दोहराव और logic को reusable functions में रखना।
