0% found this document useful (0 votes)
6 views4 pages

Tuples: Indexing, Slicing, and Functions

The document demonstrates the creation and manipulation of tuples in Python, including indexing, slicing, and iteration. It also showcases the use of built-in functions like len(), min(), max(), and sum() on tuples, as well as converting between lists and tuples while highlighting the immutability of tuples. Error handling is included to illustrate the consequences of attempting to modify a tuple.

Uploaded by

anandshah0027
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views4 pages

Tuples: Indexing, Slicing, and Functions

The document demonstrates the creation and manipulation of tuples in Python, including indexing, slicing, and iteration. It also showcases the use of built-in functions like len(), min(), max(), and sum() on tuples, as well as converting between lists and tuples while highlighting the immutability of tuples. Error handling is included to illustrate the consequences of attempting to modify a tuple.

Uploaded by

anandshah0027
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

PRACTICAL NO:- 2

Tuples and Tuple Functions


AIM:- 2(a) Create a tuple and demonstrate indexing, slicing, and iteration
INPUT:-
# Creating a tuple
numbers = (10, 20, 30, 40, 50)
print("Original Tuple:", numbers)

# Indexing
print("Element at index 0:", numbers[0])
print("Element at index 3:", numbers[3])

# Slicing
print("First three elements:", numbers[:3])
print("Elements from index 2 to 4:", numbers[2:5])

# Iteration
print("Iterating through tuple elements:")
for num in numbers:
print(num, end=" ")
Output:-
Original Tuple: (10, 20, 30, 40, 50)
Element at index 0: 10
Element at index 3: 40
First three elements: (10, 20, 30)
Elements from index 2 to 4: (30, 40, 50)
Iterating through tuple elements:
10 20 30 40 50
2(b) Use built-in functions like len(), min(), max(), and sum() on tuples
Input:-
# Tuple of numbers
numbers = (5, 10, 15, 20, 25)

# Using built-in functions


print("Tuple:", numbers)
print("Length of tuple:", len(numbers))
print("Minimum value:", min(numbers))
print("Maximum value:", max(numbers))
print("Sum of all elements:", sum(numbers))
Output:-
Tuple: (5, 10, 15, 20, 25)
Length of tuple: 5
Minimum value: 5
Maximum value: 25
Sum of all elements: 75

2(c) Convert between lists and tuples; demonstrate immutability

Input:-
# Convert list to tuple
list1 = [1, 2, 3, 4, 5]
tuple1 = tuple(list1)
print("List:", list1)
print("Converted Tuple:", tuple1)

# Convert tuple to list


tuple2 = (10, 20, 30)
list2 = list(tuple2)
print("\nTuple:", tuple2)
print("Converted List:", list2)

# Demonstrate immutability of tuple


tuple3 = (100, 200, 300)
print("\nOriginal Tuple:", tuple3)

# Trying to modify a tuple element will cause an error


try:
tuple3[1] = 250 # This will raise an error
except TypeError as e:
print("Error: Tuples are immutable!", e)

Output:-
List: [1, 2, 3, 4, 5]
Converted Tuple: (1, 2, 3, 4, 5)

Tuple: (10, 20, 30)


Converted List: [10, 20, 30]

Original Tuple: (100, 200, 300)


Error: Tuples are immutable! 'tuple' object does not support item assignment

You might also like