There are a few ways in which we can comment out a block of code in Python, let's take a look at them with examples.
Example 1: Triple Quotes - Multi-line Comments (''')
'''
def add_numbers(a, b):
no1 = a
no2 = b
sum = no1 + no2
return sum
'''
def diff_numbers(a, b):
no1 = a
no2 = b
diff = no2 - no1
return diff
result = diff_numbers(5, 10)
print("Result:", result)
Example 2: Single line Comments (using #)
# def add_numbers(a, b):
# no1 = a
# no2 = b
# sum = no1 + no2
# return sum
def diff_numbers(a, b):
no1 = a
no2 = b
diff = no2 - no1
return diff
result = diff_numbers(1, 2)
print("Result:", result)
Comment/Uncomment Code using IDE Keyboard Shortcuts:
# def add_numbers(a, b):
# no1 = a
# no2 = b
# sum = no1 + no2
# return sum
def diff_numbers(a, b):
no1 = a
no2 = b
diff = no2 - no1
return diff
result = diff_numbers(1, 2)
print("Result:", result)
| IDE | Comment Code (Mac) | Comment Code (Windows) | Uncomment Code (Mac) | Uncomment Code (Windows) |
|---|---|---|---|---|
| Visual Studio Code | ⌘ + / | Ctrl + / | ⌘ + / | Ctrl + / |
| PyCharm | ⌘ + / | Ctrl + / | ⌘ + / | Ctrl + / |
| Spyder | ⌘ + 1 | Ctrl + 1 | ⌘ + 1 | Ctrl + 1 |
| Sublime Text | ⌘ + / | Ctrl + / | ⌘ + / | Ctrl + / |
| Atom | ⌘ + / | Ctrl + / | ⌘ + / | Ctrl + / |
| Jupyter Notebook | ⌘ + / | Ctrl + / | ⌘ + / | Ctrl + / |
| IDLE | ⌘ + 3 | Ctrl + 3 | ⌘ + 4 | Ctrl + 4 |
Provide Feedback For This Article
We take your feedback seriously and use it to improve our content. Thank you for helping us serve you better!
😊 Thanks for your time, your feedback has been registered!

Comments & Discussion
Facing issues? Have questions? Post them here! We're happy to help!