Free tutorials & notes in Hindi & English · Clean code examples 🧰 मुफ़्त PDF और फ़ोटो टूल्स →
🐍 Week 1 – Data Science के लिए Python  ·  दिन 03

दिन 3: Python Basics: Variables, Types, Operators

⏱ 90 min📶 Beginner🎯 100% self-study
दिन 3 / 60

✅ आज आप सीखेंगे

  • 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 अपने आप तय करता है।

Python
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))
Wireless Earbuds 1499 4.3 True None <class 'str'> <class 'int'> <class 'float'> <class 'bool'> <class 'NoneType'>
नियमसहीग़लत
Use letters, digits, _total_salestotal-sales
Cannot start with a digitsales_20242024_sales
Use snake_case and meaningavg_order_valuex1
Case-sensitivePrice ≠ price—

हर dataset में मिलने वाले data types

🔢
intगिनती: quantity, उम्र, orders की संख्या
➗
floatमाप: पैसे वाली कीमत, rating, तापमान
🔤
strText: शहर, product name, review
✅
boolहाँ/नहीं flags: churned, approved, paid
∅
None / NaNMissing value – असली data में बहुत आम
रोज़ के पाँच types

Type conversion

Python
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"))
3897 5.0 2024-25 False True False True
Python
age = "twenty"
try:
    age = int(age)
except ValueError as e:
    print("Cannot convert:", e)
Cannot convert: invalid literal for int() with base 10: 'twenty'

Operators

Python
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)
Yearly : 540000 Per day (30) : 1500.0 Full weeks : 6 | left days: 3 Power : 1024 Price + GST : 1768.82
Python
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
True False False True True
OperatorमतलबExample → result
+ - * /Add, subtract, multiply, divide7 / 2 → 3.5
//Floor division7 // 2 → 3
%Remainder7 % 2 → 1
**Power2 ** 3 → 8
== !=Equal / not equal5 == 5 → True
> < >= <=Compare3 > 5 → False
and or notLogicTrue and False → False
inIs inside"a" in "data" → True

Data के काम की strings

Python
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))
New Delhi NEW DELHI new delhi Delhi gmail.com 21 True Order ID: BK100245

f-strings और rounding

Python
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))
Riya placed 14 orders worth ₹28,749.46 Average order value: ₹2054 Growth: 18.3% 28749.5 28700.0
⚠️ Float की हैरानी

0.1 + 0.2 का उत्तर 0.30000000000000004 आता है क्योंकि computer decimals को binary में रखता है। पैसे दिखाते समय हमेशा round करें और floats की तुलना थोड़ी छूट के साथ करें।

Python
print(0.1 + 0.2)
print(round(0.1 + 0.2, 2) == 0.3)
print(abs((0.1 + 0.2) - 0.3) < 1e-9)
0.30000000000000004 True True

असली example: shopping bill

Python
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}")
Subtotal : ₹1,947.00 After discount : ₹1,752.30 Total with GST : ₹1,839.91 You saved : ₹194.70

🏅 आज के Job Skills

✔ सही data type चुनना✔ Text numbers को सुरक्षित बदलना✔ Business maths के लिए operators✔ f-strings से reports format करना

Practice tasks

🏋️ Practice – ख़ुद करें

Task 1: Salary text "₹52,500" है। इसे integer में बदलें और yearly salary print करें।
Solution देखें
Python
salary_text = "₹52,500"
salary = int(salary_text.replace("₹", "").replace(",", ""))
print("Yearly:", salary * 12)
Yearly: 630000
Task 2: Petrol ₹96.72 प्रति litre है। गाड़ी 15 km प्रति litre चलती है। 420 km यात्रा का ख़र्च 2 decimals में print करें।
Solution देखें
Python
price, mileage, km = 96.72, 15, 420
litres = km / mileage
print(f"Litres: {litres:.1f} | Cost: ₹{litres * price:,.2f}")
Litres: 28.0 | Cost: ₹2,708.16
Task 3: Eligibility जाँचें: student पास है अगर हर subject में marks ≥ 33 हों (maths 45, science 31, english 60)। True/False print करें।
Solution देखें
Python
maths, science, english = 45, 31, 60
print(maths >= 33 and science >= 33 and english >= 33)
False

Quick quiz

🧠 Quick Quiz – ख़ुद को जाँचें

1. type(4.0) क्या है?
Decimal point वाला number float है।
2. int("1,299") क्या देता है?
Comma digit नहीं है – पहले replace से हटाएँ।
3. 17 % 5 बराबर…
% शेषफल है: 17 = 5×3 + 2।
4. f"{0.256:.1%}" क्या print करता है?
:.1% 100 से गुणा करके एक decimal दिखाता है।
5. कौन सा नाम valid variable है?
नाम digit से शुरू नहीं हो सकते, hyphen नहीं, और class जैसे keywords नहीं।

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 में रखना।

अक्सर पूछे जाने वाले प्रश्न (FAQ)

क्या Python में types declare करने पड़ते हैं?
नहीं। Python dynamically typed है – type value से आता है। जाँचने के लिए type() लिखें।
None और NaN में क्या फ़र्क है?
None Python का “कोई value नहीं” है। NaN (not a number) NumPy और pandas में missing numbers के लिए float है। pandas दोनों को missing मानता है।
f-strings क्यों?
Text के अंदर values और formatting (commas, decimals, %) डालने का यह सबसे साफ़ और तेज़ तरीका है।
← Data Science (60 Days)
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

अगला topicदिन 4: Conditions, Loops और Functions आपकी प्रगति: 3 / 60 (5%) · Lesson 4 / 60 आगे बढ़ें →

यह lesson कैसा लगा?

आपकी राय से हम content और बेहतर बनाते हैं।

अपना review और सुझाव लिखें (वैकल्पिक)

Review approve होने के बाद आपका नाम और शहर इसके साथ public दिख सकते हैं। Email या phone न लिखें।

▶ CodingEasily on YouTube

Video से सीखें – आसान Hindi में

चुनी हुई video: RDBMS भाग 3 – Class XI Commerce IT

▶ Channel subscribe करें