Data Training Courses

Home | Power BI | Excel | Python | SQL | Generative AI | Visualising Data | Analysing Data

Use AI Tools to help write Python

AI tools can help us write and learn Python. For example, we can enter a prompt like this:

Example Prompt:

> A dataset is at a public location at https://raw.githubusercontent.com/MarkWilcock/CourseDatasets/main/Iris/irisdata.csv
> It contains 150 rows with the following columns:
> * `Observation`, an integer, unique identifier 
> * `Sepal Length`, measured in cm
> * `Sepal Width`,  measured in cm
> * `Petal Length`,  measured in cm
> * `Petal Width`,  measured in cm
> * `Species` , one of values `setosa`, `versicolor`, `virginica`
> 
> Write the Python code to import this file, and create a scatter plot of Petal Length vs Sepal Length with Species on the legend.  Use the seaborn library for the visualisation.

The AI tool may respond with some Python code like the example below

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

# URL of the dataset
url = "https://raw.githubusercontent.com/MarkWilcock/CourseDatasets/main/Iris/irisdata.csv"

# Import the dataset
df = pd.read_csv(url)

# Display the first few rows of the dataset to verify its contents
print(df.head())

# Create a scatter plot of Petal Length vs Sepal Length with Species on the legend
sns.scatterplot(data=df, x='Sepal Length', y='Petal Length', hue='Species')

# Add title and labels
plt.title('Scatter Plot of Petal Length vs Sepal Length')
plt.xlabel('Sepal Length (cm)')
plt.ylabel('Petal Length (cm)')

# Show the plot
plt.show()

This code creates the chart below.
Scatter plot from Generated Python Code

Exercise: Create Python data structures

Here are a set of prompts you can use to ask the AI to generate some Python code . Use these as a starting point. The prompts are based on coding tasks for students in an exercise about data structures (list, tuple,dicts) in my Python course.

If you would like to test the Python code generated by the AI tool, and you don’t already have access to a Python environment, you can quickly set up a Colab environment following these instructions.

Prompt: Act as a Python expert. Provide Python code in response to the subsequent prompts and give a brief explanation suitable for beginners to Python.

Prompt: Create a list with at least three elements of string datatype. Choose fruit or flowers or cars or whatever.

  1. Find the first element.
  2. Find the last element.
  3. Append another element to the list
  4. Count the elements in the list
  5. Insert an element into the third position
  6. Sort the list

Prompt: Create a tuple with three elements. Assign each element of the tuple to a different variable.

Prompt: Create a dictionary with these keys and values:

Prompt: Answer these questions about this dict

  1. What is the value corresponding to the “NIR” key?
  2. What is the value corresponding to the “GBR” key?
  3. Can you set a key “JER” with value “Jerzee”
  4. Can you set another key “JER” with value “Jersey”. What happens?

Here is a link to a shared ChatGPT conversation based on this example.

Exercise: a more challenging Python task

If you are familiar with Python basics, choose a task from one of the more advanced examples in my Python course. Copy the instructions in comments or markdown into the prompt box for the AI tool and evaluate and test the response

For example, there are exercises on:

AI Code Assistants / Pair Programmers

AI Tools are exceptionally good at helping us understand, learn, and write code. When these tools are embedded into our code editor, they are known as called code-assistants or pair-programmers. They help in many ways:

Examples of code assistants include: