What is mutable and immutable in Python

What is mutable and immutable in Python

In a Python All the data is represented by objects. Every object has an identity, a type, and a value.

पायथन में सभी डेटा ऑब्जेक्ट्स द्वारा दर्शाया जाता है। प्रत्येक ऑब्जेक्ट की पहचान, एक टाईप और एक वैल्यू होता है।

Identity: An object’s identity never changes once it has been created; you may think of it as the object’s address in memory. The is operator compares the identity of two objects; the id() function returns an integer representing its identity.

पहचानः एक ऑब्जेक्ट की पहचान एक बार बनने के बाद कभी नहीं बदलती है, आप इसे मेमोरी में ऑब्जेक्ट के पते के रूप में सोच सकते हैं। ऑपरेटर दो ऑब्जेक्ट की पहचान की तुलना करता है, id() फंक्शन अपनी पहचान का

प्रतिनिधित्व करने वाला इंटीजर देता है।

Type: An object’s type defines the possible values that type supports. The type() function returns the type of an object. An object type is unchangeable like the identity.

टाईपः एक ऑब्जेक्ट का प्रकार उन संभावित वैल्यू को परिभाषित करता है जो टाइप करते हैं। टाईप() फंक्शन किसी ऑब्जेक्ट का टाईप रिटर्न करता है। एक ऑब्जेक्ट टाईप पहचान की तरह अपरिवर्तनीय है।

Value: The value of some objects can change. Objects whose value can change are said to be mutable; objects whose value is unchangeable once they are created are called immutable.

वैल्यू: कुछ ऑब्जेक्ट्स का वैल्यू बदल सकता है। जिन ऑब्जेक्ट्स के वैल्यू में परिवर्तन हो सकता है, उन्हें परस्पर कहा जा सकता है, जिन ऑब्जेक्ट का वैल्यू एक बार बनाए जाने के बाद अपरिवर्तनीय होता है, उन्हें अपरिवर्तनीय कहा जाता है।

Example

>>> print (100)
100

When presented with the statement print(100), the interpreter does the following:

स्टेटमेंट print(100) के साथ प्रेजेन्ट किए जाने पर, इंटरप्रेटर निम्न कार्य करता है:

– Creates an integer object – एक इंटीजर ऑब्जेक्ट बनाता है।

– Gives it the value 100 – इसे 100 की वैल्यू देता है।

– Displays it to the console – यह कंसोल को दिखाता है

You can see that an integer object is created using the built-in type() function:

आप देख सकते हैं कि एक इंटीजर ऑब्जेक्ट बिल्ट-इन टाईप) फंक्शन का उपयोग करके बनाया गया है:

>>> type (100)
<class ‘int’>

In Python, variable is a symbolic name that is a reference to an object. Once an object is assigned to a variable, you can refer to the object by that name. But the data itself is still contained within the object.

पायथन में, वैरियेबल एक सिम्बॉलिक नाम है जो एक ऑब्जेक्ट का संदर्भ है। एक बार जब किसी ऑब्जेक्ट को एक वैरियेबल को सौंपा जाता है, तो आप उस नाम से ऑब्जेक्ट को संदर्भित कर सकते हैं। लेकिन डेटा अभी भी ऑब्जेक्ट के भीतर समाहित है।

For example:

>>> x = 10

This assignment creates an integer object with the value 10 and assigns the variable ‘x’ to reference to that object..

यह असाइनमेंट बनाता है वैल्यू 10 के साथ एक इंटीजर ऑब्जेक्ट और उस ऑब्जेक्ट के रिफरेन्स में वैरियेबल ‘x’ असाइन करता है। The following code shows that ‘x’ reference to an integer class object: निम्न कोड दिखाता है कि इंटीजर क्लास ऑब्जेक्ट के लिए ‘x’ रिफरेन्सः

>>> x = 10
>>> type (x)
<class ‘int’>
>>> id (x)
1929771072

Suppose if you change ‘x’ value to 100, like

मान लीजिए यदि आप ‘x’ वैल्यू को 100 से बदलते हैं, जैसे

