from typing import Union def add(a: Union[int, float], b: Union[int, float]) -> Union[int, float]: return a + b result1 = add(1, 2) result2 = add(1.5, 2)
from typing import List, Union def get_first_item(lst: List[Union[int, str]]) -> Union[int, str]: return lst[0] result1 = get_first_item([1, 2, 3]) result2 = get_first_item(['a', 'b', 'c'])In this example, the `get_first_item` function takes in a list of items that could be either an `int` or a `str`. The function returns the first item in the list, which could also be either an `int` or a `str`. Again, this allows for flexibility in the input and output types. The `UnionType` is part of the `mypy` package, which is a static type checker for Python.