BCA / B.Tech 13 min read

Data Types in python in Hindi

RK

लेखक Ratan Kumawat · संस्थापक, NotesMedia

प्रकाशित:

त्वरित सारांश और मुख्य बिंदु

  • • विषय फोकस: Python Notes in Hindi | पायथन नोट्स हिंदी में (BCA / B.Tech Syllabus)
  • • मुख्य विषय: Data Types in python in Hindi - General Topics
  • • अवलोकन: Quick Answer: Python में Data Types यह बताते हैं कि किसी variable में किस तरह की value store है और उस पर कौन-से mathematical या logical operations किए ...
  • • मुख्य अवधारणाएं: Python data types, Int float str list Hindi, Tuple dict set bool,Python data types, Int float str list, Tuple dict set bool
Quick Answer: Python में Data Types यह बताते हैं कि किसी variable में किस तरह की value store है और उस पर कौन-से mathematical या logical operations किए जा सकते हैं। Python एक dynamically typed language है, इसलिए इसमें variable बनाते समय type declare नहीं करना पड़ता; interpreter runtime पर value देखकर खुद data type तय कर लेता है। Python के मुख्य built-in data types हैं: int, float, complex (Numeric), str, list, tuple (Sequence), dict (Mapping), set (Set types), और bool (Boolean)।

Data Types in Python in Hindi क्या हैं? (Data Type Meaning)

जब भी हम कोई program लिखते हैं, तो computer की memory में अलग-अलग तरह का data store करना पड़ता है। जैसे किसी student का roll number एक पूरा number (integer) होता है, fee percentage दशमलव (float) में होती है, student का नाम text (string) होता है, और पास या फेल होना हाँ या ना (boolean) होता है।

Computer memory को यह समझाने के लिए कि किसी value को कितनी memory देनी है और उस पर क्या काम हो सकता है, Data Type का इस्तेमाल होता है। सरल शब्दों में, data type किसी variable की value का रूप और स्वभाव तय करता है। Variables के basic rules आप यहाँ पढ़ सकते हैं: Variables in Python.

C, C++ या Java जैसी statically typed languages में variable बनाने से पहले उसका type लिखना ज़रूरी होता है (जैसे int x = 10;)। लेकिन Python एक dynamically typed language है। इसका मतलब है कि आप सीधे variable में value assign करते हैं, और Python interpreter value देखकर खुद समझ जाता है कि data type क्या है: Dynamic Typing in Python.

x = 25          # Python इसे अपने आप int मानेगा
price = 99.50   # Python इसे float मानेगा
name = "Ravi"   # Python इसे str मानेगा
Advertisement

Python में Data Type कैसे check करें? (type() और id() function)

Python में किसी भी variable का data type और उसकी memory location जानने के लिए दो built-in functions मिलते हैं:

  • type(): यह function बताता है कि variable किस class या data type का object है।
  • id(): यह function memory में उस object का unique identity number (address) लौटाता है।
age = 20
print(type(age))
print(id(age))

Output:

<class 'int'>
140718295627144

Python Built-in Data Types का Classification

Python में सब कुछ एक object होता है। Python standard library में कई built-in data types पहले से मौजूद हैं। नीचे दिए गए chart में इनका classification देखें:

              Python Data Types
                      │
     ┌────────────────┼────────────────┬──────────────┐
     ▼                ▼                ▼              ▼
  Numeric          Sequence         Mapping          Set
  ├── int          ├── str             └── dict       ├── set
  ├── float        ├── list                           └── frozenset
  └── complex      └── tuple
                      │
     ┌────────────────┴────────────────┐
     ▼                                 ▼
  Boolean                            None
     └── bool                           └── NoneType
Category Data Type Description Mutable या Immutable Example Syntax
Numeric int बिना दशमलव वाले पूरे numbers (positive, negative, zero) Immutable x = 100
Numeric float दशमलव (fractional) वाले real numbers Immutable pi = 3.1415
Numeric complex Real और imaginary part वाले numbers (a + bj) Immutable z = 2 + 3j
Sequence str Quotes के अंदर text या characters का group Immutable msg = "Hello"
Sequence list Ordered collection, अलग-अलग data types का group Mutable marks = [85, 90, 78]
Sequence tuple Ordered collection, read-only list की तरह Immutable point = (10, 20)
Mapping dict Key-value pairs का collection Mutable user = {"id": 1, "role": "admin"}
Set Types set Unique elements का unordered collection Mutable nums = {1, 2, 3, 4}
Boolean bool सत्य या असत्य मान (True या False) Immutable is_active = True
Binary NoneType Value की गैर-मौजूदगी (null value) Immutable result = None

Python के मुख्य Data Types detail में