>>> x = 100
>>> id (x)
1929772512

Now Python creates a new integer object with the value 100, and ‘x’ becomes a reference to it.

अब पायथन 100 वैल्यू के साथ एक नया इंटीजर ऑब्जेक्ट बनाता है, और ‘x’ इसका एक रिफरेन्स बन जाता है।

There is no longer any reference to the integer object 10. If there is no reference to an object automatically Garbage Collector will remove the object and there is no way to access it. An object stays alive at least one reference to it.

इंटीजर ऑब्जेक्ट के लिए अब कोई संदर्भ नहीं है। यदि किसी ऑब्जेक्ट का कोई संदर्भ स्वचालित रूप से नहीं है, तो गारबेज कलेक्टर ऑब्जेक्ट को हटा देगा और इसे एक्सेस करने का कोई तरीका नहीं है। एक ऑब्जेक्ट कम से कम एक रिफरेन्स में जीवित रहती है।

Question: What is immutability?
अपरिवर्तनीयता क्या है?

Answer: Immutability means “once we create an object, we can’t perform any changes in that object”. If we are going to do any changes in the object; with those changes a new object will be created. This non-changeable behavior is called as immutability.

उत्तरः अपरिवर्तनशीलता का अर्थ है “एक बार जब हम एक ऑब्जेक्ट बनाते हैं, तो हम उस ऑब्जेक्ट में कोई परिवर्तन नहीं कर सकते हैं।” यदि हम ऑब्जेक्ट में कोई परिवर्तन करने जा रहे हैं, उन परिवर्तनों के साथ एक नया ऑब्जेक्ट बनाया जाएगा। इस गैर-परिवर्तनशील व्यवहार को अपरिवर्तनशीलता कहा जाता है। Let’s see how fundamental data types are immutability. आइए देखें कि मूलभूत डेटा टाईप अपरिवर्तनीय कैसे हैं।

For example: test.py

a = 100
print(“address of a is : “, id(a))
a = 100 + 100
print (“address of a is : “, id (a))

Output:
address of a is :  1929772512
address of a is :  1929774112

When presented with the above 4 line code, the interpreter does the following:

जब उपरोक्त 4 लाइन कोड के साथ प्रस्तुत किया जाता है, तो इंटरप्रेटर निम्न कार्य करता है:

First line: Creates an integer object with the value 100 and assigns the variable ‘a’ to reference to that object.

पहली पंक्तिः वैल्यू 100 के साथ एक इंटीजर ऑब्जेक्ट बनाता है और उस ऑब्जेक्ट के रिफरेन्स में वैरियेबल ‘a’ प्रदान करता है।

Second line: Displays address of ‘a’ to the console.

दूसरी पंक्तिः कंसोल के लिए ‘a’ का पता प्रदर्शित करता है।

Third line: If we are going to do any changes in the object (a=100+100); with those changes a new object will be created with new reference and that reference assigns to the variable ‘a’. If any object not having any reference, such type of useless objects are deleted by Garbage Collector (first object deleted by Garbage collector).

तीसरी पंक्तिः यदि हम ऑब्जेक्ट में परिवर्तन करने जा रहे हैं (a=100+100), उन परिवर्तनों के साथ एक नये ऑब्जेक्ट के साथ बनाई जाएगी और वह रिफरेन्स वैरियेबल ‘a’ पर असाइन होता है। यदि किसी भी ऑब्जेक्ट का कोई रिफरेन्स नहीं है, तो इस तरह की बेकार वस्तुएं गारबेज कलेक्टर (गारबेज कलेक्टर द्वारा हटाए गए पहले ऑब्जेक्ट) द्वारा हटा दी जाती हैं।

Fourth line: This statement shows a new address of variable ‘a’.

चौथी पंक्तिः यह कथन वैरियेबल ‘a’ का एक नया पता दिखाता है।

Output:
a value is : 100 id of a is : 1555131024
a value is : 200 id of a is : 1555131000

