← Lesson
Type Hints
Joeven
Run
Reset
Python loads on first run
def parse_query(args: dict[str, object]) -> str | None: q = args.get("q") if not isinstance(q, str): return None return q def budget_left(used: dict[str, int], limit: int) -> int | None: total = 0 for n in used.values(): if not isinstance(n, int): return None total += n left = limit - total if left < 0: return None return left print("good", parse_query({"q": "python"})) print("bad json number", parse_query({"q": 12})) print("missing", parse_query({})) print("left", budget_left({"search": 2, "read": 1}, 8)) raw: dict[str, int] = {"k": "3"} # hint says int; value is str print("hint says int, real type", type(raw["k"]).__name__) print("isinstance int?", isinstance(raw["k"], int)) print("True is int?", isinstance(True, int)) print("True is bool first", isinstance(True, bool))
Run to execute this in your browser. Nothing is sent to a server.