Before knowing which types are immutable in Python, we have to know first of all, what is immutability? The basic meaning is that “cannot be changed or modified”. So in Python also we have that kind of data types in which elements cannot be changed once they are created. What are they? The answer is Tuples and Frozen sets. Become a Data Scientist with 360DigiTMG Data Science course in Hyderabad. Get trained by the alumni from IIT, IIM, and ISB. To understand mutable and immutable data let’s have a simple example: A=55; B=id(A)  #140725046277568 Here A  stores a value of 55 and B is storing the memory location of A. Now, we do this: A=40 B=id(A) What will be the values of A and B now?  A is 40 and B is the same old 140725046277568. So one can quickly notice that A is changed from  55 to 40 But B which is a memory location that is not changed. Here we can say A is mutable and B is immutable.         Well, now we discuss the immutable data types in Python. To have better  Understanding, We compare them with the data types which are mutable. Before all it begins, Are you looking to become a Data Scientist? Go through 360DigiTMG’s PG Diploma in Data Science and Artificial Intelligence! Let’s have a look, what are the data types that are available in Python:
  • Lists
  • Tuples
  • Sets
  • Dictionaries
  • Frozen sets
Now we compare List and tuple and understand what does immutable data type means actually. Create a list L and Tuple T: L=[1,2,3,4]  # square braces [] will be used to create a list in Python. T=(5,6,7,8) # in Python, Parenthesis () will be used to create a tuple. See, the list L is having 4 elements and Tuple T is also created with 4 elements. In Python, we do have a slicing concept to access individual elements of the data  structure. Example: L[1]—— will give the element at index=1 (In Python default index starts from 0) L[1] is 2  T[0:3]—- will give elements from index=0 upto index=2 (3 is exclusive) T[0:3] == T[0],T[1],T[2]— (5,6,7) Now we will try to carry out some code runs in Python3: L[1]=10 Print(L)—– [1,10,3,4] T[1]=10 Print(T) Can you guess the output? ————————————————————————— TypeError                                 Traceback (most recent call last) in () —-> 1 T[1]=10       2 print(T) TypeError: ‘tuple’ object does not support item assignment You see, Python errors are very much interpretable. It clearly shows where the error occurs. We have the list updated to [1,10,3,4], but the same thing is not working with Tuples. Want to learn more about data science? Enroll in the Best Data Science courses in Chennai to do so. We can change the second element (index=1) of List L From 2 to 10, but we were not able to change the element of Tuple from 6 to 10. This is what immutability means. This is why tuples are called immutable data types. Following works fine with mutable data types :
  1. Adding element to the existing data structure: 
L=[1,2,3];  L.append(4)  print(L) —- [1,2,3,4]
  1. Removing an element from the existing data structure:
L=[1,2,3,4,]     L.remove(3) Print(L) —– [1,2,4]
  1. Modifying the existing elements:
L=[2,3,4] L[2]=5 Print(L) ——[2,3,5] But the above-mentioned operations won’t work out for immutable data types. When trying to add/remove/modify the elements, immediately Python throws an error. Earn yourself a promising career in data science by enrolling in the Data Science Classes in Pune offered by 360DigiTMG. Another immutable data type is frozenset and is created by using  a keyword frozenset() F1=frozenset([1,2,3]) Print(F1)—- frozenset({1,2,3}) As said earlier, it is also reluctant to add/remove/modify operations like tuples. P.S: We cannot add elements to a tuple, but we concatenate 2 different tuples. T1=(1,2,3) T2=(4,5) T1=T1+T2 Print(T1)—- (1,2,3,4,5) We cannot remove an element from the tuple, but we can delete the entire tuple at once. T1.remove(5) – throws an error Del T1—Works fine. Finally, we got an idea of what is immutability and which types are immutable in Python. Also, check this Data Science Institute in Bangalore to start a career in Data Science.

Data Science Training Institutes in Other Locations

Tirunelveli, Kothrud, Ahmedabad, Hebbal, Chengalpattu, Borivali, Udaipur, Trichur, Tiruchchirappalli, Srinagar, Ludhiana, Shimoga, Shimla, Siliguri, Rourkela, Roorkee, Pondicherry, Rajkot, Ranchi, Rohtak, Pimpri, Moradabad, Mohali, Meerut, Madurai, Kolhapur, Khammam, Jodhpur, Jamshedpur, Jammu, Jalandhar, Jabalpur, Gandhinagar, Ghaziabad, Gorakhpur, Gwalior, Ernakulam, Erode, Durgapur, Dombivli, Dehradun, Cochin, Bhubaneswar, Bhopal, Anantapur, Anand, Amritsar, Agra , Kharadi, Calicut, Yelahanka, Salem, Thane, Andhra Pradesh, Greater Warangal, Kompally, Mumbai, Anna Nagar, ECIL, Guduvanchery, Kalaburagi, Porur, Chromepet, Kochi, Kolkata, Indore, Navi Mumbai, Raipur, Coimbatore, Bhilai, Dilsukhnagar, Thoraipakkam, Uppal, Vijayawada, Vizag, Gurgaon, Bangalore, Surat, Kanpur, Chennai, Aurangabad, Hoodi,Noida, Trichy, Mangalore, Mysore, Delhi NCR, Chandigarh, Guwahati, Guntur, Varanasi, Faridabad, Thiruvananthapuram, Nashik, Patna, Lucknow, Nagpur, Vadodara, Jaipur, Hyderabad, Pune, Kalyan.

Data Analyst Courses In Other Locations

Tirunelveli, Kothrud, Ahmedabad, Chengalpattu, Borivali, Udaipur, Trichur, Tiruchchirappalli, Srinagar, Ludhiana, Shimoga, Shimla, Siliguri, Rourkela, Roorkee, Pondicherry, Rohtak, Ranchi, Rajkot, Pimpri, Moradabad, Mohali, Meerut, Madurai, Kolhapur, Khammam, Jodhpur, Jamshedpur, Jammu, Jalandhar, Jabalpur, Gwalior, Gorakhpur, Ghaziabad, Gandhinagar, Erode, Ernakulam, Durgapur, Dombivli, Dehradun, Bhubaneswar, Cochin, Bhopal, Anantapur, Anand, Amritsar, Agra, Kharadi, Calicut, Yelahanka, Salem, Thane, Andhra Pradesh, Warangal, Kompally, Mumbai, Anna Nagar, Dilsukhnagar, ECIL, Chromepet, Thoraipakkam, Uppal, Bhilai, Guduvanchery, Indore, Kalaburagi, Kochi, Navi Mumbai, Porur, Raipur, Vijayawada, Vizag, Surat, Kanpur, Aurangabad, Trichy, Mangalore, Mysore, Chandigarh, Guwahati, Guntur, Varanasi, Faridabad, Thiruvananthapuram, Nashik, Patna, Lucknow, Nagpur, Vadodara, Jaipur, Hyderabad, Pune, Kalyan, Delhi, Kolkata, Noida, Chennai, Bangalore, Gurgaon, Coimbatore.

360DigiTMG – Data Analytics, Data Science Course Training Hyderabad

 Address:-2-56/2/19, 3rd floor, Vijaya towers, near Meridian school, Ayyappa Society Rd, Madhapur, Hyderabad, Telangana 500081 Contact us ( 099899 94319 ) Looking forward to becoming a Data Scientist? Check out the Data Science Course and get certified today.