The world’s leading publication for data science, AI, and ML professionals.

How to Effectively Start Coding in the Era of ChatGPT

Some advice to use ChatGPT to learn to program while actually learning it

Image by StockSnap on Pixabay
Image by StockSnap on Pixabay

If you’d like to start to program but you’re afraid AI will steal your job, then this article is definitely for you. Especially if you’re thinking about changing your career to become a Software Engineer or a Data Professional, I really feel how uncomfortable you are right now.

I started to program about 3 years ago when I was 30 with no prior experience, and this helped me find my first remote work in IT.

Now, 3 years ago ChatGPT didn’t exist and I know you’re vexed with questions like: "What if I invest months learning and practicing to program and then I don’t find an actual job because of AI?".

Well, let me tell you one thing: don’t be afraid of the recent layoffs in Big Techs: they’re not directly related to the increase of AI. Also, none really knows if AI will replace developers in 5 years, so here’s my opinion: AI will replace developers that can’t actually code. So, if you effectively learn how to code, you’ll find AI as a tool that helps you speed up your daily tasks, not an enemy that will replace you.

So, in this article, we’ll see how ChatGPT can actually be an added value for you if you’re thinking to start to program today, regardless of how old you are and what’s is your current career status.

This is what you’ll find here:

Table of Contents:

Rule number 1: how to correctly ask questions to ChatGPT
Rule number 2: always refer to the documentation
Rule number 3: troubleshooting and debugging
Examples in Python

Rule number 1: how to correctly ask questions to ChatGPT

The first thing to learn is how to correctly ask ChatGPT the right questions.

When I started learning to program I thought that developers were nerdy people who started to program at 5 years old and had a huge knowledge of programming, having in mind all the code they needed.

And you know what? I couldn’t be more wrong. Here’s a harsh truth:

  1. A lot of developers started to learn to program in their 20s and 30s.
  2. The majority of developers work by searching stuff on Google and StackOverflow (and today, they ask questions to ChatGPT).

So, first of all, if you’re starting to program right now don’t be ashamed to spend a lot of time on Google, StackOverflow, or ChatGPT. It’s completely normal. You just need to know how to correctly do so because you’ll never have in mind all the code you need (you’ll have in mind just the methods and functions you use the most, of course).

Anyway, the difference between today and the 90s is that in the 90s the Internet barely existed, so developers hadn’t all the support that have today thanks to global communities, forums, websites, and AI.

This led developers to learn to program the hard way, not rarely spending days to solve a problem (finding a bug or finding the right piece of code).

Today, you have a great opportunity: you have a senior developer available 24/7 and this is ChatGPT, but you have to treat it like so, not like an oracle.

So, here’re some guidelines on how you should correctly query ChatGPT if you’re starting to program today:

  1. Learn the basics first. Before using ChatGPT, my advice is that you need to learn the basics first for a simple reason: ChatGPT often gives you code that you need to refactor, for any reason. This means that you need to have prior knowledge of coding: you can’t just ask ChatGPT to create some code for you to solve an actual problem. The kind of developers that will be replaced by AI is properly the ones that ask questions to ChatGPT with no prior (or with poor) knowledge of coding. So, before prompting, get a course and start practicing.
  2. Ask questions, then learn. When we’re children we are very curious and our questioning makes us grow because we’re genuinely interested in the answers. This is the state of mind we’d always have as Software Professionals: having an answer and just copying and pasting it won’t make us learn how to program. If the code we get from ChatGPT works you should, at least, ask it to explain the code it used and why it is used it. As a beginner, in fact, your role is to learn so that in the future you can use ChatGPT less and be more independent: this is why you should always ask for code explanations.
  3. Solve actual problems. When programming, we can solve the same problem with different methods: this is why it doesn’t make any sense to just ask ChatGPT to generate some random code. We absolutely need to ask it something related to a project we are making or a problem we need to solve.
  4. Don’t blindly trust it. As a generative AI system, ChatGPT is prone to errors. While today with GTP-4 models we’ve seen very good improvements, there are still errors in the code. Also, errors are often due to the prompts that may be incomplete or somehow misleading. This is why, even if the code works, you can’t blindly trust the answers and need to understand deeply the code you get.

