0% found this document useful (0 votes)
9 views5 pages

Tuple Related Questions

Tuples

Uploaded by

Yuvika Sachdeva
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)
9 views5 pages

Tuple Related Questions

Tuples

Uploaded by

Yuvika Sachdeva
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
You are on page 1/ 5

XI TUPLE RELATED QUESTIONS

Tuples Practice Questions with Solutions


-------------------------------------------------------------------------------------------------------------------------------
--
Write a program to create a tuple
x = () #Create an empty tuple
print(x)
tuplex = tuple() #Create an empty tuple with tuple() function built-in Python
print(tuplex)
-------------------------------------------------------------------------------------------------------------------------------
--
Program to Create a tuple with different data types

tuplex = ("tuple", False, 3.2, 1)


print(tuplex)
-------------------------------------------------------------------------------------------------------------------------------
--
Program to Create a tuple with numbers
tuplex = 5, 10, 15, 20, 25
print(tuplex)
-------------------------------------------------------------------------------------------------------------------------------
--
Program to Create a tuple of one item
tuplex = 5,
print(tuplex)
-------------------------------------------------------------------------------------------------------------------------------
--
Write a Python program to unpack a tuple in several variables.

tuplex = 4, 8, 3
print(tuplex)
n1, n2, n3 = tuplex
#unpack a tuple in variables
print(n1 + n2 + n3)
#the number of variables must be equal to the number of items of the
tuple
n1, n2, n3, n4= tuplex
---------------------------------------------------------------
Write a Python program to add an item in a tuple.
tuplex = (4, 6, 2, 8, 3, 1)
print(tuplex)
#tuples are immutable, so you can not add new elements
#using + operator you can add an element and it will create a new
tuple
tuplex = tuplex + (9,)
print(tuplex)
#adding items in a specific index
tuplex = tuplex[:5] + (15, 20, 25) + tuplex[:5]
print(tuplex)
#converting the tuple to list
listx = list(tuplex)
#use different ways to add items in list
listx.append(30)
tuplex = tuple(listx)
print(tuplex)
----------------------------------------------------------------
Write a Python program to get the 4th element and 4th element from
last of a tuple.
tuplex = ("w", 3, "r", "e", "s", "o", "u", "r", "c", "e")
print(tuplex)
#Get item (4th element)of the tuple by index
item = tuplex[3]
print(item)
#Get item (4th element from last)by index negative
item1 = tuplex[-4]
print(item1)
----------------------------------------------------------------
----
Write a Python program to find the repeated items of a tuple.
tuplex = 2, 4, 5, 6, 2, 3, 4, 4, 7
print(tuplex)
#return the number of times it appears in the tuple.
c = tuplex.count(4)
print(c)
-------------------------------------------------------------------------------------------------------------------------------
--
Write a Python program to slice a tuple.
tuplex = (2, 4, 3, 5, 4, 6, 7, 8, 6, 1)
#used tuple[start:stop] the start index is inclusive and the stop
index
slice = tuplex[3:5]
#is exclusive
print(slice)
#if the start index isn't defined, is taken from the beginning of the
tuple
slice = tuplex[:6]
print(_slice)
#if the end index isn't defined, is taken until the end of the tuple
slice = tuplex[5:]
print(slice)
#if neither is defined, returns the full tuple
slice = tuplex[:]
print(slice)
#The indexes can be defined with negative values
slice = tuplex[-8:-4]
print(slice)
#create another tuple
tuplex = tuple("HELLO WORLD")
print(tuplex)
#step specify an increment between the elements to cut of the
tuple
#tuple[start:stop:step]
slice = tuplex[2:9:2]
print(slice)
#returns a tuple with a jump every 3 items
slice = tuplex[::4]
print(_slice)
#when step is negative the jump is made back
slice = tuplex[9:2:-4]
print(slice)
-------------------------------------------------------------------------------------------------------------------------------
--
Write a Python program to remove an item from a tuple.
tuplex = ("w", 3, "r", "s", "o", "u", "r", "c", "e")
print(tuplex)
#tuples are immutable, so you can not remove elements
#using + operator you can remove an item and it will create a new
tuple
tuplex = tuplex[:2] + tuplex[3:]
print(tuplex)
#converting the tuple to list
listx = list(tuplex)
#use different ways to remove an item of the list
listx.remove("c")
#converting the tuple to list
tuplex = tuple(listx)
print(tuplex)
-------------------------------------------------------------------------------------------------------------------------------
--

Write a Python program to find the index of an item of a tuple.


tuplex = tuple("index tuple")
print(tuplex)
#get index of the first item whose value is passed as parameter
index = tuplex.index("p")
print(index)
#define the index from which you want to search
index = tuplex.index("p", 5)
print(index)
#define the segment of the tuple to be searched
index = tuplex.index("e", 3, 6)
print(index)
#if item not exists in the tuple return ValueError Exception
index = tuplex.index("y")
-------------------------------------------------------------------------------------------------------------------------------
--
Write a Python program to reverse a tuple.
x = (5, 10, 15, 20)
# Reversed the tuple
y = reversed(x)
print(tuple(y))
-------------------------------------------------------------------------------------------------------------------------------
--
Write a Python program to find the length of a tuple.
tuplex = tuple("w3resource")
#use the len() function to known the length of tuple
print(len(tuplex))
-------------------------------------------------------------------------------------------------------------------------------
--
Write a Python program to create the tuple (‘a’,’bb’,’ccc’,’dddd’….)
that ends with 26 copies of the letter z.

n=int(input("enter no"))
num=65
for i in range(0,n):
for j in range(0,i+1):
ch=chr(num)
print(ch,end=' ')
num=num+1
print("\r")
-------------------------------------------------------------------------------------------------------------------------------
--
Write a program that create a tuple containing the squares of the
integers 1 through 10
tuplex=(1,2,3,4,5,6,7,8,9,10)
for i in tuplex:

print(i*i)

You might also like