Commenting your code or thoughts is necessary for all coding languages. Sometime we are quite lazy with comments, but then we end up regretting it a few weeks, months, or years after as it then is harder than it should be to re-read your code and to understand it as quickly as possible. Adding comments during or on completion of your coding project can really minimize the time it takes for us to understand our own or someone else's code.
Single line comment
Commenting in python can be done with a simple hashtag in front of your code line, #.
For example:
# This is a comment in python.
This is not a comment in python.
Multiline comment
In contrast to many other languages, python does not support multiline commenting, which is very useful for working with blocks of comments where you as a code owner want to write a bit more comments. Fortunately many IDEs (Integrated development environment) such as Spyder, Jupyter notebooks, PyCharm have created quick commands for adding Hashtags so that you don't have to manually add them.
A more creative way to do multiline commenting is by using triple quotation marks and not assigning the text to a variable. That will technically also serve as a multiline comment.
# ====================================================
# My Multiline comment
"""
note that multiline text can be done with triple quotation marks.
and yes this is an example.
"""
# ====================================================
Spyder
A single comment in Spyder can be done with ⌘ + 1 (Ctrl + 1 = Windows user).
Multiline comments can be done by selecting several rows and pressing: ⌘ + 4
(Ctrl + 4 = Windows user).
Reversing a multiline comment can be done with ⌘ + 5 (Ctrl + 5 = Windows user). Note that you will have to either have the same rows selected or simply stand inside your comment block.
# =============================================================
# This
# is
# a
# multiline
# comment
# =============================================================
Jupyter
For Jupyter commenting can be done with the key combination:
Mac = Command + /
Windows = Ctrl + /
This short command works for both commenting and uncommenting. However, most international keyboard (non US keyboard) seem to struggle with this short command due to needing another key to do "/".
Learn more about Python here for all my posts: https://www.pls-fix-thx.com/python
If you have found this article or website helpful. Please show your support by visiting the shop below.