HomeToolsAbout

Type Hint

Source

In the function greeting, the argument name is expected be a type of str that returns the type str

def greeting(name: str) -> str: return 'Hello ' + name

Types are not strongly enforced in Python

What is it

Below code is for Python 3.10+

# typing a variable age: int = 1 # uninitialized variable can also be typed status: int # uninitialized can be used later if response: status = 200

Built-in Types

x: int = 1 x: float = 1.0 x: bool = True x: str = "test" x: bytes = b"test" # collection x: list[int] = [1] x: set[int] = {6, 7} # mappings x: dict[str, float] = {"field": 2.0} # multiple types x: list[int | str] = [3, 5, "test", "fun"] # Function def stringify(num: int) -> str: return str(num) # Default value def show(value: str, excitement: int = 10) -> None: print(value + "!" * excitement)
AboutContact