PEP 8 — Python Style Guide
The official Python style guide.
Keep code clean, consistent, and readable. Importante para seu future self.
1. Code Layout
-
Indentation: 4 spaces (no tabs).
https://youtu.be/oRva7UxGQDw
- Line length: ≤ 79 chars
- Blank lines:
- 2 blank lines before top-level defs/classes.
- 1 blank line between class methods.
Imports
- One import per line.
2. Naming Conventions
Type | Convention | Example |
---|---|---|
Variable / Function | lower_case_with_underscores |
get_data() |
Class | CamelCase |
AgentModel |
Constant | ALL_CAPS |
MAX_SPEED = 10 |
Module / Package | short_lowercase |
tools , data_utils |
3. Whitespace Rules
- No extra spaces inside
()
,[]
,{}
x = (1, 2, 3)
- One space after commas and around operators
total = a + b
- No space before a comma or colon.
4. Strings and Docstrings
- Use
'single'
or"double"
quotes consistently. - Prefer triple quotes for multi-line strings and docstrings.
def add(a, b):
"""Return the sum of a and b."""
return a + b
5. Comments
- Use complete, clear sentences.
- Begin with a capital letter and a single space after
#
. - For inline comments, leave two spaces before
#
.
# Calculate total cost
total = price * quantity # Apply unit price
Voltem a esses abaixo mais na frente no curso…
🧮 6. Functions and Classes
- Keep one statement per line.
- Avoid mutable default arguments such as lists or dictionaries.
- Use
None
as the default argument and initialize inside the function.
def add_item(item, items=None):
if items is None:
items = []
items.append(item)
return items
⚙️ 7. Comparisons and Conditionals
- Use
is
andis not
when comparing withNone
. - Avoid comparing directly to
True
orFalse
. - Keep conditions explicit and readable.
if result is None:
return
if flag:
execute_task()
🧩 8. Line Breaks
- Use parentheses for multi-line expressions.
- Avoid the backslash (
\
) for line continuation. - Break lines to enhance readability and logical grouping.
total = (
first_value
+ second_value
+ third_value
)