Mutable and Immutable Data Types in Python

Mutable and Immutable Data Types 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()फंक्शन अपनी पहचान का प्रतिनिधित्व करने वाला इंटीजर देता है।

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

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 :  1938161120
address of a is :  1938162720

‘is’operator – Evaluates to true if the variables are pointing to the same object and false otherwise.

यदि वैरियेबल एक ही वस्तु की ओर इशारा करते हैं और अन्यथा टू हैं तो फाल्स का मूल्यांकन करता है।

x =3
if(type(x) isint): 
    print("true") 
else: 
    print("false")

Mutability – अस्थिरता

Those are the kind of objects that can be changed in-place, without creating a new one to store the updated values. Mutable objects, on the other hand, are the following:

वे उस तरह के ऑब्जेक्ट हैं जिन्हें अद्यतन वैल्यू को संग्रहीत करने के लिए एक नया बनाये बिना बदला जा सकता है। दूसरी ओर, स्थिर ऑब्जेक्ट्स, निम्नलिखित हैं: –

list – लिस्ट
dictionary – डिक्शनरी
set – सेट
byte array – बाईट्रेरी

user defined classes – यूजर डिफाइंड क्लासेज Those are the kind of objects that can be changed in-place, without creating a new one to store the updated values.

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

For example: test.py

a= [1,2,3] 
print (a) 
print (id (a)) 
a [0] =100 
print (a) 
print (id(a))

Output:
[1, 2, 3]
61345576
[100, 2, 3]
61345576

Accepting input from Console
कंसोल से इनपुट स्वीकार करना

Console (also called Shell) is basically a command line interpreter that takes input from the user i.e. one command at a time and interprets it. If it is error free then it runs the command and gives required output otherwise shows the error message.

कंसोल (जिसे शेल भी कहा जाता है) मूल रूप से एक कमांड लाइन इंटरप्रेटर है जो उपयोगकर्ता से इनपुट लेता है यानी एक समय में एक कमांड देता है और उसकी व्याख्या करता है। यदि यह त्रुटि मुक्त है तो यह कमांड चलाता है और आवश्यक आउटपुट देता है अन्यथा एरर मैसेज दिखाता है।

The primary prompt of the python console is the three greater than symbols>>>

पायथन कंसोल का प्राथमिक संकेत प्रतीकों से तीन अधिक है>>>

Here we write command and to execute the command just press enter key and your command will be interpreted. To write the next command on the shell only when after executing the first command these prompts have appeared. The Python Console accepts command in Python which you write after the prompt.

यहां हम कमांड लिखते हैं और कमांड को निष्पादित करने के लिए बस एंटर कुंजी दबाएं और आपकी कमांड की व्याख्या की जाएगी। अगले कमांड को शेल पर लिखने के लिए केवल जब पहली कमांड निष्पादित करने के बाद ये संकेत दिखाई देते हैं। Python Console पायथन में कमांड को स्वीकार करता है जिसे आप प्रॉम्प्ट के बाद लिखते हैं।

Accepting Input from Console
कंसोल से इनपुट स्वीकार करना

User enters the values in the Console and that value is then used in the program as it was required. To take input from the user we make use of a built-in function input ().

यूजर कंसोल में वैल्यू में प्रवेश करता है और उस वैल्यू को प्रोग्राम में तब उपयोग किया जाता है जैसा कि आवश्यक था। यूजर से इनपुट लेने के लिए हम एक अंतर्निहित फंक्शन input()का उपयोग करते हैं।

input ():

input () function can be used to read data directly from the keyboard in the form of string type. We have to convert that string type to our required type by using type casting method.

input() फंक्शन को स्ट्रिंग टाईप के रूप में कीबोर्ड से सीधे डेटा पढ़ने के लिए उपयोग किया जा सकता है। हमें टाईप कास्टिंग मेथड का उपयोग करके उस स्ट्रिंग टाइप को अपने आवश्यक प्रकार में बदलना होगा।

Note:

  1. In Python 2, raw_input() and input () functions are available to read dynamic input from the key board.

