Sobes.tech
Middle

What are annotations in Python?

sobes.tech AI

Answer from AI

Annotations in Python are a way to add metadata to function parameters and return values. They do not affect code execution but help document data types and can be used by static type checking tools like mypy.

Example of annotations:

def greet(name: str) -> str:
    return f"Hello, {name}!"

Here, name: str indicates that the parameter name should be a string, and -> str means the function returns a string. Annotations make the code more understandable and easier to maintain.