What are comments in Python?

Noor Fakhry
3 min readNov 11, 2021

--

Sometimes when you are writing Python code, you feel like you want to explain the code that you are writing, by writing comments on it. In order to use it as a reference for anybody who will read your code in the future including yourself, because, believe me, when you read the code that you have written yourself in the future, you are not going to remember or understand anything from it.

  • Comments can be used to explain Python code.
  • Comments can be used to make the code more readable.
  • Comments can be used to prevent execution when testing code.

But, how can you do that?

Comments start with a #, and the Python interpreter will ignore them.

Example:

Comments can be placed at the end of a line, and Python will ignore the rest of the line

Example:

A comment does not have to be text that explains the code, it can also be used to prevent Python from executing code

Example:

Multi-Line Comments

Python does not really have a syntax for multi-line comments.

To add a multiline comment you could insert a # for each line.

Example:

Or, not quite as intended, you can use a multiline string, Since Python will ignore string literals that are not assigned to a variable, you can add a multiline string (triple quotes) in your code, and place your comment inside it.

Example:

As long as the string is not assigned to a variable, Python will read the code, but then ignore it, and you have made a multiline comment.

Conclusion:

Python comments can help you write more maintainable and readable code and save you, your teammates, and future developers on your project a lot of time and effort.

If you like this article follow me here on medium for more articles.

And you can follow me on Linkedin.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Noor Fakhry
Noor Fakhry

Written by Noor Fakhry

0 Followers

Creator @Youtube | Instructor @Udemy | Software Engineer | Masters in CS | Youtube Channel => https://bit.ly/3d7ZP5j

No responses yet

Write a response