type — Easy Examples
Declares a type alias (3.12+)
Type aliases (Python 3.12+)
Using the type statement for type aliases.
python
# type statement creates a type alias # type Point = tuple[int, int] # Python 3.12+ syntax # Using typing module (works in all versions) from typing import TypeAlias # Pre-3.12 type alias Point: TypeAlias = tuple[int, int] def distance(p1: Point, p2: Point) -> float: return ((p1[0]-p2[0])**2 + (p1[1]-p2[1])**2) ** 0.5 p1 = (0, 0) p2 = (3, 4) print(f"Distance: {distance(p1, p2)}") # type() as a builtin function print(f"type(42): {type(42)}") print(f"type('hi'): {type('hi')}") print(f"type([1]): {type([1])}")
Expected Output
Distance: 5.0
type(42): <class 'int'>
type('hi'): <class 'str'>
type([1]): <class 'list'>'type' is both a soft keyword (Python 3.12+) for type aliases and a builtin function for checking types. As a keyword, it creates cleaner type alias syntax.
Want to try these examples interactively?
Open Easy Playground