1. Numeric Types: int, float, complex

Numbers को store करने के लिए Python में 3 numeric types होते हैं:

  • int (Integer): इसमें बिना दशमलव वाले पूरे numbers आते हैं। Python 3 में int की कोई fixed size limit नहीं होती; यह memory के हिसाब से कितना भी बड़ा number store कर सकता है।
  • float (Floating Point): इसमें point वाले fractional numbers आते हैं। Python internally C के double precision (64-bit IEEE 754) format का इस्तेमाल करता है। Scientific notation के लिए e या E का इस्तेमाल भी कर सकते हैं (जैसे 1.5e3 = 1500.0)।
  • complex (Complex Numbers): इसमें real part और imaginary part होता है। Python में imaginary part के साथ j लिखा जाता है, गणित के i की जगह।
a = 15          # int
b = 12.75       # float
c = 3 + 4j      # complex

print(a, type(a))
print(b, type(b))
print(c.real, c.imag, type(c))

Output:

15 <class 'int'>
12.75 <class 'float'>
3.0 4.0 <class 'complex'>

2. Sequence Types: str, list, tuple

Sequence types items के ordered collection को store करते हैं। इनमें items को index (0, 1, 2…) से access किया जाता है।

String (str)

Characters का ordered sequence string कहलाता है। इसे single quote ('...'), double quote ("...") या multi-line text के लिए triple quotes ('''...''') में लिखा जाता है। Python strings immutable होती हैं। String operations के detail नियम यहाँ पढ़ें: String in Python.

college = "MDSU"
print(college[0])     # first character: 'M'
print(college[-1])    # last character: 'U'
print(college[0:2])   # slicing: 'MD'

List

List एक mutable ordered sequence है जिसे square brackets [] में लिखा जाता है। List में अलग-अलग types के data एक साथ store किए जा सकते हैं, और बाद में values को जोड़ा, हटाया या बदला जा सकता है। List और tuple का पूरा comparison: List और Tuples.

my_list = [10, "Ajmer", 45.5, True]
my_list[1] = "Jaipur"   # list mutable है, इसलिए value बदल जाएगी
my_list.append(99)
print(my_list)

Output:

[10, 'Jaipur', 45.5, True, 99]

Tuple

Tuple भी list की तरह ordered sequence है, लेकिन यह immutable होता है। इसे parentheses () में लिखा जाता है। एक बार tuple बन जाने के बाद उसके elements बदले नहीं जा सकते। इसलिए tuple का इस्तेमाल read-only data सुरक्षित रखने के लिए होता है।

days = ("Mon", "Tue", "Wed")
print(days[0])
# days[0] = "Sun"  # ऐसा करने पर TypeError आएगा

3. Mapping Type: Dictionary (dict)

Dictionary unordered/insertion-ordered key-value pairs का collection है। इसे curly braces {} में लिखा जाता है। Key हमेशा immutable type (जैसे int, str, tuple) होनी चाहिए और unique होनी चाहिए, जबकि value किसी भी data type की हो सकती है।

student = {
    "roll_no": 101,
    "name": "Kavita",
    "course": "BCA",
    "marks": [80, 85, 92]
}
print(student["name"])
student["city"] = "Kota"   # नया key-value pair जोड़ा
print(student)

Output:

Kavita
{'roll_no': 101, 'name': 'Kavita', 'course': 'BCA', 'marks': [80, 85, 92], 'city': 'Kota'}

4. Set Types: set और frozenset

Set unique elements का unordered collection है। इसमें duplicate values allow नहीं होतीं। इसे curly braces {} या set() constructor से बनाया जाता है।

  • set: Mutable होता है; इसमें नए elements add या remove किए जा सकते हैं।
  • frozenset: Immutable set है; बनने के बाद बदला नहीं जा सकता।
numbers = {1, 2, 2, 3, 4, 4, 5}
print(numbers)   # Duplicates अपने आप हट जाएँगे

Output:

{1, 2, 3, 4, 5}

5. Boolean (bool) और None Type

  • bool: इसके केवल दो values होते हैं: True और False। Python में bool class internally int का sub-class है, जहाँ True की value 1 और False की value 0 मानी जाती है।
  • NoneType: इसका एकमात्र object None है। यह किसी value की अनुपस्थिति (absence of value) दर्शाता है। Functions जो कुछ return नहीं करते, वे default रूप से None return करते हैं।

Mutable and Immutable in Python in Hindi (मुख्य अंतर)

