python static typing list code example

Example 1: static typing in python

# Python will always remain a dynamically typed language.
# However, PEP 484 introduced type hints, which make it possible to
# also do static type checking of Python code.

name: str = 'Bob'
age: int = 32
rating: float = 7.9
is_premium: bool = True

Example 2: typing generator python

# Iterator
def infinite_stream(start: int) -> Iterator[int]:
    while True:
        yield start
        start += 1

# Generator        
def infinite_stream(start: int) -> Generator[int, None, None]:
    while True:
        yield start
        start += 1

Example 3: type declaration python

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