Python Comments

good code contains comments

The need for comments in your code is essential to writing good code. I’ve said this before in previous tutorials, but if you want to stand out as a software developer, you don’t just need to learn to write code; you need to learn to write good code.

Comments are a good way to tell the person reading your code what your code does exactly and why you made certain crucial decisions in your code. If you’ve ever had to review someone else’s code for submission, you will agree that having good inline documentation of code is extremely valuable. It become a life and death decision when it’s code written by someone who is no longer on the project.

In python, single line comments are created using a hashtag (#). Here’s a simple example of some single line comments

# print some text
print("some text")
# do some math
a = 5 * 7

In this example, the comments aren’t as necessary because it’s pretty clear what we are trying to achieve in the code, however, if this had been a complex piece of software and these comments hadn’t been there, it would have been very difficult to determine what the author meant to do.

Python Block Comment

Block comments can be created using three double quotation marks. In some IDE’s, entering the first set of triple double quotes will automatically create the block for you. Here is an example of a python block comment:

"""
you can write
as many
lines of commented text
as your heart desires
here
"""
print("done commenting")

Leave a comment