Python 3D Academyবাংলা

Type hints: labels on your values

🌍 In real life. In a kitchen, the jars have labels: sugar, salt, rice. A label tells everyone what should be inside, so nobody puts salt in their tea. But a label is only a sticker: nothing stops you pouring salt into the sugar jar.

A type hint is that label for a Python value. Step by step:

  1. after a name, : int says what kind of value it should hold: age: int = 15
  2. in a function, each parameter can have one: def double(n: int)
  3. -> int after the brackets says what the function gives back
  4. Python saves the labels (in __annotations__), but it doesn't check them when the code runs

The idea in one sentence: a type hint like age: int or -> str is a label that says what a value should be; Python doesn't check it when the program runs, but people and tools read it.

Its words:

  • type hint (annotation): name: str, def area(w: int) -> int:
  • ->: the hint for what a function returns
  • type checker: a tool (like mypy) that reads the hints and warns before the code runs
age: int = 15
name: str = "Mina"
happy: bool = True

def double(n: int) -> int:
    return n * 2

print(name, age, happy)
print(double(4))
Read `n: int` as "n should be an int" and `-> int` as "gives back an int". The code runs exactly as it would without them.

Watch the label inspector. Each value arrives as a card coloured by its real type (blue int, green str). A scanner over each slot compares the card with the label. Notice what happens when the label says int but a str arrives: the light turns amber, and the card goes in anyway.

So why write them? Because people and tools read them, before the code ever runs:

  • you, next month: def area(w: int, h: int) -> int tells you how to call it without reading the body
  • your editor (like VS Code) uses them to suggest the right methods and to underline mistakes as you type
  • a type checker (a separate program such as mypy or pyright) reads every hint and warns: "line 9: area expects an int, got a str"

This app runs your code but has no type checker, so here the hints are for you. Python keeps them in a dictionary called __annotations__:

def area(w: int, h: int) -> int:
    return w * h

print(area(3, 4))
print(area("ab", 3))
print(area.__annotations__)
area("ab", 3) breaks the label but still runs: "ab" * 3 is "ababab". Only a type checker would have warned you.

Mistakes and edge cases.

  • Expecting Python to stop a wrong type. def f(n: int) -> int: return n * 2 called with "ab" happily returns "abab": the label isn't a lock.
  • Hints that lie. A hint that no longer matches the code misleads every reader; update it with the code.
  • Overdoing it. Hint function parameters and returns; a plain count = 0 inside a function doesn't need : int.

Quiz

  1. What does this print?

    def half(n: int) -> int:
        return n // 2
    
    print(half(9))
  2. def double(n: int) -> int is called as double("ab"). What does Python do?

    • Runs it anyway and gives back 'abab'
    • Raises a TypeError because n should be an int
    • Turns "ab" into an int first
  3. What does this print?

    def twice(s: str) -> str:
        return s * 2
    
    print(twice(3))
  4. What does -> bool after a function's brackets mean?

    • The function should give back a bool
    • Every parameter must be a bool
    • Python turns the answer into a bool
  5. Which line has a mistake?

    def greet(name: str) => str:
        return "Hi " + name
    
    print(greet("Mina"))

Coding tasks

  1. Add type hints to full_name: both parameters are str, and it gives back a str. Keep what it does the same.

  2. Finish is_adult(age: int) -> bool: it gives back True when age is 18 or more, otherwise False.