Rule number 2: always refer to the documentation

The learning workflow while coding. Image by Federico Trotta.
The learning workflow while coding. Image by Federico Trotta.

I created the above image for some content I wrote on Linkedin some months ago (oh, and by the way: here’s my profile if you want to get in touch).

Here’s the moral of the story: regardless of where you’re searching for the information you need to solve your coding issue, you always need to read the documentation. Always.

This is the Gospel for us Developers/Data Professionals. We always need to read the documentation.

In fact, if we find a snippet of code that can solve a problem we’re facing, if we don’t understand the code we won’t be able to generalize the code we’re using. This means that in the future we won’t be able to solve a similar problem because we haven’t learned anything: we just copied and pasted the code.

And this is particularly true with ChatGPT, especially because sometimes it invents libraries that don’t exist. Yes, you read it right: sometimes ChatGPT invents libraries.

We have to say that, thanks to GPT-4, OpenAI made a lot of improvements. But in the last months, it happened to me at least a couple of times that ChatGPT invented some unexisting libraries.

And you know how we can see if a library doesn’t exist? There are some possibilities:

  1. You know what you’re doing and asking ChatGPT. But this will happen after months of practice when you gained some experience.
  2. You get the code from ChatGPT, read it, try to understand it, ask ChatGPT for some explanation on the usage, go online to read the documentation and find no reference.
  3. You try the code and get an error, and you learn your lesson the hard way. But don’t be ashamed of it: it happened even to me.

Remember: there’s nothing bad in the urge to solve a problem as soon as possible. So, you can even try the code and get an error. Well, the truth is that the error can even be due to a bad prompt, and this happens very frequently.

Anyway, remember: if your job is done because you solved the actual problem, then, please: go read the documentation for more understanding. You may also discover that your issue can be solved with less code.

Rule number 3: troubleshooting and debugging

ChatGPT is a great tool even for troubleshooting and debugging your code. However, even in this case, you must learn how to use it correctly, if you’re starting your learning-how-to-code path.

First of all, if you’re using an IDE like VS CODE or PYCHARM to program, then use their debugging features. They have integrated debugging features thanks to which you can just push a button, and they debug your code, showing you (eventual) troubles in your code.

At this stage, but even later (I’m still using this method), the best idea is to follow this path:

  1. Write your program.
  2. Debug your code in the IDE.
  3. Read the eventual warnings you get from the debugger if something doesn’t work.
  4. Paste the warnings on Google if you don’t know how to solve them.
  5. Paste the warnings on ChatGPT if Googling doesn’t solve your problem.
  6. Paste the whole code in ChatGPT if you still haven’t solved your issue.
  7. Go read the documentation if the previous point solved your issue.

I’ll never get tired to repeat it: we always need to refer to the documentation when Google or ChatGPT solve an issue we have with our code.

So, please: don’t blindly paste the code into ChatGPT asking to search for troubles in your code: first, strive to find them on your own the hard way because this is the only way you’ll learn things. There’s no other way to learn stuff, and I know you know it.

Finally, an important note: remember that ChatGPT works in the cloud and, also, it may use your prompts to train its algorithms (with recent changes in the data privacy usage you should be able to change the settings, and choose not to give consent to OpenAI to train its algorithms with your prompts).

However, an important thing to keep in mind is to not paste into ChatGPT code to debug with sensible information: due to data breaches and other related security issues, it’s better to keep yourself safe.

Examples in Python

Now, let’s see some examples of prompts we should write on ChatGPT to learn how to code.

I’ve asked ChatGPT the following: "How can we print in Python?"

Here’s the answer:

In Python, you can use the print() function to display output on the console or terminal. The print() function takes one or more arguments and prints them as text.

Here are a few examples of using the print() function:

Printing a simple string:

