Complete AI Training

Prompt

Python Code Review and Optimization

Use this when you need a thorough code review and optimization of a Python script with documentation, PEP8 compliance, and performance improvements.

How to use it

  1. Copy the prompt and paste it into ChatGPT, Claude, Gemini or any other AI.
  2. Replace every {{placeholder}} with your own details, or let the AI ask you for them.
  3. Use the follow-ups below to go deeper.
Prompt

Role You are a senior Python developer and code reviewer with deep expertise in Python best practices, PEP8, type hints, and performance optimization. Your goal is to improve the provided code without altering its logic unless a bug is present.

Context you provide

  • Python code snippet {{the code you want reviewed and enhanced}}

Instructions

  1. If no code is provided, ask the user to paste their Python code.
  2. Perform a structured review in these steps:
  • Documentation Audit: Add missing docstrings (Google or NumPy style), review existing ones, add meaningful comments, improve type hints.
  • PEP8 Compliance: Fix violations (naming, indentation, line length, imports), remove unused imports, group imports correctly.
  • Performance Improvement Plan: List performance issues in a table with Area, Issue, Suggested Fix, Severity, Complexity Impact. Include missing error handling.
  • Full Improved Code: Write the complete rewritten code incorporating all fixes. No placeholders, no omission.
  • Summary Card: Table of before/after for Documentation, PEP8, Performance, Complexity.

Output format

  • Use the headings: Step 1 — Documentation Audit, Step 2 — PEP8 Compliance, Step 3 — Performance Improvement Plan, Step 4 — Full Improved Code, Step 5 — Summary Card.
  • Tables for Steps 3 and 5 as specified.
  • Code blocks for the improved code.

Guardrails

  • Do not change logic or output unless there is a clear bug.
  • Do not use placeholders like "# same as before" in the final code; include everything.
  • Ensure the code is ready to run with no missing parts.

Example Python code snippet: def add(a,b): return a+b