Categories
Uncategorized

Comments in Python

How’s going everyone?

Hope you all practised how to write python syntax.

Let’s move forward with the next topic i.e, Comments in python:
Comments are descriptions that help programmers better understand the intent and functionality of the program.
They are completely ignored by the Python interpreter.

When working with any programming language, you include comments in the code to notate your work. This details what certain parts of the code are for, and lets other developers know what you were up to when you wrote the code. This is a necessary practice, and good developers make heavy use of the comment system. Without it, things can get real confusing, real fast.

Types of Comment:

1.Single-Line Comments

Such a comment starts with a hash character (#), and is followed by text that contains further explanations.
For example
# defining the post code
postCode = 490023

Single-line comment example

2.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:
For example
#This is a comment
#written in
#more than just one line
print(“Hello, World!”)

Multi-line comment 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:
For example
”’This is a comment
written in
more than just one line”’
print(“Hello, World!”)

Multi-line comment example no. 2

How to Write Better Comments?

1.Use comments to describe what a function does and not the specific details on how the function does it.


2.Try to remove as many redundant comments as possible.

3.Try writing code that can explain itself, using better function/variable name choice.


4.Try to make the comments as short and concise as possible.

Keep growing, keep Coding 😊

Leave a comment

Design a site like this with WordPress.com
Get started