Search⌘ K

Commenting Code

Explore how to use comments in Python to enhance your code's clarity and maintainability. Understand how to comment out sections to test or block code, helping you to write more manageable programs.

We'll cover the following...

Commenting code

One essential use of comments is to comment out code that is not to be executed in the current run. There are various reasons for this. For example, we may want to temporarily block out functionality or experiment with the code.

Let’s create a to-do list that stores various items and also allows us to add and remove items. Suppose we only want to see what adding a value to the list does. To do this, we’ll use a comment to comment out the remove functionality for now.

Python 3.10.4
myToDoList = ['Python Comments', 'Python If-Else Commands', 'Python Loops']
myToDoList.append('Python Functions')
print("Current To-Do List =", myToDoList)
# myToDoList.remove('Python Comments')
# print("Updated To-Do List:", myToDoList)

Note: You can uncomment the code anytime to see it in action.