MyInternships.in

Python Practice & Career

Python PEP 8 Best Practices

PEP 8 is Python’s official style guide. Following it makes your code readable, professional and easier for teams (and interviewers) to trust. Here are the rules that matter most.


What Is PEP 8?

PEP 8 is the official set of style recommendations for writing Python code. It doesn’t change what your code does — it makes it consistent and readable, which is exactly what employers expect from clean, maintainable code.

Naming Conventions

ThingStyleExample
Variable / functionsnake_casefirst_name, calculate_total()
ConstantUPPER_CASEMAX_SPEED = 120
ClassPascalCaseclass StudentRecord:
Module / filelowercaseutils.py, data_loader.py

Key Formatting Rules

  • Indent with 4 spaces — never tabs.
  • Keep lines under ~79–99 characters.
  • Put two blank lines between top-level functions and classes.
  • Use spaces around operators: x = a + b, not x=a+b.
  • One import per line, grouped: standard library, third-party, then local.
Clean, PEP 8-style code
Python
def calculate_total(price, quantity):
    """Return the total cost for a given price and quantity."""
    return price * quantity


TAX_RATE = 0.18
total = calculate_total(100, 3)
print(f"Total with tax: {total * (1 + TAX_RATE)}")
💡

Let tools do the work: run "pip install black" and format any file with "black yourfile.py". Use "flake8" to catch style issues automatically.

Related Python Topics

Keep learning with these closely related lessons.

Ready to use your Python skills?

Find Python, data science and software internships and fresher jobs across India.

Browse Python Internships