Here, address of the first object is different from address of the second object. So, from this example, we are getting much clarity about immutability. If we are going to do any changes in the object; with those changes a new object will be created. This non-changeable behavior is called as immutability.

यहाँ, पहले ऑब्जेक्ट का पता दूसरे ऑब्जेक्ट के पते से अलग है। इसलिए, इस उदाहरण से, हम अपरिवर्तनीयता के बारे में बहुत स्पष्टता प्राप्त कर रहे हैं। यदि हम ऑब्जेक्ट में कोई परिवर्तन करने जा रहे हैं, उन परिवर्तनों के साथ एक नया ऑब्जेक्ट बनाया जाएगा। इस गैर-परिवर्तनशील व्यवहार को अपरिवर्तनशीलता कहा जाता है।

Let me take another example for better clarity.

बेहतर स्पष्टता के लिए एक और उदाहरण लेता हूं।

x = 20
y = x
print (x,y)
print (id (x))
print (id(y))
y = 100
print (x,y)
print (id (x))
print (id(y))

Output:
20 20
1929771232
1929771232
20 100
1929771232
1929772512

In Python, every object that is created is given a number that uniquely identifies it. It is guaranteed that no two objects will have the same identifier during any period in which their lifetimes overlap. If any object not having any reference, such type of useless objects are deleted by Garbage Collector.

पायथन में, बनाये गये प्रत्येक ऑब्जेक्ट को एक संख्या दी जाती है जो विशिष्ट रूप से इसे पहचानती है। यह गारंटी दी| जाती है कि किसी भी दो ऑब्जेक्ट्स की किसी भी अवधि के दौरान एक ही पहचानकर्ता नहीं होगा जिसमें उनका जीवनकाल ओवरलैप होता है। यदि किसी भी ऑब्जेक्ट का कोई संदर्भ नहीं है, तो इस तरह की बेकार वस्तुएं गारबेज कलेक्टर द्वारा हटा  दी जाती हैं।

Finding the maximum – अधिकतम ढूँढ़ना

Finding the maximum, we use the python inbuilt max() functions to perform the task of getting the maximum value of a particular element position. Each key of the Dictionary may contain the elements with the numeric values. The elements are not arranged in sequence to find the maximum value. However, you can easily find the key which contains the maximum value.

अधिकतम ढूँढ़ना, हम एक विशेष एलीमेंट पोजीशन का अधिकतम वैल्यू प्राप्त करने के कार्य को करने के लिए पायथन इनबिल्ट मैक्सिमम कार्यों का उपयोग करते हैं। डिक्शनरी की प्रत्येक ‘की’ में संख्यात्मक मान वाले एलीमेंट शामिल हो सकते हैं। अधिकतम मान ज्ञात करने के लिए एलीमेंट को क्रम में व्यवस्थित नहीं किया गया है। हालांकि, आप आसानी से उस कुंजी को पा सकते हैं जिसमें अधिकतम मूल्य शामिल है।

Example:

list = [ (2, 3), (4, 7), (8, 11), (3, 6)]
list = [ (2, 3, 8), (4, 7, 1), (8, 11, 12), (3, 6, 8)]
print (“The list is : ” + str (list))
res1 = max (list) [0]
res2 = max (list) [1]
res3 = max (list) [2]
print (“The max of index 1 : ” + str (res1))
print (“The max of index 2 : ” + str (res2))
print (“The max of index 3 : ” + str (res3))

Output:
The list is : [(2, 3, 8), (4, 7, 1), (8, 11, 12), (3, 6, 8)]
The max of index 1 : 8
The max of index 2 : 11
The max of index 3 : 12

Finding the minimum – न्यूनतम ढूँढ़ना

Finding the minimum, we use the python inbuilt min() functions to perform the task of getting the minimum value of a particular element position.

न्यूनतम ढूँढना, हम एक विशेष एलीमेंट पोजीशन के अनुकूलतम वैल्यू प्राप्त करने के कार्य को करने के लिए पायथन इनबिल्ट मिनट कार्यों का उपयोग करते हैं।

Example:

