13 09 22 New Edited Tuple Program

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

TUPLE PROGRAMS for Practical’s

1 Write a Python program to input 5 subject names and put it in a tuple and display
the tuple information on the output screen.

#Roll No : Name: Practical No


#python program to input 5 subject and display the same.

t=tuple()
for i in range(5):
a=(input("Enter 5 subject names : "))
t=t+(a,)
print("\nNames of 5 subjects are :",t)

2 Write a python program using Tuples to generate the following output.


The given original Tuple.
T=(1,2,3,4,5,6),

Output :
T1=(1,3,5)
T2=(2,4,6)

#Roll No : Name: Practical No


#python program to input numbers and print in the pattern given.

tuple()
n=int(input("How many numbers to input : "))
for i in range(n):
a=int(input("Enter numbers : "))
t=t+(a,)
t1=t2=tuple()
for i in t:
if i%2==0:
t2=t2+(i,)
else:
t1=t1+(i,)
print("\nOriginal Tuple :",t)
print("\nFirst Tuple :",t1)
print("\nSecond Tuple :",t2)
3 Write a Python program to input n numbers and store in a tuple and find
minimum and maximum value from the tuple.

Or
Write a program to input ‘n’ employees’ salary and find
minimum & maximum salary among ‘n’ employees.

#Roll No : Name: Practical No

#python program to input numbers and find minimum and maximum

t=tuple()

n=int(input("How many numbers to input : "))

print("\nEnter all numbers one by one")

for i in range(n):

a=input("Enter numbers : ")

t=t+(a,)

print("\nTuple values are : ",t)

mn=min(t)

mx=max(t)

print("\nminimum Value is : ",mn)

print("\nmaximum Value is : ",mx)

4 Write a Python program to Add two tuple and find length.

#Roll No : Name: Practical No


#python program on tuple to Add two tuple and find length

V1=(100,200,300,400,500)
V2=(15,250,120)
print("Tuple one : ",V1)
print("Tuple two :",V2)

V3=V1+V2
print("\n","Two tuple added : ",V3)
print("\n","The length of the added Tuple is : ")
print(len(V3))

5 Write a python program to Check if the given element is present in tuple or not.

#Roll No : Name: Practical No :


#python program to Check if the given element is present in tuple or not.

given_tup = (10, 14, 55, 6, 98)

# printing Original tuple


print("The Original tuple : " + str(given_tup))

N = int(input("\n Enter value to be checked : "))


test = False
for i in given_tup:
if N == i:
test = True
break
print("\nDoes contain required value ? : " + str(test))

6 Write a python program to concatenate the tuples using the ‘+’ operator.

#Roll No : Name: Practical No


#python program to concatenate the tuples using the ‘+’ operator.

Tuple1 = ("Hello", 6, 5, )
Tuple2 = (10.6, 25, "Python")

print("\nTuple 1 is:", Tuple1)


print("\nTuple 2 is", Tuple2)
print("\nConcatenation of Tuple 1 and Tuple 2 is:", Tuple1+Tuple2)

7 Write python code to create two tuples and interchange the tuple values .

#Roll No : Name: Practical No 06


#python program to interchange tuple values
T1=(10,20,30,40)
T2=(50,60,70,80)

T1,T2=T2,T1

print('First tuple value interchange :',T1)

print('\nSecond tuple value interchange :',T2)

8 Write a python program to print sum of tuple elements.

#Roll No : Name: Practical No


#python program to find sum of tuple elements

# sum of tuple elements

# tuples of integer values


mytuple = (20, 10, 20, 30, 20)

# printing original tuple


print("\nThe original tuple is : ",mytuple)

# finding sum of all tuple elements


tupSum = sum(mytuple)

# Printing Tuple sum


print("\nThe sum of tuple elements are : ",tupSum)

9 Write a program to input ‘n’ numbers and store it in tuple.


#Roll No Name: Practical No
#python program on tuple to input ‘n’ numbers and store it in tuple.

t=tuple()
n=int(input("\nHow many numbers to enter : "))
print("Enter all numbers one after other")
for i in range(n):
a=input("enter number: ")
t=t+(a,)
print("\nThe entered numbers are :",t)

10
Write a Python program to clear Tuple elements

#Roll No : Name: Practical No


#Python program to clear Tuple elements

# Creating and printing the tuple


myTuple = (1,5,3,6,8)
print("Initial elements of the tuple : " + str(myTuple))

# Clearing All tuple elements from the Tuple


vartup = list(myTuple)
vartup.clear()

emptyTuple = tuple(vartup)

# Printing empty tuple


print("\nPrinting empty Tuple : " + str(emptyTuple))

11 Write a Python program to output the following tuple elements using slicing
method.

#Roll No Name: Practical No

#python program on tuple using slicing

Tuple = [1, 2.5, 3, 4.9, 5, 6, "Python"]

print("Elements from 2nd to 5th is: ",Tuple[1:5])

print("Elements beginning to 4th is: :",Tuple[:-3])

print("Elements 4th to end is: ",Tuple[3:])

print("Elements from start to end is: ",Tuple[:])

12 Write a Python program to SORT the elements in ascending and descending


order in the given tuples.

#Roll No Name: Practical No


#python program on tuple using sorting in ascending and decending.

# Ascending Sort
Tuple = (2, 3.5, 1, 6, 4)
print("\nSorted integer is:", sorted(Tuple))
Tuple1 = ('e', 'a', 'u', 'o', 'i')
print("\nSorted character is:", sorted(Tuple1))

# Descending Sort
aTuple = (2, 5, 8, 1, 9, 3, 7)
result = sorted(aTuple, reverse=True)
result = tuple(result)
print('\nSorted Tuple in descending order :', result)

You might also like