
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Why do you think tuple is an immutable in Python?
Tuples in Python are immutable, which means once we create them, we can not change their items. In this article, we will discuss why tuples are immutable before moving on, and we will understand tuples in detail.
Tuples in Python
Tuples are a data type that belongs to the sequence data type category. They are similar to lists in Python, but they have the property of being immutable.
We can't change the elements of a tuple, but we can perform operations suchas counting the number of elements, accessing elements using an index, retrieving the type of the elements, etc.
Tuples are created in Python by placing a sequence of values separated by a 'comma', with or without the use of parentheses for data grouping. Tuples can have any number of elements and any type of data (like strings, integers, lists, etc.).
Example 1: Creating a Tuple
In the example below, we will look at how to create a tuple.
tuple = ('Tutorialspoint', 'is', 'the', 'best', 'platform', 'to', 'learn', 'new', 'skills') print(tuple)
Output
This will create the following outcome -
('Tutorialspoint', 'is', 'the', 'best', 'platform', 'to', 'learn', 'new', 'skills')
Example 2: Tuple Immutability
The following example shows that tuples are immutable. Here we try to overwrite or replace "Levi" with the "Kristen" name, but as tuples are immutable, we cannot use the index method to achieve it.
tuple = ("Meredith", "Levi", "Wright", "Franklin") tuple[1]= "Kristen" print(tuple)
Output
This will generate the following result -
File "main.py", line 2, in <module> tuple[1]= "Kristen" TypeError: 'tuple' object does not support item assignment
Example 3: Tuple as Dictionary Key
In the program below, we will show you that you can use a tuple as a dictionary key. It means that tuples can be used as keys because they are immutable.
tuple = (1, 2, 3) dict = {tuple: "Tuple as key"} print(dict) print("Tuple as key:", tuple) print("Dictionary:", dict)
Output
This will produce the following result -
{(1, 2, 3): 'Tuple as key'} Tuple as key: (1, 2, 3) Dictionary: {(1, 2, 3): 'Tuple as key'}
Example 4: Tuple with Mixed Data Types
In the program below, we will show you that tuples can have mixed data types. It means that tuples can have different data types like strings, integers, lists, etc.
tuple = (1, "Tutorialspoint", 3.14, [1, 2, 3]) print("Tuple with mixed data types:", tuple) print("First element:", tuple[0]) print("Second element:", tuple[1]) print("Third element:", tuple[2])
Output
This will lead to the following outcome -
Tuple with mixed data types: (1, 'Tutorialspoint', 3.14, [1, 2, 3]) First element: 1 Second element: Tutorialspoint Third element: 3.14
Why are Tuples Immutable?
The following are a few important reasons for tuples being immutable -
-
Maintaining Order: Tuples are mainly defined in Python as a way to show order. For example, when you retrieve data from a database in the form of a list of tuples, all the tuples are in the order of the fields you fetched.
-
Copy efficiency: Rather than copying an immutable object, you can alias it (bind a variable to a reference).
-
Comparing efficiency: You can compare two variables by comparing position rather than content when using copy-by-reference.
-
Interning: Any immutable value requires just one copy to be stored. In concurrent programs, there's no requirement to synchronize access to immutable objects.
-
Constant correctness: Some values shouldn't be allowed to change.