AI & Python #1: How to Learn Python with ChatGPT
Python is the language of choice for AI. In this article, we'll see how to learn Python with ChatGPT.
Hi!
Today we start a new series of articles focused on the tech stuff behind AI. Unlike most articles on Artificial Corner, this series will be technical and cover topics such as Python, ML, data science, math/statistics, and more. This series will be very long and deserves its own space, so I named it “Behind AI“ and will be published every Monday.
If you’re not into technical stuff, don’t worry, every week we’ll still cover non-technical topics to keep you updated with the latest on AI and will also start new mini series to show you ways to use AI to make your life easier.
Without a doubt, Python is the most popular language in the AI community due to its simplicity, flexibility, and data science libraries such as Pandas, Numpy, and Scikit-learn.
Python has always been an easy programming language, and learning Python has become even easier thanks to ChatGPT.
In this article, we’ll see how to learn Python using ChatGPT.
Note: I’m starting this series from the ground up, so I’m adding at the end of this article, a 1-hour crash course video to learn the basics of Python. This is a bonus for those new to coding or Python.
ChatGPT Prompts to Learn Python
1. Create a roadmap to study Python with ChatGPT
Many times I mentioned how important is to build our own curriculum to learn technical things and Python isn’t an exception to this.
You should consider creating a customized curriculum that helps you focus on the Python stuff that you’ll need more in your field.
Here’s the prompt to use.
Act as a programming mentor. I want to learn Python for [field of interest]. Create a roadmap to learn Python each week and add resources to study each week
Now, that was a very simple prompt. I could add some background information about me like the programming concepts I already know to customize the result a bit more.
2. Ask for alternative solutions
When solving coding exercises, we tend to settle for the approach that gets the job done and then move on to the next exercise.
Although that isn’t wrong, we could enhance our learning by asking ChatGPT for alternative solutions. You might get solutions with fewer lines of code, faster code execution, or an increase in efficiency.
For example, say I have to solve the exercise below.
# Write a program in Python to turn every item of a list into its square
numbers = [1, 2, 3, 4, 5, 6, 7]
The first thing that comes to my mind is using a for loop and appending the square numbers to a new list.
That gets the job done but it might not be the best solution. Let’s see what ChatGPT has to say.
Show me different approaches to solve the exercise below.
Exercise: Write a program in Python to turn every item of a list into its square. numbers = [1, 2, 3, 4, 5, 6, 7]
As you can see, ChatGPT showed me alternative solutions using list comprehension and the map function.
3. Brainstorm unique projects
The best way to learn to code is by doing, which means that you need to start solving projects asap!
The problem is that the standard projects out there (snake game, titanic, etc) might not be attractive enough for us to complete it.
That’s when ChatGPT comes in handy — we can use ChatGPT to come up with cool and unique project ideas based on the programming knowledge we have and things we like.
Here’s the prompt to use.
Act as a programming mentor. I’m learning Python and have already learned [topic 1, topic 2, …]
What Python projects can I solve? Consider that I like [thing 1, thing 2, …]
Say we followed the curriculum created before and already learned the topics in weeks 1 and 2.
As you can see, the projects are not limited to the topics I know (which is cool because I can learn new stuff) and also consider things I like such as automating stuff, learning foreign languages, and football.
But that’s not all!
After picking up a project I can ask for the steps to solve it (without making ChatGPT to write the code for me).
Say I picked up a football project. ChatGPT can provide the steps to solve it.
And for each step, you can ask for more guidance. Say I need help with step 1 “Collect data.”
Cool isn’t it? A private programming tutor available for us 24/7!
Note: Unique projects will also help you in job interviews. Why? Because those projects won’t be on the resumes of many candidates.
4. Add comments to other peoples’ code
I’ve heard this phrase many times.
Code is read ten times more than it’s written
That’s true. We actually spend a lot of time reading other people’s code when learning to code and even at work!
The problem is that most of the code we read on GitHub or anywhere else isn’t properly commented (or doesn’t have comments at all!), so it’s a good idea to ask ChatGPT to add comments to the Python scripts you read.
For example, in one of my YouTube tutorials, I forgot to add comments to the code below (sorry about that!).
But now, you can simply copy the code from my GitHub and ask ChatGPT the following.
Add inline comments to the code below and explain what is doing.
[script]
Great! Now you can have a better understanding of the script as a whole and know what each line of code does.
5. Translate code
Say you’re new to Python, but you already have experience with another programming language. You can use ChatGPT to translate existing code written in your favorite language to Python in seconds!
Here are a few ideas:
If you know the syntax to fill null data in a SQL table, you can ask ChatGPT to find the equivalent function and translate the SQL query to pandas code.
If you have created a function in R and want to adapt it to Python, you can ask ChatGPT for help.
Again, you can ask ChatGPT to add comments and explanations to know more about the translated code.
That’s it! For more prompts, check out this article on how I’d learn to code using ChatGPT.
Bonus: Python Core Concepts
This bonus is for those new to coding or Python. Enjoy!