Skip to main content

Command Palette

Search for a command to run...

Comments and Print Statements in Python

Published
โ€ข3 min read
Comments and Print Statements in Python

In this article, we are going to learn about comments and print statements โ€” two essential building blocks for any Python beginner.


๐Ÿง  What Are Comments?

Comments are extremely useful while developing code. They act like little notes or explanations you can leave for yourself or others, especially when revisiting your code after days, months, or even years.

As your code grows into hundreds or thousands of lines, it becomes harder to remember why you wrote a particular line. That's where comments come in handy.

๐Ÿ‘‰ Important: Comments are only visible to you (the reader) and are ignored by Python during execution.

There are two types of comments in Python:

  1. Single-line Comment

  2. Multi-line Comment

๐ŸŸฉ 1. Single-line Comments

Single-line comments are used when your comment fits in a single line. You can also use them to temporarily disable a line of code (helpful during testing or debugging).

โœ… To write a single-line comment, use the # symbol.

๐Ÿ“Œ Examples:

# This is a comment
print("Hello")
# Output: Hello
print("Comments Tutorial")  # This is also a comment
# Output: Comments Tutorial
# This is line 1 comment
# This is line 2 comment
# This is line 3 comment
a = 10
print(a)
# Output: 10

๐ŸŸจ 2. Multi-line Comments

Multi-line comments are useful when your explanation takes up more than one line. Instead of writing # before every line, you can use triple quotes (''' or """).

๐Ÿ“Œ Example with triple single quotes:

'''
This is line 1 comment
This is line 2 comment
This is line 3 comment
'''

๐Ÿ“Œ Example with triple double quotes:

"""
This is line 1 comment
This is line 2 comment
This is line 3 comment
"""

โš ๏ธ Note: Make sure the quotes that you are using are straight quotes ("") instead of (โ€œโ€œ).


๐Ÿ–จ๏ธ What Is the Print Statement?

The print() function is used to display output in Python. There are multiple ways to use it:

โœ… 1. Basic Printing

a = 10
print(a)
# Output: 10

โœ… 2. With Descriptive Text

a = 10
print("a:", a)
# Output: a: 10

Adding a label helps make the output more understandable.

โœ… 3. Using f-Strings (Formatted Strings)

This is a modern and readable way to format output in Python.

name = "Prajitha"
print(f"Name: {name}")
# Output: Name: Prajitha

โœ… Just add an f before the string and wrap variables inside {} to include them in the output.


โœ… Conclusion

Thatโ€™s it for comments and print statements โ€” two simple yet powerful tools to make your code cleaner, more understandable, and easier to debug.

Practice writing your own examples with comments and print statements โ€” you'll thank yourself later when revisiting your code!