Skip to main content
Practice

Prompting for Python Code Generation

When leveraging AI to write Python code, the prompt's specificity and clarity significantly impact the accuracy and efficiency of the generated code.

In this lesson, we will explore how to create effective prompts for generating Python code.


How to Create Prompts for Generating Python Code

Simply asking the AI to "write some code" may result in code that does not meet your requirements.

To generate more accurate and efficient code, it's beneficial to create clear and specific prompts as follows:

  • Specify the Programming Language: Clearly state the language in which the code should be generated (e.g., Python, JavaScript).

  • Provide Input and Output Examples: If the function involves input and output, provide 2-3 clear examples of the expected input and output.

  • Specify the Execution Environment: Mention the code execution environment (e.g., Windows, MacOS, AWS, Chrome) as there might be constraints depending on this.


Comparing Prompts with Examples

When requesting a solution to a specific problem in Python, an ambiguous or insufficient prompt increases the likelihood of the AI generating incorrect code.

Below are two prompt examples to illustrate the difference in prompt quality.


The Problem

You need to write Python code to sort the numbers in the list [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5] in descending order.


Poor Prompt Example

Poor Prompt Example
Write code to sort a list.
AI Generated Code Example
def sort_list(lst):
return sorted(lst)

Issues with the Prompt

  • Ambiguity: It is unclear what criteria should be used to sort the list (ascending, descending, or another criterion).

  • Lack of Specificity: There is no information about the contents of the list (numbers, strings, mixed types), which can affect the accuracy of the code.

  • No Additional Requirements: There are no additional requirements like exception handling or input validation.


Well-Engineered Prompt Example

Well-Engineered Prompt Example
Please write a Python function that sorts a list of integers in descending order.

The function should accept a list as input and raise a TypeError if the input is not a list.

If the list is empty, the function should return an empty list.
AI Generated Code Example
def sort_descending(lst):
# Raise TypeError if input is not a list
if not isinstance(lst, list):
raise TypeError("Input should be a list.")

# Return an empty list if input is empty
if len(lst) == 0:
return []

# Return the list sorted in descending order
return sorted(lst, reverse=True)

Advantages of the Prompt

  • Clarity: It clearly defines what task (sorting a list of integers in descending order) needs to be performed.

  • Specificity: It provides information about the input type (integer list) to help the AI generate appropriate code.

  • Exception Handling Requirements: It specifies the need for handling exceptions, making the code more robust.

  • Instructions for Additional Functionality: It gives clear directions on how to handle an empty list.

Want to learn more?

Join CodeFriends Plus membership or enroll in a course to start your journey.