2.2. Syntax Comments
2.2.1. Summary
Line comments
Multiline comments
Inline comments
Line comment:
>>> # good
Multiline comment:
>>> # good 1
>>> # good 2
>>> # good 3
Inline comment:
>>> x = 1 # good
2.2.2. Line Comments
PEP 8 -- Style Guide for Python Code
For line comments: Hash (
#
), space and then comment
>>> # my comment
2.2.3. Multiline Comments
There are no multiline comments in Python
You can create many one line comments
All of them will be ignored
>>> # my comment 1
>>> # my comment 2
>>> # my comment 3
2.2.4. Inline Comments
PEP 8 -- Style Guide for Python Code
For inline comments add two spaces before
#
>>> x = 1 # my comment
2.2.5. Convention
By convention for inline comments there should be two spaces before
#
By convention for any comment after
#
there should be a spaceInstead of comments write more readable code, which will be self-explanatory
Never commit commented out code, use Version Control System instead - e.g.:
git blame
Line comment:
>>> # good
>>> #bad
Multiline comment:
>>> # good 1
>>> # good 2
>>> # good 3
>>> #bad 1
>>> #bad 2
>>> #bad 3
Inline comment:
>>> x = 1 # good
>>> x = 1 #bad
>>> x = 1 # bad
>>> x = 1# bad
>>> x = 1 #bad
>>> x = 1#bad
2.2.6. Docstring
PEP 257 -- Docstring Conventions
Docstring is a first multiline comment in: file/module, class, method/function
Used for
doctest
For multiline
str
always use three double quote ("""
) characters
People sometimes use multiline strings to achieve similar behavior as multiline-comment. Unfortunately this requires Python to define string, store it into the memory, and then remove it.
>>>
... """
... my comment 1
... my comment 2
... my comment 3
... """
2.2.7. Recap
Line comments
Multiline comments
Inline comments
After
#
rest of the line will be ignoredBy convention for inline comments there should be two spaces before
#
By convention for any comment after
#
there should be a spaceInstead of comments write more readable code, which will be self-explanatory
Never commit commented out code, use Version Control System instead - e.g.:
git blame
IDE has Local history (modifications) and you can compare file
Line comment:
>>> # good
Multiline comment:
>>> # good 1
>>> # good 2
>>> # good 3
Inline comment:
>>> x = 1 # good
2.2.8. Assignments
# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`
# %% About
# - Name: Syntax Comments Line
# - Difficulty: easy
# - Lines: 1
# - Minutes: 2
# %% English
# 1. Add line comment: My comment
# 2. Remember to add space after `#` character
# 3. Run doctests - all must succeed
# %% Polish
# 1. Dodaj komentarz liniowy: My comment
# 2. Pamiętaj o dodaniu spacji po znaku `#`
# 3. Uruchom doctesty - wszystkie muszą się powieść
# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> result = open(__file__).read()
>>> comment = '#' + ' ' + 'My comment'
>>> assert comment in result, \
'Please write proper line comment'
"""
# Add line comment: My comment
# Remember to add space after `#` character
# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`
# %% About
# - Name: Syntax Comments Multiline
# - Difficulty: easy
# - Lines: 3
# - Minutes: 2
# %% English
# 1. Add multiline comment:
# - first line: My comment 1
# - second line: My comment 2
# - third line: My comment 3
# 2. Remember to add space after `#` character
# 3. Run doctests - all must succeed
# %% Polish
# 1. Dodaj komentarz wieloliniowy:
# - pierwsza linia: My comment 1
# - druga linia: My comment 2
# - trzecia linia: My comment 3
# 2. Pamiętaj o dodaniu spacji po znaku `#`
# 3. Uruchom doctesty - wszystkie muszą się powieść
# %% Hints
# - Each line of a multiline comment should start with `# ` characters
# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> result = open(__file__).read()
>>> comment = '\\n'.join([
... '#' + ' ' + 'My comment 1',
... '#' + ' ' + 'My comment 2',
... '#' + ' ' + 'My comment 3',
... ])
>>> assert comment in result, \
'Please write proper multiline comment'
"""
# Add multiline comment:
# - first line: My comment 1
# - second line: My comment 2
# - third line: My comment 3
# Remember to add space after `#` character
# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`
# %% About
# - Name: Syntax Comments Inline
# - Difficulty: easy
# - Lines: 1
# - Minutes: 2
# %% English
# 1. Add inline comment to `DATA` variable definition
# with content: My comment
# 2. Remember to add space after `#` character
# 3. Remember to add two spaces before `#` character
# 4. Run doctests - all must succeed
# %% Polish
# 1. Dodaj komentarz na końcu linii do definicji zmiennej `DATA`
# z treścią: My comment
# 2. Pamiętaj o dodaniu spacji po znaku `#`
# 3. Pamiętaj o dodaniu dwóch spacji przed znakiem `#`
# 4. Uruchom doctesty - wszystkie muszą się powieść
# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> result = open(__file__).read()
>>> comment = ' ' + '#' + ' ' + 'My comment'
>>> assert DATA == 1, \
'Do not change the value of the `DATA` variable.'
>>> assert comment in result, \
'Please write proper inline comment'
"""
# Add inline comment to `DATA` variable definition
# with content: My comment
# Remember to add space after `#` character
# Remember to add two spaces before `#` character
DATA = 1