What is the Comment Line and why use it?
Readability, which is an important issue in the software world, is also a very important comment line and explanation lines. It is a magic wand that allows someone else to understand the code we write or for us to understand someone else’s code. Especially when you work in a company or when you are in a big project, you will deal with thousands of lines of code. Sometimes you may not even remember the codes you wrote when you look back. Here, we, the software developers, eliminate this problem by adding small notes or explanations to the codes we have written to solve this problem.
How to use?
It is very simple to use. Python detects the lines that has the #
sign as comment lines and does not process them. Let’s illustrate with an example.
# We created a variable named text
text = "baransel.dev python lessons"
# We printed the variable named text to the screen
print(text)
As you can see, Python did not process lines starting with the #
sign when processing the code.
What if the explanation I will add to the code is more than one line, what should we do? Do I have to put the #
sign on each line one by one, of course no. If you want to add more than one comment line to the code, just write it in three quotes. Python still detects it as a comment line and doesn’t render it. Let’s show it with an example.
""" Add the two numbers
program that prints the sum
"""
number1 = 5
number2 = 4
sum = number1 + number2
print("Total: ", sum)
As you can see the comment lines color is different. In addition, as you can see in the output you will receive, Python has again detected it as a comment line.
So, will the comment line and comment lines we add slow down our code? Of course no, the comment lines you add to your code will not slow down our program. Also, the little notes you add to our codes make you a better programmer. While you are on this path, you will see that it is important not only to write code, but also to write legible code, so you can think that writing code is an art.