Computer Science, asked by shreyaamin20, 1 month ago

Write program to demonstrate use of tuple in python
● Add and show details i.e roll no, name and marks of three subjects of N students in a list of tuple
● Display details of a student whose name is X

Answers

Answered by poojan
7

Program 1: Defining the tuples and data in the program itself.

l=[(1,'A',41,32,64),(2,'X',71,93,64), (3,'P',48,76,92)]

#displaying details of each student.

for i in l:

    print(i)

print('==========')

#extracting the details of student X  

for i in l:

   if i[1]=='X':

       print(i)

       print(type(i))

       break

Output:

(1,'A',41,32,64)

(2,'X',71,93,64)

(3,'P',48,76,92)

============

(2,'X',71,93,64)

<class 'tuple'>

Program 2: Giving values at run time.

N=int(input())

l=[]

for i in range(N):

   roll_no=int(input())

   name=input()

   sub1_marks=int(input())

   sub2_marks=int(input())

   sub3_marks=int(input())

   l.append((roll_no, name, sub1_marks, sub2_marks, sub3_marks))

Student_name=input()

for i in l:

   if i[1]==Student_name:

       print(i)

       break

Input:

3

1

A

41

32

64

2

P

56

49

78

3

X

93

71

60

X

Output:

(3, 'X', 93, 71, 60)

Tuples are ordered and unchangeable. So, it protects the data and if one wants to change the data, they need to convert it into a list explicitly.

Trying to change it:

l[0][1]=6

TypeError: 'tuple' object does not support item assignment

Learn more:

Raju has a square-shaped puzzle made up of small square pieces containing numbers on them. He wants to rearrange the puzzle by changing the elements of a row into a column element and column element into a row element. Help Raju to solve this puzzle.

https://brainly.in/question/16956126

Python program to find absolute difference between the odd and even numbers in the inputted number

https://brainly.in/question/11611140

Similar questions