Where should comments be added in code?


#1

Question

In the context of this exercise that introduces comments, where should comments be added to the code? Can I just add comments to every line of code to be safe?

Answer

Generally, comments should only be added to pieces of code that would not be obvious to others who are reading it. Adding comments to every line of code is usually not necessary, and having too much text in a file can overcrowd it, ultimately making it harder to work with or understand.

Comments help provide context for why a particular piece of code was written in that way, and also help others understand the code itself faster. Comments are typically added above the code they are describing.

# When hitpoints are below 5% and shields are gone, self-destruct.
if hp < 5 and shields == False:
  self_destruct()

FAQ: Programming with Python - Learn Python: Syntax - Comments