0% found this document useful (0 votes)
4 views4 pages

AI-Powered Python Code Assistant

This lesson demonstrates how to use ChatGPT as an AI-powered Python code assistant for various tasks such as research, syntax finding, code debugging, and documentation. It provides examples of asking ChatGPT for help with Python programming concepts, generating functions, debugging code, and enhancing existing functions. The session concludes with a summary of the capabilities of ChatGPT in assisting with Python programming tasks.

Uploaded by

ROHIT VERMA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views4 pages

AI-Powered Python Code Assistant

This lesson demonstrates how to use ChatGPT as an AI-powered Python code assistant for various tasks such as research, syntax finding, code debugging, and documentation. It provides examples of asking ChatGPT for help with Python programming concepts, generating functions, debugging code, and enhancing existing functions. The session concludes with a summary of the capabilities of ChatGPT in assisting with Python programming tasks.

Uploaded by

ROHIT VERMA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Bonus 2 - AI-Powered Python Code Assistant

[00:00:00.00] [AUDIO LOGO]

[00:00:07.98] RYAN AHMED: 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?

[00:00:24.17] 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, openai.com. And then you will find a quick introduction here, to chat ChatGPT. And
you can simply just say Try ChatGPT.

[00:00:55.10] 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.

[00:01:09.60] 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.

[00:01:23.42] 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.

[00:01:43.71] 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.

[00:02:08.12] 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.

[00:02:23.99] 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.

[00:02:40.22] 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 df.apply, 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 show you the output as well, associated with it, which is again, going to
be super powerful and helpful for you.

[00:03:07.85] 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.

[00:03:26.22] 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.

[00:03:46.91] 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.

[00:04:00.53] 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.

[00:04:24.38] 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.

[00:04:37.58] 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 multiply.three, and
the function receives three arguments. And then you can show you as well how to call this
function.

[00:04:53.51] 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.

[00:05:18.27] 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.

[00:05:34.76] 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.
[00:05:49.92] 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.

[00:06:09.56] 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.

[00:06:24.70] 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.

[00:06:47.31] 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.

[00:07:05.46] 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.

[00:07:33.81] 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.

[00:07:52.63] 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.

[00:08:17.53] 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.

[00:08:34.91] 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.
[00:08:51.13] 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.

[00:09:07.66] 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.

[00:09:25.96] 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.

[00:09:51.29] 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.

[00:10:13.28] 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.

[00:10:29.45] 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.

[00:10:45.53] 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.

[00:11:11.85] And that's it. That's all I have for this lesson. I hope you enjoyed it and see you in
future lessons.

[00:11:18.83] [MUSIC PLAYING]

You might also like