Practical No.
7: Develop Python program to perform following
operations on tuples: a) Create b) Access c) Update d) Delete
tuple elements
Practical related Questions
1. Define empty tuple. Write syntax to create empty tuple.
An empty tuple in Python is a tuple that contains no elements.
Using Empty Parentheses
Simplest way to create an empty tuple is by using ( ).
# Creating an empty tuple using parentheses
t = ()
print(t)
print(type(t))
print(len(t))
Output
()
<class 'tuple'>
0
2. Write syntax to copy specific elements existing tuple into new tuple.
t1 = (10,20,30,40,50,60,70,80,90,100)
t2 = t1[2:8]
print(t2)
Output
(30, 40, 50, 60, 70, 80)
3. Compare tuple with list (Any 4 points)
What is the Difference Between List and Tuple?
Parameter List Tuple
Mutability Mutable Immutable
Syntax Enclosed in Square ([]) bracket Enclosed in Square (()) bracket
It can be changed, i.e., a new element can Length is fixed, i.e., new elements can’t be
Length
be added. added.
Order Elements order is preserved. Elements order is preserved.
Usage It is used to store information that needs It’s used to store information that doesn’t
to be modified or changed over need to alter over time.Example:
time.Examples: Employee Record Coordinates of a point in a map.
Performanc Requires additional memory allocation and It is more efficient and faster to access
e deallocation when modified. than the list.
Elements can be added, removed, or It supports indexing, slicing, and iterating
Operation
modified. over elements.
Example Employee = [‘A’, ‘B’, ‘C’] Employee Number = (1001, 1002, 1003)
Exercise
1. Create a tuple and find the minimum and maximum number from it.
>>> tup = (22, 3, 45, 4, 2.4, 2, 56, 890, 1)
>>> max(tup)
890
>>> tup = (22, 3, 45, 4, 2.4, 2, 56, 890, 1)
>>> min(tup)
2. Write a Python program to find the repeated items of a tuple.
t = (2,34,45,6,7,2,4,5,78,34,2)
print(t)
count = t.count(2)
print(count)
Output
(2, 34, 45, 6, 7, 2, 4, 5, 78, 34, 2)
3. Print the number in words for Example: 1234 => One Two Three Four
num = int(input("Enter any number:"))
t = list()
while num > 0:
rem = num %10
if rem == 0:
t.append("Zero")
elif rem == 1:
t.append("One")
elif rem == 2:
t.append("Two")
elif rem == 3:
t.append("Three")
elif rem == 4:
t.append("Four")
elif rem == 5:
t.append("Five")
elif rem == 6:
t.append("Six")
elif rem == 7:
t.append("Seven")
elif rem == 8:
t.append("Eight")
elif rem == 9:
t.append("Nine")
num = num // 10
for i in range(len(t)-1,-1,-1):
print(t[i],end=" ")
Output
Enter any number:4568
Four Five Six Eight