DEV Community

Cover image for Day 20/100: Writing Pythonic Code – Introduction to PEP 8
 Rahul Gupta
Rahul Gupta

Posted on

Day 20/100: Writing Pythonic Code – Introduction to PEP 8

Welcome to Day 20 of the 100 Days of Python series!
You’ve learned a lot of Python by now — but are you writing it the Pythonic way?

Today, we explore PEP 8, Python’s official style guide, and what it means to write “Pythonic” code — clean, readable, and professional.

Let’s dive in and make your code look (and feel) like it was written by a pro. 🐍✨


📦 What You’ll Learn

  • What Pythonic code means
  • What PEP 8 is and why it matters
  • Key style rules from PEP 8
  • Tools to auto-format your code
  • Real before-and-after examples

🧠 What Is Pythonic Code?

"Pythonic" code is code that follows best practices and idioms of the Python language. It's:

  • Readable
  • Concise but clear
  • Consistent
  • Elegant

In short: code that feels natural in Python.


📜 What Is PEP 8?

PEP 8 stands for Python Enhancement Proposal #8, which provides the style guide for writing Python code.

It covers things like:

  • Naming conventions
  • Code layout
  • Import order
  • Indentation
  • Line length
  • Spacing

🧠 Think of PEP 8 as the grammar guide for Python code.


🧾 10 Key PEP 8 Rules (with Examples)

1. ✅ Use 4 Spaces for Indentation

# ✅ Correct
def greet():
    print("Hello")

# ❌ Incorrect (Tabs or 2 spaces)
Enter fullscreen mode Exit fullscreen mode

2. ✅ Keep Lines ≤ 79 Characters

Break long lines using \ or parentheses:

print("This is a long sentence that we break into"
      " multiple lines for better readability.")
Enter fullscreen mode Exit fullscreen mode

3. ✅ Leave 2 Blank Lines Between Functions

def func_one():
    pass


def func_two():
    pass
Enter fullscreen mode Exit fullscreen mode

4. ✅ Add Spaces Around Operators

x = 5 + 2  # ✅
x=5+2      # ❌
Enter fullscreen mode Exit fullscreen mode

5. ✅ Use Meaningful Variable Names

# ✅
user_age = 25

# ❌
ua = 25
Enter fullscreen mode Exit fullscreen mode

6. ✅ Naming Conventions

Type Convention Example
Variable snake_case user_name
Function snake_case get_user_data()
Class PascalCase UserProfile
Constant ALL_CAPS MAX_LENGTH = 100

7. ✅ Import One Module Per Line

import os
import sys  # ✅ Good

# import os, sys ❌ Bad
Enter fullscreen mode Exit fullscreen mode

Also, group imports like this:

  1. Standard library
  2. Third-party packages
  3. Local application imports

8. ✅ Use Docstrings for Functions

def greet(name):
    """Greet the user by name."""
    print(f"Hello, {name}")
Enter fullscreen mode Exit fullscreen mode

9. ✅ Avoid Unused Variables

for i in range(5):  # ✅ if i is used
    print(i)

for _ in range(5):  # ✅ if i is not used
    print("Hi")
Enter fullscreen mode Exit fullscreen mode

10. ✅ Use is/is not for None Checks

if user is None:  # ✅
    print("No user found.")
Enter fullscreen mode Exit fullscreen mode

🧰 Tools to Auto-Format Code

  • Black – Auto-formats your code to be PEP 8 compliant
  • flake8 – Linter to show warnings and errors
  • pylint – Advanced static code checker
  • isort – Automatically sorts your import statements
pip install black flake8 isort
Enter fullscreen mode Exit fullscreen mode

Format a file with Black:

black your_script.py
Enter fullscreen mode Exit fullscreen mode

✨ Before vs After Example

❌ Unpythonic

def myfunc(x):print(x+  5)
Enter fullscreen mode Exit fullscreen mode

✅ Pythonic

def my_func(x):
    print(x + 5)
Enter fullscreen mode Exit fullscreen mode

🧼 Why It Matters

  • ✅ Easier to read and maintain
  • ✅ Collaborators understand your code faster
  • ✅ Helps catch bugs early
  • ✅ Shows professionalism and pride in your work
  • ✅ Essential in team projects and open-source

🧠 Recap

Today you learned:

  • What Pythonic code means
  • What PEP 8 is and why it’s important
  • 10 practical rules for writing clean Python
  • Tools like Black, flake8, and isort
  • How better code formatting improves quality

Top comments (0)