The namedtuple in Python is a factory function from the collections module that returns a tuple subclass with named fields. It improves code readability and offers immutable, memory-efficient objects for structured data access.
Time limit: 0
Quiz Summary
0 of 15 Questions completed
Questions:
Information
You have already completed the quiz before. Hence you can not start it again.
What is the correct way to define a namedtuple called Point with fields x and y?
Correct
Incorrect
Question 4 of 15
4. Question
What is the output of:
from collections import namedtuple Color = namedtuple(‘Color’, [‘r’, ‘g’, ‘b’]) c = Color(255, 255, 0) print(c.g)
Correct
Incorrect
Question 5 of 15
5. Question
Are `namedtuple` objects mutable?
Correct
Incorrect
Question 6 of 15
6. Question
Which method returns a dictionary of field names and values in a `namedtuple`?
Correct
Incorrect
Question 7 of 15
7. Question
What does `._fields` return on a `namedtuple` class?
Correct
Incorrect
Question 8 of 15
8. Question
How do you replace a field value in a `namedtuple`?
Correct
Incorrect
Question 9 of 15
9. Question
What will this return?
Point = namedtuple(‘Point’, ‘x y’) p = Point(3, 4) p[0]
Correct
Incorrect
Question 10 of 15
10. Question
Which method returns a readable string form of a `namedtuple`?
Correct
Incorrect
Question 11 of 15
11. Question
What is the output?
City = namedtuple(‘City’, ‘name population’) c = City(‘Paris’, 2148000) print(len(c))
Correct
Incorrect
Question 12 of 15
12. Question
Can you use `namedtuple` to create nested structures?
Correct
Incorrect
Question 13 of 15
13. Question
What is the benefit of `_make(iterable)` in `namedtuple`?
Correct
Incorrect
Question 14 of 15
14. Question
How do you convert a `namedtuple` to JSON-friendly format?
Correct
Incorrect
Question 15 of 15
15. Question
What happens if you access a non-existent field in a `namedtuple`?*
Correct
Incorrect
Summary
Python’s `namedtuple` allows you to create readable, memory-efficient, and immutable structured objects. It supports field access by name, dictionary conversion, and indexing—making it a great alternative to classes for lightweight data structures.
At QuizOrbit, our team specializes in fun, informative quizzes across topics like general knowledge, technology, and entertainment. Boost your knowledge with our expert-designed quizzes.