0
0
Pythonprogramming~5 mins

Comments in Python

Choose your learning style9 modes available
Introduction

Comments help explain what the code does. They make code easier to understand for you and others.

To explain why a piece of code is written a certain way.
To leave notes for yourself or others about what a section of code does.
To temporarily stop a line of code from running while testing.
To add reminders or TODOs inside your code.
To clarify complex or tricky parts of the code.
Syntax
Python
# This is a single-line comment
'''
This is a
multi-line comment
'''
Single-line comments start with # and go to the end of the line.
Multi-line comments use triple quotes ''' or """ but are actually multi-line strings not assigned to a variable.
Examples
This comment explains what the print statement does.
Python
# This is a comment explaining the next line
print('Hello')
This style is used for longer explanations or notes.
Python
'''
This is a multi-line comment.
It can span several lines.
'''
You can put comments after code on the same line to explain it.
Python
print('Start')  # This comment is after code on the same line
Sample Program

The comments explain what the program and the print line do.

Python
# This program prints a greeting message
print('Hello, friend!')  # Print greeting
OutputSuccess
Important Notes

Comments do not affect how the program runs.

Use comments to make your code easier to read and maintain.

Keep comments clear and concise.

Summary

Comments explain code and help others understand it.

Use # for single-line comments.

Use triple quotes for multi-line comments or notes.