Python data types को memory व्यवहार के आधार पर दो मुख्य श्रेणियों में बाँटा जाता है: Mutable और Immutable। यह अंतर BCA और university exams में सबसे ज़्यादा पूछा जाने वाला topic है।

  • Mutable Objects: वे objects जिनकी value बनने के बाद उसी memory address पर सीधे बदली जा सकती है।
  • Immutable Objects: वे objects जिनकी value एक बार बन जाने के बाद बदली नहीं जा सकती। अगर आप variable की value बदलते हैं, तो पुराना object नहीं बदलता बल्कि memory में एक नया object बनता है।
Data Types in python  in Hindi

Mutable और Immutable data types में memory और value का अंतर

Memory Trace से समझें: id() का खेल

नीचे दिए गए code से साफ़ समझ आता है कि immutable data type में value बदलने पर memory address (id) बदल जाता है, जबकि mutable object में memory address वही रहता है:

# Case 1: Immutable (Integer)
a = 10
print("Original id of a:", id(a))
a = a + 1
print("New id of a:", id(a))   # id बदल जाएगी!

# Case 2: Mutable (List)
nums = [1, 2, 3]
print("Original id of nums:", id(nums))
nums.append(4)
print("New id of nums:", id(nums))   # id वही रहेगी!

Output:

Original id of a: 140718295626824
New id of a: 140718295626856
Original id of nums: 2268710343232
New id of nums: 2268710343232
Advertisement

Mutable vs Immutable Comparison Table

Feature Mutable Data Types Immutable Data Types
Value Modification उसी memory location पर value बदल सकते हैं Value बदली नहीं जा सकती; बदलने पर नया object बनता है
Memory Address (id) Modify करने के बाद भी id() समान रहती है Modification करने पर नई id() generate होती है
Python Types list, dict, set, bytearray int, float, complex, str, tuple, frozenset, bool
Memory Overhead बार-बार नए objects नहीं बनते, memory बचती है हर बदलाव पर नया object बनता है, extra memory लग सकती है
Execution Speed In-place modification तेज़ होता है New object creation की वजह से बड़े operations में थोड़ा धीमा
Thread Safety कम सुरक्षित; multiple threads एक साथ बदलें तो bug आ सकता है पूरी तरह सुरक्षित; value बदल नहीं सकती इसलिए race condition नहीं होती
Dictionary Keys Dict की key के रूप में use नहीं हो सकते (unhashable) Dict की key और Set के element आसानी से बन सकते हैं (hashable)

Solved Example: Type Checking और Type Conversion

नीचे एक practical program दिया गया है जो input data का type check करता है और manual conversion (type casting) करके real calculation दिखाता है:

# User से input लेना (input() हमेशा string लौटाता है)
marks_str = "85"
grace_str = "5"

print("Before conversion:")
print("marks_str type:", type(marks_str))

# Type Conversion (Type Casting)
marks = int(marks_str)
grace = int(grace_str)
total = marks + grace

# Float conversion
percentage = float(total) / 100 * 100

print("\nAfter conversion:")
print("marks type:", type(marks))
print("total:", total, "type:", type(total))
print("percentage:", percentage, "type:", type(percentage))

# Tuple to List conversion
fixed_coords = (10, 20)
editable_coords = list(fixed_coords)
editable_coords.append(30)
print("\nConverted list:", editable_coords, "type:", type(editable_coords))

Output:

Before conversion:
marks_str type: <class 'str'>

After conversion:
marks type: <class 'int'>
total: 90 type: <class 'int'>
percentage: 90.0 type: <class 'float'>

Converted list: [10, 20, 30] type: <class 'list'>

Exam में कैसे लिखें (5 marks)

University exam (BCA/B.Tech) में जब "Explain Data Types in Python" सवाल 5 marks में आए, तो पूरे marks पाने के लिए point-to-point इस तरह लिखें:

  1. Definition (1 mark): Data type बताता है कि variable किस प्रकार का data store करेगा और उस पर कौन-से operations मान्य हैं। Python dynamically typed language है।
  2. Type Checking Syntax: type(variable_name) function से data type पता किया जाता है।
  3. Categories और नाम (1 mark):
    • Numeric: int, float, complex
    • Sequence: str, list, tuple
    • Mapping: dict
    • Set: set, frozenset
    • Boolean: bool (True/False)
  4. Code Example (1 mark):
    x = 10          # int
    name = "BCA"    # str
    arr = [1, 2]    # list
  5. Mutable vs Immutable (1 mark): List, dict, set mutable हैं (value बदल सकती है)। Int, float, str, tuple immutable हैं (value नहीं बदल सकती)।
  6. Memory Concept (1 mark): Python में सब data types objects होते हैं, जिनका memory address id() function से देखा जाता है।

