Lesson 56 of 60
โฎPrev Nextโฏ

PY-101 ยท Advanced Python

Type hints

โฑ 8 min ๐Ÿ Real Python 3 ๐ŸŽฌ Animated

56of 60

Your note

My notes for this lesson

Select any text in the lesson to highlight it or add a note. Saved in this browser.

    No highlights yet. Select a sentence in the lesson, then pick a colour or add a note.

    All my bookmarks and notes

    Reading settings

    For every lesson, saved in this browser.

    Text size
    Line spacing
    Font

    With IaaS you rent the virtual machine. Microsoft looks after the building, power and hardware; you patch the operating system.

    โšก The 30-second answer

    Type hints are optional notes that say what type a value should be, such as def total(prices: list[float]) -> float:. Python does not enforce them at run time, but editors and checkers like mypy use them to catch mistakes early and to autocomplete. They also make code easier for people and AI tools to read.

    • Annotate parameters and return types
    • Use list[str], dict[str, int] and | None
    • Know hints are not enforced

    Try it

    Watch it come alive, run it yourself, then prove it. Real Python, right on this page.

    Watch Python run the example. Every variable is a jar; watch the values change and the output appear.

    Type hints, running
    1. def average(marks: list[float]) -> float:
    2. return sum(marks) / len(marks)
    3. def find_user(users: dict[str, int], name: str) -> int | None:
    4. return users.get(name)
    5. print(average([72.0, 88.0, 91.0]))
    6. print(find_user({"asha": 29}, "asha"), find_user({}, "ravi"))
    7. print(average.__annotations__["return"])
    Press โ–ถ to watch Python run this, one line at a time.
    1. The problemA teammate calls your function with a price as text, "99", and the bug appears weeks later. A type hint would have made the editor underline it the moment they typed it.
    2. The Python ideadef f(x: int, names: list[str]) -> float:Type hints are optional notes that say what type a value should be, such as def total(prices: list[float]) -> float:.
    3. In real lifeFastAPI reads your type hints to validate incoming JSON and generate API documentation automatically.

    Start with a question

    A teammate calls your function with a price as text, "99", and the bug appears weeks later. A type hint would have made the editor underline it the moment they typed it.

    Modern Python codebases, FastAPI and AI SDKs rely on type hints for validation, docs and autocomplete.

    Explain it like I'm 10

    Type hints are labels on jars: "sugar only". Python will not stop you putting salt in, but everyone (and your editor) can see the label.

    The technical explanation

    def greet(name: str) -> str: says name is a str and the function returns a str.

    list[int], dict[str, float] and tuple[int, int] describe collections. str | None means "a string or None".

    Python ignores hints at run time. greet(5) still runs; a checker like mypy reports it.

    Syntax

    def f(x: int, names: list[str]) -> float:
        ...
    age: int | None = None

    โ–ถ Run the example ๐ŸŽฌ Watch it come alive

    Real-world scenario

    FastAPI reads your type hints to validate incoming JSON and generate API documentation automatically.

    Ready for more?The full lesson adds the technical explanation, interview questions and quick revision.

    Test yourself

    5 questions. Get 4 right and this lesson is marked complete.

    1. 1. Does Python stop you calling def f(x: int) with a string?
    2. 2. How do you write a return type?
    3. 3. Hint for "a list of strings"?
    4. 4. What does str | None mean?
    5. 5. Which tool checks type hints?

    Questions people ask

    Do type hints make Python faster?

    No. Standard Python ignores them at run time. They help editors, checkers, documentation and readers.

    Should beginners use type hints?

    Yes on function signatures. They make your intent clear and your editor much more helpful, at very little cost.

    Interview questions

    How this topic comes up in Python interviews. Answer out loud first, then open the model answer.

    beginner Do type hints make Python faster?

    No. Standard Python ignores them at run time. They help editors, checkers, documentation and readers.

    beginner Should beginners use type hints?

    Yes on function signatures. They make your intent clear and your editor much more helpful, at very little cost.

    intermediate Does Python stop you calling def f(x: int) with a string?

    No, hints are not enforced at run time. Tools check hints; Python does not.

    intermediate How do you write a return type?

    def f() -> int:. Use -> before the colon.

    Quick revision

    Remember

    • Annotate parameters and return types
    • Use list[str], dict[str, int] and | None
    • Know hints are not enforced
    • Hints are documentation that tools can check. Add them to function signatures first; that is where they help most.

    Syntax

    def f(x: int, names: list[str]) -> float:
        ...
    age: int | None = None

    Official documentation