Python Comments¶
What is a Comment¶
- A comment can be defined as human-readable documentation" is grammatically correct and does not require any further edits.
- To write a comment, start a new line with the # character.
- Anything that follows the # for the rest of the line is part of the comment.
- Single line comments are typically used before or next to code to provide context or explanations for the code.
- A comment should be concise and easily comprehensible.
In [1]:
Copied!
students = 10 # number of students at the data armory
students
students = 10 # number of students at the data armory
students
Out[1]:
10
- A comment is not an executable code.
In [2]:
Copied!
# Comments are not executable - This line will be ignored
print("The data armory")
# Comments are not executable - This line will be ignored
print("The data armory")
The data armory
In [3]:
Copied!
# Should not be within a string
new_students = "Welcome to the data armory"
new_students
# Should not be within a string
new_students = "Welcome to the data armory"
new_students
Out[3]:
'Welcome to the data armory'
It is important to add notes to your code and within your programs;
This is helpful for defining the purpose of your code or explaining specific sections of your code, especially when:
- Your code is longer and complex.
- It is essential to keep in mind the purpose of your code, such as the reason for using a particular function, especially when you experience memory lapses.
- Meaningful comments will help others understand your code.
- As a professional programmer you need meaningful code considering that you will collaborate with others.
- It is easy to delete comments than to go back to comment your code.
Commenting out is when you put the # when trying to find out what is wrong in a program.
In [4]:
Copied!
x = 10
y = 50
z = 25
total = x + y + z
total
x = 10
y = 50
z = 25
total = x + y + z
total
Out[4]:
85
In [5]:
Copied!
# Getting the sum of variables
# Use a different variable to print the results
y = 20
x = 10
w = 10
total = y + x + w
total
# Getting the sum of variables
# Use a different variable to print the results
y = 20
x = 10
w = 10
total = y + x + w
total
Out[5]:
40
- Blank lines after the comment are also ignored.
In [6]:
Copied!
# Getting the sum of values
y = 20
x = 10
total = y + x
total
# Getting the sum of values
y = 20
x = 10
total = y + x
total
Out[6]:
30
The various methods for writing a comment are:
* Placing it before the line of code * Writing it next to the line of code * Using a comment block (multiline comment)
Multiline Comments¶
In [7]:
Copied!
# Multiline Strings for commenting
welcome_message = """ Welcome to the
data armory
for python and
data science tutorials"""
# Multiline Strings for commenting
welcome_message = """ Welcome to the
data armory
for python and
data science tutorials"""
# Multiline Strings for commenting
welcome_message = """ Welcome to the
data armory
for python and
data science tutorials"""
welcome_message
# Multiline Strings for commenting
welcome_message = """ Welcome to the
data armory
for python and
data science tutorials"""
# Multiline Strings for commenting
welcome_message = """ Welcome to the
data armory
for python and
data science tutorials"""
# Multiline Strings for commenting
welcome_message = """ Welcome to the
data armory
for python and
data science tutorials"""
welcome_message
Out[7]:
' Welcome to the\ndata armory\nfor python and \ndata science tutorials'
- Remember to place your comments either above or below the triple-quoted text, multiline comments, or block string, which are valid Python code.
In [8]:
Copied!
""" In the university, freshman year #refers to a first year student
sophomore refers to a second year students
Welcome to the data armory
"""
# Multiline comment
""" In the university, freshman year #refers to a first year student
sophomore refers to a second year students
Welcome to the data armory
"""
# Multiline comment
Out[8]:
' In the university, freshman year #refers to a first year student\n\n sophomore refers to a second year students\n\n Welcome to the data armory \n \n '
Reference and Further Reading¶
- Automate the boring stuff by Al Sweigart-https://www.amazon.com/Automate-Boring-Stuff-Python-Programming/dp/1593275994
- Python Crash Course By Eric Matthes - https://www.amazon.com/Python-Crash-Course-2nd-Edition/dp/1593279280/ref=sr_1_1?crid=2ANKISKNQPNKH&keywords=Python+Crash+Course+By+Eric+Matthes&qid=1654606797&s=books&sprefix=%2Cstripbooks-intl-ship%2C1078&sr=1-1
- Ravikiran AS "Comments in Python: Why are They Important And How to Use Them" https://www.simplilearn.com/tutorials/python-tutorial/comments-in-python#:~:text=Comments%20in%20Python%20are%20identified,a%20multi%2Dline%20comment%20block.
- Ihechikara Vincent Abba "Python Comment Block – How to Comment Out Code in Python" https://www.freecodecamp.org/news/python-comment-block-how-to-comment-out-code-in-python/