Editor

Create and Use a namedtuple

Intermediate · Write the code · from Python collections: Counter, defaultdict, deque, and More

From the lesson: Python collections: Counter, defaultdict, deque, and More

Imagine you have a basic toolbox with a hammer and a screwdriver. They work fine for simple jobs. But when you're building a deck, you really want a power drill, a level, and a tape measure.

Read the lesson

Create a namedtuple called Book with fields title, author, and pages. Create a book instance with title 'Python Basics', author 'Guido', and pages 350. Print the book's title and pages in the format: Python Basics has 350 pages.

from collections import namedtuple
# Create a Book namedtuple and print info

More collections exercises

All Python exercises · 50 practice problems · Standard Library Essentials quiz