Bonus 2 - AI-Powered Python Code Assistant
Hello, everyone, and welcome to this lesson. In this lesson, I will show you how to leverage
ChatGPT to conduct research, find the right programming language syntax, and perform code
debugging. So the first question is what is ChatGPT?
ChatGPT is an artificial intelligence, large language model developed by OpenAI. It has been
trained on a massive amount of text data from the internet, which allows it to generate human-
like responses in natural language. So to try out ChatGPT, first, you need to go to this link,
[Link]. And then you will find a quick introduction here, to chat ChatGPT. And you can
simply just say Try ChatGPT.
You need to insert your email, and then you can go ahead and try it out. Simply, all you need to
do is to write questions here, in human language. And then the chat bot is going to answer back
to you.
So it can help you write emails, write essays. It can help you, as well, with your general
workflow. So what I'm going to do right now is I'm going to show you five use cases, specifically
when it comes to Python programming.
So first, I'm going to show you how you can leverage ChatGPT to conduct research and find the
right programming language syntax. So let's assume that you would like to learn how to define
a conditional statement in Python. So I can go ahead and simply ask ChatGPT the following
question.
What is the syntax to define a conditional statement in Python? And if you press Enter on your
keyboard-- here we go-- basically, you will find that ChatGPT replied back, telling you, in
Python, the syntax to define a conditional statement using the if statement is as follows. If
condition, you add colon at the end, and then you type the code to be executed if the condition
is true.
And then you will find a quick description here. And then here, it's going to show you as well, if
you'd like to use the elif statement too. So if you have more than one condition, it's going to
show you, as well, the code associated with it. And it's also showing you the description of that.
OK, let's assume that you would like to apply a function to a Pandas DataFrame, as an example.
So I can go ahead and ask this question. How can I apply a function to a Pandas DataFrame?
And then when you press Enter, it's going to tell you when.
You can apply a function to a Pandas DataFrame using the Apply method. The Apply method
applies a function along an access to the DataFrame, and showing you as well how you can do
this. You can just say [Link], you write the name of the function, and then you specify the axis
here, either 0 or 1. And you will find as well, here, a quick example with numbers, and going to
1
Bonus 2 - AI-Powered Python Code Assistant
show you the output as well, associated with it, which is again, going to be super powerful and
helpful for you.
All right, next, let's assume that you would like to get some help in understanding some of the
concepts taught in class. So what I could do is I can simply ask ChatGPT to pretend that you are
my Python teacher assistant. Teach me the concept of Python functions, as an example.
If you press Enter, it's going to tell you, well, sure, I'll be happy to help you understand Python
functions. In Python, a function is a block of code that performs a specific task. Functions can
help break down complex programs into smaller, more manageable pieces. They allow you to
reuse code, improve code readability, and make your code more modular.
And here, it's showing you, basically, the syntax to write a Python function. And it's showing
you what each part, basically, in the function does. And you can also see an example here,
associated with it. And the function call as well, associated with it too.
OK. Next, I'm going to show you how you can leverage ChatGPT in performing code generation
and design. So let's assume that you would like to define a Python function that multiplies three
variables together. And I would like to test the function using the following arguments. These
are going to be, let's say number 3, 20, and 5.
I can simply do this, again just go ahead and type the following. Define a Python function that
multiplied three variables together. Test the function using the following arguments, 3, 20, and
5.
And then when you press Enter, it's going to tell you, well, sure, here is the Python function that
multiplies these variables together. So here, it's going to say def [Link], and the
function receives three arguments. And then you can show you as well how to call this function.
And it's also using the exact same numbers that I provided in my prompt. So I said here, 3, 20,
and 5, and that's what you see here. So basically, ChatGPT is not just a simple memorization
engine. It has reasoning capability. It understood that I would like to call the function, send that
function these arguments, and that's what you see here, reflected in the code as well.
OK, let's assume that you would like to rewrite that code using lambda functions instead. Well, I
can go ahead and write that. So I can say write the code using-- or rewrite the code using
lambda functions instead. Then you press Enter, and here we go.
What you see right now is going to tell you, well, sure, here is an example of how you can
rewrite the previous code using a lambda function. And this is, again, an additional important
feature in ChatGPT, that it actually remembered what happened before. So it's remember the
previous prompt that I showed here.
Bonus 2 - AI-Powered Python Code Assistant
So before, I asked for a Python function that multiplies three variables together. And here,
when I say rewrite the code, I'm referring to the previous code. And that's why it's just already
understood that I need to multiply three numbers together. And this is the lambda function,
and here is the call for that lambda function as well.
OK, next, I would like to show you how you can leverage ChatGPT to perform code debugging.
So what I'm going to do is I'm going to copy, basically, this prompt here. So here, I'm asking
what is wrong with this Python function in one sentence.
So this is a function that simply calculates the future value. And the function is called
calculate_FV, or Future Value. And here, I intentionally forgot to add colon at the end of the
Python function definition. And if you just simply press Enter on your keyboard, here we go. It's
going to tell you when.
The function is missing a colon after the function definition line, which will result in a syntax
error. OK, that's great. I can simply go ahead and ask ChatGPT, can you please fix that function
for me? It'll tell you, well, sure, certainly. Here's the fixed function with the missing colon and
some additional formatting.
You will find that ChatGPT here added a colon at the end, and it's showing you that this
function should now work properly and return the future value of an investment given the
present value, interest rate, number of periods, and number of compounding periods per year.
Please note that we simply didn't mention any of that information, just ChatGPT understood
what we are doing when we just provided the function in the Python language.
All right, let's assume that you are writing your own function, and you came up with an error
message. So what I could do, you can just simply say the following. I can go ahead and say what
is wrong with this Python code. And what I've done here is I simply copied the error message I
obtained from my Jupyter Notebook.
So if you do that, and you press Enter, it'll tell you, well, the error message suggests that there
is a syntax error in the Python code, specifically, on the line where the calculate future value
function is defined. It appears that there is a missing colon at the end of the function definition
line. And you will notice here that this is simply the correct code. And it's telling you that this
should fix the syntax error and allow the function to be defined properly.
All right, you can also use ChatGPT to introduce new features to your code. Let's assume that
you have a simple Python function that performs addition. The function simply receives two
arguments, and it sums up these two arguments together.
So what I could do is I can rewrite the function to perform additional features, such as
subtraction, for example. Let me show you an example prompt. So what I have here is I have
simply a function. This is a function called my_sum.
3
Bonus 2 - AI-Powered Python Code Assistant
The function receives arguments x and y, and then the function returns z. And it sums up x plus
y and just returns the z at the end. And then this is the function call. So here, I'm calling the
function, and I'm sending it arguments. These are 5 and 10.
And what I mentioned here is I said, rewrite the following function so it can have two additional
features. The function should take inputs from the user, and the function should perform
subtraction and addition. So to go ahead and run this, you can just press Enter on your
keyboard. And here we go.
What you see that ChatGPT said, OK, here is a modified my_sum function that takes input from
the user and can perform addition or subtraction based on user input. What you notice is now
simply I have addition operation. I also have subtraction operation. And you will find that the
function has been adjusted to receive inputs from the user as well. And it's also, here,
explaining to you what that function does, which is, again, super powerful.
OK what you could also do that you can also use ChatGPT to perform code documentation. So
what I could do here is I can say, can you add documentation to the following code? So this is a
code here, and I would like to add documentation, and kind of explain to me what this function
does.
And if you just press Enter-- here we go. It's going to tell you, well, certainly. Here is an updated
version of the code with documentation. So you will find that here, the ChatGPT has
commented out and added comments here, and it's also added docstrings associated with it.
It's going to tell you, well, calculate the sum of two numbers. These are the arguments, x and y,
the first number. y is going to be the second number. The function is going to return the sum of
x and y, and it's going to show you, as well, here, that this is example usage, commented out.
And in this updated code, the my_sum function is now documented using docstrings. The
docstring provides a brief description of what the function does, lists the functions arguments
and their types, and explains what the function returns. And it's telling you, as well, an example
usage of the my_sum function is also provided, which calculates the sum of 5 and 10 and
assigns the result to a variable called result. The print function is then used to display the value
of result.
And that's it. That's all I have for this lesson. I hope you enjoyed it and see you in future lessons.
Bonus 2 - AI-Powered Python Code Assistant