tuples in python class 11

Contents show

tuples in python Important Questions

class 11

tuples in python

Q 1. How are tuples different from list when both are sequences ?

Tuples in Python Tutorial 2021


ans. The tuples and list are different in following ways :
The tuples are immutable sequences while list are mutable.
Lists can grow or shrink while tuples cannot.

Q 2. How can you say that a tuple is an ordered list of objects ?


ans. A tuples is an ordered list of objects. This is evidenced by the fact that the objects can be accessed through the use of an ordinal index and for a given index, same element is returned every time.

Q 3. Following code is trying to create a tuple with a single item. But when we try to obtain the length of the tuple is, Python gives error. Why? What is the solution?

t = (6)
len(t)
Traceback (most recent call last):
File “, line 1, in
len(t)
TypeError: object of type ‘int’ has no len()

ans. The syntax for a tuple with a single item requires the item to be followed by a comma as shown below:
t = (“a”,)
Thus, above code is not creating a tuple in t but an integer, on which len() cannot be applied. To create a tuple in t with single element, the code should be modified as :
t = (6,)
len(t)

Q 4. What is the length of the tuple shown below ?
t = ((((‘a’), ‘b’, ‘c’), ‘d’, 2), ‘e’, 3)


ans. The length of this tuple is 3 because there are just three elements in the given tuple. Because a careful look at the given tuple yields that t is made up of:
t1 = “a”,1
t2 = t1, “b”, “c”
t3 = t2, “d”, 2
t = (t3, “e”, 3)

Q 5. Can tuple be nested. ?


ans. Tuples can contain other compound objects, including lists, dictionaries, and other tuples. Hence, tuples can be nested.

Q 6. Given a tuple namely cars storing car names as element :


(‘Toyota’, ‘Honda’, ‘GM’, ‘Ford’, ‘BMW’, ‘Volkswagon’, ‘Mercedes’, ‘Ferrai’, ‘porshe’)


Write a program to print names of the cars in index range 2 to 6, both inclusive.
The output should also include the index in words as shown below :
One Honda
Two GM
:


ans.
cars = (‘Toyota’, ‘Honda’, ‘GM’, ‘Ford’, ‘BMW’, ‘Volkswagon’, ‘Mercedes’, ‘Ferrai’, ‘porshe’)
Number = (“Zero”, “One”, “two”, “Three”, “Four”, “Five”, “Six”, “Seven”, “Eight”, “Nine”)
for x in range(2,7):
print(Number[x], cars[x])

Q 7. Given a tuple T(1, 2, “a”, “b”). There is a list L with some elements. Replace first four elements of list with all four elements of the tuple in single statement. Print the list before and after the list is modified.


ans.
T = (1, 2, “a”, “b”)
L = [“hello”, “there”, “are”, “some”, “numbers”, “coming”, “from2, “tuple”]
print(“Before Unpacking the list is”)
print(L)
L[0], L[1], L[2], L[3] = T
print(“After Unpacking the list is”)
print(L)

Q 8. Write a Python function secondLargest(T) which takes as input a tuple T and returns the second Largest element in the tuple. You can use any of the standard Python functions to obtain your result.


ans.
T = (23, 45, 34, 66, 77, 67, 70)
maxvalue = max(T)
length = len(T)
secmax = 0
for a in range(length) :
if secmax < T[a] < maxvalue :
secmax = T[a]
print(“Second Largest value is :”, secmax)

Q 9. Write a function getPower(x) that returns a tuple containing x, x2, x3 and x4. Read five integers from the user, and for each integer read, print that value raised to powers 2, 3 and 4.


ans.
for n in range(5) :
x = int(input(“Enter Number :”))
T = (x, x2, x3, x**4)
print(number, “raised to powers 1, 2, 3, 4 : =”, T)

GLOSSARY

tuples in python


Tuple :- An immutable sequence of elements.
Packing :- Creating a tuple from individual values.
Unpacking :- Creating individual values from a tuple’s element.
Tuple Slice :- A tuple created from another tuple containing the requested elements.

How to learn Python for beginners ?

tuples in python notes

tuples in python in more details