- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Difference between mutable and immutable in python?
Python defines variety of data types of objects. These objects are stored in memory and object mutability depends upon the type, like Lists and Dictionaries are mutable it means that we can change their content without changing their identity. Other objects like Integers, Floats, Strings and Tuples have no provision to change there assigned value for an index.
List is mutable: Lists are just like the arrays, declared in other languages. Lists need not be homogeneous always which makes it a most powerful tool in Python. Lists are mutable, and hence, they can be altered even after their creation.
#Write a python program to print list of a number? list=[1,2,3,4,5] print(list)
List is a collection which is ordered and changeable. Allows duplicate members.
list=[1,2,3,4,5] list ='a' print(list)
['a', 2, 3, 4, 5]
While running the program, assigning the zero based index with the value of '1' can be assigned a value of 'a' which is mutable(can be modified)
Tuple is immutable: A tuple is a collection which is ordered and unchangeable, does not allow duplicate members. In Python tuples ar written with round or inside parentheses (), separated by commas. The parentheses are optional, however, it is a good practice to use them.
#Write a python program to print tuple of a number? Tuple=(10,20,30) print(tuple)
Tuple is ordered and unchangeable(cannot be modified).
tuple=(10,20,30) tuple=50 print(tuple)
TypeError Traceback (most recent call last) in 1 my_yuple = (10, 20, 30) ----> 2 my_yuple = 50 3 print(my_yuple) TypeError: 'tuple' object does not support item assignment
While assigning the zero based index with the value of '50' throws an exception, because it is already assigned a value of '10' which is immutable(cannot be modified).
- Difference between mutable and immutable object
- What is the difference between a mutable and immutable string in C#?
- What does immutable mean? Which Python types are mutable and which are not?
- How to create an immutable class with mutable object references in Java?
- Difference between Python and PHP.
- Difference between Python and Bash
- Difference between C and Python
- Difference Between PHP and Python
- Difference between Python iterable and iterator
- Difference between == and is operator in python.
- Difference between Method and Function in Python
- Difference between List and Tuples in Python.
- Difference Between List and Tuple in Python
- What is immutable in Python?
- Mutable keyword in C++?