w3resource

Python NamedTuple exercises with solutions

Python NamedTuple Data Type [ 9 exercises with solution ]

[An editor is available at the bottom of the page to write and execute the scripts.   Go to the editor]

NamedTuple assign meaning to each position in a tuple and allow for more readable, self-documenting code. They can be used wherever regular tuples are used, and they add the ability to access fields by name instead of position index. Namedtuples are created using the collections.namedtuple() factory function.

Namedtuples offer several advantages over tuples:

  • The values of namedtuples cannot be changed after they have been created because they are immutable.
  • Namedtuples are hashable, which means they can be used as dictionary keys.
  • A namedtuple is comparable, which means that it can be sorted and compared with other namedtuples.

1. Write a Python program that defines a NamedTuple called Employee with fields like name, age, and country. Print each employee's name and country.
Click me to see the sample solution

2. Write a Python program that defines a NamedTuple called Point with fields x, y and z representing the coordinate of a point. Access and print the fields.
Click me to see the sample solution

3. Write a Python program that creates a dictionary where keys are food names and prices are instances of the Food named tuple.
Click me to see the sample solution

4. Write a Python program that defines a NamedTuple called Student with fields like name, age, and marks (a list of subjects and marks).
Click me to see the sample solution

5. Write a Python function that takes a namedtuple as input of the item's name and price. Returns the item's name and price.
Click me to see the sample solution

6. Write a Python function that takes a Student named tuple as an argument and calculates the average grade.
Click me to see the sample solution

7. Write a Python program that defines a NamedTuple named "Circle" with fields 'radius' and 'center' (a Point NamedTuple representing the coordinates of the center of the circle). Create an instance of the "Circle" NamedTuple and print its attributes.
Click me to see the sample solution

8. Write a Python program that defines a NamedTuple named "Triangle" with fields 'side1', 'side2', and 'side3'. Now write a function that takes a "Triangle" NamedTuple as input and calculates its area.
Click me to see the sample solution

9. Write a Python program that defines a NamedTuple named "Car" with fields 'make', 'model', 'year', and 'engine' (a NamedTuple representing engine details). Create an instance of the "Car" NamedTuple and print its attributes.
Click me to see the sample solution

Python Code Editor:

More to Come !

Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.

Test your Python skills with w3resource's quiz



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/python-exercises/extended-data-types/index_namedtuple.php