In this article let us look at 5 examples of immutable datatypes in Python.
If you wish to develop your physique like Mr. Arnold, “understanding” how to perform exercises like squats, bench presses, etc., is not enough, you need to “consistently work your muscles” to build up your mass and strength!
Learning Python is no different,
- 1st you need to understand the concepts
- Then you need to practice those concepts
Hence this article will focus on strengthening our understanding of the concepts we have learned in our other article below.
Mutable and Immutable Data Types in python explain using examples
If you haven’t read that one yet I suggest reading that before continuing this one! There you will be able to find answers to questions such as
- What are immutable datatypes?
- How can we figure out if an object is mutable or immutable? and
- Why do we need immutable objects?
Coming back to the topic, let’s start our Python workout today with the 1st example!
Example#1: Integers
It might be a surprise for you to learn that integers are actually immutable as we frequently assign integers to variables and do calculations with them as we see fit, but the truth is they actually are immutable. For those of you who are still skeptical here is the proof!
num = 5
num_id = id(num)
num = 10
new_num_id = id(num)
print("Old ID and new ID is the same:", num_id == new_num_id)
Old ID and new ID is the same: False
As we can see, when we tried to change the value of our initial integer num, a new object was created instead. This object has a different ID hence we see False in the output!
This behaviour is not limited to integers, but to all numeric datatypes such as float, complex numbers etc!
Example#2: Strings
Yup! Strings too are immutable! We can use the same technique from earlier to verify its immutability:
word = "Hello"
word_id = id(word)
word = "World!"
new_word_id = id(word)
print("Old ID and new ID is the same:", word_id == new_word_id)
Old ID and new ID is the same: False
Example#3: Ranges
As you might know, ranges are mainly used to create a sequence of numbers that might be needed in for loops. But an interesting fact about them is that they are immutable too!
We can clearly see that the IDs are different!
Example#4: Bytes
Bytes are an interesting datatype used in image processing, text processing, and in all places where we need to exchange data in bytes.
If you are interested in learning more about bytes and their mutable counterparts bytearrays you can do so here
Python: bytearray vs bytes, Similarities & Differences Explained!
Here is a YouTube video we made on ByteArray if you wish to learn more about the mutable cousin of the Bytes class!
Coming back to the topic, let us verify the fact that bytes are immutable too!
As you can see from the screenshot above, bytes indeed are immutable!
Example#5: Frozen Set
Frozen sets are just immutable sets. Here is the program showing their immutability:
frozen_set = frozenset([2,4,6,8,10])
fset_id = id(frozen_set)
new_frozen_set = frozenset([1,3,5,7,9])
new_fset_id = id(new_frozen_set)
print("Old ID:", fset_id)
print("New ID:", new_fset_id)
print("IDs are equal:", fset_id == new_fset_id)
Old ID: 140393919807072
New ID: 140393919798784
IDs are equal: False
And with that, I will end this article.
I hope you have gotten some practice on determining if a given datatype is immutable or mutable and you have learned something new!
Congratulations on making it to the end of the article, not many have the perseverance to do so!
I hope you enjoyed reading this article and found it useful!
Feel free to share it with your friends and colleagues!
If your thirst for knowledge has not been quenched yet, here are some related articles that might spark your interest!
Related Articles
Mutable and Immutable Data Types in python explain using examples
Python: bytearray vs bytes, Similarities & Differences Explained!
Python “is” versus “==”: Explained using Examples!
Thanks to Namazi Jamal for his contributions in writing this article!