Python Code Reviewer and Optimizer

5 min read
advanced

Copy-ready template

The Prompt

Replace the bracketed placeholders with your real context before running it in your AI tool.

Act as a senior Python developer and code reviewer. Analyze the following Python code and provide:

1. **Code Review**:
- Identify bugs, security vulnerabilities, and potential runtime errors
- Check for PEP 8 compliance and Python best practices
- Evaluate code readability and maintainability

2. **Performance Optimization**:
- Suggest performance improvements with time/space complexity analysis
- Recommend more efficient algorithms or data structures
- Identify unnecessary operations or redundant code

3. **Refactoring Suggestions**:
- Propose cleaner, more Pythonic implementations
- Suggest appropriate design patterns if applicable
- Recommend better variable/function names

4. **Testing Recommendations**:
- Suggest unit tests for critical functions
- Identify edge cases that need testing

Code to review:
```python
[INSERT YOUR PYTHON CODE HERE]
```

Please provide your analysis in a structured format with code examples for each suggestion.

How to Use This Prompt

This comprehensive code review prompt helps developers improve their Python code quality, performance, and maintainability. It’s ideal for both learning and professional development.

Best Practices:

  1. Include context: Add comments about what the code is supposed to do
  2. Specify constraints: Mention any specific requirements (Python version, libraries, performance targets)
  3. Ask for alternatives: Request multiple solutions if you want to compare approaches
  4. Focus areas: Highlight specific concerns (e.g., “I’m particularly worried about memory usage”)

Example Implementation:

# Example code to review
def find_duplicates(lst):
    duplicates = []
    for i in range(len(lst)):
        for j in range(i+1, len(lst)):
            if lst[i] == lst[j] and lst[i] not in duplicates:
                duplicates.append(lst[i])
    return duplicates

# Context: This function finds duplicate elements in a list
# Concern: Performance with large lists

Expected Output:

The AI will provide:

  • Detailed code analysis with specific line-by-line feedback
  • Optimized version using sets or collections.Counter
  • Time complexity comparison (O(n²) vs O(n))
  • Unit test examples
  • Edge case handling suggestions

Tips for Maximum Value:

  • Include real code snippets, not just toy examples
  • Ask follow-up questions about specific suggestions
  • Request explanations for unfamiliar concepts
  • Use the feedback to build a personal best practices checklist

Copy-Ready Prompt

Act as a practical python code reviewer and optimizer specialist. Help me turn the context below into a useful, reviewable output.

Context I will provide:
- Goal:
- Audience or user:
- Current situation:
- Constraints:
- Source material or examples:
- Tone or style:
- What the final output must include:

Your task:
1. Restate the goal in one sentence so we can confirm the direction.
2. Ask up to three clarifying questions only if key information is missing.
3. Produce review notes, rewritten code, test cases, architecture options, or documentation that can be checked by a developer.
4. Explain the reasoning behind the main choices.
5. Add a final review checklist so I can judge whether the output is ready to use.

Important constraints:
- Be specific to the context I provide.
- Do not invent facts, sources, metrics, testimonials, or credentials.
- Flag assumptions clearly.
- Do not paste secrets, production credentials, private customer data, or proprietary code you are not allowed to share.

When This Prompt Is Most Useful

Use this prompt when you need help with python code reviewer and optimizer but do not want a generic answer. It works best for developers, technical writers, engineering leads, and builders reviewing implementation decisions who already have some context and want the AI to organize it into review notes, rewritten code, test cases, architecture options, or documentation that can be checked by a developer. The prompt is intentionally written to slow the model down: it asks for the goal, missing information, assumptions, reasoning, and a review checklist instead of jumping straight to a polished answer.

This is especially useful when the task has tradeoffs. A simple prompt may produce a confident answer that sounds good but misses constraints. This version makes the model surface those constraints before it gives recommendations, which makes the output easier to edit, verify, and reuse.

Inputs to Prepare

