type
Soft KeywordPython 3.12+Advanced
Declares a type alias (3.12+)
Quick Info
- Documentation
- Official Docs
- Python Version
- 3.12+
- Install
N/A — requires Python 3.12+
Learn by Difficulty
Quick Example
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])}")
'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.
Try in PlaygroundTags
languagesyntaxcoretypingtype-alias