Python - Tuples
Why should I learn to solve Python: Tuples technical interview questions?
Learn and practise solving Python: Tuples technical interview questions and answers to enhance your skills for clearing technical interviews, HR interviews, campus interviews, and placement tests.
Where can I get technical Python: Tuples technical interview questions and answers with explanations?
IndiaBIX provides you with lots of fully solved Python: Tuples technical interview questions and answers with a short answer description. You can download Python: Tuples technical interview questions and answers as PDF files or e-books.
How do I answer Python: Tuples technical interview questions from various companies?
You can answer all kinds of Python: Tuples technical interview questions by practising the given exercises (short answer type). You can also find the frequently asked Python: Tuples technical interview questions with answers from various companies, such as TCS, Wipro, Infosys, CTS, IBM, etc.
In Python, a tuple is an ordered and immutable collection of elements. Tuples are similar to lists, but unlike lists, once a tuple is created, its elements cannot be changed or modified. Tuples are defined using parentheses ( )
. Here's an example program that illustrates the concept of a tuple:
# Creating a tuple
my_tuple = (1, 2, 3, 'a', 'b')
# Displaying the tuple
print("Tuple:", my_tuple)
# Accessing elements of the tuple
print("Element at index 2:", my_tuple[2])
# Attempting to modify an element (will raise an error)
# my_tuple[0] = 10 # Uncommenting this line will result in a TypeError
# Finding the length of the tuple
print("Length of the tuple:", len(my_tuple))
The program creates a tuple (my_tuple
) with a mix of integers and strings, displays the tuple, accesses an element using index, and attempts to modify an element (which will raise an error). Finally, it finds and displays the length of the tuple.
Outputs:
Tuple: (1, 2, 3, 'a', 'b') Element at index 2: 3 Length of the tuple: 5
In Python, an empty tuple can be created by using an empty set of parentheses ()
. Here's an example program that demonstrates how to create an empty tuple:
# Creating an empty tuple
empty_tuple = ()
# Displaying the empty tuple
print("Empty Tuple:", empty_tuple)
# Finding the length of the empty tuple
print("Length of the empty tuple:", len(empty_tuple))
The program initializes an empty tuple (empty_tuple
) using a set of parentheses. It then displays the empty tuple and finds and displays its length.
Outputs:
Empty Tuple: () Length of the empty tuple: 0
In Python, both lists and tuples are used to store collections of elements, but they have key differences in terms of mutability and syntax. Let's explore the differences between a tuple and a list with an example program:
# Creating a list
my_list = [1, 2, 3, 'a', 'b']
# Creating a tuple
my_tuple = (1, 2, 3, 'a', 'b')
# Displaying the list and tuple
print("List:", my_list)
print("Tuple:", my_tuple)
# Modifying an element in the list
my_list[0] = 10
# Attempting to modify an element in the tuple (will raise an error)
# my_tuple[0] = 10 # Uncommenting this line will result in a TypeError
# Displaying the modified list and attempting to modify the tuple
print("Modified List:", my_list)
# Finding the length of the list and tuple
print("Length of the List:", len(my_list))
print("Length of the Tuple:", len(my_tuple))
The program creates both a list (my_list
) and a tuple (my_tuple
), displays them, and demonstrates the difference in mutability. Lists are mutable, allowing elements to be modified, added, or removed after creation. Tuples are immutable, meaning their elements cannot be changed after creation.
Outputs:
List: [1, 2, 3, 'a', 'b'] Tuple: (1, 2, 3, 'a', 'b') Modified List: [10, 2, 3, 'a', 'b'] Length of the List: 5 Length of the Tuple: 5
In Python, the immutability property of tuples means that once a tuple is created, its elements cannot be modified, added, or removed. Tuples provide a fixed collection of elements that remain constant throughout their existence. Let's explore this property with an example program:
# Creating a tuple
my_tuple = (1, 2, 3, 'a', 'b')
# Displaying the original tuple
print("Original Tuple:", my_tuple)
# Attempting to modify an element in the tuple (will raise an error)
# my_tuple[0] = 10 # Uncommenting this line will result in a TypeError
# Attempting to add an element to the tuple (will raise an error)
# my_tuple += (4, 5) # Uncommenting this line will result in a TypeError
# Attempting to remove an element from the tuple (will raise an error)
# del my_tuple[3] # Uncommenting this line will result in a TypeError
# Displaying the original tuple after attempted modifications
print("Tuple after attempted modifications:", my_tuple)
The program creates a tuple (my_tuple
) and attempts to perform actions that would modify the tuple (modify an element, add an element, and remove an element). Each attempt results in a TypeError
because tuples are immutable.
Outputs:
Original Tuple: (1, 2, 3, 'a', 'b')
Traceback (most recent call last): File "script.py", line 9, in <module> my_tuple[0] = 10 TypeError: 'tuple' object does not support item assignment
Traceback (most recent call last): File "script.py", line 12, in <module> my_tuple += (4, 5) TypeError: 'tuple' object does not support item assignment
Traceback (most recent call last): File "script.py", line 15, in <module> del my_tuple[3] TypeError: 'tuple' object doesn't support item deletion