Tuple Related Questions
Tuple Related Questions
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)
-------------------------------------------------------------------------------------------------------------------------------
--
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)