NamedTuple with Defaults in Python
Level 4132 keystrokes
This creates a NamedTuple with type hints and default values.
Python code
# typing.NamedTuple defines named tuples with class syntax1from typing import NamedTuple2 # Point is an immutable tuple whose fields have names3class Point(NamedTuple): # x and y are required fields with type hints4 x: int5 y: int # z defaults to 0; fields with defaults must come last6 z: int = 07 # z is omitted, so it uses the default 08p1 = Point(1, 2)# All three values are given, so z is 39p2 = Point(1, 2, 3)# Prints Point(x=1, y=2, z=0) Point(x=1, y=2, z=3)10print(p1, p2)Output
Point(x=1, y=2, z=0) Point(x=1, y=2, z=3)
Practice typing this code. Results are not recorded in the rankings.
Symbols in this code
The symbols you type in this Python code and how often they appear.
,× 4:× 4(× 4)× 4=× 3