Skip to content

Instantly share code, notes, and snippets.

@jpalala
Created June 15, 2026 14:03
Show Gist options
  • Select an option

  • Save jpalala/e54492a65c2076801f2dfee988aae73d to your computer and use it in GitHub Desktop.

Select an option

Save jpalala/e54492a65c2076801f2dfee988aae73d to your computer and use it in GitHub Desktop.
PYTHON BEST PRACTICES

It is great that you are leveling up your coding standards! Moving from "making code work" to "writing clean, professional code" is exactly how you become a strong Python developer.

To follow Pythonic standards, you need to think beyond just syntax and embrace the "Pythonic" philosophy.


1. The "Zen of Python"

Before you start linting, you should know the philosophy that drives the language. You can actually see this by typing import this into your Python terminal. Here are the most critical takeaways for your code:

  • Beautiful is better than ugly.
  • Simple is better than complex.
  • Readability counts.
  • If the implementation is hard to explain, it's a bad idea.

2. Pythonic Idioms (Beyond PEP8)

Writing "Pythonic" code means using built-in features that make your code shorter and faster. Here are three common ways to improve your code structure:

Instead of this (Non-Pythonic) Use this (Pythonic) Why?
for i in range(len(items)): print(items[i]) for item in items: print(item) Direct iteration is cleaner and faster.
f = open("file.txt"); ...; f.close() with open("file.txt") as f: ... Context managers auto-close files even if errors occur.
result = []; for x in data: result.append(x*2) result = [x*2 for x in data] List comprehensions are highly efficient.

3. How to use Pylint (Static Code Analysis)

Pylint is your "automated code reviewer." It checks your code for errors, style violations (PEP8), and potential bugs without you having to run the program.

Step 1: Installation

Open your terminal (make sure you are in your project environment) and run:

pip install pylint

Step 2: Running the Linter

Once installed, run this command in your terminal pointing to your file:

pylint your_script.py

Step 3: Understanding the Output

Pylint will give you:

  1. Messages/Warnings: It will point to the exact line number where you violated PEP8 (e.g., C0303: Trailing whitespace).
  2. A Score: It will give you a score out of 10.0.
  • Tip: Don't stress about getting a perfect 10.0 immediately. Aiming for an 8.0 or above is excellent for most production-grade scripts.

Pro Tip: If you want to stop manually fixing indentation and spaces, install black (pip install black). It is an "opinionated" auto-formatter that will rewrite your code to be PEP8 compliant for you automatically when you run black your_script.py.


4. Modernizing Your Testing

You mentioned unit testing. While Python has a built-in unittest module, most professional Python developers today use pytest. It is much more readable and requires less "boilerplate" code.

Example pytest format:

# test_geometry.py
def test_addition():
    assert 2 + 2 == 4

To run it, you simply type pytest in your terminal. It will automatically find all files starting with test_ and run them.


Your Toolkit Checklist

  1. Style: Use black to auto-format your code.
  2. Analysis: Use pylint to find bugs and bad habits.
  3. Testing: Use pytest to ensure your code logic remains sound as you grow your project.

What specific type of project are you currently working on that you would like to apply these standards to?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment