Python Programs on Tuples
1. Create and Display a Tuple
colors = ("Red", "Green", "Blue", "Yellow")
print("Tuple:", colors)
print("Length of tuple:", len(colors))
2. Access Elements by Index
fruits = ("Apple", "Banana", "Mango")
print("First fruit:", fruits[0])
print("Last fruit:", fruits[-1])
3. Loop Through a Tuple
cities = ("Delhi", "Mumbai", "Chennai", "Kolkata")
for city in cities:
print(city)
4. Check if an Item Exists
animals = ("Cat", "Dog", "Horse")
if "Dog" in animals:
print("Dog is present in the tuple")
else:
print("Dog is not present")
5. Count & Index Methods
numbers = (1, 2, 3, 2, 4, 2)
print("Count of 2:", [Link](2))
print("Index of 4:", [Link](4))
6. Find Maximum and Minimum
marks = (88, 92, 76, 81, 95)
print("Maximum marks:", max(marks))
print("Minimum marks:", min(marks))
7. Concatenate and Repeat Tuples
tuple1 = (1, 2, 3)
tuple2 = (4, 5)
combined = tuple1 + tuple2
print("Concatenated tuple:", combined)
repeated = tuple1 * 3
print("Repeated tuple:", repeated)
8. Demonstrate Immutability
data = (10, 20, 30)
# data[0] = 100 # This will raise TypeError
print("Tuples cannot be modified!")