﻿ Python NamedTuple function: Calculate average grade

Python NamedTuple function: Calculate average grade

Python NamedTuple Data Type: Exercise-6 with Solution

Write a Python function that takes a Student named tuple as an argument and calculates the average grade.

Sample Solution:

Code:

``````from collections import namedtuple

# Define a NamedTuple named 'Student' with fields 'name', 'age', and 'marks'
Student = namedtuple("Student", ["name", "age", "marks"])

total_marks = sum(student.marks)

# Create an instance of the Student NamedTuple
student1 = Student(name="Ain Ruth", age=22, marks=[89, 92, 75, 90, 86])

# Calculate and print the average grade for the student
``````

Output:

```Average Grade: 86.4
```

In the exercise above, we declare a "Student" NamedTuple with the fields 'name', 'age', and 'marks'. The "calculate_average_grade()" function takes a "Student" NamedTuple as input, calculates the average grade from the 'marks' field, and returns it. Next, we create an instance of the "Student" NamedTuple and calculate and print the average grade.

Flowchart:

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.

﻿