Python Code Style Guide for DSA Examples
Python Code Style Guide
This guide explains how to write clean, PEP 8 compliant, and beginner-friendly Python code for DSA examples.
The main goal is to keep Python solutions simple, readable, and easy to understand for students and new contributors.
Why This Guide Is Neededโ
DSA examples are easier to learn when the code follows one clean style.
This guide helps contributors with:
- Clear class and function names
- Meaningful variable names
- PEP 8 compliant formatting
- Useful comments and docstrings
- Time and space complexity annotations
- Input and output examples
- Edge case handling
1. Naming Conventionsโ
Python uses snake_case for functions and variables, and PascalCase for classes.
Class Namingโ
Use PascalCase for class names. Describe the algorithm or data structure clearly.
Goodโ
class Node:
pass
class BinarySearchTree:
pass
Avoidโ
class node:
pass
class binarySearchTree:
pass
Function Namingโ
Use snake_case for function names. They should describe what the function does.
Goodโ
def binary_search(arr, target):
return -1
def merge_sort(arr):
pass
Avoidโ
def binarySearch(arr, target):
return -1
def MergeSort(arr):
pass
Variable Namingโ
Use meaningful snake_case variable names. Avoid single-letter variables unless they are simple loop iterators (i, j).
Goodโ
left = 0
right = len(arr) - 1
mid = left + (right - left) // 2
Avoidโ
a = 0
b = len(arr) - 1
c = (a + b) // 2
2. Formatting and Indentationโ
Use consistent indentation of 4 spaces. Do not use tabs.
Goodโ
if arr[mid] == target:
return mid
Avoidโ
if arr[mid] == target: return mid # Inline statements reduce readability
3. Keep Code Simple and Pythonicโ
Avoid overly dense list comprehensions or ternary statements for core logic. Keep it readable for beginners.
Goodโ
result = []
for num in arr:
if num % 2 == 0:
result.append(num)
Avoid for beginner examplesโ
result = [num for num in arr if num % 2 == 0] # Fine for simple cases, but avoid excessively nested list comprehensions
4. Comments and Docstringsโ
Use PEP 257 docstrings for functions and classes to document parameters and return types.
Goodโ
def binary_search(arr, target):
"""
Performs binary search on a sorted list.
Parameters:
arr (list): Sorted list of integers
target (int): Target value to find
Returns:
int: Index of target if found, otherwise -1
"""
left, right = 0, len(arr) - 1
# Loop until the pointers meet
while left <= right:
# ... logic ...
5. Complexity Formatโ
Every Python DSA solution should mention its time and space complexity at the bottom of the page in the following format:
Time Complexity: O(log n)
Space Complexity: O(1)
6. Example Python DSA Structureโ
Here is a complete example of a Python DSA solution implementation matching our guidelines:
class BinarySearch:
@staticmethod
def search(arr, target):
"""
Searches for target in a sorted array.
"""
if not arr:
return -1
left = 0
right = len(arr) - 1
while left <= right:
mid = left + (right - left) // 2
if arr[mid] == target:
return mid
elif arr[mid] < target:
left = mid + 1
else:
right = mid - 1
return -1
Time Complexity: O(log n)
Space Complexity: O(1)
7. Checklist for Python Contributionsโ
Before submitting a Python DSA example, verify these points:
- Class name uses PascalCase
- Function name uses snake_case
- Variable names are descriptive
- 4-space indentation is used
- PEP 257 docstrings are added
- Time and space complexities are documented
- Edge cases (e.g. empty inputs) are handled
Completed working through this block? Sync progress to workspace.