-
Hajipur, Bihar, 844101
Hajipur, Bihar, 844101
Introduction to Python
Python Basics
Python Syntax
Python Comments
Python Variables
Python Data Types
Python Casting
Python I/O
Python Operators
Cotrol Structures
Data Structures
Python Strings
Python Lists
Python Tuples
Python Dictionaries
Python Sets
Python Arrays
Python Bytes and Bytearray
Date and Time
Functions and Module
File Handling
Python OOP
Advanced Concepts
Python Scope
Python Modules
Python JSON
Python RegEx
Python PIP
Python Try...Except
Python String Formatting
Python User Input
Python VirtualEnv
Python Math
Python DSA
Python DSA
Lists and Arrays
Python Stacks
Python Queues
Linked Lists
Python Hash Tables
Python Trees
Python Binary Trees
Binary Search Trees
Python AVL Trees
Python Graphs
Searching Algorithms
Sorting Algorithms
In Python, comments are notes written in code that are ignored by the Python interpreter. They're used to make code easier to understand, explain logic, or temporarily disable code during debugging.
💡 Comments are meant for humans, not the machine.
🔹 Why Use Comments?
To explain complex code.
To improve code readability.
To document the purpose of code sections.
To disable code without deleting it.
Single-line comments start with a #
symbol. Everything after #
on that line is ignored.
# This is a single-line comment
print("Hello, World!") # This is an inline comment
Python doesn't have a specific multi-line comment syntax. You can:
#
lines:# This is a multi-line comment
# written in Python using
# the hash symbol
'''
or """
):Although intended for documentation strings, they can act as comments if not assigned to a variable.
'''
This is a multi-line string
used as a comment.
'''
print("Code continues...")
These are comments placed on the same line as a statement.
x = 5 # Assign 5 to variable x
ℹ️ Keep inline comments concise and relevant.
They do not affect your program’s output.
They are not compiled or executed.
Type | Syntax | Example |
---|---|---|
Single-Line | # comment |
# This is a comment |
Inline | After statement | x = 10 # inline comment |
Multi-Line | ''' text ''' or # # # |
'''multi-line comment''' |
Use clear and meaningful language.
Avoid over-commenting obvious code.
Keep comments up to date as code changes.
Q1. Write a Python program with a single-line comment above a print()
statement.
Q2. Write a Python program that uses a comment to describe a variable assignment.
Q3. Write a Python program that adds a comment after a line which prints your name.
Q4. Write a Python program using three single-line comments (#
) to explain three steps in your code.
Q5. Write a Python program that includes a multi-line comment using triple quotes ('''
or """
).
Q6. Write a Python program where you comment out a line that adds two numbers so it doesn't execute.
Q7. Write a Python program that includes both code and comments to explain what each part does.
Q8. Write a Python program that uses a comment to label a section, such as # Calculation
.
Q9. Write a Python program that starts with a multi-line description at the top using triple quotes (''' '''
).
Q10. Write a Python program and comment each block of the program: input, process, and output.
Introduction to Python
Python Basics
Python Syntax
Python Comments
Python Variables
Python Data Types
Python Casting
Python I/O
Python Operators
Cotrol Structures
Data Structures
Python Strings
Python Lists
Python Tuples
Python Dictionaries
Python Sets
Python Arrays
Python Bytes and Bytearray
Date and Time
Functions and Module
File Handling
Python OOP
Advanced Concepts
Python Scope
Python Modules
Python JSON
Python RegEx
Python PIP
Python Try...Except
Python String Formatting
Python User Input
Python VirtualEnv
Python Math
Python DSA
Python DSA
Lists and Arrays
Python Stacks
Python Queues
Linked Lists
Python Hash Tables
Python Trees
Python Binary Trees
Binary Search Trees
Python AVL Trees
Python Graphs
Searching Algorithms
Sorting Algorithms