ASSIGNMENT
ASSIGNMENT
Q.4 What is tuple in Python? Explain various methods with example.
Answer :-Â
A tuple is an immutable and ordered collection in Python used to store multiple items in a single variable.
Like lists, tuples can hold elements of different data types, but unlike lists, tuples cannot be modified (i.e., elements cannot be added, removed, or changed after the tuple is created).
Key Features of Tuples
Immutable: Once created, the elements of a tuple cannot be changed.
Ordered: The order of elements is preserved.
Allows Duplicates: Tuples can have repeated elements.
Heterogeneous: Can store elements of different data types.
Syntax of a Tuple
A tuple is defined by enclosing elements within parentheses ‘()’:
my_tuple = (1, 2, 3, “Python”, 4.5)
If a tuple contains only one element, a trailing comma is required to distinguish it from a regular value:
single_element_tuple = (5,) # Valid tuple
Common Tuple Methods.
Although tuples are immutable, there are methods available for accessing, searching, and manipulating data indirectly. Here are the most common methods and operations with examples:
1. count()Â
Purpose: Returns the number of times a specified value appears in the tuple.
Example:
my_tuple = (1, 2, 2, 3, 4, 2)
print(my_tuple.count(2)) # Output: 3
2. index() :-Â
Purpose: Returns the index of the first occurrence of a specified value. Raises a `ValueError` if the value is not found.
Example:
my_tuple = (10, 20, 30, 40, 30)
print(my_tuple.index(30)) # Output: 2
3. Accessing Elements :-Â
Purpose: Retrieve elements using indexing.
Example:
my_tuple = (“Python”, “Java”, “C++”)
print(my_tuple[1]) # Output: Java
4. Slicing :-Â
Purpose: Extract a portion of the tuple using slicing.
Example:
my_tuple = (1, 2, 3, 4, 5)
print(my_tuple[1:4]) # Output: (2, 3, 4)
5. Tuple Concatenation :-Â
Purpose: Combine two tuples into one.
Example:
tuple1 = (1, 2, 3)
tuple2 = (4, 5, 6)
result = tuple1 + tuple2
print(result) # Output: (1, 2, 3, 4, 5, 6)
6. Tuple Repetition :-Â
Purpose: Repeat elements of a tuple a specified number of times.
Example:
my_tuple = (1, 2, 3)
print(my_tuple * 3) # Output: (1, 2, 3, 1, 2, 3, 1, 2, 3)
7. Membership Testing :-Â
Purpose: Check if an element exists in a tuple.
Example:
my_tuple = (“apple”, “banana”, “cherry”)
print(“banana” in my_tuple) # Output: True
8. Length of Tuple :-Â
Purpose: Get the number of elements in a tuple using the `len()` function.
Example:
my_tuple = (1, 2, 3, 4)
print(len(my_tuple)) # Output: 4
9. Maximum and Minimum :-Â
Purpose: Find the largest (`max()`) and smallest (`min()`) values in a tuple (for numeric or comparable elements).
Example:
my_tuple = (5, 1, 8, 3)
print(max(my_tuple)) # Output: 8
print(min(my_tuple)) # Output: 1
10. Convert List to Tuple :-Â
Purpose: Convert a list into a tuple using the `tuple()` constructor.
Example:
my_list = [1, 2, 3]
my_tuple = tuple(my_list)
print(my_tuple) # Output: (1, 2, 3)
11. Nested Tuples :-Â
Purpose: Create tuples containing other tuples.
Example:
nested_tuple = (1, (2, 3), (4, 5))
print(nested_tuple[1]) # Output: (2, 3)
print(nested_tuple[1][0]) # Output: 2