3 ways to safely use ChatGPT to assist you with learning code

3 ways to safely use ChatGPT to assist you with learning code

By Bella Ratmelia, Librarian, Data Services

In a recent State of GPT talk at the Microsoft Build 2023 event, Andrej Kapathy, one of the co-founders of OpenAI, emphasized that ChatGPT (and LLMs in general) should be seen as "co-pilots" rather than standalone solutions. He also recommended LLMs be restricted to use cases that suit them well, such as in low-stake applications or as sources of inspiration or suggestions. One of such “low stake” applications suitable for ChatGPT is to enlist it as your co-pilot in your code learning journey.

ChatGPT has been to be quite excellent at generating beginner-level codes as well as explaining them, which is exceptionally helpful for novice coders or those in the early stages of learning. Let's explore some ways you can enlist ChatGPT as your co-pilot:

Use #1: Explaining Complicated Code from Stack Overflow

Stack Overflow is a popular platform for coders from all skill levels seeking solutions to coding problems. Naturally, many novices flock here to seek advice from other coders. However, sometimes the code snippets found on Stack Overflow can be complex and challenging for novices. With the correct prompt, you can get ChatGPT to explain these code snippets in a way that’s easy to understand.

Here is an example prompt that I used, that you can try yourself! In this example, I asked ChatGPT to explain list comprehension in Python (I chose list comprehension here because it is one of those things that often trip a novice coder):

Hi! Please explain step-by-step this code snippet to a novice coder. Thanks!
new_list = ['even' if x % 2 == 0 else 'number three' if x == 3 else 'odd'
for x in range(1, 10)]

In the above prompt, “step-by-step” is added to elicit the reasoning from ChatGPT.

Another convenient thing about using ChatGPT is that you can continue the conversation if you have more questions about the code snippets, which is very handy for novices!

Use #2: Exploring Alternative Coding Approaches

While there are often multiple ways to accomplish a coding task, you may find yourself sticking to one approach that you’re familiar with. ChatGPT can be an invaluable resource in such situations, as it can suggest alternative ways to solve a coding problem. Although these alternatives may not always be better than your own approach, they would at least present different perspectives that could help to expand your coding knowledge.

Here is an example prompt that I used. In this example, I am asking ChatGPT to provide alternative ways to check if a string is a palindrome (this is a common coding challenge):

H! Can you suggest other ways to write this code below? Please explain the alternative step-by-step. Thanks!
def isPalindrome(str):
for i in range(0, int(len(str)/2)):
if str[i] != str[len(str)-i-1]:
return False 
return True 

Use #3: Generate Code Challenges (and its solutions)

Practice makes perfect, and of course that also applies to coding. You can use ChatGPT to generate coding challenges for you to practice your skills with. You can even gamify this process to make it more engaging.

Here is a prompt that I used, as an example:

Let's play a coding game! Come up with 5 coding challenges that’s suitable for python novices worth 10 points each and you will check if my answer is correct. Be sure to include these topics: [list of topics here, e.g., dataframe, stats, loops, etc.]

Conveniently, if you are stuck, ChatGPT can offer solutions to the challenges. Although as you progress to the more complex challenges, ChatGPT can sometimes give you answers riddled with errors.

Word of caution

This should go without saying, but code snippets and suggestions from ChatGPT should be carefully reviewed and tested before you apply them in high-stake applications (such as your assignments or your final year projects). ChatGPT should be used as a complementary learning tool and it’s not meant to replace other learning resources that you have e.g., your peers, professors, and other professionals. Embrace the learning opportunities it provides, but always remember to use it together with other trusted learning resources!