Table of Content
Comments
A comment is a part of the coding file that the programmer does not want to execute, rather the programmer uses it to either explain a block of code or to avoid the execution of a specific part of code while testing.
Python supports both single-line and multi-line comments.
Single-Line Comments:
To write a comment just add a ‘#’ at the start of the line.
python
# This is a single-line comment
"""
This is a
multi-line comment
"""
# Example dictionary
my_dict = {
"name": "John",
"age": 30,
"city": "New York"
}
# Accessing values using keys
print(my_dict["name"]) # Output: "John"
print(my_dict["age"]) # Output: 30
Output:
python
John
30
Multi-Line Comments
To write multi-line comments you can use ‘#’ at each line or you can use the multiline string.
python
'''
This is a multiline comment
in Python using triple single quotes.
It can span multiple lines.
'''
"""
This is another way to write
a multiline comment in Python using
triple double quotes. It can also span
multiple lines.
"""
'''
Calculates the average of grades.
'''
# List of student grades
grades = [85, 90, 75, 88]
# Calculate the average
average = sum(grades) / len(grades)
print("The average grade is:", average)
Output:
python
The average grade is: 84.5
Notice how the commented part does not affect the execution of the program.