Tuples in Python - Interview Questions and Answers
A tuple is a collection of ordered, immutable elements. It is similar to a list but cannot be changed once created. It is defined by placing elements within parentheses ().
A tuple is defined by enclosing elements in parentheses, separated by commas.
my_tuple = (1, 2, 3)
- Tuples are immutable (cannot be changed), while lists are mutable (can be modified).
- Tuples use parentheses
(), while lists use square brackets[].
No, tuples are immutable, which means their elements cannot be changed once they are created.
- Tuples are faster than lists due to their immutability.
- They can be used as keys in dictionaries, whereas lists cannot.
Elements can be accessed using indexing, similar to lists.
my_tuple = (1, 2, 3)
print(my_tuple[0]) # Output: 1
Yes, a tuple can contain mutable objects (like lists), but the tuple itself cannot be modified.
my_tuple = ([1, 2], 3)
my_tuple[0][0] = 10 # Modifies the list inside the tuple
You can concatenate tuples using the + operator.
tuple1 = (1, 2)
tuple2 = (3, 4)
result = tuple1 + tuple2 # Output: (1, 2, 3, 4)
You can repeat a tuple using the * operator
my_tuple = (1, 2)
repeated_tuple = my_tuple * 3 # Output: (1, 2, 1, 2, 1, 2)
You can use the len() function to get the length of a tuple.
my_tuple = (1, 2, 3)
print(len(my_tuple)) # Output: 3
You can use the in keyword to check if an element is present in a tuple.
my_tuple = (1, 2, 3)
print(2 in my_tuple) # Output: True
You can use the index() method to get the index of an element.
my_tuple = (1, 2, 3)
print(my_tuple.index(2)) # Output: 1
Yes, a tuple can contain elements of different data types, such as integers, strings, and other tuples.
my_tuple = (1, "hello", 3.14)
If you try to modify an element of a tuple, a TypeError will be raised because tuples are immutable.
my_tuple = (1, 2, 3)
my_tuple[0] = 10 # TypeError: 'tuple' object does not support item assignment
You can unpack a tuple by assigning its elements to variables.
my_tuple = (1, 2, 3)
a, b, c = my_tuple # a=1, b=2, c=3
Packing: Assigning multiple values to a tuple.
my_tuple = 1, 2, 3Unpacking: Extracting values from a tuple into variables
a, b, c = my_tuple
You can convert a tuple into a list using the list() constructor
my_tuple = (1, 2, 3)
my_list = list(my_tuple) # Output: [1, 2, 3]
You can convert a list into a tuple using the tuple() constructor
my_list = [1, 2, 3]
my_tuple = tuple(my_list) # Output: (1, 2, 3)
Yes, because tuples are immutable, they can be used as keys in a dictionary.
my_dict = { (1, 2): "value" }
The count() method returns the number of times a specified element appears in a tuple.
my_tuple = (1, 2, 3, 2)
print(my_tuple.count(2)) # Output: 2
The index() method returns the index of the first occurrence of a specified element in a tuple.
my_tuple = (1, 2, 3, 4)
print(my_tuple.index(3)) # Output: 2
A tuple can contain other tuples, forming a nested structure
my_tuple = (1, (2, 3), 4)
You can merge multiple tuples by concatenating them using the + operator
tuple1 = (1, 2)
tuple2 = (3, 4)
result = tuple1 + tuple2 # Output: (1, 2, 3, 4)
*args allows a function to accept a variable number of positional arguments, which are stored in a tuple.
def my_function(*args):
print(args)
my_function(1, 2, 3) # Output: (1, 2, 3)
Yes, a tuple with one element is created by adding a trailing comma.
single_element_tuple = (1,)
You cannot delete a specific element of a tuple, but you can delete the entire tuple using the del keyword.
my_tuple = (1, 2, 3)
del my_tuple
- Tuples are useful when you need an immutable sequence of data.
- They are used as keys in dictionaries.
- Tuples are also helpful in returning multiple values from a function.
Tuples can be compared using relational operators. They are compared element by element.
tuple1 = (1, 2)
tuple2 = (1, 2)
print(tuple1 == tuple2) # Output: True
You can slice a tuple to get a subtuple
my_tuple = (1, 2, 3, 4)
print(my_tuple[1:3]) # Output: (2, 3)
You can sort a tuple using the sorted() function, but it returns a list, not a tuple
my_tuple = (3, 1, 2)
sorted_tuple = sorted(my_tuple) # Output: [1, 2, 3]
Tutorials
Random Blogs
- Create Virtual Host for Nginx on Ubuntu (For Yii2 Basic & Advanced Templates)
- Understanding SQL vs MySQL vs PostgreSQL vs MS SQL vs Oracle and Other Popular Databases
- The Ultimate Guide to Artificial Intelligence (AI) for Beginners
- Top 10 Blogs of Digital Marketing you Must Follow
- AI is Replacing Search Engines: The Future of Online Search
- Deep Learning (DL): The Core of Modern AI
- Internet of Things (IoT) & AI – Smart Devices and AI Working Together
- Google’s Core Update in May 2020: What You Need to Know
- Variable Assignment in Python
- AI in Cybersecurity: The Future of Digital Protection
