Python Basics Tutorial 6 – Python Comments

Notes from the video Python Course Singapore | Learn Python Programming | Tutorial 6 – Python Comments’:

 

What are comments?

Comments are text that are ignored by Python and are used to clarify your code. This is especially useful if you need to come back to the code after a while or when you are working in a team.

Comments start with the hash or pound sign (#) and they extend to the end of the line. Comments may begin at the start of a line or after a code.

Here are some examples of comments in green text:

Also note that each line of comment should start with a hash or a pound sign.

It’s important to note that you shouldn’t have a comment in a string, otherwise it would be treated as a string and interpreted by Python.

For example, if you were to type comments in the code:

print(“Hi #this is a comment”)

Python displays Hi #this is a comment

Multi-line comments

If you want to type in comments that are a few lines long you can also enclose the multi-line comments in three single quotes (”’) instead of using the hash or pound sign (#) for every line of comment.

 

Here’s an example:

This is the end of the video.

We hope you learnt more about comments.

We will see you in the next video. 

For this multi-line comment, you can either enclose it with the three single quotes (”’) or you can start each line of comment with a hash sign (#). 

Get FREE LESSONS and TIPS on how YOU can excel at programming and business

Awesome! Thank you for subscribing!