list = [(2, 3), (4, 7), (8, 11), (3, 6)]
print (“The list is : ” + str(list))
res1 = min (list) [0]
res2 = min (list) [1]
print (“The min of index 1 : ” + str (res1))
print (“The min of index 2 : ” + str (res2))

Output:

The list is : [(2, 3), (4, 7), (8, 11), (3, 6)]
The min of index 1 : 2
The min of index 2 : 3

Finding the mean – माध्य खोजना

The mean() is an inbuilt function that used to calculate average of numbers and list. The mean() function can be used to calculate the mean/average of the given list of numbers. It returns the mean of the data set passed as parameters. The given data will always be in the form of a sequence or iterator such as list, tuple, etc.

मीन() एक इनबिल्ट फंक्शन है जो संख्याओं और सूची के औसत की गणना करने के लिए उपयोग किया जाता है। mean() फंक्शन का उपयोग संख्याओं की दी गई लिस्ट के माध्य/औसत की गणना करने के लिए किया जाता है। यह पैरामीटर के रूप में पारित डेटा सेट का मतलब देता है। दिया गया डेटा हमेशा एक अनुक्रम या पुनरावृत्त के रूप में होंगे जैसे लिस्ट, टपल, आदि।

Syntax: mean ( [data-set] )

Parameters: पैरामीटर्स

[data-set]: List or tuple of a set of numbers.

संख्याओं के समूह की लिस्ट या टपल

Returns: Sample arithmetic mean of the provided data-set.

प्रदान किए गए डेटा-सेट का नमूना अंकगणितीय माध्य

Example:

# Python program to demonstrate mean() function from the statistics module
# Importing the statistics module
import statistics
list = [2, 3, 4, 5]
Mean = statistics.mean (list)
print (“Mean is :”, Mean)

Output:
Mean is : 3.5

Linear search on list/tuple of numbers:

संख्या की लिस्ट/टपल पर लिनियर सर्च A simple approach is to do linear search (list/tuple): एक सरल दृष्टिकोण है लिनियर सर्च (लिस्ट/टपल) करनाः

Example : Linear Search on List – लिस्ट पर लिनियर सर्च

def check (list,n): 
    for i in range (len (list)): 
        if list[i] == n:
             return True 
    return False
# list which contains both string and numbers. 
list = [10, 20, 30, 40, 50, 60] 
n = int (input ("Enter a number : "))
if check (list, n):
    print("Found") 
else:
    print("Not Found") 

Output:
Enter a number : 2
Not Found
#again run the program with different value.
# अलग-अलग वैल्यू के साथ प्रोग्राम चलाएं
Enter a number : 20
Found

Counting the frequency of elements in a list using a dictionary:

एक डिक्शनरी का उपयोग करके लिस्ट में एलीमेंट की आवृत्ति की गिनती:

It is a process to develop a program to calculate the frequency of each element present in a list.

यह एक लिस्ट में मौजूद प्रत्येक एलीमेंट की आवृत्ति की गणना करने के लिए एक कार्यक्रम विकसित करने की प्रक्रिया है।

Example 1:

def cnt(myList):
        freq={} 
        for i in myList: 
            if (i in freq):
                freq[i] += 1 
            else:
                freq[i] = 1
        for key, value in freq.items():
            print ("%d : % d"% (key, value))
myList =[5, 1, 3, 5, 1, 3, 1, 3, 4, 1, 5, 4, 3, 2, 1, 2, 4]
cnt(myList)

Output:
5 :  3
1 :  5
3 :  4
4 :  3
2 :  2

Example 2: Above program using count () method.

count () मेथड का उपयोग करके उपरोक्त प्रोग्राम ।

def count (myList):
    frequency = {}
    for i in myList:
        frequency[i] = myList.count (i)
    for key, value in frequency.items () :
        print ("% d : %d"% (key, value))
myList = [5, 1, 3, 5, 1, 3, 2, 3, 4, 1, 5, 4, 4, 2, 1, 2, 4]
count (myList)

Chapter wise Model Paper Link

About Me