Python Comments — 2-Minute Guide with Exercise

Rate this post

Wouldn’t reading code be much easier if the author constantly shared their thoughts with you? Commenting is good practice in Python because it helps others (and your future self) understanding your code much better. Writing commented code makes you more productive in the long term!

There are two types of comments: one-line comments and multi-line comments. Let’s dive right into them!

One-Line Comments

One-line comments begin with the hash (#) character and reach to the end of the line. The newline character terminates the meaning of the comment—which is for the Python interpreter to ignore the commented text. A special case are inline comments that are used after a regular Python statement but before the newline character. The PEP 8 standard recommends to use them sparingly.

# This is a one-line comment

print('hi') # This is an inline comment

Multi-Line Comments

While one-line comments terminate by the end of line, multi-line comments span multiple lines to describe code in greater detail. There are two ways to create multi-line comments: (1) enclosing the commenting text in triple quotes """ or ''' to create a documentation string, or (2) creating a block comment by using multiple hash # characters, one per line. According to the PEP 8 standard, multi-line comments should be avoided.

# 1. Multi-Line Comment: Documentation String
def say_hi():
    '''Print the string 'hi'
    to the shell.'''
    print('hi')

# 2. Multi-Line Comment: Block Comment
# This is still a comment. It's
# called 'Block Comment'. 

Exercise: Use Comments to Fix This Faulty Code

The following code snippet is faulty.

Exercise: Use one-line comments, inline comments, block comments, or documentation strings to fix this code. Run the code to check if the output is:

Comments
are
great!

You’ll find the correct solution at the end of this article!

Where to Go From Here?

Enough theory. Letโ€™s get some practice!

Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation.

To become more successful in coding, solve more real problems for real people. Thatโ€™s how you polish the skills you really need in practice. After all, whatโ€™s the use of learning theory that nobody ever needs?

You build high-value coding skills by working on practical coding projects!

Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?

๐Ÿš€ If your answer is YES!, consider becoming a Python freelance developer! Itโ€™s the best way of approaching the task of improving your Python skillsโ€”even if you are a complete beginner.

If you just want to learn about the freelancing opportunity, feel free to watch my free webinar โ€œHow to Build Your High-Income Skill Pythonโ€ and learn how I grew my coding business online and how you can, tooโ€”from the comfort of your own home.

Join the free webinar now!

Exercise Solution

#print('Python')
print('Comments')#; print('Suck')
print('are')
#print('not')s=42;print(s)
#print('at')
#print('all')
print('great!')