print("Hello, world!")

>>>

Hello, world!
  1. Printing variables:
name = "Alice"
age = 25
print("My name is", name, "and I am", age, "years old.")

>>>

My name is Alice and I am 25 years old.
  1. Printing multiple arguments separated by a specific character:
print("One", "Two", "Three", sep="-")

>>>

One-Two-Three
  1. Printing formatted output using f-strings (Python 3.6 and above):
name = "Bob"
age = 30
print(f"My name is {name} and I am {age} years old.")

>>>

My name is Bob and I am 30 years old.

These are just a few examples to demonstrate the usage of the print() function. You can pass various types of objects as arguments to the print() function, and it will automatically convert them into a string representation before printing.

Now, a question: which method would you use?

Exactly, here’s the problem! The prompt we made was too generic so ChatGPT gave us a generic answer.

So, here’s another lesson: prompts must be very specific. One could be: "I have a list with some strings in Python: how can I print them efficiently?"

Here’s the answer:

To print a list of strings efficiently in Python, you can use the join() method along with the print() function. The join() method concatenates all the strings in the list into a single string using a specified separator. Then you can simply pass this concatenated string to the print() function to display it.

Here’s an example:

my_list = ['Hello', 'World', 'Python']

# Using join() to concatenate the strings in the list with a space separator
result = ' '.join(my_list)

# Printing the result
print(result)

>>>

Hello World Python

In the example above, the join() method joins the strings in my_list using a space ' ' as the separator. Then the print() function is used to display the concatenated string.

By using the join() method, you avoid the overhead of multiple calls to the print() function, resulting in more efficient string concatenation and printing.

And here we are: with a specific question we got a specific answer.

And now, you know what we have to do? Yes, you know: if we don’t know how it works, we have to ask ChatGPT some more explanations on the join() method. We can do it in the same chat like so: "What is the method ‘join()’ and how does it work?"

And here’s the answer:

The join() method is a string method in Python that concatenates the elements of an iterable (such as a list, tuple, or set) into a single string using a specified separator. It returns a new string that contains all the elements joined together.

The syntax for using the join() method is as follows:

separator.join(iterable)

Here, separator is the string that will be used to join the elements, and iterable is the collection of elements to be joined.

When join() is called on a separator string, it iterates over the elements of the iterable and concatenates them, inserting the separator string between each pair of elements. The separator is not added at the beginning or the end of the resulting string.

Here’s an example to illustrate how the join() method works:

my_list = ['Hello', 'World', 'Python']

# Using join() to concatenate the strings in the list with a space separator
result = ' '.join(my_list)

print(result)

>>>

Hello World Python

In the example above, the join() method is called on the separator string ' '. It iterates over the elements of my_list and concatenates them, inserting a space between each pair of elements. The resulting string 'Hello World Python' is then printed.

It’s worth noting that the join() method is a more efficient way to concatenate multiple strings compared to using the + operator in a loop. This is because join() internally optimizes the process of concatenation, resulting in better performance, especially when dealing with a large number of strings.

Now, at this point, you should do other two things:

  1. Searching for "join() Python" on Google and see some other examples and read the documentation.
  2. As we can see, ChatGPT tells us that this method is more efficient than using the + operator with a loop to solve this problem. You should continue the chat and ask for an example (then, read the documentation that may come with the next examples).

Conclusions

I hope you find useful this article on how to start your programming journey with the help of ChatGPT.

Remember: don’t take anything for granted. Use it to learn, not as an oracle. And after you solved your issue, write a specific prompt, read the documentation, learn the code and, if possible, make practice with the code you get as an answer.


FREE PYTHON EBOOK:

Started learning Python Data Science but struggling with it? Subscribe to my newsletter and get my free ebook: this will give you the right learning path to follow to learn Python for Data Science with hands-on experience.

Enjoyed the story? Become a Medium member for 5$/month through my referral link: I’ll earn a small commission to no additional fee to you:

Join Medium with my referral link – Federico Trotta


Related Articles