🟢 Beginner  ·  Lesson 05

Python में Variables और Data Types

परिचय

Python में variable एक नाम है जो memory में रखी value की ओर इशारा करता है। C या Java की तरह आपको type declare नहीं करना पड़ता — Python value देखकर खुद type समझ लेता है। इसे dynamic typing कहते हैं।

💡 यह क्यों ज़रूरी है

Variables हर program की नींव हैं। Marks, names, totals, flags — जो कुछ program याद रखता है, वह variable में रहता है। इसे पहले master करें; बाकी Python इसी पर बनता है।

Variable क्या है?

Variable को एक label वाला डिब्बा समझें। Label है name, और डिब्बे में रहती है value। जब आप marks = 90 लिखते हैं, Python marks नाम का डिब्बा बनाकर उसमें 90 रख देता है।

= चिह्न assignment operator है — इसका मतलब "बराबर" नहीं (वह == है)। इसका मतलब है "दाईं ओर की value को बाईं ओर के नाम में store करो"।

Built-in Data Types

Python में कई built-in data types हैं। ये पाँच Class 12 से B.Tech level पर सबसे ज़्यादा use होते हैं:

TypeExampleकिसके लिए
intage = 17पूर्ण संख्याएं
floatpct = 91.5दशमलव संख्याएं
strname = "Aman"Text
boolpassed = TrueTrue / False
NoneTyperesult = None"अभी कोई value नहीं"

Program 1: Variables बनाना

Python – variables.py
# हर line अलग type का variable बनाती है
name = "Aman"        # str
age = 17             # int
percentage = 91.5    # float
is_pass = True       # bool

print("Name:", name)
print("Age:", age)
print("Percentage:", percentage)
print("Passed:", is_pass)
Name: Aman Age: 17 Percentage: 91.5 Passed: True

Line-by-line:

  • name = "Aman" — text quotes में है, इसलिए Python इसे str रखता है।
  • age = 17 — बिना दशमलव वाली पूर्ण संख्या int बनती है।
  • percentage = 91.5 — दशमलव बिंदु इसे float बना देता है।
  • is_pass = TrueTrue/False (बड़े T/F) bool values हैं।
  • print(...) — comma अपने आप label और value के बीच space देता है।

Program 2: Type Check और Convert

Type check करने के लिए type(), और convert करने के लिए int()/float()/str() (इसे type casting कहते हैं)।

Python – type_convert.py
marks = "85"               # यह STRING है, number नहीं!
print(type(marks))          # <class 'str'>

# maths करने से पहले string को integer में बदलें
marks_int = int(marks)
print(marks_int + 5)        # अब arithmetic चलेगा
print(type(marks_int))
<class 'str'> 90 <class 'int'>

यह क्यों ज़रूरी है: input() से आया data हमेशा string होता है। अगर convert करना भूल गए, तो "85" + 5 error देगा। Program 3 असली fix दिखाता है।

Program 3: Student Report Card

एक practical program जो हर type एक साथ use करता है:

Python – report_card.py
name = input("Enter student name: ")
maths = int(input("Maths marks: "))
science = int(input("Science marks: "))
english = int(input("English marks: "))

total = maths + science + english
percentage = total / 3
is_pass = percentage >= 33

print("\n----- REPORT CARD -----")
print("Name       :", name)
print("Total      :", total, "/ 300")
print("Percentage :", round(percentage, 2), "%")
print("Result     :", "PASS" if is_pass else "FAIL")
Enter student name: Aman Maths marks: 90 Science marks: 85 English marks: 80 ----- REPORT CARD ----- Name : Aman Total : 255 / 300 Percentage : 85.0 % Result : PASS
  • int(input(...)) text पढ़कर एक ही step में number बना देता है।
  • percentage >= 33 एक bool बनाता है जो is_pass में रहता है।
  • round(percentage, 2) सिर्फ 2 दशमलव रखता है।
  • "PASS" if is_pass else "FAIL" एक-line का conditional (ternary) है।

Variable Naming Rules

  • letters, digits और underscore (_) हो सकते हैं, जैसे total_marks
  • digit से शुरू नहीं हो सकता: 2marks गलत, marks2 सही।
  • Case-sensitive: Name और name अलग हैं।
  • keyword नहीं हो सकता (if, class, for...)।
  • साफ नाम रखें: percentage, न कि p

सामान्य गलतियाँ

  • maths से पहले input() को int में convert करना भूलना।
  • == (compare) की जगह = (assign) लगाना।
  • true/false lowercase में लिखना — Python को True/False चाहिए।
  • गलत indentation (Python spaces को लेकर सख्त है)।

Practice Tasks

  1. अपने नाम, class और तीन subjects के marks के variables बनाकर formatted report print करें।
  2. User से दो numbers लेकर उनका sum, difference और product print करें।
  3. अपनी height metres (float) में रखकर print करें कि वह 1.5 से ऊपर है या नहीं (bool)।
  4. पाँच अलग values पर type() use करके output नोट करें।

सारांश

  • Variable एक नाम है जो value की ओर इशारा करता है; Python type अपने आप पहचानता है।
  • मुख्य types: int, float, str, bool, None।
  • input() हमेशा string लौटाता है — int()/float() से convert करें।
  • type() type check करता है; = assign, == compare।
← Back to Python Tutorial
🔗

Share this topic with a friend

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

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

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

💻 लाइव कोड एडिटर

इस पेज के प्रोग्राम यहीं तैयार हैं — चलाएँ, बदलें और सीखें। कुछ भी इंस्टॉल किए बिना।
OneCompiler द्वारा संचालित। कोड एडिटर में अपने आप आ जाता है — Run दबाकर आउटपुट देखें। अगर एडिटर न खुले तो नए टैब में खोलें.