As a Harry Potter fan and a programmer, I can’t help but be reminded of the wizarding world when I think about coding. Just like a young wizard learning spells and incantations, we programmers spend hours upon hours learning syntax and debugging code. But what if we had a way to get a little help with the heavy lifting? What if we could wave a wand and it could effortlessly write code for us!! Seems like it might be actually achievable. In this post, we will allow ChatGPT to do the magic and help us lend a hand in writing code.
If you’re not familiar with ChatGPT, you can refer to the previous blog in this series: Meet ChatGPT: The Conversational AI. Now, let’s move forward turning the magic into reality.
#1 Web Scrapping using ChatGPT
Web Scrapping is probably an interesting and challenging task. I was curious to see how ChatGPT would respond when given the task of web scraping.
I gave the task to web scrap quotes with authors from a website called quotes.toscrape.com. ChatGPT did a great job handling this task.
web scrape quotes.toscrape.com using python and beautiful soup.

Here’s the Python code generated by ChatGPT…
import requests
from bs4 import BeautifulSoup
# Make a request to the website
page = requests.get("http://quotes.toscrape.com/")
# Parse the HTML of the page
soup = BeautifulSoup(page.content, 'html.parser')
# Find all the quotes on the page
quotes = soup.find_all(class_='quote')
# Loop through each quote
for quote in quotes:
# Find the quote text and author
text = quote.find(class_='text').get_text()
author = quote.find(class_='author').get_text()
# Print the quote and author
print(f"{text} - {author}")
And here’s the extracted data..
“The world as we have created it is a process of our thinking. It cannot be changed without changing our thinking.” - Albert Einstein
“It is our choices, Harry, that show what we truly are, far more than our abilities.” - J.K. Rowling
“There are only two ways to live your life. One is as though nothing is a miracle. The other is as though everything is a miracle.” - Albert Einstein
“The person, be it gentleman or lady, who has not pleasure in a good novel, must be intolerably stupid.” - Jane Austen
“Imperfection is beauty, madness is genius and it's better to be absolutely ridiculous than absolutely boring.” - Marilyn Monroe
“Try not to become a man of success. Rather become a man of value.” - Albert Einstein
“It is better to be hated for what you are than to be loved for what you are not.” - André Gide
“I have not failed. I've just found 10,000 ways that won't work.” - Thomas A. Edison
“A woman is like a tea bag; you never know how strong it is until it's in hot water.” - Eleanor Roosevelt
“A day without sunshine is like, you know, night.” - Steve Martin
#2 Implement K-means clustering from scratch in python on a custom generated dataset and also plot results using matplotlib
The next task that I gave ChatGPT was to implement the K-means clustering algorithm. It handled it beautifully. Here you go…
Implement K-means clustering from scratch in python on a custom generated dataset and also plot results using matplotlib.



import numpy as np
import matplotlib.pyplot as plt
# Generate random data
data = np.random.rand(100, 2)
# Number of clusters
k = 3
# Number of training data
n = data.shape[0]
# Number of features in the data
c = data.shape[1]
# Generate random centers for the clusters
mean = np.mean(data, axis = 0)
std = np.std(data, axis = 0)
centers = np.random.randn(k,c)*std + mean
# Plot the data and the centers generated as random
plt.scatter(data[:,0], data[:,1], s=7)
plt.scatter(centers[:,0], centers[:,1], marker='*', c='g', s=150)
# Function to calculate distance between two points
def dist(a, b, ax=1):
return np.linalg.norm(a - b, axis=ax)
# Loop for the number of iterations
for i in range(10):
# Assign each value to its closest center
clusters = np.zeros(n)
for i in range(n):
distances = dist(data[i], centers)
cluster = np.argmin(distances)
clusters[i] = cluster
# Store the old centers
centers_old = np.copy(centers)
# Find the new centers
for i in range(k):
points = [data[j] for j in range(n) if clusters[j] == i]
centers[i] = np.mean(points, axis=0)
# Calculate the distance between the old and the new centers
error = dist(centers, centers_old, None)
# If the distance is zero, the clusters are optimal
if error == 0:
break
This is the plot generated after running the above code…

#3 Generate a large custom dataset using Python resembling Titanic Dataset. Save this data as csv
For those learning Machine Learning or Data Science, ChatGPT can be used as an excellent tool to quickly generate a dataset. I asked ChatGPT to generate Titanic Dataset and the results are amazing…
Generate a large custom dataset using Python resembling Titanic Dataset. Save this data as csv.


Here is the code…
import csv
import random
# Set the number of rows in the dataset
num_rows = 1000
# Set the probability that a given row will be a survivor
survival_prob = 0.5
# Set the column names
columns = ['passenger_id', 'survived', 'class', 'name', 'gender', 'age', 'siblings_spouses', 'parents_children', 'fare']
# Initialize an empty list to store the rows
rows = []
# Add rows to the list
for i in range(num_rows):
passenger_id = i
survived = random.random() < survival_prob
passenger_class = random.randint(1, 3)
name = f'Person {i}'
gender = random.choice(['male', 'female'])
age = random.randint(0, 100)
siblings_spouses = random.randint(0, 8)
parents_children = random.randint(0, 8)
fare = random.uniform(0, 500)
rows.append([passenger_id, survived, passenger_class, name, gender, age, siblings_spouses, parents_children, fare])
# Write the rows to a CSV file
with open('titanic_dataset.csv', 'w') as f:
writer = csv.writer(f)
writer.writerow(columns)
writer.writerows(rows)
And this is the output…

Bonus: Text Summarizing using ChatGPT
Have you ever found yourself in a situation where you spent hours summarizing your content? Not more, apart from using ChatGPT to do the heavy lifting, it can also be used to summarize the text.
The below paragraph is taken from Wikipedia to demonstrate text summarization using ChatGPT.

And so, with ChatGPT at our fingertips, we are able to turn even the most boring and tedious tasks into a magical experience. We can simply speak the words of our desires and watch as ChatGPT works its magic, automating our work while we sit having our favourite snacks. So why not give ChatGPT a try and see for yourself the wonders it can do? Whether you’re a busy auror or a hardworking house elf, ChatGPT is the perfect tool to bring a little extra magic into your daily routine.
Try ChatGPT: https://chat.openai.com/
If you like the post, don’t forget to share it with your peers and give your reactions!