- Ctrl+C+Python
- Posts
- π Python Basics: Understanding Comments
π Python Basics: Understanding Comments
Comments are an essential part of writing clean and readable Python code. They help you explain what your code does, making it easier for you (and others) to understand and maintain. In this post, we'll cover everything you need to know about comments in Python!
Take your learning to the next level with quick, aesthetic coding tutorials on my YouTube channel! πβ¨
π₯ Watch now: @CtrlCPython
Subscribe for bite-sized Python lessons with lofi vibes & clean code. βπΆ
1. What Are Comments?
A comment is a line of text in your code that Python ignores when executing the program. Comments are used to add explanations, make notes, or temporarily disable parts of the code.
2. Single-Line Comments
In Python, you create a single-line comment by using the # symbol. Anything after # on that line is ignored by Python.

Python will execute the print() function but ignore the comments.
3. Multi-Line Comments (Using Multiple #)
Python doesn't have built-in multi-line comment syntax, but you can create them by using multiple # symbols:

This is a common way to write longer explanations in Python.
4. Multi-Line Strings as Comments
Another way to write multi-line comments is by using triple quotes """ or '''). Technically, these are multi-line strings, but when not assigned to a variable, Python ignores them.

This method is often used for docstrings (which weβll cover next).
5. Docstrings: Special Comments for Documentation
A docstring is a special type of multi-line comment used to describe modules, functions, and classes.

You can access a functionβs docstring using:

Output:
This function prints a greeting message.6. Why Use Comments?
β Improve Code Readability β Explain what complex parts of your code do.
β Make Maintenance Easier β Help future developers (or your future self) understand the code.
β Debugging β Temporarily disable parts of your code using comments.
Bad Example:

Better Example:

Use comments wiselyβdon't overdo them!
π Conclusion
Comments are a powerful tool in Python that help you write clearer, more maintainable code. Use them to explain why your code does something, not just what it does.
What Python basics do you want to learn next? Let me know in the comments! ππ
Happy coding! ππ
Digital Shade
Reply