Tuples Programs in Python: Important For O Level Exam

Tuples Programs in Python

Tuples Programs in Python: A tuple is an ordered collection of elements, typically of heterogeneous data types, that is immutable, meaning its contents cannot be altered after creation. Tuples are commonly used in programming languages like Python to group related data.

Key Characteristics of Tuples
  • Ordered: Tuples maintain the order of elements, allowing for indexing and slicing.
  • Immutable: Once created, the elements of a tuple cannot be modified, added, or removed.
  • Allow Duplicates: Tuples can contain duplicate values.
1. Write a program to reverse a given tuple.
CODE
tuple=(10,20,30,40,50)
reversed=tuple[::-1]
print(reversed)
OUTPUT
Tuples Programs in Python
2. In a nested tuple, how do you access a specific element?
CODE
tuple=("Orange",[10,20,30],(40,50))
element=tuple[0][1]
print(element)
OUTPUT
Tuples Programs in Python
3. Write a program to create a tuple containing only one item.
CODE
tuple=(2,)
print(tuple)
OUTPUT
Tuples Programs in Python
4. Write a program to swap the contents of two tuples.
CODE
tuple1=(10,20)
tuple2=(30,40)
tuple1,tuple2=tuple2,tuple1
print("tuple1:",tuple1)
print("tuple2:",tuple2)
OUTPUT
Tuples Programs in Python
5. Write a program to concatenate two tuples into one.
CODE
tuple1=(20,40)
tuple2=(60,80)
concatenate=tuple1+tuple2
print(concatenate)
OUTPUT
Tuples Programs in Python
6. Write a program to repeat the contents of a tuple multiple times.
CODE
tuple=(10,20)
repeat=tuple*5
print(repeat)
OUTPUT
Tuples Programs in Python
7. Write a program to check if an element exists in a tuple.
CODE
tuple=(10,20,30,40,50,60,70,80,90,100)
print(1000 in tuple)
OUTPUT
Tuples Programs in Python
8. Write a program to find the length of a tuple.
CODE
tuple=(10,20,30,40,50,60,70,80,90,100)
length=len(tuple)
print(length)
OUTPUT
Tuples Programs in Python
9. Write a program to find the index of a specific element in a tuple.
CODE
tuple=(10,20,30,40,50,60,70,80,90,100)
index=tuple.index(30)
print(index)
OUTPUT
Tuples Programs in Python

Important IoT Programs for Exam

10. Write a program to count how many times an element appears in a tuple.
CODE
tuple=(10,20,30,40,50,60,70,80,90,100)
count=tuple.count(30)
print(count)
OUTPUT
Tuples Programs in Python
11. Write a program to convert a list into a tuple.
CODE
list1=[10,20,30]
tuple1=tuple(list1)
print(tuple1)
OUTPUT
Tuples Programs in Python
12. Write a program to remove an item from a tuple.
CODE
tuple1=(10,20,30,40,50)
tuple1=tuple1[:2]+tuple1[4:]
print(tuple1)
OUTPUT
Tuples Programs in Python
13. Write a program to extract a portion of a tuple.
CODE
tuple1=(10,20,30,40,50)
sliced=tuple1[1:4]
print(sliced)
OUTPUT
Tuples Programs in Python
14. Write a program to find the largest and smallest elements in a tuple.
CODE
tuple1=(10,20,30,40,50)
maximum=max(tuple1)
minimum=min(tuple1)
print("max",maximum)
print("min",minimum)
OUTPUT
Tuples Programs in Python
Share your love
Er. Saloni
Er. Saloni
Articles: 31

Leave a Reply

Your email address will not be published. Required fields are marked *