#8 Comments

Previous post: https://codinghelp.site/knowledgebase/python/7-else-statement/

Next post: https://codinghelp.site/knowledgebase/python/9-data-types/

The “comments”

Comments in python are used as “notes” to the reader. They do not interfere with the code.

“Comments” Syntax

Comments are written with a hashtag, “#”. Some of the usages are, making the code more readable, preventing a line of code from executing and to explain code.

Whatever we put in the comment, it will not have an effect on the output of the code. There are different sorts of comments, multiline and single line.

Both of them work, we can make multiline comments from “#” too. We do this by putting a “#” at the start of each line, comment.

Examples

Example of making code more readable:

This code looks messy, don’t worry, we can make it more readable with some formatting and comments!

Example of explaining code:

Example of commenting out code so it doesn’t run:

Something is wrong it seems, let’s comment out the first print statement and see if the code runs!

Voila! Now the code will work.

Exercise

Uh oh! Seems like something is wrong with the following code… Please comment out the mistakes!

Answer key

image