🐍 Python में Data Types और Variables – आसान उदाहरण [2025 Updated]
Table of Contents
TogglePython में Data Types और Variables प्रोग्रामिंग की नींव हैं। किसी भी प्रोग्राम में डाटा को स्टोर और मैनेज करने के लिए यह जरूरी है कि आप Data Types को सही तरीके से समझें और Variables का सही इस्तेमाल करें। इस गाइड में हम आपको Python Data Types in Hindi को आसान भाषा और उदाहरणों के साथ समझाएंगे, ताकि आप Coding की शुरुआत मज़बूती से कर सकें।
📌 Data Type क्या है?
Data Type यह बताता है कि किसी Variable में किस प्रकार का डाटा स्टोर होगा जैसे – Number, Text, List, आदि।
📌 Variable क्या है?
Variable एक नामित स्थान है जहां हम डाटा को मेमोरी में स्टोर करते हैं ताकि उसे प्रोग्राम में इस्तेमाल किया जा सके।
📌 यह क्यों जरूरी है?
अगर आपको सही Data Type का पता होगा, तो आप Memory और Performance दोनों को Optimize कर पाएंगे।
🔤 Python के Core Data Types (Cheat Sheet + Examples)
Python में सबसे ज़्यादा इस्तेमाल होने वाले Data Types—int, float, bool, str, list, tuple, dict, set, NoneType—को एक जगह पर समझें। नीचे दी गई टेबल में Definition, Example, Common Use और Notes दिए गए हैं।
Type | Definition | Example | Common Use | Notes |
---|---|---|---|---|
int |
Whole numbers | age = 21 |
Counting, indexing | Unlimited precision |
float |
Decimal numbers | pi = 3.14 |
Measurements, averages | Floating-point rounding |
bool |
True/False | is_admin = True |
Conditions, flags | Subclass of int |
str |
Text | name = "Vista" |
Messages, parsing | Immutable, slicing |
list |
Ordered, mutable collection | scores = [89, 92, 95] |
Dynamic arrays | Append, pop, sort |
tuple |
Ordered, immutable | pt = (10, 20) |
Fixed records | Hashable if elements hashable |
dict |
Key–value store | user = {"id": 1, "name": "A"} |
Lookups, JSON-like data | Keys must be hashable |
set |
Unique, unordered | tags = {"ai","ml"} |
Membership, dedup | No duplicates |
NoneType |
Absence of value | x = None |
Defaults, optional | is None for checks |
is None
🧱 str/tuple immutable, list/dict mutable
# Python Data Types in Hindi — Quick Demo
age = 21 # int
price = 499.99 # float
is_student = True # bool
name = "Vista Academy" # str
scores = [88, 92, 95] # list
point = (10, 20) # tuple
user = {"id": 1, "name": "A"} # dict
tags = {"ai", "ml"} # set
x = None # NoneType
print(type(age), isinstance(age, int))
print(type(price), isinstance(price, float))
print(type(name), isinstance(name, str))
print(type(scores), isinstance(scores, list))
print(type(user), isinstance(user, dict))
print(x is None) # None check
# Dynamic typing: same variable, different types
value = 10 # int
value = "10" # now str
# Type casting
a = int("7") # '7' -> 7
b = float("7.5") # '7.5' -> 7.5
c = str(123) # 123 -> '123'
# Safe numeric add after cast
total = int("5") + 3
print(value, type(value))
print(a, b, c, total)
🧠 Variables: Naming Rules, Best Practices, Mutability & Memory Model
Python में Variable एक नामित संदर्भ है जो किसी object की ओर इशारा करता है।
सही naming, mutability की समझ और memory model (जैसे id()
, assignment, shallow/deep copy)
आपके कोड को साफ, तेज़ और bug-free बनाता है।
✅ Valid Naming Rules
- अक्षर/अंडरस्कोर से शुरू करें:
name
,_count
- अक्षर + संख्या + अंडरस्कोर:
roll_no_1
- Case-sensitive:
score
≠Score
- खाली जगह/हाइफ़न नहीं: ❌
total marks
, ❌total-marks
🚫 Reserved Keywords
जैसे for
, while
, class
, def
, True
, None
आदि नाम के रूप में इस्तेमाल न करें।
यदि संदेह हो तो: import keyword; keyword.kwlist
✨ Style & Best Practices
- snake_case:
total_marks
,max_score
- अर्थपूर्ण नाम:
tax_rate
बेहतर है बनिस्बतx
- Constants को UPPER_SNAKE_CASE:
PI = 3.14159
- एक अक्षर वाले नाम avoid (loop इटर में
i
/j
ठीक)
data1
, temp2
🧪 Test with pylint/flake8
# ❌ Bad
x = 18.0
y = 0.18
z = x * y
# ✅ Good
bill_amount = 18.0
tax_rate = 0.18
total_tax = bill_amount * tax_rate
print(total_tax)
🧩 Mutability & Memory (id, =, copy)
Python में variables objects को संदर्भित करते हैं। id()
object की identity दिखाता है।
Immutable (जैसे int
, str
, tuple
) बदलने पर नया object बनता है।
Mutable (जैसे list
, dict
) उसी object को बदलते हैं।
a = [1, 2, 3]
b = a # b और a एक ही list को point करते हैं
print(id(a), id(b)) # same ids
b.append(4)
print(a) # [1, 2, 3, 4] (a भी बदल गया)
import copy
nested = [[1, 2], [3, 4]]
shallow = copy.copy(nested) # या nested[:]
deep = copy.deepcopy(nested)
# अंदरूनी list mutate करें
nested[0].append(99)
print("nested:", nested) # [[1,2,99],[3,4]]
print("shallow:", shallow) # प्रभावित होगा (shallow shared inner)
print("deep:", deep) # प्रभावित नहीं होगा
⚠️ Mutable Default Arguments
फ़ंक्शन में list/dict
को default arg बनाने से state leak होती है।
def add_item(x, items=None):
if items is None:
items = []
items.append(x)
return items
♻️ Rebinding vs Mutating
# Rebinding (new object)
s = "Hi"; s = s + "!"
# Mutating (same object)
nums = [1,2]; nums.append(3)
Immutable = rebinding; Mutable = in-place change
🔒 Constants are Conventions
Python में real constants नहीं—UPPER_CASE सिर्फ convention है; बदल सकते हैं इसलिए सावधानी रखें।
🔁 Type Casting in Python (in Hindi) — Common Conversions & Safe Parsing
इस सेक्शन में हम type casting in Python in Hindi को आसान उदाहरणों से समझेंगे—जैसे
int()
, float()
, str()
, bool()
का इस्तेमाल कब और कैसे करें।
इससे आप Python data types in Hindi की समझ को practically apply कर पाएंगे।
From → To | Function | Works For | Notes |
---|---|---|---|
str → int | int("42") |
Pure digits | “42” ✅, “42.0” ❌ (ValueError) |
str → float | float("42.0") |
Digits + decimal | “NaN”/”inf” valid but rarely needed |
number → str | str(42) |
int/float/bool | Formatting के लिए f"{x:.2f}" बेहतर |
any → bool | bool(x) |
Truthiness rules | 0,"",[],{},None → False , बाकी → True |
def to_int(value, default=None):
try:
return int(value)
except (TypeError, ValueError):
return default
print(to_int("123")) # 123
print(to_int("12.3")) # None (safe)
print(to_int(None, 0)) # 0 (fallback)
a = "50"
b = int(a) # 50
c = float(a) # 50.0
s = str(3.14159) # '3.14159'
# Pretty formatting for UI:
price = 249.9
label = f"₹{price:.2f}" # '₹249.90'
print(b, c, s, label)
print(bool(0), bool(1)) # False True
print(bool(""), bool("text")) # False True
print(bool([]), bool([0])) # False True
print(bool(None)) # False
💡 Money/Precision? Use decimal.Decimal
Floating-point rounding से बचने के लिए Decimal
इस्तेमाल करें—billing, finance जैसे use-cases में perfect.
from decimal import Decimal, ROUND_HALF_UP
amount = Decimal("249.90")
tax = Decimal("0.18")
total = (amount * (Decimal("1") + tax)).quantize(Decimal("0.01"), rounding=ROUND_HALF_UP)
print(total) # 294.88
⚠️ “12.3” → int() Error
पहले float()
, फिर int()
(या regex/validation) का उपयोग करें।
🌐 Locale Decimal
कुछ देशों में comma decimal होता है ("12,5"
)—पहले replace करें: s.replace(",", ".")
.
🧪 Validate Before Cast
User input को पहले strip/isdigit/regex से validate करना बेहतर है।
🏆 Practice Time — Variables & Data Types in Python (in Hindi)
अब तक आपने Python data types in Hindi और variables in Python in Hindi को समझ लिया है। चलिए अब 5 real-world mini tasks से practice करते हैं। हर task के नीचे hint और solution toggle दिया गया है।
📌 Task 1: GST Bill Calculator
यूज़र से price और GST rate लें, final bill amount निकालें।
💡 Hint
Use float()
to convert input to numbers, then multiply.
✅ Solution
price = float(input("Enter price: "))
gst = float(input("Enter GST rate: "))
total = price * (1 + gst/100)
print("Final bill:", total)
📌 Task 2: Student Pass/Fail
Marks (0–100) लें, और pass/fail print करें।
💡 Hint
Use if
condition and bool()
if needed.
✅ Solution
marks = int(input("Enter marks: "))
if marks >= 33:
print("Pass")
else:
print("Fail")
📌 Task 3: Type Identifier
यूज़र से कोई भी input लें, उसका data type बताएं।
💡 Hint
Use type()
और eval()
carefully.
✅ Solution
val = input("Enter value: ")
try:
val_eval = eval(val)
print("Type:", type(val_eval))
except:
print("Type: str")
📌 Task 4: Temperature Converter
Celsius को Fahrenheit में convert करें।
💡 Hint
Formula: F = C × 9/5 + 32
✅ Solution
c = float(input("Enter temp in C: "))
f = c * 9/5 + 32
print(f"Celsius: {c} → Fahrenheit: {f}")
📌 Task 5: Word Count
एक sentence लें और total words count करें।
💡 Hint
Use .split()
method on string.
✅ Solution
sentence = input("Enter sentence: ")
words = sentence.split()
print("Word count:", len(words))
❓ Python Data Types और Variables — FAQ (2025 Updated)
Python में Data Types कितने प्रकार के होते हैं?
Python में कई built-in data types होते हैं, जैसे int
, float
, bool
, str
, list
, tuple
, dict
, set
और NoneType
।
Python में Variable क्या है?
Variable एक नामित reference है जो किसी object को point करता है और उसके data को store करता है।
Example: name = "Vista Academy"
Python में Type Casting क्या होती है?
Type casting का मतलब एक data type को दूसरे में बदलना है, जैसे int("42")
से string को integer में बदलना।
Functions: int()
, float()
, str()
, bool()
Mutable और Immutable data types में क्या फर्क है?
Mutable data types बदल सकते हैं (जैसे list
, dict
), जबकि Immutable data types बदलने पर नया object बनाते हैं (जैसे int
, str
, tuple
)।
Python में Variable naming के rules क्या हैं?
Variable नाम अक्षर या underscore से शुरू होना चाहिए, spaces और special characters नहीं होने चाहिए, और यह case-sensitive होते हैं।
Example: ✅ total_marks
, ❌ total marks