Table of Contents

Student Records – Tuple

Example Description
Learn how to store and print student records using tuples in Python. See the example code and understand the logic behind it.

Introduction:

This Python code shows how to store records of students in a tuple and print them. Each record contains the student’s serial number, roll number, name, and marks.

Code:

#To store records of students in tuple and print them
st=((101,"Aman",98),(102,"Geet",95),(103,"Sahil",87),(104,"Pawan",79))
print("S_No"," Roll_No"," Name"," Marks")
for i in range(0,len(st)):
 print((i+1),'\t',st[i][0],'\t',st[i][1],'\t',st[i][2])

Logic:

  1. Create a tuple called ‘st’ that contains the student records.
  2. Each record is represented as a tuple itself, with the serial number, roll number, name, and marks as elements.
  3. Print the header of the table, specifying the column names.
  4. Use a for loop to iterate through each record in the ‘st’ tuple.
  5. Inside the loop, print the serial number, roll number, name, and marks of each student.

Output:

>> S_No Roll_No Name Marks

>> 1         101           Aman   98

>> 2        102          Geet       95

>> 3        103          Sahil      87

>> 4        104          Pawan   79