voidex/20-29 Areas/20 Programming/20.01 Python/comments.md

32 lines
No EOL
1.1 KiB
Markdown

---
title: "Comments"
tags: [python, syntax, comments]
aliases: [hash, commenting, commented-out code]
---
> [!note] What Are Comments?
> Comments are lines in your code that start with a `#`. Python completely ignores them during execution.
>
> > [!example] Example
> > ```python
> > # This will not run
> > print('This will run!')
> > ```
>
> > [!info] Spacing and Readability
> > - Blank lines are also ignored by Python.
> > - Use blank lines and comments to group related code — just like paragraphs in writing.
> > - Helps you (and others) read your code more easily later.
>
> > [!important] Best Practices
> > - Use comments to **explain why**, not just **what** the code does.
> > - Avoid redundant comments:
> > ```python
> > x = 5 # set x to 5 ❌ unnecessary
> > ```
> > - Use inline comments sparingly and only when clarity is needed.
>
> > [!info] Related Notes
> > - [[pythonSyntaxOverview]] — comments are part of Python's syntax rules
> > - [[variableNamingBestPractices]] — both promote writing readable, self-explanatory code
> > - [[conditionals]] — comments are often used to clarify branches or logic
>