Python Tuples
Python Tuples Interview Questions
What is a tuple in Python?
A tuple is an ordered, immutable sequence data type in Python. Tuples are defined using parentheses () and can contain elements of different data types. Example: my_tuple = (1, "hello", 3.14).
What does "immutable" mean for tuples?
Immutable means that once a tuple is created, its elements cannot be changed, added, or removed. However, if a tuple contains mutable objects (like lists), those objects can be modified. Example: You cannot do my_tuple[0] = 10.
How to create a tuple with a single element?
To create a tuple with a single element, you must include a trailing comma: single_tuple = (5,). Without the comma, Python interprets (5) as just the integer 5.
What is tuple packing and unpacking?
Tuple packing: Creating a tuple by assigning multiple values: t = 1, 2, 3. Tuple unpacking: Extracting values from a tuple into variables: a, b, c = t. You can also use the asterisk (*) for extended unpacking.
How is a tuple different from a list?
1. Tuples are immutable, lists are mutable. 2. Tuples use parentheses (), lists use square brackets []. 3. Tuples are generally faster than lists. 4. Tuples can be used as dictionary keys, lists cannot.
What are the advantages of using tuples over lists?
1. Tuples are faster than lists. 2. Tuples can be used as dictionary keys (if all elements are immutable). 3. Tuples protect data from accidental modification. 4. Tuples use less memory than lists.
Can you modify a tuple after creation?
No, you cannot directly modify a tuple's elements. However, you can create a new tuple by concatenating or slicing existing tuples. Also, if a tuple contains mutable objects (like lists), those objects can be modified.
How to access elements in a tuple?
You can access tuple elements using indexing (starts at 0) and slicing. Example: my_tuple[0] gets the first element, my_tuple[1:3] gets elements from index 1 to 2. Negative indexing also works: my_tuple[-1].
What tuple methods are available in Python?
Tuples have only two built-in methods: count() - returns the number of occurrences of a value, and index() - returns the index of the first occurrence of a value.
Can a tuple be used as a dictionary key?
Yes, tuples can be used as dictionary keys if all elements in the tuple are immutable (strings, numbers, other tuples). Example: my_dict = {(1, 2): "value"} is valid, but {(1, [2, 3]): "value"} is not.
What is namedtuple in Python?
namedtuple is a factory function from the collections module that creates tuple subclasses with named fields. It makes code more readable: Point = namedtuple('Point', ['x', 'y']), then p = Point(10, 20) and access with p.x.
How to concatenate tuples?
Use the + operator to concatenate tuples: tuple1 = (1, 2), tuple2 = (3, 4), result = tuple1 + tuple2 gives (1, 2, 3, 4). This creates a new tuple.
How to repeat a tuple?
Use the * operator to repeat a tuple: my_tuple = (1, 2), repeated = my_tuple * 3 gives (1, 2, 1, 2, 1, 2).
What is the significance of tuples in function returns?
Functions can return multiple values using tuples. Python packs the return values into a tuple. Example: def get_coordinates(): return 10, 20 returns (10, 20). The caller can unpack: x, y = get_coordinates().
How to check if an element exists in a tuple?
Use the in operator: my_tuple = (1, 2, 3), if 2 in my_tuple: print("Found"). This works similarly to lists and other sequences.
What is tuple comprehension?
Python doesn't have tuple comprehension like list comprehension. However, you can create a tuple using a generator expression inside tuple(): squares = tuple(x*x for x in range(5)) gives (0, 1, 4, 9, 16).
How to convert a list to a tuple?
Use the tuple() constructor: my_list = [1, 2, 3], my_tuple = tuple(my_list) gives (1, 2, 3). This creates a new tuple with the same elements.
What happens when you try to modify a tuple element?
Python raises a TypeError with the message: "'tuple' object does not support item assignment". This is because tuples are immutable.
Can tuples contain mutable objects?
Yes, tuples can contain mutable objects like lists, dictionaries, or sets. While the tuple itself is immutable (you can't add/remove elements), the mutable objects inside can be modified.
What is the use of tuples in real-world applications?
1. Returning multiple values from functions. 2. As dictionary keys (for hashable elements). 3. Storing constant data. 4. In database operations (records). 5. For function arguments (args, kwargs). 6. In threading (immutable data is thread-safe).
Note: Tuples are an essential Python data structure that provides immutability, performance benefits, and safety for data that shouldn't change. Understanding tuples is crucial for writing efficient, thread-safe Python code. They're particularly useful for returning multiple values from functions and as dictionary keys.