पायथन 2 में, raw_input()और input()की बोर्ड से डायनामिक इनपुट पढ़ने के लिए फंक्शन उपलब्ध हैं।

  1. raw_input() reads the data from the keyboard in the form of string. We have to convert explicitly, but input() function can be used to read the data directly in our required format. PVM converts the data implicitly.

raw_input()स्ट्रिंग के रूप में कीबोर्ड से डेटा रीड करता है। हमें स्पष्ट रूप से परिवर्तित करना होगा, लेकिन input()फंक्शन का उपयोग डेटा को हमारे आवश्यक प्रारूप में सीधे रीड करने के लिए किया जा सकता है। पीवीएम डेटा को अंतर्निहित रूप से परिवर्तित करता है।

  1. In Python 3, to read dynamic input from the key board we have only one method i.e.,input().raw_input() function of Python 2.0 is renamed as input () function in Python 3.x

पायथन 3 में, की बोर्ड से डायनामिक इनपुट पढ़ने के लिए हमारे पास केवल एक तरीका है यानी, input.raw_input()पायथन 2.0 के फंक्शन का नाम पायथन 3.x में input() फंक्शन के रूप में बदला गया

How the input () function works in Python:
पायथन में input()फंक्शन कैसे काम करता है:

  • When input function executes program flow will be stopped until the user has given an input.

जब input()फंक्शन निष्पादित होता है, तो प्रोग्राम फ्लो को रोक दिया जाएगा जब तक कि यूजर ने इनपुट नहीं दिया हो।

Whatever you enter as input, input function convert it into a string. If you enter an integer value still input () function convert it into a string. You need to explicitly convert it into an integer in your code using typecasting.

आप जो भी इनपुट के रूप में दर्ज करते हैं, इनपुट फंक्शन उसे एक स्ट्रिंग में बदल देता है। यदि आप एक इंटीजर वैल्यू अभी भी दर्ज करते हैं input()फंक्शन इसे स्ट्रिंग में परिवर्तित करता है। आपको टाइपकास्टिंग का उपयोग करके इसे स्पष्ट रूप से अपने कोड को इंटीजर में बदलना होगा।

For example:

>>> a = input () 
20 
>>>a
'20'
>>> print (a) 
20 
>>> type (a) 
<class 'str'># type (a) is type of class 'str' 
# enter any float value 
>>> val = input ("Enter a float value :") 
Enter a float value : 44.34 
>>> type (val) 
<class 'str'>
# enter Boolean value 
>>> val = input ("Enter a Boolean value :")
Enter a Boolean value :44.34 
>>> type (val) 
<class 'str'>

Example 1: The following code takes two input() from console and typecasts them to integer then prints the sum.

निम्न कोड कंसोल से दो इनपुट) लेता है और उन्हें इंटीजर में टाइप करता है फिर योग को प्रिंट करता है।

#1. input
num1 = int (input ()) 
num2 = int (input()) 
# printing the sum in integer
print (num1 + num2) 
#2. we can write above code in single line
print (int (input ( ) ) + int (input ()))

Example 2: How to input multiple values from user in one line in Python?

पायथन में एक पंक्ति में यूजर से कई मान इनपुट कैसे करें?

x, y = input (), input () 

Example 3: Write a program which will accept principle amount, time and rate of interest. Calculate the simple interest& total amount.

एक प्रोग्राम लिखें जो इंट्रेस्ट की प्रिंसिपल एमाउंट, समय और दर को स्वीकार करेगा। सरल अंतर और कुल राशि की गणना करें।

simpleIntrest.py

p = float (input ("Enter Principle amount : "))
t = float (input ("Enter Time period : ")) 
r = float (input ("Enter Rate of intrest: ")) 
si = (p * t * r)/100 
totalAmount = p + si 
print ("*"*30) 
print ("Intrest on amount = ",si) 
print ("Total Amount = ", totalAmount) 
print("*"*30)

Output:
Enter Principle amount : 50000
Enter Time period : 12
Enter Rate of interest: 12
*****************************
Interest on amount = 72000.0
Total Amount = 122000.0
*****************************

Chapter wise Model Paper Link

About Me