Before running the prompt, gather:

  • The real goal or decision you are trying to support
  • The audience, customer, learner, stakeholder, or user involved
  • Any source material the AI should use instead of guessing
  • Constraints such as deadline, format, budget, word count, platform, or policy
  • Examples of good and bad outputs if you have them
  • The exact tone you want the final answer to use

For this page, the most important context is: language, framework, repository constraints, target behavior, failing examples, performance limits, and security concerns. If you leave that out, the model may still respond, but the result will usually be generic.

Example Input

Stack: Next.js and PostgreSQL. Task: review a route handler. Concern: SQL safety, duplicated validation, and missing tests.

How to Review the Output

Do not use the first answer blindly. Check whether it:

  • names concrete failure modes instead of generic best practices
  • includes tests or verification steps
  • separates security, correctness, and maintainability concerns
  • makes assumptions visible instead of hiding them in confident language
  • gives you something you can act on, test, or revise within the same work session

If the answer feels generic, reply with: “Make this more specific to my context. Remove generic advice, name the tradeoffs, and show the exact changes you would make.” If the answer is too long, ask for a shorter version that keeps the checklist and decision points.

Common Failure Modes

  • Too little context: the AI fills gaps with generic advice.
  • No review criteria: the output sounds polished but is hard to judge.
  • Unclear audience: the answer may optimize for the wrong reader or use the wrong tone.
  • Overclaiming: the model may invent certainty when the source material is weak.

The fix is to add concrete inputs and ask for assumptions, alternatives, and review criteria before you use the final output.

Practical Variations for Python Code Reviewer and Optimizer

  • Review mode: Paste the smallest relevant code slice, expected behavior, and known failure. Ask for findings ordered by severity before any rewrite.
  • Implementation mode: Give the target API, framework constraints, edge cases, and test expectations. Ask for a minimal implementation plan before code.
  • Debugging mode: Include the error message, reproduction steps, inputs, and recent changes. Ask the model to separate evidence from hypotheses.

Follow-Up Prompts

Use these after the first answer:

  • “Rewrite this using only the context I provided. Label assumptions instead of hiding them.”
  • “Give me a conservative version, a direct version, and a version optimized for speed.”
  • “Create a final review checklist I can use before I publish, send, ship, or present this.”

What Makes This Page Different

This page is useful when you are working on python code reviewer and optimizer and need more than a blank chat box. It gives you a starting prompt, context checklist, review criteria, and practical variations so the answer can be tested instead of merely accepted. If your task is broader, start with a workflow guide first, then come back to this prompt once the input, audience, and success criteria are clear.

Input checklist

Before You Run This Prompt

  • Define the exact outcome you want from Python Code Reviewer and Optimizer.
  • Add the audience, use case, constraints, deadline, and preferred format.
  • Include one strong example of the style or quality level you expect.
  • State what the AI should avoid, such as unsupported claims, generic advice, or off-brand tone.

Quality bar

What a Good Output Should Include

  • A clear structure that can be used without heavy rewriting.
  • Specific recommendations tied to your provided context.
  • Tradeoffs, assumptions, and missing information called out explicitly.
  • Next steps or validation checks so you can judge whether the output is usable.

Iteration workflow

How to Improve the First Answer

1. Tighten the context

Ask the AI to identify missing inputs before it rewrites the answer.

2. Request alternatives

Generate two or three variants for different audiences, tones, or levels of detail.

3. Run a critique pass

Ask for risks, weak assumptions, and edits that would make the result more actionable.

Best Use Cases

  • Projects where Coding context needs a repeatable starting point.
  • Projects where Development context needs a repeatable starting point.
  • Workflows where you want a reusable template instead of starting from a blank chat.
  • Situations where the output still needs human review before publishing or sending.

When to Be Careful

  • Do not treat the answer as final when legal, medical, financial, or safety decisions are involved.
  • Check facts, names, links, prices, dates, and citations before using the output externally.
  • Remove any invented evidence, exaggerated claims, or details that were not present in your input.

Workflow guides

Make This Prompt More Reliable

Use This Prompt Responsibly

AI output quality depends on the context you provide. Treat this template as a structured starting point, then review the result for accuracy, tone, originality, and fit before using it in real work.

Related Prompts