Skip to content

11.2 Python Code Style

PEP 8 and Best Practices


Why Code Standards?

  • Improve readability
  • Facilitate collaboration
  • Reduce errors
  • Professional image

PEP 8 Core Rules

1. Naming Conventions

python
# Good naming
student_age = 25
total_income = 50000
calculate_mean()

class StudentRecord:
    pass

CONSTANT_VALUE = 100

# Poor naming
s_age = 25         # Too brief
totalIncome = 50   # camelCase (not Python style)
CalculateMean()    # Functions use PascalCase

2. Indentation and Spacing

python
# 4 spaces indentation
def process_data(data):
    for item in data:
        if item > 0:
            print(item)

# Spaces around operators
x = 5 + 3
income = salary * 1.1

# Space after comma
numbers = [1, 2, 3, 4]

3. Line Length

python
# Maximum 79 characters per line
result = some_function(
    parameter1,
    parameter2,
    parameter3
)

# Long string line break
message = (
    "This is a very long text, "
    "to maintain readability, "
    "we split it into multiple lines"
)

4. Import Order

python
# Correct order
# 1. Standard library
import os
import sys
from datetime import datetime

# 2. Third-party libraries
import pandas as pd
import numpy as np

# 3. Your own modules
from mymodule import myfunction

Best Practices

1. Function Documentation

python
def calculate_tax(income, rate=0.25):
    """Calculate tax amount

    Args:
        income (float): Annual income
        rate (float): Tax rate, default 0.25

    Returns:
        float: Tax payable

    Example:
        >>> calculate_tax(100000, 0.3)
        30000.0
    """
    return income * rate

2. Exception Handling

python
# Specific exceptions
try:
    df = pd.read_csv('data.csv')
except FileNotFoundError:
    print("File not found")
except pd.errors.EmptyDataError:
    print("File is empty")

# Generic exception
try:
    df = pd.read_csv('data.csv')
except:
    print("An error occurred")

3. Use List Comprehensions

python
# Concise
squares = [x**2 for x in range(10)]

# Verbose
squares = []
for x in range(10):
    squares.append(x**2)

Tool Recommendations

1. Black (Automatic Formatting)

bash
pip install black
black my_script.py

2. Flake8 (Check Standards)

bash
pip install flake8
flake8 my_script.py

3. isort (Sort Imports)

bash
pip install isort
isort my_script.py

Practical Template

Data Analysis Script Template

python
"""
Data Analysis Script Template
Author: Your Name
Date: 2024-01-01
"""

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

# Constants
DATA_PATH = 'data/raw/survey.csv'
OUTPUT_PATH = 'outputs/'

def load_data(filepath):
    """Load data"""
    return pd.read_csv(filepath)

def clean_data(df):
    """Clean data"""
    df = df.dropna()
    df = df[df['age'] > 0]
    return df

def analyze(df):
    """Analyze data"""
    stats = df.describe()
    return stats

def main():
    """Main function"""
    # Load
    df = load_data(DATA_PATH)

    # Clean
    df_clean = clean_data(df)

    # Analyze
    results = analyze(df_clean)

    # Save
    results.to_csv(f'{OUTPUT_PATH}results.csv')

if __name__ == '__main__':
    main()

Next Steps

Next Section: Debugging and Profiling

Continue!

Released under the MIT License. Content © Author.