Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
What are Annotations? | Annotations
Mastering Python: Annotations, Errors and Environment

What are Annotations?

To become a proficient programmer, it is important to annotate your code effectively.

Annotations are code documentation within your code that help other programmers understand its functionality and purpose.

In Python, both annotations and comments are ways to provide additional information about your code. Annotations are used to specify the types of arguments and return values for functions, whereas comments are used to provide more general information and explanations about the code. Annotations are a part of the code and are interpreted by tools such as linters and type checkers, whereas comments are simply ignored by the Python interpreter.

Types of Annotations:

  • Variable/Argument Annotation
  • Return Annotation
  • Docstring

Let's take a look at an example in VS Code:

If you hover your cursor over a function or any other object in VS Code, information about that object will be displayed.

Variable/Argument Annotation

This is the type of data that the variable or argument expects.

Return Annotation

This annotation informs programmers about the type of data that is returned by the function.

Docstring

The docstring is a description of the function. It is used to provide a brief overview of how the function works for programmers.

Additionally, docstrings can also be used for classes (which will be covered in the OOP in Python course).

Everything was clear?

Section 1. Chapter 1
course content

Course Content

Mastering Python: Annotations, Errors and Environment

What are Annotations?

To become a proficient programmer, it is important to annotate your code effectively.

Annotations are code documentation within your code that help other programmers understand its functionality and purpose.

In Python, both annotations and comments are ways to provide additional information about your code. Annotations are used to specify the types of arguments and return values for functions, whereas comments are used to provide more general information and explanations about the code. Annotations are a part of the code and are interpreted by tools such as linters and type checkers, whereas comments are simply ignored by the Python interpreter.

Types of Annotations:

  • Variable/Argument Annotation
  • Return Annotation
  • Docstring

Let's take a look at an example in VS Code:

If you hover your cursor over a function or any other object in VS Code, information about that object will be displayed.

Variable/Argument Annotation

This is the type of data that the variable or argument expects.

Return Annotation

This annotation informs programmers about the type of data that is returned by the function.

Docstring

The docstring is a description of the function. It is used to provide a brief overview of how the function works for programmers.

Additionally, docstrings can also be used for classes (which will be covered in the OOP in Python course).

Everything was clear?

Section 1. Chapter 1
some-alt