Comments in python- Step1 To Code Like A Pro

Comments are nothing but the lines ignored😒 by the python interpreter.

To comment out any line of code in python you just need to put a ‘#’ hash mark before the line of code you want to comment out.

Example:-

#This is a comment.
print("however, this will be printed") #This is also a comment

Official documentation python.

Hello Pythonistas, welcome back.

Previous Post’s Challenge’s Solution

In the last post, I gave you the challenge to print your name and age.

Here’s how you had to do that:

print("Your Name")
print(5)

Output:-

Your Name
5

You just need to put your👩🧑 name inside the quotations "" inside the parenthesis of the print function.

And to print your age you just need to put the number🔢 inside of the print function.

And then I asked to put a hash mark '#' before the lines of code.

#print("Your Name")
#print(5)

Did you get no☹ output when you run the program? Did you feel it was an error❌?

Well, it isn’t.

It is a comment in python.

Were you able to guess🤔 it right✅ or had to take help from Google 🌐? Tell me in the comment section below.

Here in this post, we will dive deeper to know more about them. Let’s get started.

Types of comments

There are broadly three types of comments in python.

Single line

These comments are the simplest ones as the name suggests they are just one line to be ignored by the interpreter. You simply put a hash mark before them and that’s it.

Examples:

#This is a single line comment
print("Hello World")
#print("This too will be treated as a comment")

Multi-line

Multi-line comments cover more than one line. However, python doesn’t itself provide a way to give a multiline comment but, there are still two ways you can still do it.

#First
#Put a
# hash 
#before all the lines
#you want to comment

print('This must be quite obvious, anyways the next option is:')

  #Second
"""put the
multi line either between three 
double quotes or"""
'''between
three
single quotes'''
#

These are not comments these are just strings not assigned to any variable or any function.

You’ll get more clear about this as you go further in the python journey for now just understand this much.

Even Guido van Rossum the creator of the Python programming language suggests this method for multiline comments.

In-line

These are given with the line that contains code that will be executed and not alone. Let’s see some examples to make it more clear.

print("Hiii")#this is an inline comment
print(" '#' This is not a comment this will be printed")

Why are comments so important?

You must be wondering🤨 ok fine I got to know that comments are the line of code that the python interpreter ignores.

But, what is the need for any such line of code which is going to be ignored😒?

Well, comments are there to improve the readability👀 of your code. Using them in the code that you write is very important❗ for others and your understanding.

Still in doubt🧐 of its importance?

Read the examples below.

How are comments useful to you?

Tell me do you remember what did you study in grade 6th(of course only if you are not in 6th or 7th grade)?

Most probably no❌, you don’t remember that anymore.

The same is the case with the code you write✍ you don’t remember it for long. But there is a great in fact obvious chance that you will need that code again.

As Guido Van Rossum, the creator of Python has noted: Code is read more often than it is written.

Comments come here to help🤝, to make your task easier as they explain in simple language the logic of the program.

How are comments useful to Others?

Now take this example you🧑 and your friend👦 are working on the same project.

You are to complete half of it and your friend is responsible to complete the other half of it. You finished your part and gave it to your friend to complete the other half.

But, here comes a problem your friend is unable to understand🥴 the logic of the program. And you both couldn’t complete the project on time🕒 and you guys lost your client😡.

Feeling sad😭?

Well, don’t because this was just an example.

The reality is when you were writing the project you included comments in the it because you read the above scenario and knew what could happen if you don’t comment your project well.

Your friend understood the logic of the project quickly 👩‍💻in minutes and you guys finished the project 10 days before time.

The client became happy😀 with your speed and clarity and doubled your payment🏅.

I hope these examples are enough😅 to get the importance of comments.

Conclusion

In this post, we saw:

  • what are comments in python,
  • the three types of comments, and
  • how learning them will save you dollars💵 and dollars💵 in the future.

Challenge🧗‍♀️

Okay, the challenge for this post is:

  • to find out what is meant by data type, and
  • how can you know the data type of any object in python?
  • And then finally, write a code to know the data type of 10.

Go fast. I am waiting. Comment your answers below.

This was it for the post. Comment below suggestions if there and tell us whether you liked it or not. Do consider sharing this with your friends.

See you in the next post till then have a great time.😊 Bye-bye 👋

This Post Has 7 Comments

  1. www.xmc.pl

    TY for blogging this, it had been quite handy and helped a ton Great theme, did you make it on your own? Looks superb! Facinating post to check on this awesome blog. Never write any replies but now i just did not resist ..

  2. Maitry

    Thanks a lot. Happy to know you liked it. Stay tuned to get more on python programming. And yes I do this on my own.

  3. SteveLooto

    H?, eg vildi vita ver? ?itt.

  4. Maitry

    Sorry, I cannot understand what you are saying. Can you ask in English?

Comments are closed.