UNIT-5: Python Collections and Library
UNIT-5: Python Collections and Library
5.1.1 Tuples in Python.
A tuple in Python is an ordered, immutable collection used to store multiple items in a single variable.
Unlike lists, tuples cannot be modified (i.e., no adding, removing, or changing elements after creation).
Tuples are useful when we want to ensure that data remains unchanged.
Declaring a Tuple
- A tuple is defined using parentheses
(), with elements separated by commas.
Example:
# Creating a tuple fruits = ("Apple", "Banana", "Cherry") numbers = (10, 20, 30, 40) mixed = (1, "Hello", 3.5, True) print(fruits) print(numbers) print(mixed)
Note: If a tuple has only one element, a trailing comma , is required.
single_element_tuple = (5,) # Correct not_a_tuple = (5) # Incorrect (treated as integer) print(type(single_element_tuple)) # Output:
print(type(not_a_tuple)) # Output:
Indexing in a Tuple
Elements in a tuple are accessed using indexing, starting from
0.
| Tuple Item | "A" | "B" | "C" | "D" |
|---|---|---|---|---|
| Index | 0 | 1 | 2 | 3 |
| Negative Index | -4 | -3 | -2 | -1 |
letters = ("A", "B", "C", "D") # Accessing elements using positive index print(letters[0]) # Output: A print(letters[2]) # Output: C # Accessing elements using negative index print(letters[-1]) # Output: D print(letters[-3]) # Output: B
Changing Tuple Values
Since tuples are immutable, their values cannot be changed after creation.
Example (Not Allowed):
numbers = (10, 20, 30) numbers[1] = 50 # Error! Tuples cannot be modified
Workaround: Convert Tuple to List
If modification is necessary, convert the tuple into a list, change the values, and convert it back to a tuple.
numbers = (10, 20, 30) numbers_list = list(numbers) # Convert tuple to list numbers_list[1] = 50 # Modify the list numbers = tuple(numbers_list) # Convert list back to tuple print(numbers) # Output: (10, 50, 30)
Adding and Removing Data from Tuple
1. Adding Elements to a Tuple
Tuples cannot be modified directly. However, elements can be added by creating a new tuple.
fruits = ("Apple", "Banana") new_fruits = fruits + ("Cherry",) # Concatenating tuples print(new_fruits) # Output: ('Apple', 'Banana', 'Cherry')
2. Removing Elements from a Tuple
Since tuples do not support item deletion, we need to convert it to a list, remove elements, and convert it back.
numbers = (10, 20, 30, 40) numbers_list = list(numbers) # Convert to list numbers_list.remove(20) # Remove 20 numbers = tuple(numbers_list) # Convert back to tuple print(numbers) # Output: (10, 30, 40)
Using tuple() Method to Create a Tuple
The
tuple()constructor can be used to create a tuple from an iterable like a list or a string.
# Creating a tuple from a list numbers_list = [1, 2, 3, 4] numbers_tuple = tuple(numbers_list) print(numbers_tuple) # Output: (1, 2, 3, 4) # Creating a tuple from a string text_tuple = tuple("Hello") print(text_tuple) # Output: ('H', 'e', 'l', 'l', 'o')
Tuple Methods
Python provides built-in methods to work with tuples.
1. count() – Returns the number of occurrences of an element
numbers = (1, 2, 2, 3, 4, 2) print(numbers.count(2)) # Output: 3 (2 appears 3 times)
2. index() – Returns the index of the first occurrence of an element.
animals = ("Dog", "Cat", "Rabbit", "Cat") print(animals.index("Cat")) # Output: 1 (First occurrence of "Cat")
Python Sets
A set in Python is an unordered, mutable, and unique collection of elements.
Sets do not allow duplicate values and are useful when handling unique data.
Unlike lists and tuples, sets do not support indexing or slicing.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.