Common Mistakes जो Students करते हैं

  1. Single Element Tuple की गलती: Student लिखते हैं t = (5) और सोचते हैं कि यह tuple बन गया। Python इसे simple integer मानता है। 1 element वाला tuple बनाने के लिए comma लगाना ज़रूरी है: t = (5,)।
  2. String को direct modify करना: Strings immutable होती हैं। अगर आप s = "hello"; s[0] = 'H' लिखेंगे, तो Python TypeError: 'str' object does not support item assignment देगा। नया string बनाने के लिए slicing या replacement function इस्तेमाल करना पड़ता है।
  3. List को Dictionary Key बनाना: Dictionary key हमेशा immutable होनी चाहिए। अगर आप list को key बनाएँगे (जैसे {[1, 2]: "val"}), तो Python TypeError: unhashable type: 'list' throw करेगा। Key के लिए tuple का इस्तेमाल करें।
  4. Input Function का return type भूलना: input() function हमेशा string data type लौटाता है। अगर user से 2 numbers लेकर जोड़ना हो और type casting (int()) न करें, तो "10" + "20" का result "1020" आएगा, 30 नहीं।

Exam के लिए Important Questions

Short Questions (2–3 marks)

  1. Python में Data Type क्या है? Built-in data types के नाम लिखिए। (Data type variable की value का रूप तय करता है; int, float, str, list, tuple, dict, set आदि)
  2. Python dynamically typed language क्यों कहलाती है? (क्योंकि इसमें variable declare करते समय type नहीं लिखना पड़ता, interpreter runtime पर तय करता है)
  3. Mutable और Immutable data types में क्या मुख्य अंतर है? (Mutable की value उसी address पर बदल सकती है; Immutable की value बदलने पर नया object बनता है)
  4. Python में single item tuple कैसे बनाते हैं? (Element के बाद comma लगाकर, जैसे t = (10,))
  5. type() और id() functions का क्या काम है? (type() class type बताता है और id() memory identity number देता है)

Long Questions (5–10 marks)

  1. Python में Data Types क्या हैं? सभी standard built-in data types को code examples और output के साथ detail में समझाइए।
  2. Mutable और Immutable data types से आप क्या समझते हैं? Memory allocation और id() function के साथ दोनों का तुलनात्मक विश्लेषण कीजिए।
  3. Sequence data types (String, List, Tuple) में क्या समानताएं और अंतर हैं? इन तीनों के practical use cases उदाहरण देकर स्पष्ट कीजिए।

FAQs

Data types in Python in Hindi क्या होते हैं?

Python में data types यह तय करते हैं कि variable में किस format का data store है (जैसे number, text या collection) और computer memory उस पर कौन-से operations करने की अनुमति देती है। Python value के अनुसार खुद data type पहचान लेता है।

Python में data type declare क्यों नहीं करना पड़ता?

Python dynamically typed language है। जब code run होता है, तो Python interpreter variable में assign की गई value देखकर runtime पर उसका object type तय कर लेता है। इसलिए programmer को int a; जैसा static declaration नहीं लिखना पड़ता।

Python में list और tuple में क्या अंतर है?

List mutable होती है (square brackets [] में लिखी जाती है) और इसके elements को बदला या बढ़ाया जा सकता है। Tuple immutable होता है (parentheses () में लिखा जाता है) और बनने के बाद इसमें कोई बदलाव नहीं किया जा सकता। Tuple, list से fast और memory-efficient होता है।

क्या Python में float की size limit होती है?

Python standard implementation (CPython) में float C language के double data type के बराबर होता है, जो 64-bit IEEE 754 standard follow करता है। इसकी maximum value लगभग 1.79e+308 तक होती है।

Python में String immutable क्यों है?

String immutable होने से program में security, thread safety और performance बेहतर होती है। एक ही string value को memory में कई variables बिना copy किए share कर सकते हैं (string interning), और strings को dictionary keys की तरह safely use किया जा सकता है।

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

Quick Answer: Python में Data Types यह बताते हैं कि किसी variable में किस तरह की value store है और उस पर कौन-से mathematical या logical operations किए जा सकते हैं।
Python Notes in Hindi | पायथन नोट्स हिंदी में के पाठ्यक्रम में Data Types in python in Hindi एक महत्वपूर्ण अध्याय है। यह मुख्य सिद्धांतों, परीक्षा प्रश्नों और सूत्रों को समझने के लिए अत्यधिक आवश्यक है।
हाँ, NotesMedia पर आपको Data Types in python in Hindi के महत्वपूर्ण प्रश्नोत्तर, हल किए गए उदाहरण और नोट्स बिल्कुल मुफ्त मिलेंगे। अधिक अभ्यास के लिए आप हमारा ऐप भी डाउनलोड कर सकते हैं।
Advertisement