A) [1, 2, 3]
B) (1, 2, 3)
C) {1, 2, 3}
D) {}
The correct answer is option B: (1, 2, 3). Option B is correct because tuples use parentheses () to define the collection of elements. Tuples are useful when you want to store a sequence of items that cannot be modified after creation.
Table of Content
What is a tuple in Python?
Tuples are the most important part of data structure in Python. It follows an order and is an immutable collection of elements. Once a tuple is created, it cannot be changed, and the item in the collection will remain permanent. The tuples are identified by parentheses “().”
Let’s see an example for tuples in Python:
Tuple Example in Python
The below code will create a tuple, print its type, and the tuple’s elements.
# Creating a tuple
my_tuple = (10, 20, 30)
print(type(my_tuple))
print(my_tuple)
Output
Key characteristics of tuples
- Ordered: The elements in a tuple have a defined order. These orders cannot be changed, as they are part of the tuple’s pre-defined structure.
- Immutable: Once the dictionary is created and saved, Tuples will not let you change or modify its content, which will prevent the value from being robust.
- Allow duplicates: Tuples can contain duplicate elements. These duplicate values will be stored in a defined order.
So, if this is the correct way of defining a tuple, then what do options A: [1, 2, 3], C: {1, 2, 3}, and D: {} mean?
We will discuss each of them one by one.
Option A is a LIST
Option A: [1, 2, 3]
Option A cannot be considered a tuple because tuples will have elements within parentheses “()” and not in square brackets []. The option A is a list.
A list is a collection of mutable items, which means that the items inside a list can be modified. Lists are always written in square brackets [].
Example of a List in Python
The below example, creates a list, prints its type, then modifies the list by adding an element 4 to the end, and finally prints the updated list.
# Creating a list
my_list = [1, 2, 3]
print(type(my_list))
# Modifying a list
my_list.append(4)
print(my_list)
Output
Option C is a SET
Option C {1, 2, 3}
Option C represents a set in Python, not a tuple. Tuples are defined using parentheses () and not curly braces {}.
A set is an unordered collection of unique items, meaning that duplicate elements are not allowed. A set in Python can be created using curly braces {} or the set() function.
Example for Set in Python
In the below example, this code creates a set my_set with unique elements, prints its type, and then prints the set {1, 2, 3, 4}.
# Creating a set
my_set = {1, 2, 3, 3, 4}
print(type(my_set))
print(my_set)
Output
OPTION D is an EMPTY DICTIONARY
Option D {}
Option D can be considered an empty dictionary because ‘{}’ cannot be an empty set because an empty set can only be created using “set{}.” So, without the function set, if there is any empty set, then it is considered an empty dictionary.
An empty dictionary is a disordered collection of key-value pairs, and it does not contain any value.
Example of an empty dictionary in Python
This code below creates an empty set using the set() function and prints its type.
# Creating an empty set
empty_set = set()
print(type(empty_set))
Output
Conclusion
Tuples are very important in the data structure of Python. It gives an immutable and ordered set of elements, which makes the data more efficient and enhances your programming skills. It is very useful while writing code due to its robustness and efficient code. Whether you’re returning multiple values from a function, storing fixed collections of data, or using tuples as dictionary keys, learning tuples will make you more proficient in the Python language. If you want to excel in your skills and pursue a career in Python, you can refer to the Intellipaat Python Programming course.