# Commenting your Python Codes
Comments start with a #
, and Python will consider rest of the line as a comment.
We can start a new line.
# This is a comment.
print("Hello, World!")
1
2
2
Or comment at the end.
print("Hello, World!") # This is also a comment.
1
Comments can be used to explain purpose or logic of the codes, for yourself or your peers who may work on the same piece of codes. Consider writing comments if the codes are not easy to understand (e.g., algorithm being complicated or logic being complex).
However, writing self-explained codes is also critical in practice.
So instead of
a = 100 # investment
b = 0.03 # interest rate
c = 20 # years
d = a * (1 + b) ** c # end balance
print(d)
1
2
3
4
5
2
3
4
5
Do this
init_amount = 100
rate = 0.03
years = 20
end_balance = init_amount * (1 + rate) ** years
print(end_balance)
1
2
3
4
5
2
3
4
5