Important Notice:

Concept of Mutability

Concept of Mutability

1 views 1 min read

Concept of Mutability (म्यूटेबिलिटी की अवधारणा) :-

Python में Mutability का अर्थ है कि किसी Object को बनाने के बाद उसकी existing value/content को उसी Object में बदला जा सकता है या नहीं।

English

Mutability refers to whether the contents of an Object can be changed after the Object has been created, without creating a new Object.

Python में Data Types को मुख्य रूप से दो categories में समझा जाता है:

  1. Mutable Data Types –

    ऐसे Data Types जिनके contents को Object बनने के बाद modify किया जा सकता है, उन्हें Mutable Data Types कहते हैं।

    English

    Data Types whose contents can be modified after the Object is created are called Mutable Data Types.

    Python के प्रमुख Mutable Data Types हैं:

    • List
    • Dictionary
    • Set
  2. Immutable Data Types – 

    ऐसे Data Types जिनके Object बनने के बाद उनके existing contents को उसी Object में modify नहीं किया जा सकता, उन्हें Immutable Data Types कहते हैं।

    English

    Data Types whose existing contents cannot be modified after the Object is created are called Immutable Data Types.

    Python के प्रमुख Immutable Data Types हैं:

    • Integer (int)
    • Float (float)
    • Complex (complex)
    • String (str)
    • Tuple (tuple)
    • Boolean (bool)

Example -

 
numbers = [10, 20, 30]

numbers[0] = 100

print(numbers)
 

Output-

 
[100, 20, 30]
 

यहाँ List की existing value 10 को 100 से बदल दिया गया। इसलिए List Mutable Data Type है।

Immutable Example :-

x = (10, 20, 30)   

x[1] = 200       #Tuple के existing element को इस तरह change नहीं कर सकते:

इससे TypeError आएगा।

इसलिए:Tuple → Immutable

Related Notes