Python for AI - Full Beginner Course
By Dave Ebbelaar
Summary
## Key takeaways - **Master Python Setup for AI Development**: Focus on setting up a professional development environment from the start, including Python, VS Code with essential extensions like Pylance, and understanding virtual environments. This foundational step is crucial for managing packages and dependencies effectively, mirroring real-world AI development practices. [02:23], [03:37] - **Understand Core Python Syntax & Data Types**: Grasp fundamental Python concepts like variables, data types (integers, floats, strings, booleans), and operators. Pay close attention to syntax rules, including indentation and PEP 8 style guidelines, as these are critical for writing error-free and readable code. [01:01:33], [01:09:48] - **Leverage Functions and Control Flow**: Utilize functions to create reusable code blocks and control flow statements like if, elif, and else to make decisions within your programs. Loops, such as for loops, enable repeating actions efficiently, forming the basis of automation and complex logic. [02:05:51], [01:41:35] - **Work with Data Structures and Libraries**: Master data structures like lists and dictionaries for organizing data, and learn to use external libraries such as Requests for API calls and Pandas for data manipulation. Visualizing data with Matplotlib is also key for analysis and reporting. [01:52:13], [02:56:04] - **Implement Version Control with Git & GitHub**: Learn the fundamentals of Git for tracking code changes and version control, including initializing repositories, staging, committing, and pushing to GitHub. This is essential for collaboration and managing projects effectively. [04:09:44], [04:36:12] - **Securely Manage API Keys with Environment Variables**: Protect sensitive credentials like API keys by storing them in environment variables using a .env file and the python-dotenv package. This prevents hardcoding secrets directly into your code and enhances security. [04:44:05], [04:52:13]
Topics Covered
- The Real Challenge: Environment Setup, Not Just Code
- VS Code: Free, Extensible, and Essential for Python
- Essential VS Code Extensions for Python Development
- Real-World Data Skills: From Weather Reports to Paid IoT Analysis
- Junior Data Analyst Toolkit: Essential Skills Covered
Full Transcript
If you want to learn the Python
programming language specifically to
build AI applications, then this video
is for you. We're going from absolute
beginner to writing real Python code for
AI applications all in one video. And
right now, Python is the language for
artificial intelligence. So whether you
want to build your own AI apps, get into
data science, or simply become more
valuable in your career, this is one of
the most in demand skills you can learn
right now. And now there are already a
ton of learned Python tutorials and
courses out there, right? You're
probably wondering which one should I
follow and why should I watch this
video? Well, most of the videos out
there actually waste your time on a lot
of stuff that you actually don't need to
know if you want to use Python
specifically for AI. So, this video is
different. We'll start from the basics
and I'll make sure to explain everything
step by step so you can follow along.
Then, I'll teach you all the core
concepts you need to understand in order
to learn Python fast. We'll build up in
complexity, work with data, and create
reports. And as a final exercise, you're
going to build your own AI chat
assistant that can chat with your data.
My name is Dave Ealar. I'm the founder
of Datal Lumina, an AI development
company where we built custom AI
solutions for clients across many
industries now. I've been using Python
almost every single day for over a
decade, and I already taught this
language to more than 35,000 students
worldwide. So, if you're looking to
learn Python from a professor who only
teaches, this is not the course for you.
Everything that we'll be doing in this
course is practical, battle tested, and
it's based on real work in the AI field.
The stuff companies and clients are
paying for right now. And look, it
doesn't matter what your background is.
You're never too young, too old, or too
late to start. If you can follow simple
explanations and have a bit of
curiosity, that's important. You can do
this. So, grab a coffee and take out
your notes. This is Python for AAI, the
course I wish I had when I started.
Let's dive in. All right, so let's get
started. I am super excited to go
through this entire course with you and
to teach you everything that you need to
know about Python to build AI
applications. Before getting started and
installing Python, let me give you a
quick overview in 30 seconds how to get
the most out of this course. What you
are looking at right now is the handbook
which I will make available for you link
in the description. It's hosted under
python.dataluminina.com.
All of the other information, links,
codelocks that you need will be in here.
So use that to your advantage. Now,
whenever I go too fast, you can go back
here, find more information. And
whenever you feel like I'm going too
slow, use the chapters below this video
to skip ahead or use the speed controls
below this video to increase the speed
of the video. And now, what's going to
make this course different from a lot of
other Python courses out there beyond
just the focus on AI. In the beginning,
we're going to spend a lot of time on
setting up a professional work
environment for you. You see, most
Python tutorials and courses go through
the Python setup and then they
immediately jump into writing out
isolated Python code examples. But what
I found over the years teaching
thousands of students this language is
that in the beginning, writing the code
itself isn't the hardest. And especially
nowadays with AI, you can literally ask
JPT to come up with anything and you can
plug it into your code editor. What you
need to focus on in the beginning is
having a solid understanding of the
tools you use, how to set up your
environment, how to manage package and
dependencies and how you can create
projects so that whenever you go out in
the real world beyond this course and
you want to build something or follow a
course or tutorial, you know exactly
what it is that you need to do and
you'll have a professional coding setup
exactly the same as what I am using
right now and what you would actually
use when you get a job and work with
Python in the real world. So just know
that we're going to spend quite some
time there in the beginning. That's the
whole getting started chapter and it's
there in the beginning for a reason. You
don't want to skip this if you're not
familiar with these concepts. So, we'll
get into all of the installations,
setups, and configurations that you need
in order to work professionally with
Python. Then, in the Python basics,
that's where we actually get into the
programming language. And now, even if
you already have some coding skills or
you're coming from a different
programming language, I still highly
recommend to go through this entire
getting started chapter with me because
I will show you the exact setup that I
am using right now so that if you follow
through with the course, everything that
you see will be exactly the same on my
screen as well as on your screen. All
right, so let's get started. The first
thing that we are going to do is install
Python on your machine. So following the
handbook, I am here in the section
installing Python to get it up and
running on your computer. So, I'm first
going to walk through the Windows setup.
If you're on Mac, you can use the
timestamps below to skip ahead. So, the
first thing that you need to do is you
can go to python.org/d downloads or
follow the link over here. Then you will
find this page where you can simply
click on download python. And then once
that's uh completely done, you can
doubleclick on it to start the
installer. Now, here is a very important
thing. When you go through the
installer, you should check the
following box. When it asks, should you
add Python to the path? You should check
mark that. It will look somewhat like
this. So, you can see it over here. Let
me add a little bit smaller for you.
Now, this is an older version of Python,
but the setup will look something like
this. And you should add Python to your
path. Then, you can just follow through
with the installer. Do not customize
anything unless you know what you're
doing. All right. And that's it for the
quick installation. Super easy to
follow. So now next in this video I will
show you how to do this on Mac but
you're on Windows. So make sure to skip
to the next chapter where we're going to
install your code editor. All right. So
now I'll show you how you can install
Python if you are on Mac. And the first
thing that we need to check here is
check whether Python is maybe already
installed on your system because for a
lot of Mac systems this is already the
case. So what we are going to do is
we're going to open up the terminal. And
don't worry if you've never used the
terminal. We just use it to type in a
command and it's not that scary. So what
we can do is we can open up Spotlight.
And you can do that by hitting command
and spacebar. Then this will show up and
you can type in terminal. So if I open
that up, you'll see this console coming
up over here. This is a terminal where
we can run commands. And all you need to
do right now is come over here to the
documentation and you should type in
Python 3 space- version. So you can type
it out or you can copy paste it like I
do over here. Come to the terminal and
then I'm going to paste that in with
command V. And what I can now do is I
can hit enter. Now you can see on my Mac
it is already installed because it's
showing me a Python version meaning it's
installed. If you also see a version
over here that also means Python is
installed. If you get an error over here
mentioning like look Python is not
available or it's not recognized then
you still need to install Python and you
can continue with the rest of the
instructions in here. So in case you do
need to install it you can scroll down
over here on this install Mac page and
you can go to python.org/d downloads
which we have open over here. You can
click the big download button. It
doesn't matter if it's a different
version. If you watch this in the
future, it will be different. So, you
click the download button and then over
here it will start to download. So, then
while you wait for that to finish, you
can go to your download folders. Simply
doubleclick on it and install Python
like any other app. All right.
Congratulations on installing Python on
your machine. Now, the next step is to
install an application that actually
allows us to work with the Python
programming language. So, we are going
to install my favorite code editor to
work with the Python programming
language, and that is Visual Studio
Code. And that is really all you need.
I've been using this application for the
past 5 years already. I love it. It's
completely free. It's super extensible,
and it's really it got it's got
everything that you need to
professionally work with the Python
programming language. And nowadays,
there are, of course, a lot of AI
assisted code editors out there. Cursor
being one of the most popular ones right
now. The good thing is that everything
that you do in setup within Visual
Studio Code can also be done exactly the
same and imported within Cursor because
cursor is built on top of VS Code. So
let's get into how to set up Visual
Studio Code. So I come over here to the
install VS Code tab and again depending
on whether you're on Windows or Mac OS,
it's a little bit different, but you can
just go to the website and this big
download button over here, it will be
dynamic for the operating system that
you're that you're on. So for me, it
will say Mac. for you. If you're on
Windows, it will say Windows. So again,
download it. It will go to your download
folder. Click on it and follow the
installation in there. One more time.
It's important that you add it to your
path on Windows. And then you just click
install, finish when done. Process is
exactly the same for Mac OS. You can
keep it in your doc. There might be some
security questions that pop up where you
need to give it permission. So just
follow that. It's completely safe. The
application is for Microsoft. And then
we can go and verify the installation.
So what we can do right now is we're
going to open it. And there we go. So
this is the first screen that I see
right now with versions updates, a
welcome screen, and now since Visual
Studio Code is so extensible and it it
can work with a lot of programming
languages out of the box, there's not
much there. So, what we're going to do
right now is I'm going to share my exact
setup with all the extensions and
settings that I use to work on all of my
Python projects that I've been doing for
the past 5 years. This is really a
professionalgrade environment that we're
going to set up. And this is so so
crucial to spend a little bit of time
here to understand this tool because
what you'll find on this journey of
learning Python is that actually the act
of writing code, writing out the like
line by line sentences and the
characters like you will quickly get a
hang of that and AI can work really well
with that. AI can help you as well, but
you need to be able to know where to
find it, how to set it up, how to create
a new project, how to add new things to
it, how to run code, how to debug code.
So, that's also going to be a a big
difference between a lot of the other
Python tutorials and courses that you'll
find out there. And I know that in the
beginning when I was learning Python,
this was so annoying to me where always,
yeah, I would get it on like a single
file. I could follow a tutorial, but
then when I was on my own project,
things looked different and I just
didn't have the full system and it just
didn't understand the full process. So
that's what we're going to spend some
time on right now. Okay. And the first
thing that we're going to do is we're
going to install the Python extension.
So I can click on the link over here to
see where this is going. But we can also
do this directly from Visual Studio
Code. So what I'm going to do is we're
going to the extensions tab and then
we're going to search for Python. So let
me walk you through that. Here is Visual
Studio Code completely empty. You can
see the extensions over here. So, it's
this little icon. There's also a
keyboard shortcut to it. And you can see
it's already recommending something to
me. Python over here. But in case this
is not there for you, you can type in
Python. And then it should show you
exactly what's over here. Python. It
should be from Microsoft. You see the
blue check mark over here. You can click
on it or directly install here or over
here. So what this is going to do, this
is going to behind the scenes install
the Python extension so that when we
work with Python files, our editor, our
code editor can recognize it. So this is
an essential extension that we need.
Okay, so that was the Python extension
and now we're going to change a quick
setting. We're going to use this later.
Don't worry about it for now. All you
need to do is go to the settings of
Visual Studio Code and you should copy
paste this or search for this. So Python
terminal execute in file directory. This
is really useful for later down the
line. So I can come in here. I go to
settings. And then you can search for
this. And you want to make sure that
this is checked on a fresh new install.
It's like this. And you want to click
this. And that's it. You can just X out
of it. It will automatically save. So
this will make sure that later when
we're going to execute Python files, the
editor will execute the file from the
directory instead of the workspace
route. So when we're going to reference
files, it will be a lot easier. All
right. Then next, there are two more
extensions that I recommend to install.
The first one is pylance and the other
one is Jupiter. So let's start with P
lens first. We do exactly the same
process. We come in here. We search for
pilance. We see it pop up over here also
from Microsoft. And you can see it's
it's actually already installed by
installing Python. But you can double
check this. So make sure that if you go
to Pilance it it if it says install you
should click it. And then we go to
Jupiter which is something we are going
to heavily use throughout this course
but I will show that later. For now, we
can just click install and then we
should be good. This takes a couple of
seconds. Now, coming back to the
instructions over here. What we're going
to do right now is we're going to
customize the code editor. And this is
completely optional. This is not going
to change anything about how the code
editor works, but I like to set up uh my
environment in a specific way. So, for
the past years, I've been using this uh
theme and I really like it, the Atom one
dark. So, let me show you how to install
that and what that looks like. And you
can also search for other teams that you
like. So, this is really just like the
coloring, the fonts. You'll see it in a
bit. So, I can search for Atom one dark
team. Hit install. Trust publisher and
install. There we go. And it
automatically right now it already sets
the coloring. You saw it, right? So,
yeah, you can see right over here it now
asked, do you want dark modern or Adam
one dark? So, I set this over here. You
can also click on set the color team. So
that's now installed. You can also at
any point come all the way to the bottom
over here where you see the little gear
icon and you can see themes over here
and there you can see the color theme
and the file icon. So if I click on this
the color theme you can see everything
that is currently installed. So if you
for example let's click on the Kimi dark
you can see this is a very different
look to what we had right. So this is
for you a way to go through it and to
pick a theme that you like. All right.
Now, there's one more setting, the three
indentation, but we'll actually get back
to that in a couple of minutes because
then I can actually show you what it
does. So, for now, we are going to go to
the next section, and that is creating
projects. One of the most important
things in the beginning to fully
understand because this is really the
point where look, you see examples
online, you see me go through this
tutorial, but now you're in the real
world and you have an idea, you want to
try some. Where do you start? Right? We
have Python installed. We have a code
editor, but how does it work? How where
are the projects? So, let's set that up
right now. So, what I recommend you to
do is creating a folder somewhere on
your computer. Doesn't matter if it's in
your documents, in your home directory,
any type of place where we are going to
store all of our Python projects. So,
for the sake of demonstration, I'm going
to create one on our data luminina drive
over here. So, I'm going to call it
Python projects. All right. And now what
I want you to do next is inside this
folder. So this will be the master
folder. And in here we can do we can
create all kinds of folders for all the
projects. So first we're going to create
a folder for this course and we're going
to call it Python for AI. But whenever
you have another ID you want to try
something else I invite you to do that
in that same folder and follow a similar
structure and a similar setup so that
you work very organized and you always
know where all of your Python projects
are. Now for the main folder, it doesn't
really matter what type of naming
convention you use, whether you use
capital letters or not. But for the
project folders itself, I recommend to
use all lowercase and dashes. And why
this is the case, this is actually
called kebab case. And this is going to
match how projects are going to appear
on GitHub. This is something we'll cover
later. But this is just a best practice
that I always do and I recommend you to
do as well. So what it then looks like
if I come in here Python projects I'm
going to create a new folder and I'm
going to plug in Python for AI all
lowerase and dashes using the kebab
case. So now we have that created. What
we can do right now is now from Visual
Studio Code we can open up this folder.
So let me come back over here. This is
what your Visual Studio Code should look
like right now. If you go over here to
the explorer. So before we were
installing all of the extensions. Now we
can go to the explorer and you can see
this is empty. You have not opened a
folder yet. So we can click on that
click on open a folder and then we go to
the place where we just created our
Python projects and then Python 4AI. So
depending whether you're on Mac, Windows
is a little bit different but make sure
to navigate through the explorer or the
finder to this folder. And then what you
can do right here is you can click on
open. And now what you can see is now
this folder is opening. And the first
time you do this, Visual Studio Code
will ask, do you trust the outers of
this file? We created it. So yes, should
be good. Okay. So now we have this empty
screen over here. And now there's one
more important step that you need to do
for every project that a lot of
beginners forget. And that is creating a
workspace in Visual Studio Code. Because
what we do right now, we open up a
folder and now we need to save that
folder and everything in there as a
workspace. A workspace is Visual Studios
Code way of defining projects. And why
we want to do that is in this way we
create a single file that we can
doubleclick it will open and everything
will be there. So when you're working on
something, you hit X, you shut down your
computer, you restart it, you can
doubleclick on one file and everything
will be there exactly how you left it.
That's what the workspace is about. It's
like a bookmark. It remembers
everything. So now what you want to do
is you want to save the workspace. So
within Visual Studio Code, we go over
here, we go to file and then over here
you should see save workspace as. So we
can click on this and it will by default
open up the the explorer or the finder
in the same folder that you are
currently in and it will also give it a
default name. It will say save as
Python4i.code
test-workspace. This is the VS code file
extension that you need. So if you set
it up like this, there's no need to
change anything about this. So this
folder is already selected and I can
just hit save. So what you now see is
you should now see this workspace folder
show up over here and we can also
confirm that by going over to the finder
or explorer and in the folder we now see
the following workspace. Right. So now
we can test this if this works correctly
because what we can do is we can close
out of video code. So now it's gone. We
can even quit it. And now we can go to
the finder Python projects Python for
AAI and then we double click it. And
there we go. We are back in our folder
now with the workspace file in here. All
right. So now with the workspace set up,
it's time to create your first Python
file. What we're going to do is we're
going to create a new file and call it
hello.py. So let's come back to Visual
Studio Code. And what we can do over
here is we can either use the buttons
over here. So you can click a new file
over here, but I can also rightclick and
say new file. So let's come in here and
let's type in hello.py. Now please be
mindful of the extension that you have
over here. So right now you can see that
here if I type in hello.py you will see
that Visual Studio Code displays the
Python logo over here. So it recognizes
that this is a Python file. Now this is
very important. It doesn't matter what
name you give it in here. That doesn't
matter. But the extension should be.py
if you want to work with Python files.
Now you can also uh for example do a txt
to do a text file or you can do a
markdown file or you could do a
JavaScript file. Visual Studio Code is
flexible enough to understand that based
on the file extension. So for now do
hello.py and hit enter. Now we can open
up this file in our editor and see it
pop up over here. So on the left you
have your files and now you can see the
file over here hello.py. So this is
where we can now actually start to write
Python code. And the first thing that I
want you to do is write a simple print
statement similar to how we did that in
the terminal at the beginning of this
tutorial. Right? Remember? So we do
print and then we do an opening bracket
and Visual Studio Code will
automatically just by hitting the
opening bracket will already create the
closing bracket. You can also see how we
have different colors in there. This is
all possible because of the Python
extension and pyance that we have
installed. Then we can do double quotes
and we type in hello world and an
exclamation mark. So we now have pretty
much the most basic Python script that
you can think of only with a simple
print statement. So let's actually see
how we can run this because in the
terminal we would type this out and we
would hit enter right but if I hit enter
right now I will simply go to the next
line similar to what you have in word or
any other text setter and I can write
more print statements. So now let's
cover how we can run this file over
here. And before running your Python
file, there's one thing that you should
always do at the beginning of every
project. And this is also one of those
things that in the beginning you often
forget it. You just have to double check
it. But don't worry throughout this
course. We will cover it lots of times
so you'll remember. And that is setting
the correct Python installation because
we have installed Python on our machine.
But we also need to tell Visual Studio
Code which Python version to use. Now
out of the box generally nine out of 10
times this in right now automatically
goes correct because you can see right
now here in the bottom screen you can
see 3.13.5.
So right now you can see it is using
this Python installation on my machine.
If I click on this you can see I have
more Python installations over here. If
you use virtual environments, something
we will cover later in this program,
which is a core skill that you need to
understand, you'll end up with multiple
Python versions that you can select. So,
for now, it's important that we select
the default one. If you've just
installed Python, there's likely only
just one version here, but make sure
that you refer to the Python
installation that you installed at the
beginning of this session. So I click on
this and now Visual Studio Code knows
okay if I want to run this this Python
file here is the installation that I
need to use. And now in order to
actually run the Python file and execute
the code what you need to do is you go
to the top right corner over here and
click on the play button. So what this
will do is this will open up the
terminal window within Visual Studio
Code and it will point to the file on
our system. So you can see the full pot
over here and then it will use your
Python installation to run this. And you
can now see that when we run this, you
first see the line over here. Where is
the Python file located? And then you
can see hello world over here. Now I can
simply close this out. I can also click
in here and I can do run Python file in
dedicated terminal. You'll find that
this is exactly the same. It create it
will create a new terminal session. The
easiest way for now is just to remember
when I want to run the script, I can
click on this. And now what is actually
going on here behind the scenes is
something I want to explain. Because
when we click on this play button, what
our editor does for us, it executes a
command. But if we were to not use an
editor like this and simply run this
file via the terminal, how would we do
it? So let's show uh let's have a look
at what what is actually happening. So
right now we can also execute this file
by saying look we want to use Python and
then we point to our hello.py pi file
and we simply run that. So you can see
that right now this is also executing
this file and it's printing this hello
world in here. So if we now close this
out and one more time click on this. So
let's have a look at what's going on
here in the terminal because it's a lot
of information but all that there is to
it is first of all it says look we're in
this current folder. So we go within our
terminal, we go into this folder where
we were currently at. Then we point to
the specific Python location and before
in the example I just did Python, right?
But in the terminal when we do this via
editor, it's explicit. So it completely
says, look, we're using this version of
Python. And then we point to the Python
path. So here you can see even though
let's see, it's cut off a little bit,
but you can see it's pointing to this
hello.py file over here. So that is just
what you should know about what's going
on behind the scenes when you click on
that play button because you could also
do this within the terminal without
needing Visual Studio Code and you can
do it by simply checking look I have the
Python command this is available I can
do that and I can quit out of that and I
can use that Python command to reference
certain files you need to write out the
entire file path and now because we're
in this folder already we can execute
it. So as you are learning the Python
programming language, this is really a
fundamental skill and an idea that you
need to understand. But luckily when we
are using an editor, we can just hit the
play button. So we don't have to do it,
but we need to understand what's going
on behind the scenes. There's also a
default keyboard shortcut that you can
use and that's Ctrl F5 or CMD F5 on Mac.
I actually like to change this. So let's
do that right now to see. You can set it
to anything you want. So if I come over
here to the settings and then go to
keyart keyboard shortcuts and then type
in run python you can find the python
run python file over here and then under
the key binding column if I click in
here I can now plug in any keyboard
combination that I want. I actually like
to do cmd enter and then overwrite that.
There are already commands in Visual
Studio Code that have that as well but I
find if that you have it like this I can
simply now hit cmd enter or control
enter if you're on Windows and I can now
run this. If this is causing any issues
for you or you want another shortcut,
feel free to change it or just use the
play button over here. One more thing
that you should also know right now is
that if you click on this play button or
run the code, this terminal will open up
over here. And this terminal that we're
seeing right here is exactly the same as
the terminal we used before, right? So
here on Mac you have the terminal, on
Windows you have the Windows terminal.
So I can do here python- version and it
will show the Python version that I'm
using. In a similar way, I can do that
here as well. So I can do python-
version and I can also run that here. So
that's something important to keep in
mind. Whenever we're working with Python
code over here and we send it to the
terminal, all the things that we can do
in a regular terminal, we can also do
that here in here right from Visual
Studio Code. So we don't have to leave
it when later we have to install certain
things. So generally, especially in the
beginning, you actually don't have to
deal a lot with the terminal. you just
close it, delete it and then whenever we
need to run something, it will open and
then occasionally we need to install
some things or tweak some settings and
that's where the terminal will come into
play. And whenever the terminal is gone,
so for example, you X out of it or you
lose it. You can also go always go to
the top uh menu bar, click on terminal
and then for example do new terminal.
Now it will show up again. There's also
a default command. It's either cmd or
controll J. So I can hit that and that
will just like it will not close it but
it will hide it. So now if I come in
here and I do python d- version one more
time I show it I do uh command j I can
hide it and then here you see I have the
exact same terminal session still open.
All right so now as an exercise you can
give this a try and experiment with some
different print statements just to get a
feel for what it's like writing code
hitting command or control enter and
sending it to the terminal. So coming
back over here every print line that we
have in here will be a new print. So I
can come in here and I say print and
then I say I am learning AI. And now
when I run this you'll find that first
you see hello world and then you see I
am learning AI. So whenever you are
writing Python scripts you should
remember that code gets executed from
top to bottom and from left to right.
It's pretty much exactly the same to how
you read a book. All right. So you can
play with it, get a feel for it, and
just know that a simple print statement
like this isn't really programming, but
it's the first time that you executed a
Python file, send it to the terminal,
and everything is running. So if you
made it to this point in the course
already, that is already a huge
milestone because if we actually come
back to the handbook and see what we've
learned and say always save your work,
this is important as well. So come in
here, I can hit command controls or you
can go to file save. What this will do,
it will save everything, all the files,
all your work. Uh we have the workspace
in here. So now I can simply close this
out and then remember right, I can come
to my Python projects, Python for AAI
and now I can open up the workspace over
here. So this is already such a big
milestone because by now we have
installed Python on your system. You
created your first Python file, selected
the interpreter. So we know we have the
installation, we have the app, we know
how to sync them and how to point them
into the right direction. We created a
Python program. Remember, we do
hello.py. The pi extension tells us that
it's a Python file. And now we
understand how to run it and send it to
the terminal as well. All right. And now
there's one more quick setting that I
want to do with you. And that is the
following. If we go to settings over
here and then search for tree indent. So
you'll find this setting over here. The
default is eight. I like I personally uh
recommend I like to set this to 20. So
look at what's happening over here in
the uh explorer over here. If I set this
to 20, you'll find that you have a
little bit more indentation in here. Now
you might be wondering Dave like what is
this about? But is this is primarily if
we're going to work with different
folders. So let me actually uh if we
have a folder in here and let's say I
put this in here and I move it and now I
want to see what's in this folder. Look,
you can see there is a tree structure in
here where you can clearly see the
hierarchy. All right, so that was a lot
of setup, right? and you are doing
awesome and the good thing is that this
is just stuff that we have to do once
but I fully understand that as you are
going through this with me you might be
wondering like Dave I get all of this
but how does it all fit together trust
me I felt exactly the same way when I
was learning all these new things in the
beginning but trust me it will get
better we just need to set up a few more
things cover some core concepts and then
you are actually fully prepared to
actually start writing code which from
there will actually become a lot easier
all right so we're making great progress
now the next tab. What I have for you,
this is really cool. I have some
resources on a resource hub that I
created for this course that I want you
to download right now because we will
use most of them later. So, if you
follow the handbook, you have the
resources in the course resources. And
now then, if you click on here, you will
go to this page. You'll see Python fori
resource hub and community access. Now,
this is important because this is where
you can access all the resources. So,
the templates, files, and cheat sheets
that you will need for this course. And
don't worry, this is all completely for
free. And on top of that, you'll also
get exclusive community access to the
data luminina academy where you'll find
bonus lessons, advanced tutorials, and
also free follow-up courses to continue
your AI learning journey. Now, I am
hosting this on Gumroad, which made it
really easy for me to host these
resources, and I can also update them,
and then you will get a notification.
Now, the only thing is I couldn't select
free. I can just do zero. So, don't
worry, you don't have to pay anything.
You can just put in zero or any tip you
want. If you want to give me a tip or a
coffee, that's totally welcome. But also
if you want to do zero that's totally
fine. You click on I want this and then
here you see the Pyth resource hub and
community. You'll see an overview and
then you just need to plug in your email
address and you can get access. So
throughout this course I will sometimes
refer to files from the resource hub. So
make sure you download that right now.
And then the first thing you can check
out is this Visual Studio Code cheat
sheet with all of the keyboard
shortcuts. There's one for Mac OS and
for Windows. And in here are all the
default keyboard shortcuts. But this
will also give you an ID into all of the
actions that are available within the
code editor and that you can also
change. This is really fun to play
around with and you can really like make
it your own. All right. And with the
resources downloaded, we can now
continue. And now before we get into the
Python basics, which you can see over
here is the next chapter within this
course where we're actually going to
write code, execute it, build cool
things, and see how all of this come is
coming together. On the getting started
page, there's one more section over here
called Python environment. And I want to
go through this first because it's so so
important and it will really help you to
better understand what's to come and how
it would also translate to other videos
and tutorials you see. So I know that
this this is a lot of setup but it is so
so crucial and it's that is really what
makes all the difference in my opinion
from just a a basic Python course that
shows you look here's how you can write
Python. Yes, we know chatbt can do that
as well, but how does everything work
together? And virtual environments is a
key component of that. So what we've
seen right now, right, we installed
Python, we then opened up Visual Studio
Code and we selected the Python
interpreter that we wanted to use,
right? So remember when we were working
in our project over here, you have this
whole I have this list over here. You
probably have one or two and we selected
a a Python version in there. What you
should know is that when you are working
with Python in the real world, you're
often working on multiple projects side
by side. And next to Python having a
version like 3.13 within the Python
programming language, what we also do is
we work with a lot of packages. We can
use packages code that someone else has
written written and we take that and we
import it into our project and then we
can use that. These packages also have
version. So now we have our Python
installation and we have our packages.
Now what happens if you're working on
project A and it needs version 1.0 of a
tool and you're working in project B and
that needs version 2.0 of a certain
package. If you now install version two
for project B, you just broke project A.
So now you might be working on
something. You run your code, everything
works. then you like end of the day the
next the next day you open up another
project you try to run it and now you
get an error we don't want that of
course so that's where virtual
environments come in so these are
separated private copies for each
project now before getting into that you
need to understand uh packages and what
they are about so I already gave a
little bit of an introduction right
these are uh literally pieces of code
code snippets and files that other
people created where we now don't have
to start from scratch and that is one of
the most amazing things about Python is
its like developer ecosystem and all the
packages that we can already use out of
the box where now with typically just a
few lines of Python we can do incredibly
powerful things and we can create in
incredibly powerful automations. So a
very common one is the request library
which we'll be using in this course the
pandas library for working with data or
the openi library to work with the
models from openai. So to create your
first virtual environment in our own
project that we're currently working on.
What we can do is we can use two
methods. I'm going to show both of them
to you. We have an easier method and we
can do it via the terminal. So let's do
the easy method first and this is
through the command pallet. So within
Visual Studio Code, if you do cmd or
control shift +b, you'll open up the
command pallet. Let's see what that
looks like. If I now do cmd shiftp,
you'll see the command pallet pop up. So
if I now do python create environment,
you can see this one over here, create
environment, right? And then I get two
options over here. So fanf or cond. If
you don't have cond installed, you will
probably only see f for now. Don't worry
about what is and just click on fanf.
And now what you can see over here is
here you can see all of the Python
versions that I have installed on my
system. So you can even see that there
are some more some older versions 3.9 in
here. What I'm going to do is I'm going
to select the latest version. And for
you there will probably only be one or
two. I recommend just picking the
highest version. So we click on that.
And now what's going to happen is Visual
Studio Code is going to create a virtual
environment for us. So you can see it
happening over here. We now have this
folder. It's called Fenf. And if I open
this up, you can see files and folders
in here. And what we've we've now done
is we created a local copy of the Python
installation. And if I now come back to
hello.py, what I can now do, you can see
it's already selected over here. Visual
Studio Code does an excellent job at
autodetecting the environment when it's
uh within your project. But you can see
that we now have the ven over here and
which stands for virtual environment.
And you can select that and what we can
do right now if we now click on the play
button one more time we will execute the
code and we will execute it using our
own environment. So I get it this is
again this is abstract right at this
point but think of it like this when
you're working on different projects you
really want to isolate everything and
this is a way to get Python within our
project in that folder in that folder
that we could just created defense and
we can make all the changes and all the
updates just there. And this is also one
of those things that's the nice things.
This is like set it forget it once on a
new project. And because we save it in
the workspace right now the virtual
environment is there. We've selected it.
Now if I come back to uh the project
over here I save this and I just close
out of this. I can come to the folder
over here. Open up the project and then
you can see that it correctly selected
the virtual environment and I can just
come in here and run it without a
problem. It is now using our virtual
environment that we just created. All
right. So that was method number one to
create a virtual environment. The easy
method. Now I'm going to show you the
second method which is through the
terminal. And I'm going to show you this
so you understand it that whenever you
see a tutorial you understand, oh
they're creating a virtual environment
using the terminal, but I never use
this. So I'm just going to come over
here and I'm going to copy this line. So
python- mf and then the name of the
folder. And let's close all of this out.
I'm going to completely remove the
folder again. Now I'm going to open up a
new terminal and I'm going to paste the
command in here and run it. And what
you'll find is that right now you can
see we have created a similar folder. So
this is what h what's happening behind
the scenes when you do the command
pallet python create environment. And
now because we have created the new
folder in here we should select it one
more time just to make sure that we
refresh it and we should be good to go
again. So that's another way to set up a
virtual environment. All right. Now then
a quick note about Anaconda because I
want this to be a complete course and
whenever you encounter it maybe in
another tutorial I want you to be aware
of it. So Anaconda is another way of
managing Python distributions and
virtual environments on your machine.
It's very popular in the data science
world and right now I would say I
actually don't recommend using it. So I
used this earlier on in my career but
now when you want to focus on AI there's
actually a much better way to manage
Python environments and packages. So
later on I'm going to introduce you to
UV. Don't worry about that for now, but
that's now my go-to. So Anaconda is a
whole other application that you need to
download and install on your system.
It's free, but for now you just need to
be aware of it. So whenever you
encounter it and someone mentions using
Anaconda, you can literally just replace
that with using a virtual environment
and just install the packages in there.
It's exactly the same. You can so you
can still follow along with that
tutorial or course wherever they're
using Anaconda. All right. And with
virtual environments covered, let's get
into Python packages. Another very
important concept to understand because
out in the world, whatever tutorial or
course you'll follow, there is a high
chance that at the beginning of the
file, they will do some kind of imports
and use some kind of packages and I can
explain you everything about Python and
how it works and how to write and what
it means. If you don't understand this,
you will run your code and you will
still get an error. That's why I want to
start here. So I already gave the
introduction, right? Packages are
collections of Python code that solve
specific problems that other people
created and that we can now very easily
download and use for free. It's really
awesome. And in order to get those
packages and get them into our project,
we can use pip. And pip installs these
Python packages. It can manage all of
that. So we don't have to go to a
website. We don't need to do uh go
through an install menu. We can simply
run commands from the terminal and it
will pull it straight into our project
and specifically into our virtual
environment. So pip is a command that
you need to be f familiar with. So now
let's install your first Python package.
And to do that we're going to go through
the terminal again. So open up the
project and let's see. You can open up a
new terminal or I can also do command J
to pull up the one that I have active
over here. So you can see I have
previous sessions. You can close and
manage them over here. And if I want to
clear it out, I can do clear on Mac or
cls on Windows which is not recognized
over here. This is the starting point.
This is now going to be the input for
which we're going to use pip in order to
pull those packages from the internet.
So let's continue. So we understand the
terminal right now and we are we want to
make sure this is important that we are
in our virtual environment. So let me
actually check if I did that correctly
as I am uh walking you through this and
no I did not because I used a terminal
session that was already open and later
we installed the environment. So let me
show you what I mean with this because
this is a very common uh mistake. I just
like click on the trash bin icon and now
I go to new terminal. I open it up. This
will activate it. Look what you can see
right now over here. This means that we
have a terminal open and it signals that
it's using this virtual environment
because remember right we have all of
the different isolations. Whenever we
want to install something we need to
tell our terminal it needs to be in this
particular folder. So Visual Studio Code
will handle all of that automatically
for us. So right now whenever this is
available, this is active, it will show
up using the virtual environment. One of
the most common errors that you run into
if you install packages and if it
doesn't work if this wasn't correctly
configured. So then you just need to go
to okay which is the environment make
sure to close it out and then create a
new terminal. I'm being a little bit
redundant over here but these are the
things that will actually give you a lot
of headaches when you are working in the
real world. So make sure to take notes
and refer back to this video and then
you can say ah they've already told me
this is exactly because you will make
the these mistakes trust me. Now let's
install your first package. And we're
going to install the requests library.
And the how to do that is really simple.
It's pip install and then the name of
the package. And in this case, we're
going to install the requests library.
Let's come over here. Let's clear this
out. We do pip install requests. And
then we hit enter. It doesn't take that
long. Now we have downloaded the
package. What just happened? Well,
through the terminal and through the pip
command, we connected to the Python
package index, which is a huge websites
where developers share their code. Then
we downloaded a package, which are
really just a bunch of Python files that
someone else wrote, and we saved it to
your virtual environment. So, let me
also show you what's going on behind the
scenes because now if we go into our
virtual environment folder over here, we
can come in here and we go to library
and then we scroll down, you can see
requests. So, this is the folder. So
this is the package that we installed.
And now you can see this is what we just
pulled in here. Really just a bunch of
Python files that someone else wrote
that we can now use. You never have to
like look in here. You rarely touch
this, but just know that that's where
it's installed and that's where it's on
your system. And now then in order to
use a package, what you can do in any
Python file, you can start with an
import statement. And later in this
course, we'll go much deeper into this.
Right now it's just about like showing
you how to do it. So you've seen it once
and later like comes the understanding.
So right now we can simply copy paste
this whole snippet over here to import
the request library and try to download
the content of a web page. So if I now
come back to my project let's see
hello.py. So this is the same file that
you should have in here. We can remove
everything here and plug this in. And
now you can see it is still giving
squiggly lines. Uh, and that can
sometimes happen in the beginning. If
you selected the right environment and
you installed it correctly, this should
all work. So now if I run this, we
should see that this is now going to
run. And there we should get a 200 back.
So this is just a status code. So we're
just literally like pinging the website.
Again, doesn't matter what that means.
You can now see we've imported the
request library and it works. So the
squiggly lines tells you that there is
potentially an import error. And how to
usually fix this is if I now close out
this project and I open it up one more
time. So come in here and let's open up
the workspace. You can see that now it's
gone. So sometimes this is a little bit
of a bug where it tells you it's not
installed correctly. Just reopen your
Visual Studio Code. It should be gone.
And let's see. We can run it one more
time and everything still works. All
right. And that's all you need to know
for now about using Python packages. If
you look into the handbook, there's more
information on this list, but don't
worry about it for now. We'll get back
to that later. So now the final thing
that I want to walk you through as part
of the getting started section is
interactive Python. And this is going to
be really cool. This is actually one of
my favorite ways to work with Python.
And when I discovered this, I pretty
much never went back. And to this day,
almost every day, someone on my YouTube
channel, like in in one of the comments
on one of the many videos that I have,
someone asked me, "How do you do that?"
I have multiple videos on this already,
but like this really changed the game
for me and how fast I can work and how
much fun it is to work with the Python
programming language. So, let's set it
up. Right now, I've shown you how you
can execute Python code using the
terminal. Right, we have this over here.
We hit command enter or we do run Python
file and it runs. But to me, this always
really bothered me a lot because pretty
much look at it right now. What's all
this mess going on right here? Right, we
get a 200 as an output. But there's just
so much stuff uh going on. And there's
actually in my opinion a much easier way
to work with Python and that is using
interactive mode. So let's first set it
up and then I'll explain what it is. So
there's one prerequisite and we need to
install a package and that's why I
wanted to share how you can install
packages because you need this one. So
what you can do is right now you can
simply click on copy or like follow the
command pip install IPI kernel. That's
the one we need. So coming back to our
editor to a terminal session. All right,
let's come in here and do pip install
ipi kernel. So this will install
everything in there. Takes a couple of
seconds. And this will also work with
the Jupiter extension that we have
installed in Visual Studio Code as well.
So we have that and we have the IPI
kernel. And now what we can do, we need
to change a setting to make this super
easy. So the easiest way to do this is
simply copy this whole line because
there are a lot of settings in Visual
Studio Code. So Jupyter inter so copy
this whole line and then come over here
to the gear icon to the settings and
then I just plug that in there and then
you should see this Jupyter interactive
window text editor execution selection.
So here you can see when pressing shift
enter send selected code in Python file
to the Jupiter interactive window as
opposed to the Python terminal and we
select that. That's it. Now if I close
out on this. So we have the same script
over here. And now what I can do is
instead of hitting command and enter to
like run it in the terminal, right?
That's the uh hotkey that I set in the
beginning. What I can also do is I can
select a certain section of the code. It
can either be everything or a single
line. And then I do shift enter and a
window to the right will open. And now
you can see the 200 status over here as
well. So the difference is is that
instead of like taking the entire file
and sending it to the terminal and then
all of the outputs showing up in my
opinion in kind of like a messy way with
a lot of like noise in there, now we
have this very clean window here on the
right where we can see the output. But
it even gets better because instead of
just running the entire file from like
top to bottom, what we can also do is we
can also execute certain sections or
certain lines or pieces of the code
itself. So I can come in here and when I
am on a single line, so I'm now on line
one and I do shift enter. You can see
that we only run import request. So now
we import the library and I can for
example also just run the request over
here. So just do that. So I just get the
response and then finally I do the print
statement. So, not only does it allow us
to have a cleaner interface on the
right, it also allows us to go through
our code more in a more granular fashion
and highlight certain section of this.
So, as we go through this course and as
we start to actually work with the
Python programming language, this will
really become apparent to you as to why
this is nice and why this is so useful
because we can check back and we can
actually go here and say, look, we can
even select partial lines. So, not even
a full line, but I can do a selection
and I can use the shift and the uh alt
and option keys to go through this and
even look at specific variables in here.
All right, so that was the getting
started section. Congratulations. We
already covered a lot. Now, this would
also be a good moment to take a break
and to really think about what we've
learned so far. So, let me quickly walk
you through it. Right. So, when you
started with this course, you did not
have a Python installation. So, we got
that onto your system. We got Visual
Studio Code. You have the application.
You now know how to create projects in
there. Save them as a workspace and then
you also understand the importance of
virtual environments to create isolated
in instances of the Python programming
languages so that we can install the
packages. You've seen how to work with
the basics of Visual Studio Code, the
extensions, how to style it. You know
how to run Python files either using the
terminal or using the interactive window
which I am a big fan of. So right now
you really have all the tools to start
to get to work and you are so much
better prepared than pretty much anyone
else trying to learn Python because this
really is a cheat code. Everything that
I shared in here and that I boiled down
into this step-by-step format, this all
really took me years to learn. And I
really wish someone in the beginning of
my Python programming journey like
literally over 12 years ago, I wish
someone would have shown me this
approach because it's a complete
gamecher. And now if you're up for an
exercise to really check if you
understand this, here's what you can do.
And I in the beginning this is tricky
because I've given so many Python
trainings already and students follow
along up until this point. But then when
they start again from scratch and they
have to do it on their own with without
essentially me like holding their hands
and showing everything step by step, it
can get tricky. So here's what I want
you to do. Go to your Python projects
folder and create a new project. So in
there, create a new folder, give it a
name, and then I want you to open it up
in Visual Studio Code and go through the
entire process. Everything that you need
to do in order to set up to create a
file, put some Python code in there. You
can do a simple print statement similar
to what we already did. use a virtual
environment. Make sure you select it and
make sure that you can run it both in
the terminal and in the Jupyter
interactive session. Make sure you save
everything to a workspace and then close
it out to see if you can open it up
again. If you can follow along with
that, you pass this first section of the
course and you're ready for the next
stage. All right, so welcome to the
Python basics chapter. We are now
actually going to start writing Python
code and learning the language, learning
what this is all about and how we can
use it. So now with our environment set
up, VS Code ready, our everything
installed, we can use our Hello Pi file.
And this is going to be the file that
I'm going to continuously to work in.
You can use this as a scratch pad,
right? You so you can be sloppy in here.
You can put some code in there. We can
run it and then the next example, we can
get back to this and we can keep using
this. So we don't have to worry about
managing all of the files. So what I'm
going to do for now is I'm also going to
close the sidebar over here to create a
little bit more real estate on the
screen here. And the command for that is
command or control D to show the
explorer. But for now, this is what we
need to do. Okay. So coming back to the
foundations, we're going to start with
really the very basic building blocks of
the Python programming language like
variables, data types, operators. But
before getting into that, I want to give
you a little bit of an understanding
like to understand the bigger picture of
what programming is all about. Now, if
you're coming from a different language,
you can skip this part because you're
already fully aware with this. But if
you're completely new to programming, I
this is a great moment to take a step
back and to explain you what we're
fundamentally doing. Because when you
are following this program like step by
step, action by action, you are so
zoomed in into following exactly what I
do, right? Installation, I get it. The
app, I get it. An environment and but
you need to understand what's the goal
of all of this. What are we trying to
do? And programming is all about writing
instructions for computers. And
computers are very literal. So you need
to tell them exactly what it is that you
want them to do. So if you for example
tell a friend of yours like hey go make
a coffee like internally they understand
that they need to get a cup add coffee
grounds pour hot water wait a couple of
minutes the whole process right a
computer doesn't understand this so when
we want to try and accomplish something
when we are trying to automate something
where where when we try to create an AI
agent or build a model we need to be
very specific and we need to instruct
the computer in such a way to follow the
exact order of operations in order to
accomplish our goal and there are many
ways to do that and many ways and many
programming languages to accomplish that
and Python is one of them. So that's the
first thing that you keep in mind. We
are working on all of these isolated
bits and pieces that in isolation don't
mean a thing and are pretty useless. But
when once we start to understand all of
these building blocks and we start to
combine them in different ways like Lego
blocks, we can now all of a sudden
create an AI chatbot. We create an AI
agent. we can uh create a simple script
that can take a document, extract
information, give a summary and put it
into a CRM. It open up opens up a whole
world of possibilities which we can all
do through the Python programming
language. So that's what I want you to
keep in mind as we start with these
really really fundamental concepts.
They're isolated. They're small. On
their own, they mean nothing. And you're
probably wondering like what can I
automate this? What does this have to do
with AI? But once we start to layer
them, that's where everything starts to
click. And with all programming
languages, there are only a handful of
basic operations that we do over and
over again. So here the first one, a
very common one is storing information.
We want to get some type of data and we
want to store that information so that
we can process it. Let's say we have an
age 25, a name is Sarah. Now later we
can use that. We can reference Sarah by
using the name variable. We can also
make decisions. we can use if and else
statements. So if this happens, we do
that. But if that is the case, we do
that. This allows us to make smart
programs that don't only do just one
thing, but based on the data and the
information that is coming in, we can
make decisions. Programs can also repeat
actions. So they can repeat things 10
times. So when a document is coming in
maybe it has like 10 pages and for every
page we want to do something maybe
scrape the data extract it and then
calculate things. So what's the total
price? We have numbers. We want to add
them, subtract, sub subtract them, apply
any kind of math. And through this
storing information, making the
decisions, repeating the action and
calculating things, we can solve a lot
of problems actually using a programming
language. All right. And then now let's
get to the next section which is called
Python syntax. This is all about how we
properly write Python code. And similar
to the English language where we have
grammar, Python and every other
programming language for that matter
also follows follows certain rules. And
instead of grammar, we call it syntax.
So this is an important topic to
understand. Even though Python is known
for being very clean and readable,
right? We saw that in the beginning. It
pretty much just reads like plain
English, there are still rules that we
need to follow. And this is good of
course because otherwise all the code
from everyone would look different and
it would all be messy. So throughout all
of the following examples I will refer
to the specific syntax and just know
that if you ever run into an error there
is a high chance that you will have some
type of syntax error. Maybe it's a
bracket that did not close. Maybe there
is a quote missing. Maybe the
indentation and the spaces are off. It's
literally that tricky. So remember that
for now. Syntax is all about how you
write Python code and the rules you need
to follow. And now as you go through
this course, syntax is something I will
bring up all the time because whenever I
introduce a new concept, we will start
with the syntax first. How to write it
and what are the rules. And one of the
most important things there which is
also unique to Python is the indentation
which is the spacing that we use to
separate blocks of code and to bundle
pieces of code together. So if you for
example look at a JavaScript language
you can for example use curly brackets
and that signals look everything between
these curly bracket belongs together.
With Python we solely do that through
indentation. So we'll go through
indentation rules which we will
constantly use and our editor will also
help us with this because whenever we go
to a new line where we need an
indentation VS code will automatically
do this for us. Now don't just worry
about that right now. It will all become
more clear as we go through the
examples. But just know that indentation
rules and using those four spaces is
important in Python. Now, beyond just
syntax rules that make your code run,
Python also has style guidelines that
make code more readable. And we as
developers have agreed to a certain
standard and these this is called PEP 8
or the Python enhancement proposal 8.
This is the official style guide for
Python code. It covers things like that.
We use four spaces instead of taps. We
limit lines to 79 characters, naming
conventions like using underscores
lowercase instead of using camelc case
and where to put spaces around
separators. And now as we go through
this course, I will follow these PEP 8
style guidelines so you don't have to
worry about them right now. But just
know that there are rules for
indentation for how we work with spaces,
blank lines, and even line length. And
at the end of this course, I'm even
going to introduce you to a tool that
can automatically take care of that. All
right. So now let's have a look at what
happens when you don't follow the syntax
and you break your code. So let's say we
do another print statement. Then we type
hello world but then we remove the final
quote over here. And you can already see
that our editor is giving us these red
squiggly lines indicating that something
is wrong. So the string literal is
unterminated meaning it is opened but
it's not closed. But for the sake of
demonstration I am going to run this
code. So what I'm going to do in this
case I'm going forward. You will see me
do this over and over again. My cursor
is here on this line and I am
controlling that with the arrow keys
left and right. And now on this line I
simply press shift enter. And what this
will do is it will open up the
interactive window over here. And now
here you can see the error. So you can
see the line on which the error
occurred. You can see it starts here. We
opened up a string and then you see this
syntax error. So now you know what
syntax means and where this is coming
from. So it says unterminated string
literal detected which is just Python's
way of explaining you opened up the
string but you're missing a closing
quote. So if we plug that in here and
run it one more time, you'll find that
the code will now run without any errors
and we will get the hello world
statement. And now reading errors is
something you have to become good at
because errors do not only happen when
you're a beginner. Trust me, making
errors is literally part of programming.
even like the best programmers in the
world, you build programs by trying
things out, breaking things, and then
fixing the bugs. So, it's a core part of
the development process. So, how you
read Python errors is something you get
more and more used to, but there is a
general principle that you can follow
and it's in the documentation over here
as well. So, first you'll see where the
error happened. So, here you can see
this is in line one. We're running the
interactive session with this file. So,
this is where it happened. Then, next
you can also see what went wrong. So all
the way at the bottom of the error and
this is right now in this example this
is a pretty short error. There can be a
lot of information all the way at the
bottom the final line you'll see in this
case the syntax error. So Python will
tell you what went wrong. And then the
third component is the arrow pointing
exactly to the spot where it happened.
So following this three-step process to
debug your code whenever something goes
wrong. Look at what happened. Syntax
error. Okay. Where line one and then you
can see the exact snippet. Oh, it is
something to do with a string. Now, in
the beginning, all of these syntax
errors right now, they will seem very
alien to you. Unterminated string
literal detected at line one. Pretty
abstract, right? This is a great spot.
This is a great time to plug that into
your AI assistant or just plug it into
chat GPT and ask, look, I'm writing a
Python program and I get this error.
What does this mean? And if you show
your code even better, it will tell you
exactly what went wrong and how to solve
it. And over time, you'll get a much
better feel for quickly spotting errors
and what went wrong. And your IDE will
also help with that. And in this case,
we could have easily prevented the error
because our IDE already signals that we
need to close the string. And now
throughout this course, if you follow
along, you're going to make a lot of
errors. And that's the goal, right?
Because with that, you'll become more
and more used to it. That's also why I
keep hammering on make sure to write
everything out and not just simply hit
copy paste on all of the examples in
there because then out of the box they
will be good but you need to train
yourself in order to spot the
differences to spot the spaces to spot
the quotes. So make sure to type it out
on the keyboard make those errors
correct them and continue. All right now
it's time to get into one of the most
fundamental concepts in all of
programming not just the Python
programming language but in general and
that's variables. And variables allow
you to store and name your data. So
coming over to the hello.py file over
here. Let's say you're building a
program where you want to take in some
user information. So you want a name and
let's say we set that to Alice. This is
Python's way of setting a variable
called name and we set that equal to
Alice which in this case is a string. We
can now run this program and if we now
come over here and we make a reference
to name under the hood Python now
understands that name is equal to Alice
and similar I can for example set H to
let's say 25. So now if I go in here and
I just run H which I do here with the
interactive session which I can do by
putting it on another line hitting shift
enter but I can also do it by taking the
cursor and just selecting this part. So
again I'm using this with the arrow keys
and then I'm holding in shift. I select
this and I do shift enter to see under
the hood what the variable is that is
assigned over here. So we can set age to
25 and we can also overwrite things
that's why they are called variables. So
if I for example now say name equals and
now I for example say Dave and I run
this shift enter we can now check and
now Python understands that name is
equal to Dave. So this is how we can
pass data around and we can do that with
all kinds of data. We can do that with
strings which you see over here. We can
do that with numbers and we can even do
it with uh true or false values. So we
can for example set is student and then
we set that equal to true which is also
something we can do in Python. So the
things to keep in mind here about the
syntax about working with variables is
that on the left you have the variable
name. Then you use the equal sign which
means you store the value on the right
in the variable on the left. We store
Alice on the variable name and Alice is
the value that we're storing. Now there
are also some rules that you should keep
in mind when working with variables. So
here you see all kinds of variable and
variable names that are allowed. So we
can use an underscore, we can use a
capital letter, we can use a number in
here, we can even use an underscore over
here. But there are also some things
that are not allowed. So for example, a
variable name can start with a number.
It cannot use a hyphen or it cannot use
a space. And you can also not use
certain Python keywords. And we can
validate that by coming over here,
getting rid of this, and then say to age
is 30. When I run this, you'll get a
syntax error. So we get an invalid decim
decimal literal. This is not possible.
We can also do new age is 30. This will
also not work. So here is an example.
Things that are allowed, things that are
not allowed. And to make that even
simpler for you, in the Python naming
conventions, we use lowercase letters
with underscores between words. This is
also called snake case. So when we for
example want to spell out a first name
is Alice or a user age you can see that
instead of doing a space or a hyphen we
use an underscore. So if I do first
underscore name and then I set that to
Alice I can run this and it will be no
problem. We can just store this similar
to user age equals 25. That's all fine.
So the important thing to keep in mind
here is that there is a difference
between the strict rules of Python. So
when it will actually throw an error
versus the style guides because within
Python it's totally fine to use capital
letters here in the middle of a variable
which is called camel case. I can do
that right now. So first name with
capital N equals Alice. I can run that
and it will not throw an error. But this
is just a style thing. This is not
something we do in Python. So to make
that simple when we talk about variables
lowerase and when you need to create a
space use an underscore. And now, as we
go through all of these examples, I want
you to know that in the handbook,
there's almost always a little bit more
information that we don't cover in the
video. So, common mistakes, things that
go wrong, more examples, but otherwise,
it would be a 10-hour long video. So, I
cover the basics in here, what you need
to know. But if you're still like, hm,
variables, I don't still quite get it.
Make sure to go through the handbook and
then ask follow-up questions to ChatBT
or your favorite AI assistant if you
still need additional help. Next, let's
get into comments. So, what are they and
how do we use them? So, if we come back
to our script over here, you can see
that in here we have the first name, we
have the username. This is code that we
execute. But Python also allows us to
put human readable comments in our
scripts that Python will simply ignore.
It will not do anything with what we put
in there. We can put anything we want in
that case. And we can use descriptions.
We can explain certain bits and pieces.
We can add to-dos or reminders. And this
is a way for us as developers to not
only uh help our future selves, but also
potentially other people with which we
work on the same project. And the way we
do that within Python is by doing a
hashtag. So if you type a hashtag,
you'll see that on that whole line
starting with the hashtag, it will be
grayed out. So I can start typing in
here and I can say this is a comment in
Python. So you can see it is grayed out.
And if I now select all of this and I
run this, nothing will happen with this.
It won't output anything similar to what
we have with Alice and the user age. I
can for example put a print statement
over here to actually show you. If we do
the user age and add that to it as well,
I can execute this whole line and it
will print 25 because we have instructed
Python to print this. Similar to this,
we can also run this in the terminal and
you'll find that the output of this
script will just be 25. So there will be
no trace of this comment whatsoever. So
we can do that on a whole line like
this. But we can also do it after uh at
the end of a line. So if I'm here and I
want to write a comment saying this is
the first name, which in this case
doesn't really add a lot of value, but
let's say you wanted to give a little
bit more clarification. This also
totally works. We can also do multiple
lines by coming over here saying this is
another comment. This all works. No
problem. Now, if you want to write
comments over multiple lines, there's
actually one other way that you can do
this and that is by using triple quotes.
So, another way to write a comment is
doing it like this. You'll see three
opening quotes and then I can go over
here and I do three closing quotes. and
anything that I write in between here.
For example, this is a multi-line quote
that can go on and on and on. And in
this case, you don't have to start every
line with a hashtag. And this helps for
readability. You'll also find that this
is green instead of the grade out
version in here. So, typically, if you
want to do short and snappy comments to
just give a little bit of instruction or
clarification, you use the hashtags. If
you want to have more explanatory
paragraphs to later also describe
functions and classes and what they do
and what kind of data you need to put in
there, then you'll use multi-line
comments with the triple quotes. And as
a rule of thumb, you want to use
comments to explain the why, not the
what. Because since Python is relatively
easy to read and it usually reads like
plain English, you for example, here's
here's a bad example. So let's say you
have you want to calculate a total and
you take the subtotal and you m you
multiply it to calculate the text and
then a comment here is multiply subtotal
by 1.6
like we can already tell that by reading
this line. But over here in the top
example, we for example have a tax
calculation. And if if people are from
California, I I don't think I think this
is off. But let's assume this was
correct. You explain the the calculation
why you're doing this. So someone
looking into this code might might be
like, okay, what's this number? Oh, it's
to calculate the sales tax. So that's a
good reason why instead of explaining
what. Okay. And up next, we're going to
talk about data types. So these are the
different kinds of information that we
can store in Python and when we were
experimenting with the variables right I
already brief briefly mentioned like
look it can be a string which is text it
can be a number it can be a true or
false so let's cover all of the options
that are there because that's also a
fundamental concept that you need to
understand and let's start with numbers
so under the hood there are actually two
types of numbers when you are working
with Python and with most programming
languages If you're new to programming,
this is always a little bit weird like
why why is it different? There is a
difference between what we call integers
and floats. And integers are simply
whole numbers without decimals. So 25.
And we could set h equals 25. And this
would be considered an integer or floats
which have decimal points. So 19.99
that's a float. Now in the real world in
practice, Python is so flexible that you
often don't really have to think about
this. and you can just write your
programs as is and Python will
seamlessly convert one into the other.
But under the hood there is a difference
in how they are stored in memory. Uh why
is this? It simply has to do with
efficiency about how the computer like
can process it. But that's all you need
to know for now. So what can we do with
numbers, right? Well, we can apply basic
math operation. It works similar to how
you would use a calculator. So let's run
through all of the examples and show you
how that works. So if we come in here
and we can literally like use this as a
calculator. So we can do 5 + 5 and we
can run this shift enter. You'll see
this 10. We can do a minus it will be
zero. We can do multiplication with an
asterisk. We can do a division.
Everything works like this. And we can
also store that into variables. Right?
So let's say we have our total and we
want to calculate. we have 10 and we do
it minus 7 and we come in here we store
that. So now we do we perform the
calculation and we store it in the
total. I come in here and I run that we
can see three. Now again as I go through
this just remember because we're using
the interactive session instead of
having to write out print all the time
and print what the total is I can come
in here and just select total and run
it. And we can also do powers. So power
equals we could do 10 and then we use
double asterisk. let's say to the power
of two and then this should be 100. So
give it a couple of tries. Make sure to
play with this and also to get used to
typing to selecting the pieces that you
want to see and just get comfortable
with this interactive window over here
knowing at all times that this is quite
a unique way of working with Python. And
if I run this script like this, you'll
see nothing is happening. There's no
output because there are no print
statements. So if I were to print the
power in here, I would need to like
explicitly write that out, say print,
and then run it in the terminal session
like a lot of other examples you'll find
online do. Then you see the 100 over
here. But with the interactive window,
we can just start it up. Come here to
the total, say, I want just this. I want
just this. And I want to see the power.
And here in the beginning, I know that
I'm repeating myself with this, but you
need to get those reps in in the
beginning. you need to see like how I go
through the code and select things and I
am so used to this for me it's seamless
I'm using the arrow keys I'm using shift
but I'm I totally understand that when
you go through this you might be like oh
what how is he doing that so fast so
follow along run the examples try and
build your own calculator see what it
does and as you go through all of those
examples you'll build the muscle memory
to do it just like I do all right so
those are numbers and the basic math
operations that you can do now let's
continue to the next data types And
those are strings. So strings is just a
fancy word of for text. Any type of text
we can put into a string. And in Python
we do that between quotes. Now Python
doesn't care actually if you use single
or double quotes. You just got to be
consistent with this. I always use
double quotes. So you can also just do
that. Totally fine. Just be consistent.
And now there are a lot of things that
you can do with strings. So let's run
through a couple of examples. So I'm
going to create a string here. And I'm
going to say my name is Dave. This is a
string over here. And we can even add
some comments just showcase everything
that we learned. And now I can select
all of this and run it. We'll open up
the session over here. So this is how we
create a string. And in here we can put
anything really. We can even go as far
as let's say let's create my long
string. We can also use triple quotes.
And remember how we could also do that
with comments, right? But if you do it
after a equal sign, we can put in a long
string. So you can say my name equals
Dave and um my last name is Eilar. So in
this way, let me clear up the capital
letters over here. We can do this string
and this is one line, but we can also
have a long string. And if we now show
and look what's in here, you can see
that we have multiple lines in here. So
that you can see by this uh back slashn
that signals that it's a new line which
actually means if we print that. So we
can print my long string it will show
and it will print that on a new line. So
those are the two ways that we can
specify a string and put it into a
variable. Now with the string as a data
type there are a lot of operations that
we can do. So remember when we had
numbers right integers or floats we
could perform math operations. So
strings have similar features. So one
thing that we can do is for example
first name let's set that to first name
equals Dave and then we say last name
equals Ear. What we can now do for
example is we can set full name equals
and then we do first name and then we do
a plus sign. So we can actually perform
some kind of like math operations on
strings and then we say we want a space
in between. So I do an empty string over
here with just a space. Then do another
plus sign and then I do last name and
write that in here. Let me make this a
little bit smaller for you so you can so
you can see the full line. You can see
everything. So now if I run just this
line you can see first name is not
defined. Oh yeah this is a very common
error because I forgot to uh run these
lines of code. So first I need to store
this in memory. So now you can see over
here Python understands okay first name
is Dave and similar to last name is
Eilar. And now when I run this whole
line you can see that this will run and
I can get the full name out of it. Okay.
So now we're performing what's called
string manipulation. And there are a lot
of actions going on here. So try to
follow along. Try to put everything in.
Try to run them line by line and then
look at what's in the variables. Try to
select them. Get a feel for working with
this programming language and the
interactive window. Now what we can also
do is we next to the plus sign we can
also for example let's say we have an
asterisk and we can say or let's let's
actually do a dash. We can do a dash and
we can say long dash equals and then we
do the dash and we do it times. Let's do
it times 10. We save that. So let's see
what we have over here. Now we have well
we I think we can do longer. Let's
actually do um 30. So now you can see we
have a really long dash. So now if you
for example wanted to create a program
and you want to display some
information. So first let's say you
print and then you do we do the full
name. So we can print the full name and
then afterwards we print the long dash.
You can see if we do it like this. There
we go. And now we can tailor it to kind
of like the length. I think it will be
around 12. So now you can see we have
our name and a dash underneath that. So
if you want to do some print statements
or logging this can be helpful. So there
are things that we can do with strings
as well. Another operation is we can
also check the length of a string. And
this is using a Python built-in function
similar to print. So if I type print,
you will see that it turns blue, right?
This means that this is a built-in
function from Python. And how do you
know if it's built in? In the beginning,
you don't know. That's why you're going
through this course. As as you'll find
me explaining variables, sometimes there
are just things off the shelf that we
can use. And so getting the length of a
string is also a built-in function, and
that's through length. So not the
complete length but we shorten it. And
then for example we can plug in the long
dash in here. And if we run that you'll
find that it says 12. So what this
function does is instead of just print
instead of just printing it it will take
the string and it will calculate how
long it is. So if I take the full name
and do that you'll find that my full
name. So that's the first name plus the
space in here plus the last name equals
13. So that's a length of 13. Whereas if
I plug in my first name, the length
should be four because my name has four
letters. So that's a built-in function
that you can use. Now there are many
many built-in functions and we'll cover
some of them and as you progress on your
Python journey, you'll encounter more
and more of these. So when you're trying
to solve a particular problem and you
ask JBT, how do I do this in Python?
Sometimes it will tell you look there's
actually already a function or later a
package for this that you can use. Now
that's all you need to know for now
about using strings. We can store text,
we can manipulate it and we can do
things with it to process information.
Now next let's get into the third data
type and those are booleans. So booleans
are simply true or false. They can just
be that and they are very handy in
programming to make decisions because
based on whether something is true or
whether something is false we can make
decisions and we can route our program
or our data either to for example to the
left or to the right. Now and one common
example of this is so we with data
luminina we build a lot of customer care
automation systems and one of the steps
that we have in there is we let an AI
decide whether an incoming ticket should
be escalated to a human. So there's a
whole processing logic behind that but
ultimately what the AI decides is
escalate is true or false and when it's
set to true meaning it should escalate
we route it to a human to refute and if
it's false the AI can handle it
themselves. So th those are booleans and
we literally write it out with like a
true or a false and we use a capital
letter. That's a syntax thing that you
need to remember. So if I come in here
and follow the examples. So let me clear
this up. We can say let's say we have a
user status and is uh logged in. If we
set that to true, you will see that it
will turn orange. This means this is the
actual boolean the boolean value within
Python. If I don't do the capital
letter, you'll find that it's it doesn't
turn orange and it will give the
squiggly lines where it says look, you
should not do this. It should either be
true or it should be false. So those are
boolean values. So right now I can say
is the user logged in? This is false.
Now booleans often come from creating
comparisons. So we can do a direct
assignment which is what we just did,
right? So we can say is true and we can
literally just set that to true. So that
is a direct assignment. But we can also
perform conditional logic to decide
whether something is true or false. So
if you look in the example over here you
can do a comparison. So let's go through
this and let's say someone is 18. So we
have age is 18 and then we create a
variable called can vote and then we do
a comparison where we say look we take h
and we check whether it's greater than
or equal to 18 and we store that can
vote is true because we have set age to
true. So this is another like very cool
way you can where you can play with the
interactive session by just selecting
this over here. So again I'm using the
arrows arrows. I'm holding shift and
then I select just this piece over here.
And now I can check this. This is now
true. But if I for example set this to
16. So age over here age now is 16. And
I do a comparison whether it's greater
or equal to 18. You'll find it is now
false. And now if I run the entire line
I can now set can vote to false. So, do
you already get a feel for what this is
like and how this works? So, now the uh
comparison operators in here. That's
just one of those syntax things that you
need to see a couple of times in the
beginning. So, let's actually see what
we can do over here. So, you have things
like equal, which we do with double uh
equals. So, remember the assignment of a
variable is with one equal sign. But if
we do two equal signs, we actually form
a comparison. So if we let's let's again
let's set this to 18 and then let's see
we make another like we do is 18 is a
variable and then we check whether age
is equal to 18. So we set the age now
it's 18 look then we perform this it's
true. Now we do this not true. So these
are ways that we can perform really any
type of comparison that you want to
make. So coming back over here, is it
equal to? We can also is it not equal
to? So we invert it. We do that using an
exclamation mark. Then we have greater
and less than. So we could use greater
than 20, less than 30 or greater than or
equal or less or equal. So these are all
the types of common operators, right? If
you ever built some kind of like logic
or worked with a program or worked with
something like sapier or make.com or nat
these are very common operations and
this is just a way this is just a python
syntax to write that out. So this is
something you won't remember right now.
So this is always something you have to
refer to. That's also really one why I
wanted to create this handbook. So you
can bookmark this and whenever you're
working with variables you can come here
in here check it out or of course simply
ask AI and it will also tell you what to
do. Okay, so those are booleans and
again as with any section if you're
running into errors make sure you check
out the common mistakes. All right and
up next we are going to the section
working with data. So we already covered
the math, right? The arithmetic
operators to do simple math and to
perform calculations. And one thing you
should know about Python is that it it
follows the same rules really when it
comes to arithmetics that you would use
and on your math math exams in high
school, right? Or when you're using a
calculator. So for example, the
multiplication becomes for an addition.
So this will be 14 and not 20. And if
you use parentheses, that's how you can
prioritize certain operations to do them
before. So, similar to what it was like
with math in Python, it works the same
way. Then we have the comparison
operators, which I just shown you. And
feel free to stop here and take some
time to play with these. I'm quickly
going over this because I want to get to
the logical operators because we can
also combine boolean values and
conditions and we can even include the
words and and or. So let's have a look
at what that looks like. So let me
actually take this snippet over here and
we're going to play with that. So let's
see over here we have h equals 25 and
let me actually I can zoom out and I can
clear all of this for you and then zoom
in a little bit. So we have age is 25.
We have a license which is true. And now
we perform a check and we want to
perform a check and both must be true.
So to determine whether someone can
drive both the age needs to be like in
the US needs to be greater or equal than
16 and also has a license should be true
and this is also one of the cool things
about Python right because look at how
easily we can read this it's almost pure
English so let's go through this can
drive we set that equal to and then we
perform the first operation so age is
greater than or equal to 60 is that the
case it's 25. So let's check that. Let's
just run this line. That is true. Okay.
Now we see end over here. And we have
has license. So has license is set to
true. So what now will happen if we use
the end? This line over here. This will
output true if both of the clauses in
here are true, which is the case. So if
I now run this, you'll find that this is
true. And if we then print can drive it
will be set to true. But if we now set
this to false. So someone is 25. So they
are still over 16 true but has a license
is false. And now we run this and print
can drive is false. So you can see how
we can now start to build this logic
right. And similar we can also do or and
this means it's either one of the two
should be true. So if we do it like
this, they can still drive because
they're 16. This is maybe in like some
weird country or state where you can
either have a license or 16 and you're
good to go. So coming back to the
examples over here, you'll find that you
can combine these in all kinds of ways.
So whenever you're trying to solve a
problem, even if you want to stack 10
conditions and very complex combinations
of all kinds of variables that you can
think about using all of these operators
equal to, greater than, and or and using
these truth tables and understanding how
they work, you can see that you can
almost like create any kind of
decision-making logic. So go through
this and here can here you see
understanding the truth table. So that
this is what I was going uh for true and
true will be true and this is using the
and this is using the or and not is
another one. So this is how you can flip
it. So let me actually show you what
that looks like. So let's see if set has
a license is set to true. Let's see.
Let's do that. I can also now for
example do not has license. And now if I
run this this will say false because has
a license is true. And when when I
invert that it turns into false. So
let's do another example here. Let's do
drunk. Let's say drunk is equal to true.
So can drive now becomes age should be
greater than or equal than 16 and has
license. And here's the thing. You can
also do equals true. But you don't need
to do this. So this is like as you type
it out, you might want to do this. But
since has license is already a boolean
value, you don't have to make a direct
comparison with this. But both will
work. But let's for the sake of
simplicity leave it out. And then also
we do the third clause in there. We
literally say and not drunk. Okay. So
now what you can see over here, let me
actually zoom out a little bit so you
can see everything. Now if I run this
can drive like make sure we have
everything in here in memory. Now you
can see if we run this and then we do
can't drive is false because they're
drunk. They're 25. They have a license
but they're drunk and they cannot drive.
If we do it in false, they can't drive.
Now here was one more Python trick that
you'll often encounter. There are also
shortcut assignments. So when you have a
score, let's say score is 10. So I come
in here, let's clear everything out.
Let's say score equals 10. And you want
to add something to that. You could say
score equals score. and then we add
five. So now score will be 15. You can
also directly do score plus equals five.
And then we add another five to the
score and now it's 20. And we can do
this over and over and over again. So if
I run this a couple of times, you can
now see score is 90. So this is a simple
shortcut assignment where we don't have
to do this redundant like score equals
score + 5, but we can do it directly and
this works with almost all math
operators. All right. So, those were
some common operators. Now, let's get to
into the string manipulation. And I want
to show you two more things when it
comes to working with strings that can
be that are very useful, especially when
you're working with AI and when you're
working with prompts. So, we already saw
that you can use the plus sign, right?
But now I want to show you frings. So,
let's see what they are and why they are
so common in uh when you're working with
AI and language models. So let's say we
have our string and let's say we have uh
let's say we do hi there my name is
Dave. We can run this. We have the
string and we have their hi name there
my name is Dave. But let's say you have
a program and you don't want to hardcode
this name but you want to do it based on
the username right? So let's say name
equals Dave and then oh I should put
that in quotes right to make it a
string. How can we now make this in such
a way where we make this dynamic and we
replace this with the variable over
here? So now depending on who is
interacting with the system, it will
give them a different greeting. And this
is used all the times when you are
working with AI models and prompts, you
want to dynamically inject information
that you retrieve from another system,
store that in a variable and then put it
in a string. So the way we do that in
Python is through what they call
fstrings. And this is again this this is
now a syntax thing because this is just
something you need to remember. There's
not really any logic behind it, but it's
called fstrings. And we literally put an
F before it. So you have the string, you
have the quotes, and you just put an F
in here. And when we plug that F in
here, what we can now do is we can come
in here and we can use curly brackets
and we can now plug in any variable. So
you can now see that this is a string
and it's not giving us any errors. And
if I now run this and I print this, you
will see that if I move my oh wait, let
me actually move myself to the left side
so you can actually see something. So
here you can now see my name is Dave.
And if we change this to I don't know
John and we run this you'll find that
the string is now hi there my name is
John. So that's the notion of fstrings.
Super useful. And let's see if I for
example get rid of this you can see
right now if I run this and print it you
can see that it literally like hardcodes
the curly brackets in here. It it will
be orange. So it will Python will
indicate that it recognizes that you
want to do something there but it will
literally just print it out exactly as
is. So that's why you need to put the f
there and then it works. Now then
another thing that I want you to know
about strings is that in Python you have
a lot of what they call string methods.
So these are methods functions that you
can apply to a string and it will
perform an action. And the way you do
this is so here's an example. So you
have text, you have the string in here
and instead of putting it before it, so
print and then using the the brackets,
you directly take the string variable,
you do a dot and then you apply the
string method by typing out the method
and then using the brackets in here. So
let's follow uh let's follow along with
this to get a feel for what that is. So
let's say let's come over here. We have
Dave and we can do name. We do dot lower
and then we do the parenthesis. And now
you can see this is my name in lowerase.
But I can also do my name in uppercase.
Let's see in uppercase. And I can even
do let's say if we have a sentence. So
you could for example do sentence equals
hi my name is Dave. You can even do a
title. So you can do sentence.title
and then it will apply title case if I
store it in memory. And there we go. So
you can see it will capitalize all of
the first letters. So these are called
string methods. And I'm going to show
you one more example over here. I'm not
going to walk through everything but you
can even see if you have this message
over here. So I love the Python
programming language. We can also for
example uh create some boolean logic
around this where we can say starts with
ends with and that gives us true or
false values. We can find certain
phrases within a sentence. We can count
the occurrence or number of times Python
is occurring in this message and we can
even replace certain parts. So here if
we have the message and we replace
Python with JavaScript it will now print
I love JavaScript programming with
JavaScript. now and I totally understand
that that is a lot of information right
and you definitely don't need to
remember all of that like even I often
have to look up these things like oh
what was it again like there's something
in Python with starts with and ends with
these are such niche use cases that when
you need them all you need to know is
that Python has a lot of functionality
out of the box to solve common problems
so whenever you're dealing with
something there is a high chance that
you don't need to come up with the
entire logic yourself or create a really
complex function, you can first just ask
chatbt and say, "Look, I'm trying to
solve this problem. I need to I have
this sentence and I need to understand
if it if it starts with this and then I
want to replace a certain part and JGPT
will literally give you the most
effective way to do it through a
combination of all of these methods that
I just shared." So the goal really is to
get you familiar with what you can do
with data. What are string methods? What
is the notation? seeing them, walking
through them, and just getting a feel
for how this programming language works
is really the goal at this point, not to
fully understand and remember
everything. That's important to keep in
mind. All right. And now we get into the
section control flow. So, how can we
actually make decisions within our
program, right? We've already we already
saw that we can use true and false and
we can do calculations. But now whenever
we're building programs, we need to do
something like look if at an ATM a
password is correct then we allow
access. If not we give an error. If a
user clicks on a button only then we
perform an action. In a game if the
health is equal to zero it's game over.
Otherwise we can continue. And within
programming we do this through what you
call if statements. So let's run through
some examples. So let's say we have a
temperature and we set that equal to 25.
Now what we can do is we can build a
system that based on the temperature
does either a certain action when it's
above a threshold or something else when
it's below a threshold. So we can simply
start by saying look if the temperature
is greater than 25 we now use a colon
and we get to the next line. This is
something new. We haven't done this. So
we introduce the if statement which is
an operator that we can use. An if
statement always just starts with the
if. Then we have the conditional logic
over here and then when we end that
logic we do a colon, we do a enter and
then we get to the next line and here we
use an indentation. This is very
specific to Python. So it goes to the
next line and what's doing uh what it's
doing behind the scenes. You can see in
the highlight over here it's adding four
spaces. Now our code editor is doing
this automatically for us because it
understands the Python programming
language and it understands what it
does. So just so you know that's what's
going on behind the scenes. And now what
we can do is we can for example write a
print statement over here where we say
it's hot. If we run this nothing will
happen. And that is because the
temperature is equal to 25. If we set it
to 26 and we run this it will say it's
hot. Remember right similar to the
example I gave about the customer care.
Do we need to escalate it to a human?
Yes or no. This is it's a similar type
of logic. So let's set it back to 25.
Nothing will happen. Now let's create
another case. So what we can now do is
we can now go back. So we exit out of
this if statement and we continue with
an else statement. And now here we don't
have to specify any logic because this
will simply execute in all other cases.
And then we say print it's nice weather.
Now if I run this it will say it's nice
weather because if we follow the order
of operations which in Python will be
from top to bottom and from left to
right it will first check if the
temperature is greater uh if the
temperature is greater than 25 it will
print it. This is not the case. Okay.
Now we go into the else condition. This
will always run. If this is not true
this will run. That is how it is
currently set up. It's it's a boolean
boolean decision-making logic. And now
the syntax here with if and then a colon
and an else and then nothing there and
another colon is just one of those
things. You just have to see it and do
it a couple of times before it clicks.
But let's say you want to add more
conditions to the mix. So actually what
we want to do is say look if the
temperature is greater than 25 we say
it's uh it's hot. But we can also say if
the temperature is greater than 30, we
say it's very hot. And now what we do in
here, if we if we set it up like this,
we have an if statement and then another
if statement. And let's see, let's
actually put this at 24. What will
happen right now? You'll find that
currently it will say it's nice weather.
But then if we put this to 31 and we run
this, you'll find that it's both running
is very hot and it's hot. But we in this
case we don't want that because we want
it to be either one of the decision-m
points. So for that Python has something
else and that's called an L if. So here
we can chain things together. So it will
first check if the temperature is
greater than 30 and if that is the case
it will print is very hot and then it
knows that it can end the loop. So it
will not check for this. So what this
looks like is now it will say it's very
hot even though this condition is also
true. It will not get to that because of
how we structured it. So another example
over here is uh here another chain and
you can also use multiple alifs. you can
actually chain them together. So let's
say we have a score and if we say if
it's uh greater or equal to 90 we say a
excellent here we we get a B here we get
a C here it needs improvement. So let me
actually copy paste this to see what
this looks like. So now with 85 you get
a B and if you have a 90 you get
excellent. So remember the order of
operations right from top to bottom from
left to right and if we use an if l if
chain the script will stop once one of
the conditions is true and if it gets
through everything and there is none of
these conditions is true it will always
execute the else statement. This is
something you have to get used to but
this is really the bread and butter of
building programs. You take in the data
whatever type of data type it is whe
whether it's a number a boolean or a
number you combine that into some kind
of like variables and you apply some
operations and then you want to do stuff
with it. If it's this we do that if we
get to here then we execute this
function. If the escalation is true we
we make sure we get a real human in the
loop. Really the fundamental building
blocks of how you build systems and how
you put something together in a
programming language. Now the nice thing
is that everything we've learned up
until this point can also be re
reapplied and combined. Right? So here
when you're using if else statements we
can also use the and or and not
operators to determine the boolean
logic. So similar to how we were first
looking at age and license and we're
just printing what it was. We can also
create if else statements around this
and combine them in those two. So just
to showcase that to you, you can come in
here and here this will print you can
drive and if we put put it to 12. So
this is a 12y old we cannot drive. If we
want to let the program know that this
is not possible we can print this and we
will say you cannot drive. Now we run
this and it will go and we can put any
type of l if statements in between. And
now with if else statements we can even
do nested conditions. So we can for
example say if has ticket and then we go
to the next line but we also say if age
greater or equal to 18 we print enjoy
the movie and then we do an else
statement in here so they have a ticket
but they're not 18. we print needs
supervision
and then we can come in here all the way
to the end and then add another print
statement and say please buy a ticket.
So now you can see someone's 15 has a
ticket they need supervision. Whenever
they're 18 they can enjoy the movie.
When they don't have a ticket they need
to buy a ticket. Now the next concept
that we will cover in the control flow
section is the notion of loops where we
can repeat code multiple times. So this
is often something that you need to do
when you're building programs. And for
example, let's say you want to do
something like this. You want to perform
an operation five times. Instead of
writing this out in your program, which
is which is really for verbose, we can
create a loop around this which is much
cleaner and much shorter. The most
common way to like do this is using a
for loop. So let's give an example of
this. And again there is a specific
syntax that we need to understand in
here. So let's clear this up. When we do
a four, we start with four. That's why
it's called a for loop. And we select a
counter in here. And this is one of
those things in the beginning it's a
little bit tricky because this can
pretty much be anything. But let's take
i for now. But we can also put x. And we
can also put whatever in here. But a
common way if you want to create a
counter is I because it's really short.
And then we say for i in and then in
this case we can give it a range. And
let's say we give it five. We can then
do something like print and then we do
i. So in this case if I run this you
will see that we'll print 0 to four in
this case. Here is there's a lot going
on in here because right now like up
until this point you probably were like
okay Dave I can follow all of this but
this this is not plain English. This is
weird. Why does it start with zero?
First of all, one thing to keep in mind
is that Python is a uh zero index-based
programming language. Meaning that
Python starts to count from zero. This
is something that we will also encounter
later when we're working with lists and
indexes. If you want to get the first
element, we say we want element zero
instead of element one. That's why you
see zero over here. And what we are
essentially doing here is we're creating
an iteration where we say look for i in
range five and range five is a range
where we say look we start with zero and
then we go to one to two to three to
four and then we stop. It's syntax. It's
a notation. It's something you have to
get used to. And then we perform an
operation. And in this case we use i. So
we use the counter uh variable here as
well. But we can also for example print
hello world and then if we run this it
will just simply five times print hello
world. But the i refers to the counter
in here. But let's get back to the docs
over here and see what's going on over
here. So again python starts counting at
zero not at one. It's called zero
indexing. And you can also count from
different starting points. So just this
is just a way of repeating things over
and over again. We can do it through
numbers by giving a range let's say 1 to
six. uh we can even count with steps of
two all things that are possible. So let
me actually run this over here to give
you an example for for uh what that
means. So before we had five, right? So
we said range five and then we print
everything. But we can also say look we
want to go from one to six and then
you'll find that it will print 1 to five
because it will stop before six. That's
just the way it works. And that's one
thing like right now you're probably
wondering like oh it's a little bit
tricky so you start at zero and you put
in six but you actually go to five even
I to this date whenever I do four loops
um you're always tinkering with this so
what you end up doing is you're
experimenting with this and you oh wait
no I actually want to get to to six so I
plug in seven then you correct it and
that's the iterative process right
that's also what I love about using the
interactive session it's so fast you can
select bits and pieces and continue. And
then here you have, you can see we add
another variable to it. So we say we
want to go from zero. We want to start
at zero. We want to go to 10, but we
want to do it in steps of two. And then
what you'll find is that it will go from
0 to 2 to 4 to 6 to 8. So that's
something we can also do with the range
function. And there are more components
to it. But that's just enough for now to
understand. Look, if we want to do
things over and over and over again, we
don't want to do this. That's the
takeaway for this section. And that's
all for now. All right. And that brings
us to data structures. So how we can
store multiple values together. So up
until this point, we have stored one
value per variable, right? So we were
talking about the numbers, we were
talking about the strings and the
boolean values. But what if you need to
store multiple values? So you want to
create a collection and put them
together. That's where data structures
come in. So these are like containers
and they come in the form of lists like
a shopping list ordered items. So I want
this, this, this, and I also want
bananas in there and I also want apples
or a dictionary. These have key value
pairs. So it could be like a phone book.
So you have a name and there is a number
associated to it. So they come in pairs
and then you have what is called tupils.
These are fixed. So you create them once
and you can't really mess with them. So
for example like coordinates and then
you have sets. These are a bag of unique
items. So you can take a whole lot of
information and you just get rid of all
the duplicates. So you only have the
unique values in there. Those are the
common data structures that you'll use
within the Python programming language.
So let's start with lists which is
actually a really common one. You'll use
this all the time. So, we're going to
start with the syntax and that is as
follows. So, we're going to clear this
up and we're going to say my list
equals. So, spell that out correctly. My
list equals and now here comes the
syntax. We use brackets, square
brackets, not the curly brackets. That's
for the dictionaries we'll use later.
We'll use square brackets. Now, this is
the container in which we can start to
put stuff. And Python is really
flexible. So you can literally view this
as some kind of bag or container and we
can dump all types of stuff in it. So
remember we had Alice, right? We can
dump Alice in here. Alice is a string,
but we can also plug in 25. Um we can
also say look we have uh h equals 25.
And now next to 25 we also actually plug
in the h which is now a variable. But we
can also plug in actually true. And you
know what? We also uh had the has
license uh which in this case is false
and now we plug that in and that's all
totally fine if we store it in memory
and then run it. So let me clear this up
to get rid of the errors. So now we have
a list all types of different data
formats and data types that are in
there. That's the beauty of Python is
really flexible uh and it doesn't really
care about that. Now whenever we have a
list we often want to do something with
that. So now we have this list and now
we can index this list. So let's say
you're working on a problem you have
this container and you want to take
Alice out of that list or whatever the
that element is. Lists are ordered
meaning that how you store it in this
way will like Python will remember and
through this we can access certain
indexes of this list to grab information
out of it and we do that in a similar
way using the square brackets but now
directly on the list variable itself. So
we can now do the square brackets and
now we're going to index things. So a
couple of minutes ago I explained how
Python is a um zero B uses a zerobased
index. So it starts at zero. So that
means if we want to grab Alice out of
here we do a zero and that will give us
Alice. So we could for example say name
is equal to my list and then we take the
first element. Now that is Alice. Now we
can set H equals my list and then we
take the second element but we index it
with one. And now we get 25 and so on.
We can go on and on and on. And also
what what you can also do is you can
also say look what if we want the last
we want the last item but like what is
it? Is it like three four five? Python
has a way uh to do this. So this is
actually like has license. So we can say
let's say the license equals oh look and
here actually this is interesting. we
get a blue uh highlight over here
meaning that there could be a problem
where this is an internal uh variable
that Python is using. Now in this case
we should be fine. Uh but let's actually
like override it with has license. So
sometimes you have that for example when
you write print that's that's something
that we can use inside Python internally
and that's often not how you want to
name your variables. So that's just a
quick side note. So we can now say my
list and I was explaining that how we
can get the last element. We can do a
minus one. So we can also refers the
index. So you can now see we get to has
license. We can also do minus2 and that
will give us the true which should be
true. And then we can even get minus
three. So there are multiple ways like
forward or backward to work with the
list and to get elements out of it. Now
next to grabbing single items that we
just saw, we can also take slices. So we
can also specify a range using the
colon. Now next to storing information
in a list and then grabbing something
from it. We can also over overwrite data
in a list we can add to it. We can grab
from it. We can remove things. So let's
walk through some common operators in
here. So let's say we have the list and
we say my list and now we say look we
have my list and we take the first
element in there. We say Alice and we
say look now this is Dave. Now if we
look at our list you can see Alice has
now been updated with Dave. We can also
use a list method. Uh in this case is
append and we can just add something to
it. So let's actually add Alice back to
the list. And what this will do is it
will append it. Let's see show you over
here. So you can see it has now added
Alice to the end of the list. So append
is just taking a list as is and then
stacking that to the end. And we can
also remove stuff. So if we come in here
and let's say hey we have my list and we
now do remove and again we want to
remove Alice from the list. So Alice is
having a hard time with list this list.
We're not sure if we want her on or off
or whether it's Dave Alice is gone
again. So all operations that we can
follow. Often in Python there are also
multiple ways to accomplish the same
goal. Uh with append uh there is also an
insert. So instead of just saying look
we want this at the end we can also
insert it into a specific index. So
coming here again let's see so Alice is
from the list again and then we can say
look we want to insert and then we say
look we want it on index one and we
bring Alice back in there and let's run
this. So you can see Alice is back in
there. Lots of different ways to think
about look what do we what do I want to
accomplish? What do I want to do? How do
I want to store the data? And then
Python likely has something for that for
you to accomplish that goal. And similar
to strings, lists also have methods to
do even more with it. So we can check
how long a list is. We can count, we can
index, we can sort, we can reverse. All
stuff that you may need to do at some
point where you run into an issue and
then you'll find, oh, look, Python can
actually reverse this list for me. Easy.
But again, these are more these are not
so common, I would say, in the real
world, especially when you're working on
AI cases. Every now and then they pop
up, but for now, it's totally fine. Just
high level be aware of them. Know that
you can do stuff with lists, but it's
much more important to understand the
fundamentals, right? What is the list?
We can store data into it and we use
brackets to do that. That's important.
And we index them. That's the most
important thing for now. So, let's get
into dictionaries. So remember, right,
these are key value pairs. So within a
list, we simply have a grocery list,
whatever. Alice doesn't have to be
related to 25. They're just in the same
list, but could be entirely different
concepts. A dictionary changes that. A
dictionary uses key value pairs. And we
do that using the curly brackets. So
here we can start to store information
in a way where I can say look we have a
person and now I'm going to open up the
uh I'm going to create the curly
brackets in here. I'm going to do do an
enter and I'm going to say hey let's say
a person has a name and there comes
Alice again and I should put that in
quotes over here. So this is the key. So
the name is Alice and Alice has an age
and now that is a number. So let's say
Alice is 25 and we separate all of the
new elements in the dictionary similar
to how we did that in the list with a
comma. So whenever we go to the next
line we do a comma and then we can say
look Alice also has um a city and we set
that equal to New York. So Alice is 25
lives in New York. Now this becomes a
dictionary and what we can do with
dictionary is we can look up certain
elements in there. So if we have this
and we look at the entire dictionary we
get everything. But now if we want to
for example grab the we want to know how
old Alice is in this case what we do is
instead of using indexing with numbers
we use the keys to look up the value. So
we do that also with square brackets.
Again this is a syntax thing. So we
define the dictionary with curly
brackets but when we want to retrieve
information from it we use the square
brackets. Something we need to remember.
And now I can plug in the key. So I can
say h. And now that will pull the
information from the dictionary. It will
perform a lookup and we'll say look we
got 25. If we do the name we'll get
Alice. If we do the city we'll get New
York and so on. And similar to lists, we
can also update or overwrite information
in the dictionary. So we can also say
look person uh name we can just
overwrite that and say Dave. So now all
of a sudden you can see over here the
output now this dictionary contains
Dave. And we can also add stuff to the
dictionary which is actually quite
straightforward. Uh we can just specify
another key. So something that's not in
there. So we can for example say we do
license equals uh true. And now if we
run this you'll find that the name is
still Dave. You can see age25 city. And
now we have added the license to that as
well. And if we want to get rid of that
what we can do is we can simply do a
delete. So we do delete person license.
And now if we check that you will now
find that license is not in here
anymore. So those are the same type of
operations that you can do with a list
and within a dictionary. And when to use
which one is something you have to get a
feel for when you're are when you're
working with Python, when you're
programming in general, there are often
infinite ways to solve the same problem.
So you could very well solve problems by
just using list and storing everything
in there. But sometimes it's much easier
to have key value pairs where if you
want to look up something, you don't
have to figure out look what was the
index of this list and then need to
figure out where was it. You simply
know, oh, I stored it at the H uh I
sorted at the H key. So if I want to
know the H, we just need to plug in
that. This is all something you'll get a
feel for as you get more experienced and
more exposure to working with the Python
programming language. All right. And
lastly, dictionary also have methods. So
we can use the keys, we can get the
values, we can can get the items. So all
different ways of accessing the
information that's available within the
dictionary. This is a nice exercise to
try for yourself and see how they work.
Okay, then let's get into tupils and
then finally in sets. So these are data
structures that actually in the wild you
don't really use a lot, but it's good to
know how they work just so when you
encounter them. So a pupil is immutable.
So remember right with the list and the
dictionary I saw look we can create it
and then look we can just override the
name we can remove Alice all of that.
Tupils are immutable. So the the idea of
a tupil is you want to create it and the
program should never touch that. So for
example when you have a coordinate or an
RGB color and just when you want to
adjust it you just write create a new
one because the coordinate is not going
to change and tupils are specified with
parenthesis. So remember we had lists
square brackets we had dictionaries
curly brackets now we have tupils
parenthesis okay so tupils go in either
in pairs or you can add more to it. So
we can have a point and uh we could have
three and five and we have colors. So
let's actually plug this in and get a
feel for what this is like. So we have
an empty pupil. There's nothing in
there. We have a point which is three
and five and then we have the colors in
here. So what I can now do is let's say
colors and I want to get the first
element in there. So this works similar
to how we're working with the list,
right? It's very similar uh but it's
just in a way where look if we now say
look this is now blue. this will get an
error because if this were a list, this
would be totally acceptable. But now
tupil object does not support item
assignment. That's what the error says
because that's one of the properties of
why you want to use a tupil. So other
than that, it's very similar to a list
and we can do all the same things,
right? We can do get items, we can use
zero, we can do minus one, we can do
slicing. That's all you need to know for
now. So let's get into sets. final data
structure of really the Python basics,
all the really most fundamental Lego
blocks. And a set is a way to create
unique values to make sure that whenever
we're working with a collection, there
are no duplicates in there. There are
two ways to create sets. We can use the
set notation over here. Or we can use
curly brackets, but then not assigning
the key value pairs because otherwise it
would be a dictionary. So let's quickly
go through this because you just need to
see it at this point rather than like
fully understanding everything that's
going on. So we can create an empty set.
So there's nothing in here and then we
could later add things to it. But we can
also set it with values. Both ways work.
So here let's say we have a set of
numbers 1 2 3 4 5. So these are the
numbers. So this is now a set even
though we use curly braces because we
don't do the assignment with uh the
colon and using uh strings in there.
It's a set. Now we can also do it like
this. So again in a similar way we use
set parenthesis and then we plug the
list in there. That also works. Now we
can use it to dduplicate items because
if you don't do that like if you don't
have or don't intend to do that like you
could just as well use a list. There's
no uh there's no point for that. So
let's say we have the scores over here.
So notice how 90 appears in here two
times. So first this is a list. See
we're using square brackets. So if I
store this in memory and let me actually
switch this over here so you can see it.
If I store this in memory and print it,
you can see uh this is the list. But now
we can create unique scores by using the
set notation and we plug that in here
and you can see oh it's actually like it
was 90 and 85. It's also twice in there.
I didn't even notice that. So now you
can see it get it gets rid of the
duplicate values and now we can store
that and now our unique scores become
just that and we can do that exactly the
same over here. Apple, banana, orange.
These are the fruits but now if we add
banana one more time it works in exactly
the same way. Oh, I missed a quote over
here. So now fruits is still apple,
banana, orange even though banana is in
there two times. So that's really what
you should know about sets and the basic
operations. We can remove, we can
discard, we can check things, but it's
to dduplicate collections. All right,
and that brings you to the end of the
Python basics section. So we covered
almost all of the very fundamental
building blocks that you need to
understand about the Python programming
language. Data types, data structures,
operators, how you can combine them. And
you should now get a feel for what it's
like to write simple Python functions,
right? You should be getting comfortable
with either executing code through the
terminal terminal or see why the Jupyter
interactor session is so nice to work
with and how you can quickly explore
what the data is, check if you made any
errors and course correct. So with this
fundamental understanding out of the way
right now, the next section, building
programs, is all about how we can
actually use all of these building
blocks and create longer scripts instead
of just isolated lines that we've been
doing and how to piece things together.
So it starts to look more and more and
more like we're actually building
something and automating something.
We're getting there. You're crushing it.
And right now, I still understand this
is new. These concepts are very
abstract. They're isolated. You don't
have to remember all of this. This is
just the first exposure and it will get
better over time. And now this would be
another great time to take a quick
break. Maybe grab another fresh cup of
coffee or your favorite beverage and
then we'll get into the building
programs section. All right, so let's
dive into building programs. I also took
a fresh cup of coffee here. And in
building programs, we are going to cover
here on the left side. We can see we're
going to get into functions, external
tools, practical tips to use, how you
use Python in the real world, error
handling, and then we get into classes.
So, this is really where you'll get to
see Python come to life in action, and
we can actually start to do stuff with
the language. So, let's dive into
functions, which are used to create
reusable blocks of code, so we don't
have to repeat ourselves. You can think
of a function like a recipe. you can
follow multiple times. a machine that
takes input and produces output or a
named shortcut for for example complex
operations and before we already saw
that we could use loops right so when we
did the five times print hello world
that's a little bit redundant and we
don't want to do that so we created the
loop to do that but let's say we have
more complex logic so it's not just a
oneline print statement but there are
actually calculations there steps
printing steps maybe we need to get some
information manipulate some information
and every time something happens within
in our system, we need to do that.
That's where functions come into place
where we can define it once and then
later within our code, we can simply
call that function and we can now use
that functionality. So we don't have to
write the same thing over and over
again. And you've actually already been
using some of Python's built-in
function. So for example the print
statement that we use all the time or
the length that we use to um count the
items or the length of an array we have
also used that already. So those are
Python built-in functions and within
functions we are right now we're going
to cover three main things. We're going
to I'm going to teach you how you can
define functions. Then we're going to
talk about parameters. So these are
things you put in the functions to
control the inputs and the outputs and
then returning the values. So this is
what the output of the function will be.
All right. So let's start with defining
functions. And for this I'm going to
come over here to Visual Studio Code and
we're going to start with a new fresh
clean slate here in hello.py. So as with
anything with any new concept we're
going to start with some syntax, right?
Because first there are some rules that
we need to understand before we can
actually get into the functionality. Now
every function in Python starts with
defaf from definition meaning we're
going to define a function then followed
by a space and then we're going to write
the name of the function. This can be
literally anything. You can put anything
here. Now as a rule of thumb you want
the function to be descriptive of what
it does. So let's start with a simple
greeting function to create a print
statement. And what you then want to do
after the name of the function, you need
parenthesis opening and closing
parenthesis and then a colon. Again,
this is all syntax. You need to see it
once and then you can quickly refer to
it. For now, this is all we need. And
then we can hit enter. And now we get
into the function. So we have now
specified our function over here. And
here in here with the tabs or four
spaces, we can actually put stuff in
here. So we can for example write a
print statement and say hello. All
right. And now let's have a look at what
happens when we run this. So you can see
right here we we load the function into
memory but there's no output. We don't
see anything here. And the same would be
true if we run it in the terminal. You
can see it just tries to run the script
but there's no output. It just mentioned
that it runs the file. So that is the
case because when you define a function
it is simply a placeholder for that
logic. But when you want to actually use
a function, we need to specifically tell
Python to do that. So what we can do is
we can come over to a new line over here
and type in greet and then we do opening
and closing parenthesis. And now this is
actually where we would call the
function. So now if I one more time open
this up. Now if I do greet you can see
that it executes the function and that
it outputs the print statement over
here. Similar if I do a copy paste over
here uh and I can do hello again and I
store this into memory and then do
greet. You can see now we've changed our
function and then now when we call the
function now it prints this. So that was
your first function. So the important
thing to remember is the syntax. We
start with defaf function name opening
and closing parenthesis and then we do
the colon. Then you can add your code
here. Make sure it's indented. And then
if you don't want to return something,
you don't have to put in this pass over
here. But you can also do it for
clarity. So normally functions will
often return something. And this pass
putting this pass over here is simply a
kind of like a readable way for humans
to say look this function doesn't return
anything. I in the next sections I'll
show you how we can return stuff. But
you can see it doesn't change anything.
So you can also just get rid of this and
it will be exactly the same. but Python
accepts it. So those are the key parts
and then some tips on naming functions.
So like I've said, you want functions,
you want functions to be descriptive of
what they're doing, right? And there's
also there al there are also some style
rules to this similar to how we talked
about there are style rules to
variables, right? We use a similar setup
over here. So we use lowercase letters
and whenever there's a space, we use an
underscore. So if we want to calculate
some kind of total, we do calculate
underscore total. We want to send an
email, send underscore email, no capital
letters. So we don't want to do uh we
don't want to use capital letters in
here. Those are reserved for classes
which we will cover later. But remember
these are styling styling guidelines. So
it will work perfectly fine. You won't
get an error. But other developers will
look at you and they will see that
you're a beginner and we don't want
that, right? So then calling the
functions this is what I already showed
you right. So let let me actually copy
paste this example over here to see we
can also call uh functions multiple
times. So that's the nice thing about a
function. So here we have say goodbye we
can do it once and now if we do want to
do it one more time in our script we can
simply just refer and just call the
function instead of having to do it over
and over again. And now what this means
whenever you run your entire script so
everything runs you can see that all of
the logic here is executed. by three
times. And beyond just simple print
statements, we can really do anything
with in a function. So we can also come
back to some of the logic we already
tested before and we can input if else
statements. And the most important thing
here to keep in mind is the indentation
and how you structure how you structure
these functions, right? So let me walk
you through it. So when we create the
function, we do defaf and we do the
check weather. And you can also see over
here like how your ID how the code
editor is helping with this. So now this
this is how we specify the function. Now
we do an indent. Now if we want to check
what if we want to uh include an if
statement we do if and then let's say uh
temp
greater than 25. We for example print
and we say it's hot. And now you can see
these squiggly lines under here under
temperature. And that is because right
now Python doesn't know what temperature
is. And that's because we haven't
specified it yet. So you want to store
that over here and say look temperature
is 16. If temperature is greater than 25
print it's hot. Then we go in here and
then I do a shift tab to go back to this
line. I do an else. I do another enter.
Our IDE will cover the indentation and
otherwise print it's nice weather. So
this is why it's also good to like I've
said beyond just copy and pasting the
examples over here really writing it out
because if you just do copy paste and
you run it everything will run but it's
those nitty-gritty things where like
look how how does this work? Should I
have it like this or have do I have it
on this line? And you can immediately
tell that your IDE will tell you like
something's off. And this is how you
build the like the the muscle memory and
the the uh the skill really to look at
Python code and think about okay look
this is a function we have if else
statements this is how it should be
structured. So now if we do this
remember if we run this what happens
nothing correctly because we haven't
called the function yet. So now I go to
the other line over here. I go all the
way to the back all the way really to
the beginning. So no indentation and I'm
going to do the check weather. I hit
enter to autocomplete. So that's also
something you'll notice, right? So I do
check and now my IDE already says look
check weather is a function that I have
over here. So now I can just press enter
and it will autocomplete that. So that's
also one of those things where you need
to you need to get your hands on the
keyboard. You need to be able to type
and then you'll find all kinds of
interesting things that your IDE will
show you. So you'll find that as I go
through all of these examples, I'll also
explain more and more of these things
behind the scenes. So now we have to
check weather over here. We can put it
into memory. Check the weather. So it's
nice weather. I can come over here.
Let's say let's make it 30. Store this.
Check weather. It's hot. Okay, that's
our function to check the weather. All
right. Okay. So let's come back over
here. You can check out some common
mistakes. If uh things don't go wrong,
things is like you forget the uh
parenthesis. That's a that's a big one.
You forget the colon at the end. So do
greet no colon. All beginner mistakes
that you'll uh run into or the bad
indentation. So it's not formatted
correctly. All right. So let's now let's
continue on this and let's talk about
parameters. So parameters work with
functions to make them dynamic. Right?
So if I come back to this example. So
right now like this is hardcoded and
hardcoded means whenever this script
runs it will always be this. I can do
this all the time. I can do check
weather, check weather, check weather.
It will always be hot and this will
never change. That's because there are
no parameters that we can
programmatically or dynamically change.
So that's where parameters come in. You
can now see that we can also add
parameters in here and parameters go in
between the parenthesis. So over here
you can see we have the greet function
but then we plug in the name over here.
Now we can use that variable name in the
function itself. And then when we call
the function we can plug in that
parameter and it will use that it will
use that data within the function. Okay.
So let's have a look at how this works.
Again we have our simple function over
here. So let's do another one. So we do
greet. So dev greet this is all the same
and then we do the parenthesis. And now
what we do is we do name and then we do
a colon. So now this function takes name
as a parameter. And what we can now do
is we do print and we create a string.
we say hello and then remember if we
wanted to to use variables within
strings, do you remember what it was? It
was called using an f string. So we put
a little f before the string and then we
could use the curly brackets in here to
plug in a parameter. So now when we have
this function and we go to greet, what
we can do is we can now say name equals
and let's say we can put in Dave. So we
save this and we do greet and you'll
find that it says hello Dave but we can
also do this one more time and we can do
hello Alice. So this is a way for us
this is a really this is a key concept
about working with function because this
is something you'll do all the time
where when you're building programs you
think about look what logic do I want to
implement what data what information is
is coming at me and how do I want to use
that information within my function so
now we've made a dynamic script that we
can change and one thing to uh remember
here is that for Python it doesn't
matter in this case if I specifically
say name equals Dave or I just do greet
Dave because how this works is you can
also have multiple parameters and it
will just follow the order of
operations. So let me show you what I
mean with that. So I can also for
example let's let's do let's do last
name first name last name and then we
say look we have a first name in here
and then I just do let's see I do space
in here and then we do last name and so
now we have two parameters in here that
we can insert. And now what you'll find
is if I do this and if I run this
function we'll get an error because it
says look greed. Let me actually move me
to the side. So you can see now greed is
missing one required positional argument
last name. So Python now understands
look in this function you actually need
two parameters and you only gave one. So
if we now plug in my last name over here
you can see that right now this is all
uh correct and now we can see hello Dave
ablar. So now it's all good and you'll
see that you actually don't have to
specify first name last name because
Python understands look this is the
order of operation. First we do the
first name and then we do the last name.
So if I swap this around and do it like
this and I agreed it will be reverse. It
will it will take my last name and we'll
plug it in the first name. So I what I
always uh like to do I always like to be
explicit with this and really say look
first name equals Dave and then I put
the last name in here. So you don't have
to do this. Python will also work
without but especially if you're working
with multiple functions right now you
can clearly see look this function is
greed and I plug in the first name and
the last name. So you add a little bit
more code to it but it helps for
readability. So now when I plug this in
and I greet it, you'll find that it
works correctly. And now what's also
interesting now we can also like if you
want to for whatever reason you can also
swap it around. So now the order doesn't
matter because we are explicit about it.
So now if I run this even though the
order is in reverse it still says my
name correctly over here because we
mapped them to the correct parameters
over here. And now functions can also
have default parameters. So what you can
do over here whenever you specify the
function let's say first name let's say
you can put an equal sign in here and
let's say we do John and then last name.
So oh this is another interesting thing
you can specify default values in here.
Now you'll see that we get red squiggly
lines in here and that means that here's
an error. So this is another syntax
thing. Whenever you want to specify
default values, meaning that right now
uh I for example don't have to put in a
first name and then it will if there's
no first name, it will default to John.
But there's an order to things. So you
need to uh you need to set the
parameters that don't have a default
value first and then you set the ones
that have default values. So now it's
just like it's just like swapping things
around in different order. The
functionality is still the same but this
is just like what Python expects. So now
if I store this function now let's see I
can uh come in here and look now I can
actually run this function and I only
need to plug in the last name and it
won't give me it won't give me an error
because now it will simply say look okay
first name is not there in that case I
always take John and let's see we can do
something similar for the last name and
now because this is now also has a
default value we can swap it around
again just for the order of things. And
now I don't actually have to plug
anything into my function and I can just
run it and it will run with the default
values of John Doe. But I can simply
override that and say look I want to
greet it with Dave and vice versa. You
can do it with last name, first name,
both. So that is a way to work with
parameters. You can plug them in. You
can give them default values. You can
change them to make your programs
dynamic. Okay. So we've already seen
that you can just work with multiple
parameters. So you can add as much
information as you want there. So let me
quickly show you this example about
calculator. So here you can see we have
a function calculate total. We take the
price, the tax rate and the discount.
And now instead of doing simple print
statements, we're going to apply some
math. Right? So we we first calculate
the tax by u multiplying the price with
a tax rate. Then we have a final price.
So we're storing v variables inside this
function over here and then we print the
final price using an fstring and then
you can see over here order matters. So
let's see because right now we are not
specifying the uh names right we already
saw this then order matters if we are
specific about this that's why I always
recommend to do that you cannot make
that mistake so you'll always find that
it's very clear that the discount is
this this is the tax rate and this is
the price so that's why I always
recommend using these keyword arguments
that you can see over here right you
specify the function and then when you
use them just make it explicit. Say the
city is this instead of just like
relying on the order uh of operations.
Now, one more other thing that I want to
show you here when it comes to functions
is the notion of global versus local
variables within Python. This is a
common mistake and this can be really
frustrating in the beginning. But
whenever so by now you know you when you
uh run a Python file, it goes from top
to bottom, from left to right. But there
is also a notion as to when we are
inside a function or in a class for
Python that information now becomes
locally and I'll show you in a bit what
that means but it essentially means that
you can use that information within the
function and within the function Python
understands what it is but outside of
the function we cannot use it. Let me
show you an example. So let's take the
calculate the calculate total over here.
So we have this function and over here
let's even make it simpler by setting by
hard coding the price over here. So or
let's say we we just take the price and
we get rid of the tax and the discount.
So we set the tax rate we hardcode it.
So we set that to
8 and then we set the discount to 10.
And actually let's make it even more
explicit. So we put the tax rate here.
So we plug that and then we can put the
text right here and then we say the
discount is 10 and then we put that
here. So now we can see and you can also
see how I can play with this. I can add
a comment here. So we can for example so
these are the default values and then
over here we are going to do the
calculation. So and then we do print
final answer final price. Okay. So now
we have made the function simpler in a
way where it only takes the price as an
input and we can calculate this and you
can see this show up over here. So now
what happens if we for example are in
another part of our script later down
the line and we want to continue. So we
first made one calculation and now we
want to maybe add something to it and we
want to use the discount one more time.
So if we for example now say look okay I
want to use that discount again because
we we set it to 10 right uh discount is
10 over here you see if I just type in
discount in here it will give us the
yellow squiggly line saying discount is
not defined so when I run this it will
get an error even though this right now
is all stored in memory it's in the same
file discount is not available so you
can see I'll actually move that discount
is not available and that is because
this is the difference between local and
global variables. Whatever is inside a
function is considered locally in Python
and then outside the function we cannot
use that. So there is an order to
things. So if I for example now set over
here at the top I set discount is let's
say 20. I run this you'll find that we
set the discount to 20. Now we'll get
inside the function and it will perform
the calculation. you you'll still see
that the total is the same. So we get
the same answer. We get the 98 and
that's because inside this function,
Python prioritizes the variable that you
have over here. So even though first we
say discount is is 10 because we go from
top to bottom and from left to right, it
will overwrite the 10 over here. Then it
will perform here the calculation using
all of the information in here. But then
later when outside the function we refer
to the discount, it is still 20. And
this is really just one of those
examples that you need to run into this
a couple of times in the real world and
think about like, oh look, I thought
discount was 10, right? Why is it 20
right now? And then why is it 20 right
now? And they see, oh wait, it's a
global variable over here and here is
the local variable. Now, um I've shown
you that if you don't have a global
variable and you just have local
variables in the function and you try to
access it, it won't work. But here's the
thing, it works the other way around. So
within functions, we can use global
variables. So if I specify the discount
outside of the function, we can use that
discount in here and Python will know,
look, we have a global variable. So
that's one level higher, it overrides it
and we can use that inside a function.
Now, this is not common, but it can uh
it works. And why I said it's not common
because typically you want to be very
specific about this. and then still add
the discount as a parameter because
otherwise information can get lost
meaning that if I specify the discount
over here and then let's see I have the
function and now we calculate the total
price you can see that the output is
still the same so this discount is
referring to what's specified over here
but now when you're working on real
projects you often have multiple files
multiple functions and now someone's
working on this and you see this
function over here and someone decides
to get rid of this now this function
doesn't work anymore because now uh
let's see if I get rid of this. Now it
says look there's no discount over here.
So adding that and making that specific
in this function is best practice in
that case so that you always know look
whenever you need to calculate the total
we also need to plug in that discount
specifically over here and it doesn't
matter where we get it from but this is
the place and that's how you don't
forget it that it needs to be in there.
So that was a little bit of playing with
global local variables. You get some
exposure to what it's like to work with
the function to input the parameters and
then how to call the function. And
really it's about practice here. You
need to see multiple examples. So later
on when we're going to the practical
Python in this session, we're going to
actually do some stuff because I'm fully
aware and this is some of the things
that like really bothered me when I was
learning Python in the beginning. You
always see these like cookie cutter
examples. So like look I can yes I can
print someone's name but what what is
actually the point of that right? So
bear with me we need those fundamental
building blocks. So then here I'm
literally going to show you like how you
can take a CSV file import it do
calculations and then write it back to a
file and that's really where things will
start to click and we're going to reuse
these building blocks again but we need
to be step by step. Okay so then the
final section in here of functions is
returning values. So up until this point
we created functions that just do
something and then either just print
something or do nothing. Functions can
also return something and we do that
with the return syntax over here. So
let's dive into some examples. Okay. So
let's say we have the simple function
and we do a print and we take a and b as
parameters and we just in this case we
just print a and b. So this is now what
we saw before very simple function we
don't return anything. So now if we do
add print and we call this and we plug
in a= 5 and let's say b= 10. Okay, wait.
We can for example do let's actually do
plus. Okay, there we go. So now you can
see we run this function and it just
does a print statement but it just shows
up in the terminal but now we cannot
access that 15. It's like it's gone
right now. It just it was a one-time
print but now we there's nothing. Let's
say we want to add something else to it.
There's no way to to reference that 15
or a variable where that is stored in.
So that where that's where return comes
in. So when we want to perform an
operation and we also want to like store
that and do something with it. So then
what do you do? You add a return. So we
do a return. We do add and we do a
return. So we do this. We do the same
stuff. So we do a and b. We plug that in
and let's see colon. And now what we can
do is instead of a print we do a return
and then we say a and then we do plus b.
If we now run this and we say add return
and we call that function. The thing is
in the terminal if I run this over here
let's see oh sorry I should plug in the
variables over here let's see we will
also see 15 but there is a difference
and that is when we do the at print so
we just print it it will like printing
is just show showing it but we cannot do
anything with that later whereas
returning it it is actually returning it
and we can capture and we can store that
information and the way we do that is
simply by saying look we have our
function and it outputs something and we
store that into a variable. So now what
we can do now if we run this now you'll
see we'll we'll just run this line and
you won't see the 15 show up here
because it's stored in result and now
result is something that we can use and
we can do whatever with that. So now we
can do + 50, we can do + 20 and that
information is stored. And here it's
also important to let's see to give a a
quick distinction between the
interactive session that we use all the
time and running it in the terminal uh
when programs run in the background for
example because because when I for
example comment this out, I select
something and I do command forward slash
or on Windows uh control slash you can
comment out entire blocks very easy.
This helps if you want to run your
program without running this. For
example, if I now run it in the
terminal, let's see. So, we do run in
dedicated terminal. You can see this
show up. Now, you can see that it will
it will print the 15 out over here. So,
you'll see that if we close this out and
now this and I encourage encourage you
to follow along with this and also like
play with like commenting these blocks
out and then moving quickly between
them. Now, if we run this, you'll see
that we run it, there's no 15. So this
is a difference between the interactive
session and the terminal if you do it in
the interactive session. Even though
it's not like printing it, we can still
see the the output. But that's just
something that you have to keep in mind.
But there's there is under the hood
there's a difference because this 15 is
something that we that we can use and
this is print statement that that just
shows up because let me show you if I
now do so we have the result you saw we
can store that right we have that 15 but
if we do that over here so we say if the
add print so we do print results equals
we can save the output we will see the
15 but now when we do print result it's
empty there's nothing here so little
tiny difference but something in
beginning that you need to get used to.
Okay. And so with that we can perform
all kinds of calculations. So let's take
the following example over here. Let's
copy paste it. So here we have a
function to calculate the area. So it
takes as an argument the width and the
height and then we calculate the area by
multiplying it and then we return the
area. So we can also store in between
variables. We can expand on them do
calculations and then return the
variable. So we don't specifically have
to do the return statement here. Whereas
this would also work. It would be
exactly the same. But this is a little
bit easier to also help you as a
programmer think through your program,
right? What's the goal? First, we want
to calculate the area. How do we do
that? Width time height. Okay. And the
area is the stuff that we want to
return. So if I run this and I calculate
the area, we can print the room size and
it's 120 square ft. And we can simply
for example come in here and let's say
you know we want to er uh we want to add
a margin of error. So we want to say
look area is area and then we do it
times 1
let's see let's add a 5% margin and then
you'll find that it's bigger and these
returned values we can use them later.
So if I take this example over here. So
let's say we have a very simple function
to double a number. We can have a result
which is now uh 10 because we input five
and the function doubles it number times
two. We get that result and then we can
use it in another expression. So let's
say we have the total we do double five
double3 and then we get 16. So you can
see how we can now start to combine
things right. We take this function and
we use it just in an expression. So
instead of just first storing the
results we look double five that will
output 10 and double three will output
six and we can simply combine that right
like this will be 10 we store it in
total and now we have total and we can
print this over here and we can also use
it in a condition. So we can say look if
double 7 is bigger than 10 we say it's a
big number. So the like this is this
this can get a little bit tricky to to
work with but that's why I why I love
the interactive session because you can
literally look at okay what does double
7 do by selecting this using the uh
arrow command uh arrow on your keyboard
and shift and I can look this is 14 so
now I do if double 7 which is 14 is
greater than 10 it's a big number so now
if I run this it will say big number but
if I say let's say we do three and I run
this it will not do anything. And within
functions, we can also return multiple
values similar to how we could also
input multiple parameters, right? So,
let's do a let's do a just a simple
function just as an example. Uh we
define it and then say look, we have a
we have our numbers. Let's say we have
our numbers which is a list. We do 1 2 3
4 5. You should actually do some spaces
in here for clarity. So we have the
numbers in here and then what we want to
return or let's say first we do we do
the first number and that is going to be
our we have our numbers and then
remember this is a list right and we can
index that by using the uh square
brackets and then we do index zero this
refers to the one so this is the first
number and then let's say we do the last
number and that is going to be uh let's
see we take that same thing numbers and
then we do minus one and then what we
can do is we can return first number and
then we do a comma. So this is syntax
again. So it's not end or n. We do a
comma to say look we want to return the
first number and we want to return the
last number. And then what we can do is
we can for example let's say if we have
this here in memory we can call our
simple uh function with doesn't take any
input parameters right now. And then if
we want to store that we could for
example say first number and then comma
again last number and we can capture
that. So now if we run this you'll find
that we have first number one last
number is five. And the thing to keep in
mind here is that the variables over
here do not have to be the same as what
we put over here. So we could for
example just as well do f and l and it
would also work. So now we have if we we
do it via print statement we could print
our f which is one or we could just look
at f over here is one and in a similar
fashion our l is five. So that's how you
return multiple values from a function.
And that also wraps up the section here
on functions where the most important
thing to keep in mind here is that when
you're using functions, if you use a
print statement, it just displays it.
When you want to return something, which
is what you often want to do in the real
world, use the return. And in the
beginning, you'll make a lot of mistakes
with the syntax. You have to do it a
couple of times before it really clicks.
def function name colon indentation it
will get more and more familiar. Okay,
so with that let's dive into the next
section se section called external
tools. So here we are going to get into
one of Python's core tracks where we can
actually use a lot of like out ofthe-box
tools that other people have created for
us to make our life as a programmer much
more fun and way easier. So, we already
talked about packages, right? The pip
install things. In the beginning, I
showed you the request library. So, now
we're going to dive a little bit deeper
into that and show you a little bit more
examples. So, why Python is so awesome
and why I also like back in university.
So, this is 10 years ago when I when it
really start to click for me working
with Python was beyond all of the boring
stuff that they showed in the lectures
like what I show you right now. I know
this is boring, right? Writing a
function, like doing a print statement.
Yeah, but but Dave, what can we do with
it? This is where it gets cool because
now with simple packages that we can
import, we can scrape an entire website.
We can perform data analysis. We can
import AI and machine learning models.
We can use external APIs and we can
automate stuff on our computer. And
building something like that from
scratch as a beginner would be
impossible. But I'm going to show you
how you can import those packages, work
with them, and it's going to be super
fun. So let's first quickly cover some
terminology and then we'll get into the
examples. But in Python there is a big
difference between built-in packages.
These already come with Python. So you
can use them right now without an
installation and external libraries. You
need to install those for example
through pip first. That's a distinction.
Not everything comes uh out of the box.
Now here's some terminology that you
should be aware of. So this will get you
get more familiar with this over time
but you sometimes hear me refer to like
now I'm talking about packages and
before we were uh creating functions
right later we'll get into classes and
you also have what's called modules. So
here's a quick overview in Python as
Python programmers among each other when
we say module we mean a single Python
file. So our hello.py Pi over here. You
could consider this a module. And right
now we like this is all we know, right?
We have only have worked with a single
Python file. But when we go beyond that,
what you actually do in the real world
is you create all kinds of files that
work with each other and that talk with
each other. All of these files you can
call those modules, right? So then a
package is a folder containing multiple
modules. So we'll we'll cover this
later. When we install a package, you'll
find that we can use various bits and
pieces from it. And then within the uh
packages and within the modules, we can
then import the functions. And that is
really where we get to the core
functionality of what we can do with it.
Right? So think of it like this. There's
kind of like a mental model. A module is
like a toolbox or tools in there. A
package is like a garage with multiple
toolboxes. So there's lots of stuff in
here. Then a function is a specific tool
from the toolbox like a hammer or a
screwdriver. You you really need it to
solve a particular problem. And then a
class is like a blueprint for building
tools. I think this is a really good
mental model to keep in mind right now.
Okay. So let's look at an example of
imports. And when you are importing uh
packages or modules you'll start at the
very top of the of the script. So we do
that at the top. That is just how we do
that in Python. And we also need it
before we can use it really. So if I
come in here and I show you I show you
first show you this step by step. There
are comments in here. uh we don't need
those but this is just to explain how it
works. We can do an import statement. So
this is the syntax import and then for
example we do math. So this is a builtin
module that is available within Python.
We don't have to install it. I can
simply run this and it will say look
import math and it will say okay this is
fine we can now use this. So I can for
example now use this module and I can
use a function from this module by
taking math dot and then for example
taking the square root which is sqrt
short for square root and I can plug in
16 and I can run that and now the output
is four. So now instead of having to
come up with like how do I calculate the
square root and how do I need to create
a custom function for that I just use
math and then I just use the square root
function. So that's how that works and
again like I've said you don't need
this. So you it could be as simple as
just import math and then here we do the
square root and now we we can store it
right that's how we can work with it.
But there is another way that's the
pattern over here. We can also import
specific items from a module. And here's
the syntax instead of just starting with
the import. So import math, we do from
math, import, and then the specific
functions that we want to use. So it's
very readable. It reads just like plain
English. That's why it's really nice.
And we can now instead of having to do
math dots square root, you'll see the
squiggly lines because Python will say,
look, I don't know what math is because
we didn't import math. We did from math.
So we looked into the toolbox and we
just took the square root and the pi. We
just took the hammer and the wrench, not
the entire toolbox. So pi python doesn't
know what the toolbox is right now, but
we do know what square root is. And we
can plug in 16 and it works in a similar
way. So this is an introduction to how
you work with imports. And almost every
Python file that you will create will
start with some types of imports. So
this is really important to get used to.
So, Python has some very useful built-in
modules. So, for example, take the
random library and I'm just going to
show you these examples over here. So,
we can import the random module. So,
we'll do the entire thing. Import random
and we can use module functions. So, in
here we can say look, we can do random
and then rand int from random integer is
a function that's available in here and
it takes as arguments a starting a
starting point and an end point. So it
gives a range and then we say look we
want a random number uh between 1 and 10
and now it gives us nine but we can run
this over and over and over again and
you'll see that every time we run this
it will be different. So that's how we
can rate random numbers but we can also
do a random choice. So whenever we can
also have a list and we can say random.
Choice and then in here if we run this
let's see um apple banana orange and
then we get what do we get? All right.
So, if you want to start an online uh
gambling app or casino, you can do that
with Python. You just plug in a bunch of
random integers and random choices and
you can now let people bet on whether
it's going to be an apple, banana, or an
orange. Well, there there's your next uh
project idea that you can start with.
Okay, so now coming back to other
built-in modules. There are plenty and
here are some useful ones. So for
example we have the datetime uh module
the library where we can work with all
kinds of dates. So we can get the date
right now we can get the time we can
calculate what the time will be or the
date will be in a week from now. Uh we
can interact with the operating system.
We can work with JSON data and these are
all libraries like I want to I just want
to show it to you right now. But there
is really a lot to these. So the best
way to like really learn about this. So
look, so now we have import daytime, we
have today, and then we can see today uh
today's date. But knowing
what how to use this. So daytime uh
knowing that you need to do date today,
that's all stuff where for example you
go to chat GPT how what can I do with
the daytime lip in Python? this is
really the way to go because even for me
like under or remembering the very
specifics of oh the daytime library how
do I specifically get the time right now
in a specific format I don't know that
and I don't have to remember that
because AI is there for me so now you
can see look we can get the current date
and time you can also just get the date
and time you can also get specific dates
or time so there's so much stuff that
you can do with it and all kinds of
formatting but that's the beauty of AI
right now you just need to be aware of
oh look whenever I want to do something
in Python with dates we have the daytime
library and if you forget even that you
can just ask chat GBT like what's that
library again that you can do stuff with
dates with in Python it be look it's the
daytime library and that's how you do it
so with this you can like figure out all
kinds of uh calculations uh everything
that you want to do with a date is
possible in here okay and then we have
working with the operating system so
Python can also like do literally do
anything on on your computer. It can can
create files. It can browse files. It
can create folders. It can read files.
We can do all of that stuff. And we'll
we'll see some examples later down the
line. So I can come in here. So that's
the OS from the operating system. So we
import it and we can say look what's the
current uh working directory. So we can
print it. You can say look uh we're
currently in here in my Dropbox in my
Python projects. So this is where we're
currently at. So it understands that.
And we have one to work with JSON data
which we'll cover later. is very common
when you're working with AI. So there's
a lot of stuff in the Python uh
programming language already that we can
use. All right, then quick uh summary on
importing methods. So we can import the
entire module, right? We've seen that we
can import specific functions by doing
from module import and then the function
name. We can also import with an alias.
So let's say you have the pandas library
which we will uh use in the in the next
lessons. you'll find that we're going to
do import pandas as then we import it as
pd. So whenever we want to use pandas,
we don't just have to write it out. We
do pd. And in Python, this is also like
one of those um styling things where for
some libraries, there's just like we all
as developers have agreed agreed like if
you use pandas, you import it as pandas
spd. That's just the way we do it. Do
you have to do it like that? No. Uh but
that's using an alias. So that's also
one more thing that there is. You can
also import everything from a module. So
by using the asterisk, you don't want to
do that. Okay. So then let's get into
installing packages. And we've already
uh seen a sneak preview of this. So now
we need to get back to the terminal
again. Remember what that was, right? So
quick refresher. Let's see. We're in
here. Let's open up the side window
again to uh quickly get zoom out a
little bit. Look where are we? We are in
our Python forai folder and we have this
virtual environment, right? And uh if we
want to open up the terminal, you go
over here, you do new terminal. And
there we are. And now remember, right,
we can use the pip install. So now we
can do pip install. We already installed
the request library. So you can do that
one more time. It will probably say look
this is already satisfied. And we can
for example also let's do a pip install.
Do pandas. Let's do the pandas library.
Well, because we'll use that uh later
on. So this will take some time. We'll
go through everything. So this is how we
install packages in there. And once
that's done, what we can now do, I can
now do an import. So let's do uh import
pandas. And remember pandas, you import
that as an alias, right? Um so we do
import pandas pd. And now let's see if
this works. And there we go. We can now
import the pandas library. But if I try
to import a library that's not installed
yet. So for example, let's do from let's
say from pientic import uh base. Let's
do base model. So if I do it like this
you can see uh there's no module named
paidentic. So this is a very common
issue in the beginning even when you
install pandas for example and you want
to try and import it if you get module
not found errors it's not installed or
it's not installed in the correct
environment. So you may open up a
terminal that where your virtual
environment is not activated. So you
don't see the fan over here. That's one
of the most common beginner mistakes and
it can be super frustrating because then
you're following along and you're typing
everything but then you just need to
select the right Python interpreter and
then make sure when you open up the
terminal when you have the right
environment selected that the virtual
environment is activated. So that's one
of the most common mistakes that you'll
run into. Now when it comes to packages
we can also install specific versions.
So if we for example look up this
request library and do we do request pip
and we go to the Python package index
you can find the whole history of the
request library and you can see that
currently the latest version uh is this
one over here like usually you can go to
the uh to the GitHub page as well. Yeah,
here's the source. So you can literally
see like all this is is just a
collection of Python files that other
people already created and you can see
all of the info here. You can see the
examples. Uh so here you can also say
look this is now the latest version but
if I want to install a specific version
or I want to lock a certain function
because like sometimes things change and
you have an older project where you need
a specific version otherwise you get
errors you can do it like this you do
pip install and you do two equals so
that's uh it's not one it's two and then
if I come in here let's see we open up
another terminal so the request library
is already installed but I can override
this by running this command and now it
will downgrade it. So you will find over
here it successfully uninstalled the the
the version that we had. So it was the
latest version and it successfully
installed an older version in this case.
So that's also something that you can
do. You can also install multiple
packages. So on one line you can do for
example you can install pandas, numpy,
mattplot lip all in once. And let's see
let's come over here. It will totally
work. So we'll run this. As you can see
over here, this is all finishing going
after each other. So that's how you can
install multiple packages. But when
you're working with Python, there's one
more key concept that you'll find in
almost every tutorial that you'll follow
and it's also essential when you are
working together with other people on a
project. And that is the notion of a
requirements.txt
file. So this is a file that we can
store inside our project where that
looks something like this where there is
a specification of all the packages that
we use and also the versions and then
instead of new members manually adding
everything we can run a simple a single
command that will install everything
from that text file and that text file
is going to be the requirements.txt
file. So we can in a similar fashion we
don't also have to create this manually.
we can simply work on our project and
then whenever we want to freeze all of
the requirements, we can hit a pip pip
freeze and then uh push it to
requirements.txt.
So let's see what that looks like in
action. So first uh I'm I want you to
create one from scratch. So what we can
do is we can simply copy
requirements.txt.
We come in here and let's see I am going
to create a new file requirements.txt.
So this is just a text file. We can put
anything in here. And now what we can do
is for example we can come in here. We
can copy this specific section over
here. Come back to our project. And now
we have a requirements.txt file. And now
what we can do is we can use the
following following command. So it's pip
install-r requirements.txt.
So let's copy this. Come back to our
project. So make sure you have your
virtual environment selected. We can
open up a new terminal and let's see
that will open. It will load the
environment. And now if I do this pip
install our requirement.txt,
we run this, it will install everything
that's in here. And now we have
successfully uh added all of that to our
environment. Now if I close out of this
and now I can show you how the pip
freeze works. So let's say we completely
get rid of this and we don't keep track
of that, but we were just working on our
project. We do pip install this, pip
install that, and now we want to put
that into a requirements.txt file. We
can come over here and we can do pip pip
freeze and we can use the arrow sign to
put it in requirements.txt.
So we can copy this. Come over here. We
do pip freeze requirements.xt. And what
you'll find right now is now it's
creating the txt file in here and you
find all of the exact same verses in
there. Now what's also one nice thing
about working with Visual Studio Code.
If you are in here for example and let's
say we get rid of our virtual
environment. So let's say this is a
fresh new project that someone else was
working on and they shared it with you.
So there are requirements in here. What
we can now do we can follow the same
process where we do look we want to
create a virtual environment. We say we
use fence and now we can say we want to
use this python installation. We use the
latest one. And now because there is a
requirement.txt file in our folder it is
asking us like look do you want to
install any dependencies. So we can say
yes and we do okay. And now it's
creating the environment and also
installing the dependencies in one shot.
So this is something like I said very
common whenever you are working on an
external project. Someone is sharing
code via git and github that we will
show uh I will introduce to you later if
you're new to that. This will always be
a starting point because they'll likely
be using packages and you want to make
sure you get all the same versions so
you you will exactly know that the code
will work. All right. So that's a quick
introduction to how you can install
external packages and then also how you
can import them right now. How do you
find packages? Well, right now the best
place to do that really is chatgbt.
There are lots of list and websites. But
right now if you want to solve a problem
with Python, you should really ask like
how can I do this in Python and are
there any packages that can help me with
that? And GBT will literally give you a
list of all the packages that are useful
for that, how to install them, how to
work with them and we'll point you to
the documentation. Okay. Common
mistakes, import errors. make sure that
is installed correctly. Naming conflicts
can also be an issue. So this is also
one of the things you should be aware
of. If you for example import uh daytime
and then you create a variable now the
daytime then the import is then gone.
You override it. Remember right so when
we for example were talking about the
discount set discount to 10 and then you
if you override that it's 20. Similar is
with import. It's just a variable name.
It's just a placeholder for uh Python.
So that's something you should be aware
of. Okay, so we've covered external
tools introduction importing packages
both builtin um and external. Now let's
cover working with APIs. So this is one
of the it's a really common thing when
you are uh working on AI projects and
that's why I want to explain it already
here. it it may already be a little bit
more advanced, but this is just such a
core skill that you need to understand
because when you are building AI systems
and agents, what you are essentially
doing is you're taking information from
some type of input from some type of
system and you want to do something with
that, right? You want to pull data from
a CRM. You want to get order information
from Shopify or Wukcommerse or you want
to send a message through Slack or an
email service or use the models from
OpenAI or Entropic. That's all done
through using APIs. So, let's look at
this. And for this, we're going to use
the requests library which we've already
installed. And I'm going to show you how
to work with a free and open-source API.
This is a weather API. So, let's go
through this uh step by step. And you
can for this you can actually copy paste
it because I don't want you to fully
like type this over and then we are
going to work with this. So, let's see.
Don't save this. Uh let's go. We can
close this out again. Okay, there we go.
Back into things. So from the top, we
import the request library. The quest
request library is a Python library that
we can use to interact with APIs. Now
the uh URL that we have over here is
just something that you need to look up
in this case. So every API that you use
will have instructions. You can look
into the documentation says look here's
the URL and here are parameters that you
can plug in there in order to interact
with this uh API. So you can see this is
api.openo.com
or v1 forecast and then what we need to
plug in is a latitude and a longitude.
So that's how we can get the weather
information from a location. Here we we
know that the latitude and longitude of
Paris like we just set that to this.
This is not something you have to
remember but this is just as an example.
this AI gave this to me. So, we're
assuming this is correct. And then we
have the URL. And look what you can see
right now. Now, we have built up the URL
over here. And you can see that because
we were using frings, you can now see
that the latitude and the longitude are
plugged in. Right? So, now we're
bringing more and more of what we've
learned together. So, this is an F
string. Now, we can use the request
library. So we do request and then we do
get using the URL over here to make an
API call. So let's run that. And you can
see we have a green check mark. Let's
have a look at what's in here. We get a
response that says 200. And in the world
of APIs that means 200 is okay. 200 is
good. And for now you don't have to
understand why. We're just going through
this exercise. Uh so just follow along
with me. And what we can now do is the
response that we got back, we want to
look at the data that's underneath that.
And for that within the request library,
we can call thejson function to have a
look at the data. So now we do that. And
then if we print the data or look at the
data over here, you can see that this is
the information that we got back from
the API. So you can see the input
parameters let long. And then over here,
let's see. You can see like the current
time over here. So this is the time in
uh UTC right now in the UTC time zone.
And you can see that let's see over
here. Make sure we can see everything.
We have the temperature and then the
interval. And we can see it's uh 13.1°
C. The interval here is seconds. So this
is really this is really cool, right? So
now we were able with just a few lines
of Python to extract and capture the
information from what's the weather
right now, right now in Paris. Okay, so
now you can hopefully see that this is
actually starting to get a little bit
useful, right? We can actually do stuff
and let's actually have a look at let's
do this one more time and let's look at
this data object over here that we're
getting back. So if we look at this and
if we look at the type of this data that
we have over here, we can see that this
is a Python dictionary. And this means
that what we can do right now based on
everything that we learned about using a
dictionary, we can also access specific
keys and then the values in there using
the keys. So if in our program right now
for example, we just wanted to get the
temperature. What we could do is we
could either look at the data over here
and we say look we have at the top level
over here you so you need to be aware of
the small indents over here which are
nested dictionaries. You can see that
here we have under current we have a a
nested dictionary and in here is the
temperature. So we can view that like
this. So first of all we can have a look
at all of the keys. So we can indeed see
look there is a current. So you can see
it all the way. Let me move this around
so you can see a current. That is the
last key that is in here. So let's have
a look at what that looks like if we now
take the dictionary and remember
indexing for a dictionary is using the
square brackets and then we plug in the
key. So now if I let's clear this up for
you to show it. Now you can say look
from this data it's it's it's a
dictionary. There's a lot of information
in there but now I just want the current
and the current key has the following
information here. So we have the time,
we have the interval and we have the
temperature. So then what we can do is
we can continue to do that because this
if I look at this, this is still a
dictionary. You can have dictionaries
inside dictionaries. So now we can just
continue the pattern and we can simply
do another lookup with another key. And
now you can see that we have the actual
temperature. So this is now something
that we could actually store. So we can
store the temperature and now we have
captured this in such a way where we can
program programmatically use the
temperature and I know that we're moving
quick here. We're make we're making big
steps because right now instead of like
just focusing on new concepts we're also
bringing in previous concepts that we
already covered, right? And now it's
like oh wait wait wait yes dictionaries
and then oh what was the syntax again?
How do we do that? Uh but that's the
learning process. We make it more and
more complex over time. So here you can
see one more time in the docs uh and at
any time if you feel like oh this is a
little bit too much go back to the docs
there's more info there you can see the
nested structure that's uh that's in
there. Okay so now we can also go one
step further and actually start to
create a function around this to capture
just what we need. So let's have a look
at what's going on over here. So if we
structure it like this, we can now
actually start to build a program where
we say look we import the request
library. We have a function which is
called get weather and we do def get
weather and then we plug in the
parameters. So we have latitude
longitude and then we say look the
response is and then we do exactly the
same that we already did, right? So this
is what we did in the previous step. We
get the data and then we return the data
current and then we take the
temperature. So you see what we're doing
over here. This is where like now this
is where we start to actually like
become a programmer. This is
programming. We figure out some logic.
We know how to solve it and then we we
put it into a function and then we we
return that. And now we can say look we
can get the weather or the temperature
in Paris right now which is 13.2.
We can get the temperature in London
11.8. And we can get the temperature in
Tokyo which is 19.1. And now we can
print all of those. This is cool, right?
So here you can see the beauty of using
functions and why why they are so useful
and how everything comes together and
especially if you can if you combine it
with external uh services and APIs that
other people make available and we
import the request library. Now all of a
sudden we have we can literally build a
weather alarm. So you could build a
program around this for your like
hometown or city. Whenever a temperature
is like either below or above a certain
threshold you can trigger something. So
summary of what we've learned. You send
the request to the API URL with
parameters like the cor the coordinates
that we did. The API processes your
requests and find the data sends it back
to you. You receive that data in a JSON
format which we then load into Python in
a dictionary and then you extract the
specific parts you need. So the pattern
here is always the same when you are
working with external APIs. You connect,
send requests, get data back and then
you use the information from it in
whatever shape or form you need it. And
APIs are really how modern software
systems comm communicate with each
other. So when you're building an AI
system, for example, that needs to
integrate with a ticketing system, you
look up that ticketing systems uh API
documentation, you need to probably get
an API key and then you can write Python
functions around that to capture
customer information or order
information or all of the apps that you
use on your laptop like right now.
Probably like 90% of them have a public
API that you can use. So, for example,
Slack or your favorite to-do app, they
all have APIs where you can extract the
key, understand the uh URL that you need
to connect with and the parameters that
you need to plug in there and then you
can send and receive information from it
into it. All right, so now next we are
going to we're going to the section
working with data and this is going to
be really cool. And here we're going to
ramp things up a little bit. In this
specific section, what I want you uh to
do is I just want you to follow along
and we're going to copy paste the code
snippets. You don't have to like type
all of this out because it's way too
much. And my goal with this section is
to show you what's possible with Python
because I always like I remember when I
was learning Python, the my favorite way
to learn was through reverse
engineering. So I would find the script
online and now with GPT it's even
easier. You literally tell it like,
"Hey, look to this." And then you see
the capabilities and you're like, "Whoa,
this is cool." And then you start to
connect the dots where you're like,
"Hey, if I can do this, I can probably
also do this. What would that look
like?" And then you start to look at the
code and then you start to play with the
the parameters and with the variables.
And then that's how you get a feel for
what you can do with this language. So
what we're going to do right now is
we're going to build on the API that we
just saw. We're going to load u a lot of
data and we're going to manipulate that
and we're going to visualize that. So in
order to follow along with this section,
you need uh the request library, the
pundas library and the mud plot lib
library. I already showed that in a
previous section where I showed you how
you can install multiple libraries. If
you did not run the command at that
point, you should do it now because we
need all of these libraries. Uh request
is to work with APIs, pundas is to work
with data and then the metplot library
is to create visualizations. So we can
create plots. First we're going to what
we're going to do and I what I recommend
is I recommend creating a new file for
this. So let's come in here. Let's clean
it up. And then I do uh command B. I do
new file. Let's call it do get data.py.
So new file. Make sure to give it a.py
extension. And then right now I can
close this one more time because this is
going to be a big script. And I want you
to I want you to save this as well so
you can later reference it. So, first
we're going to get 7 days of uh weather
data. So, I'm going to copy paste this
script. I'm going to plug it in here.
And I just want you to briefly go
through it with me to understand what's
going on over here. So, first at the
top, we start with the imports, right?
The request and then we do the daytime
library. Now you can see right now we
also import uh time delta which is a a
function in here that we can say look if
we have the daytime today which we can
get daytime now we can actually get a
week ago by saying time delta days is 7
and this programmatically understands
that okay look right now is today's date
we need to go 7 days back so let's let's
walk through this and I'm going to zoom
out a little bit so you can see
everything uh side by side let me see I
can go one over here. So I can do uh
daytime now which is going to give us
the exact timing right now like to the
millisecond and then we can do today and
then time delta 7 is a week ago. So you
can see a week ago is now uh visualized
over here. So you can see we can get uh
if you look at the date it goes all the
way back exactly one week. Okay. Now
we're going to format the dates for the
API because the API that we're working
with here the uh Paris AP or the get
weather API for Paris it expects this uh
format. So we need to uh specify the
year the month and then the date. So
what we can do is we can use this method
over here used essentially to conver
convert a day time into a specific
format. And why I said like look just
copy this right now because I understand
this is a lot. We have time delta. We
have this like strive time thingy. We
have all these uh commands over here.
For now we can just say look this gives
us the start date in exactly the way we
need it for the API and also the end
date in exactly the way we need it. So
now we have a date range. And now if you
look in here we can plug this into the
uh function where right now the latitude
and the longitude are hardcoded. So this
is always for Paris. We can change that
right now. And the the dynamic variables
in there are now the start date and the
end date. So let's run this. This is now
our URL. And now we can do the response.
We can get the data. And then we can
print it. And you can see right now,
let's see if I actually do it like this.
Now you can see that we have more data
in here where you can see the that we
have daily data for the past seven days.
And you can see the maximum and the
minimum for those those days. So now
instead of just getting like the current
temperature right now, we have more
information. So that was step number
one. So now let's continue. So now we're
going to load it into pandas. So pandas
is a library that you can use to
manipulate data. You can really think of
it as Excel but for uh for Python.
Pretty much anything that you can do in
Excel plus more you can also do with the
pandas library. And it's it's a really
great library. So I have a background as
a data scientist like for the first like
3 years of my career like pandas is like
pretty much all I did. Pandas mod lip
and then machine learning libraries. But
this pandas was always the starting
point. So what we're going to do right
now is we're actually going to uh turn
this into a nice little script. So what
I'm going to do right now is I'm going
to paste this here. But now what I want
you to do is instead of take doing the
import here, I'm going to put that at
the top. That's just a style thing like
it works like to Python is totally fine
with having the imports in here. But you
should get used to having all the
imports at the top. And now what what
you can do over here is you can even
like do let's say we do a hashtag and
then we do kind of like a big line to
say look first this was the stuff with
the API. Now we're getting to the stuff
that we're doing with pandas. So when we
have the data that's still in memory. So
remember if you close this out and now
you come in here and you will try to do
this it will say data is not defined. So
make sure that whenever you're working
with the interactive session you keep it
open and you make sure you should think
about what what does Python currently
have in memory and if that's not the
case you need to rerun it. So now we
have the data once more and you can also
see how quick this is right this is
super nice if you close out of it now in
1 second we have the last week week's
worth of data again. So now in here in
the data object we now it changed a
little bit. So before we had the current
key right but now that is not in there
because we didn't get the current data
we get the we get a date range. So now
we have the daily key. So we can get the
daily data which is a dictionary in here
which we see right now. So now we have
that information in here and now we're
going to turn that into a data frame.
And this is pandanda's uh way of saying
look if you have an excel spreadsheet
right you have the columns and you have
the rows you can create a nice little
table in pandas that is called a data
frame and again we have specific syntax
for how to do that we use in this case
pd because we imported pandas pd then we
use the data frame and then we need to
specify a specific structure in here for
now don't worry about how this works you
don't need to know that for now all you
need to do is run this. And now what we
can do if we now look into the data
frame, you can see that we actually have
a table in here that's very readable.
And that's one of the things that I like
about the panos library where if I look
at this, this is messy. Even though it's
the exact same information, right? And
computers love this. Computers love JSON
data and dictionaries because it's very
fast. It's very memory efficient. But
like I when I analyze data, this is this
is what I want to see because this is
very clear. I can exactly see just like
in Excel. Okay, awesome. This is
something that I can understand and that
we can now do stuff with. Now, one more
thing that we're going to do is we're
going to turn the actual date and we're
going to change the uh format because
out of the box and when you're working
with uh pandas, this is a very common
operation. the date right now it will be
a string because when you are uh sending
information back and forth via an API
using JSON JSON doesn't care about data
types it's all string based so now we
need to tell Python look this is
actually a uh this is actually a date
because that's going to unlock some of
the features that we can use for
plotting so it understands that uh there
is an order of operations and we can
change the scale so we run that and now
we have a data frame Let's see over here
it is exactly the same but now if we uh
now under the hood the date uh Python
understand that this is a date just
follow along copy executed make sure
that you have the data frame over here
like this and then we get to the next
step so you should see the output now
we're going to visualize it and this is
what we can do with the mattplot lip
library so first let's see I'm going to
copy all of this so you can see there's
a lot of information uh going on over
here over here let's do one more let's
just do a Quick break here to see look
now we're getting into Mattplot lip and
remember we put the imports at the top.
So now you can see yellow squiggly
lines. Python says look I don't know
what plt is. So we come over here we
plug it in here. And this is just one of
those things where mattplot lip and then
you need pipplot. Importing it as plt is
just something we do. It's just
something we all agreed on. I could do
anything here and it would work but plt
is the way we do it. So make sure you
import that as well. So now we import
the met plot lip library and now Python
understands look I can now use that PLT
and this again is a library has a whole
specific set of syntax and instructions
where we where we can create figures. So
we have a figure, we can plot it, we can
do the X label, we can do the Y label,
we can create a title, we can create a
legend. There are almost infinite
possibilities with this library as well.
But let's let's show what we can do over
here. There we go. Now we have created a
figure. So now you can see on the x-axis
over here you can see the dates of last
week. We see the minimum temperature and
the maximum temperature. And we have a
title Paris weather past 7 days
temperature here on uh on the y-axis.
This is now already a report that you
can create. And like I've said in the
beginning of my career working as a data
scientist and data analyst like this was
core part of my work and not necessarily
getting weather from weather data from
Paris but using company information and
more specifically IoT data from sensors
to analyze trends and to create reports.
And actually right now to this day like
still one client that I'm working with
I've I have a task in my inbox right now
where I need to create a report like
this and we have a whole Python uh
codebase that gets the data from from
the IoT sensors from the database and we
have it set up in such such a way where
the researchers get exactly the type of
plots that they're looking for. So now
we are quickly making moves and already
actually getting close to skills that
you can actually pli apply in the real
world and that you can even get paid
for. That's why I wanted to take a quick
leap here without you worrying about
okay look what's everything that's going
on over here but just show you some of
the possibilities and have a look over
here you also have the save fic in here.
Now if I open this up you can see it's
even saved as a PNG. So that's already
in here. So I can now create an email
say hey look client here's the weather
from in Paris from the past seven days
and I can email that. Python can also do
that. Now let's see there's one more
thing. Now we're also going to save it
to a CSV file. Create some separation.
Now we're going to use the OS library.
Put it at the top. Import it. Make sure
to import it. Let's see. Let's close
this. What we can do right now is we can
use the OS library to say look we want
to so we want to store we want to export
our data as a CSV file so we can for
example both send the image and also the
data set and now with the OS library we
say look if this path doesn't exist so
data we make the directory so look at
this in this folder there's no uh data
folder if I run this we now have a data
folder pretty cool right pretty cool and
now we can save it and this is all
possible because of pandas. It makes it
super easy similar to how Excel you can
just like export to CSV. We can take our
data frame which is the one over here
and we can say to CSV. So that's a
method that's available on there and
then we can provide a path data and then
we say we call it Paris uh weather CSV
and then we do that. And now you can see
a confirmation over here because we have
a print statement which says we have the
data saved to Paris weather.csv. So now
let's see let's close out of this. Let's
actually have a look inside the data
folder. Wow, look we have a weather.csv.
We can even reveal that in the finder
and you can actually see that now we
have a file over here that we can share
and that we can work with even in other
tools. So this is pretty cool, right? So
here you can see the complete example.
And like I said, we're making big steps
right here. We're going from isolated
building blocks to literally bringing
everything together. We connected to a
real API. We worked with dates and
times. We processed data with pandas. We
created the visualizations. We handled
files and folders. And we saved the
results. These are literally the skills
of like a junior data analyst. These are
the skills that you need to understand.
And now, as an exercise, here's what I
want you to do. I want you to try and
modify the code to get the weather for
your city. So you can come in here and
find uh where wherever you live and then
update everything in here from like the
latitude, longitude to also uh the
titles over here so it doesn't say
weather in Paris and then you can even
uh change the file name over here. So go
through the entire thing, play with it
and see if you can create the weather
chart and the export for the weather in
your city. Okay, and now it's time to
dive into the next section which is
called practical python. So here, beyond
just showing you isolated code snippets,
I'm going to actually teach you what
it's like to work with Python in the
real world. So we're going to build real
programs with a proper structure. And
you got a sneak peek of that in the
previous section already. But here, what
we're going to do is we're we're going
to set up a project. We're going to
understand Python paths and how they
work with each other. We're going to
work with even more data, and we're
going to organize our code. So, we're
going to split our code into reusable
functions and also across files. This is
really, really important and a lot of
beginner courses and tutorials skip that
and it took me years to like fully
figure this out and to really be
confident with this. So, let's first get
into the project structure. Super
important talking about organizing your
workspace. So, currently we are in this
folder, right? We already created some
some folders in here. We have the
hello.py, PI we have the weather chart
which I'm going to get rid of right now
and we have the get data that we created
but now let's actually start to create
some structure in here and see how all
of that works together. So what I want
you to do is I want you to create a uh a
new folder and we're going to call this
sales analysis. So let's come in here
and let's see we are going to create
sorry not a new file we do a new folder
and we do sales analysis. So this this
can be another like project inside this.
So sales analysis in here. Okay. And
then what you want to do is inside the
sales analysis create these subfolders.
So you do data and you do output. So in
here we do new folder. We have data and
we do another folder. We do output.
Okay. All right. So it should should
look like this. Now what we're going to
do is we're going to create a data file
that we are going to work with. So you
can simply copy paste this this uh over
here and then inside the data folder
we're going to over here we're going to
do new file and I'm going to do sales
and I'm going to specify the uh uh the
file extension CSV. So this will signal
look this is a CSV file and then I'm
going to paste this in here and I am
going to save it. Now you can see over
here that do you want to install uh
rainbow CSV? This is actually something
that I recommend. So let's see over
here. Yes, I trust this. I'm going to
install rainbow CSV. If you if you don't
get the popup, you can also just go to
the extensions over here, right?
Remember these are the building blocks
and then you search for rainbow CSV and
then you click on it and then you click
install because look what this will now
do is it will show us uh different
coloring. So you can see the columns
easily coming back. So now we have a CSV
file in here and then let's see over
here. Now let's understand paths and how
they work. So when we are for example in
the sales analysis folder. So let's say
this is a project you're you're working
on in here and you want to reference the
sales the sales.csv the file that we
just created. We actually need to refer
to it to say look we need data. We need
to go into data folder and then we want
sales. CSV. So it's all like with
respect to where things are live on your
file system. So let's give an example of
that to make that more clear. In the
sales uh analysis folder, we're going to
create a file analyzer.py and we're
going to copy paste and we're going to
plug this in. So this is a way for us to
get a feel for it. So in sales analysis,
we do analyzer.py
and we plug this in. So now when we are
in this file, when we for example want
to access the data or the outputs, we
need to work with specific parts. So
let's give an example of what that looks
like. Let's say we import OS which is
the library we can use to get
information about our operating system
and we then print the current working
directory which is the OS to get current
working directory and we print that we
can see all the way over here that we
are currently in the sales analysis
folder and now we can check let me
actually zoom out a little bit now we
can check whether Python can access our
uh sales.csv CSV file. So we do an if
statement and we say look if the OS path
the path over here exists we print we
found the data. Else we say we did not
find the data. So let's run this and we
can see look it found the data and that
is because from analyzer.py if we want
to get to here we need to say first go
into data then access sales.csv. If I
get rid of this so for example of the
data just just give me sales.csv CSV and
I run this, you'll find that we'll run
into an error. Cannot find it because
this file is looking then into the sales
analysis folder for a file called
sales.csv and that doesn't exist. So
that is how you can start to organize
your projects and create structure. And
a best practice here is keep data
separate. So don't mix code and data
files. Create separate folders like data
and outputs. Use clear names. Data for
input files, outputs for results,
figures for images. Use simple
structures. Keep your main script at
like the top level and go deeper. Also,
don't go to really like other folders
somewhere else on your computer. Keep it
focused and isolated. And then always uh
keep on testing as you go. Right? So,
both using the interactive session which
is great for running line by line, but
then also make sure that when you run
the entire file all at once, you can
also access it. And this is one of the
things where in the beginning uh
remember I reconfigured when we were
installing uh Visual Studio Code we
configured the Python execution. So if
you run into any errors here you need to
come back to the settings. So one more
time for Visual Studio Code settings.
This is the one where you need to do
that otherwise it will look from the
project root and your uh folders and
your paths will become much more complex
and is a lot harder to keep track of
everything. So this is what I recommend.
keep on testing. So both of the methods
work seamlessly. All right. So that was
an introduction to how you can structure
the projects and how you can work with
Python pass. In the handbook over here
in Python pass there is a whole section
with a lot more instructions and working
with Python pass can be a little bit
tricky but it's also too much for now to
really dive into. The only thing that
you should really need to understand
here like the mental model whenever you
want to for example access data you want
to write to or read files is you should
really think about look where am I what
folder is my python script in so for
example here in the analyzer.py I am in
here then if I want to access the data
what you can then do is you could simply
follow the structure by saying you go in
the folder and then you do a you do a
forward slash and then you access the
file if you want to go one level up. So
you are for example in the sales
analysis and you want to access the
Paris weather. What you then do? So let
me actually show you that. So then what
you use is you use two dots. So this
means and then a slash. This means we go
one level up. So let me zoom in a little
bit. So we are in analyzer.py which is
in the sales analysis folder. Let's say
I want to go to this data folder. Now I
go one level up. I go into data and now
the sales CSV is not there but the Paris
weather CSV is there. So now if I check
this, this should also work. Look, now
we can access data that's outside of the
folder. So if we go in, we simply use
the folder names or the file names and
we use forward slashes. If we want to go
up, uh, we do it with the two dots. And
this this can stack. You can change
this. So you can also like if you have a
really big project, you can also
literally go three folder levels up. Uh
it works exactly the same way. So this
is just also one of those things where
you just need to get started. You need
to just start working on stuff and then
you run into issues where you're in a
file like I cannot access this. I'm
getting errors. Python is throwing me
errors. Then this Python pause section
will give you like simple rules to
follow, how you can check, how you can
debug, how you can find your files and
give you a few like differences as to
how you should go about it. So make sure
to bookmark this uh handbook and also
use chatgpt. Then we get into working
with files. So yeah, I want to spend
some time here because this is also
super important for when you are uh
going to work on AI projects because AI
is always about processing some incoming
data and we want to do something with
that to usually automate something and
there data comes in a lot of shape way
shapes and and forms. So we can have for
example CSV files, JSON files, XML
files, te text files or even paret files
which are efficient ways for for storing
lots of information. And the good news
is Python has like excellent libraries
for all of these. Um, that was so nice
also when I started out as a data
scientist. Whatever like kind of like
data set people would throw at me
whether it was Excel, CSV, a parket or
like a SQL database like within a couple
of minutes you could tinker with a
library and you could make a connection.
So now let's start to play a little bit
with our sales data. So again make sure
you have pondas installed. You should
already have this uh already. Now we are
going to adjust our analyzer.py and
we're going to copy and paste this chunk
in there again just to get a feel for
what we can do with this not to fully
understand everything line by line. So
if we come in here and let's see I'm
going to close this out. We can first of
all read the CSV file. So this is the
CSV file that we could just that we just
created right. So we now have it in a
pandas data frame over here. Now we can
also look at the shape which is like how
many rows and columns do we have? So we
can print that. Those are common
operations and we can also add more
columns to it. So we can calculate the
total. So let's have a look at this. So
you can see we have quantity and price.
And now if we run this. So we do we take
the data frame and we take the quantity
column over here which we can do through
indexing similar how we would do with a
dictionary. That's how pandas works. We
take the quantity and we take the price
and we multiply that and we store that
into a new column. Now when we look at
the data set or the data frame, you can
see that we have a total value in here
that we can also uh check. So this is
how we can now start to use pandas as
pretty much as Excel. We can now also
create an output directory if it's not
already there yet. So we already created
that, right? So let me actually get rid
of this. move to trash and then we
create the directory. So now we have
that one more time and now we can save
it in multiple ways. So you can see
before I show you how you can export it
to a CSV file, right? Well, we can also
output it to JSON. There we go. We now
have a JSON file. Very convenient. We
can output it to Excel. So now you will
get an error where it says look we need
let's see we need open py excel. So let
me actually show you that full error.
This is now this is also very common
when you're working with Python and it
says something like module not found and
in this case we need open pi excel in
the background as well. So what we can
do is we can open up our terminal again
and you know how to do this right now.
Whenever you need something in python
you just do pip install open pi excel
and now it will get that into our
virtual environment. We can close out of
this and now let me clear this up. I
think we should already be good to go.
So now do to excel. I don't think even
we have to run uh all of the other parts
again. Look, now we have an Excel file.
Like literally like this is an Excel
file that we can just like open in
Microsoft Excel. So you can see there we
go. We created an Excel file. Now let's
see. We can also do a CSV file. And now
you have the totals in here. And you can
see all of the output parts created over
here. And it prints it. So this is now
also a little automator script that we
can have. So let's see if I delete this
file over here and completely get rid of
the output. Instead of going through it
uh line by line with the interactive
session, I can also just hit the play
button or on my keyboard do uh command
enter and now it will run in the
terminal. You can see boom all
everything it created the folder, it
created the JSON file, excel file and
the CSV file. Now this is going to set
the stage for pretty powerful
automations, right? You can also already
start to think about some data analysis,
some data that you get via an API and
just every time that you want to run
this just like run this script or later
you can also schedule this so it becomes
fully automated and now you have all of
these files here. So let's say you want
to send like weekly sales reports to uh
to your sales manager or something like
that. You can create a simple script do
the calculations and send it to it send
it to him in an Excel file because
that's what he understands is he's not a
programmer. all ways that you can work
with the Python programming language and
similar to exporting the files with
pandas we can also like read in these
files again so within pandas if you want
to read in a specific CSV file so let's
say you have a data folder and in there
is a file called file CSV you can do
read CSV or we can read a JSON or we can
read excel and all of these functions
also come with their own set of
parameters where you can tweak it you
can there are literally like 15 to 20
types of arguments that you can plug
plug in there to skip rows to convert
certain data formats to have specific en
encoding files all possible really the
sky is the limit there. So just so you
know you can export you can take data
you can read all kinds of formats you
can manipulate it and then you can also
export it again in all types of formats
to do whatever really you want. If you
want to learn more about this like
pandas uh just a documentation great
resource the JSON module reading files
and also I keep saying it chat GPT plug
it in. What can I do with pandas? Give
me an example. I want to read XML data.
How do I do this in Python? I want to
work with an Excel spreadsheet in
Python. How do I do this? All possible.
Okay, let's get to the last component of
the practical Python session is
organizing code. This is a big one. It
took me it took me way too long to like
to properly figure out how to do this
because it's not that hard but no one
like just showed me how to do this uh
professionally really because when you
are starting out as a beginner when you
are working with Python files it's
almost always that you have an idea I
want to do something and you start in
one single file right so here we have
the analyzer.py Pi this is we work on
something and oh here this is something
completely else we we work with data but
in the real world in real projects you
want to make your code modular. So you
want to have tiny files ideally that
have some specific logic and then
whenever you want to use that logic you
import it into another file. You can do
that and I'm going to show you how you
can do that right now. It's similar to
the imports actually that we use for the
packages. So what does that look like
and what are the benefits? Well, your
code becomes reusable. becomes way more
readable because instead of having
thousand lines in one big file which is
called maincript.pby where everything is
you can now have calculations I don't
know user info export data visualize
data you have all of these files within
your project that now have a descriptive
name that are short that you can easily
read and understand. So let's create a
uh let's start by creating a helper
function in our sales analysis folder.
We are going to create a helpers.py
folder and we're going to plug in these
functions. So let's actually do that.
Come over here. So sales analysis new
file. We do helpers.py
and we plug this in. We can get rid of
this. So now you can by now you should
already be familiar with the concept of
functions. And you should be able to
read this and understand what it's
doing, right? So calculated total it's a
function. It takes the quantity and the
price as input parameters and then it
returns the quantity times the price. So
it calculates the total. Here you can
see we have a multi-line dock string
that we have in here or it's it is in
triple quotes but you could also do
multiple lines in here to give a
description of what this function does.
And then we're going to format the
currency. So, we're going to take any
number which is the input parameter, the
amount, and we're going to format it in
a string where we say, look, we're going
to put a dollar sign before it. It's an
F string, so we can put in the amount.
And then this is Python's way of saying
we just want two decimals. Also syntax,
something you just need to see. We can
also change this to one, to five, or to
two. So now we've created helper
functions. So let's come back to what we
can do with this. So now we can actually
use these functions. So now if we come
over to our analyzer.py and let's see
we're going to I'm going to copy paste
this. So we can just do do it line by
line here together. Analyzer get rid of
everything that's in here and we're
going to make this a lot simpler and
we're going to paste this in. Look what
we're doing right now. So now we import
pandas spd. This is we know that we can
do this right because we installed
pandas in our virtual environment. So
now pandas so now python knows I can
import this. But look, here's the cool
thing. We have now created our own
module called helpers. Remember modules
is a single single file and from within
there we can import functions. So we can
say from helpers we import calculate
total and format currency which are the
exact names of the functions. So they
need to be exact otherwise you get
import errors. So now if I run this you
see we don't have any uh import errors
right now. We can import import these
functions and it it needs to be exact.
And now we can go ahead and we can read
the CSV data. And now we can calculate
the total for each row by creating a for
loop over the data frame. Again, don't
worry about the syntax what we're doing
over here. But we're going to create the
totals. And now the totals you can see
is a list of numbers in here. And that's
now very easy because we have our
helpers functions. And what this
essentially does is say look we have go
over all the rows in a data frame and
then take the quantity take the price
and and put those into the calculate
total function. So that's how we can
work back and forth between different
files. And in a similar format uh we can
also format the currency in here. So if
we then do everything now all of a
sudden we have a sales report. You can
see we have all all of the uh we have
all of the totals for all of the
categories. It's formatted using the
dollar sign and it has two decimals over
here. So now instead of having
everything in one file, we make things
modular. And now your helpers functions
could you could have like plenty of
functions in there and they could be
helpful not only in the analyzer.py
file, but maybe you have another script,
another analysis where you need to do
the same uh the same operations. All
right, let's cover what we've learned in
this practical Python section because
we're making big steps right now. This
is how you use Python in the real world.
You now know how to organize and
structure your project. Uh you know how
Python finds files, right? How you can
do that whether it's data files or
whether it's Python files. You have
created code that reads real data and
saves real results. And you also created
reusable functions in separate files.
This is how real Python works. All
right. And with that we have come to a
section on error handling. Also
something really important because as a
programmer you are going to make errors
and even if you don't make errors it can
also just happen that your program your
code might be totally fine but just
because of the data that's flowing
through things or data that's either not
available or available within an API it
your program will respond differently
and then it will crash. So there are
ways to go about this and to handle
this. So when it comes to error
handling, there are different types of
errors that you make as a as a
programmer and also errors that are
beyond your control. So you as the
programmer can for example have typos
and syntax errors, right? Forgetting a
comma or using a a dot instead of a
comma or forgetting a cola colon. You
can also have logic errors. So for
example, dividing by zero uh that really
only shows up when your code runs or
using a variable before it's defined or
using the making a mistake with local or
global variables or calling methods that
don't exist. That's all stuff that you
as a programmer can do. But beyond that
when for example you are interacting
with a system and a user for example
inputs ABC when it needs to be a number.
So for example like what's your age and
that's ABC it cannot do a comparison on
that or a file gets deleted if you after
your program starts or an API that
you're using is suddenly offline or
changed. So you should know you should
be aware of these types of different
errors. Now here are then some examples
of what can happen in the real world. So
a syntax error right? So for this I'm
going to come back to my hello.py. Let's
see hello.py to give some examples. So
here you can see a syntax error. Look,
we we have an if statement and we are
missing the colon. So right now Python
will say look we expected the colon in
here. So we can put that in here. But
now also it says look the x is not
defined. So that's another error that we
have in here. So now if we say x equals
2 now we're all good and we have fixed
the program. In this case you also have
runtime errors. So look at this. So for
example this we don't see any error. So
we have 10 and we divide it by zero.
This is not something we can do uh also
not in Python. So now when we run this,
this is a runtime error because like you
will not see it until you run it. That's
why it's called runtime division by
zero. So that's another error. You can
also for example have a variable that
doesn't exist or a type error. So let's
look at this. So over here we have print
score. Python is like dude I don't know
what score is. I can help you with that.
And then you can also for example have a
type error. So when you take uh a string
and in this case an integer and we try
to uh subtract or add the two it says it
can only connect concatenate strings
with other strings. So, we've seen that
it's totally fine to do 5 + 5 or in this
case hello plus I don't know hi like
this is all totally fine we can do that
but when we mix data types then all of a
sudden Python says ho I'm going to stop
you there this is not how we do things
so those are all different types of
errors that you can get into and here is
another like common list where you can
go through I won't cover all of these
right Now, but what I'm going to show
you is the try and accept blocks in
Python because this is a very common way
to make your programs more robust. And
with try and accept, we can we can say
to Python, look, go and try this. And if
it fails, don't worry about it. Just do
this. Because if we don't use try and
accept, our program will stop if it hits
an error. The examples that I just gave
you, if you, for example, let's do the
uh the deficient over here. Let's say if
we now do uh we do print hi there. If I
run this, you won't see the print
because it will never get there. And it
doesn't matter if I do it in the
interactive window or I do it in the
terminal, it will just say look, we
cannot continue here. So the rest of our
program will also not be executed. So
let's say you're building a data
pipeline and you're getting information
from let's say five systems. You're
generating the reports. First you you
look into system one. You perform the
calculations. Once you create the
report, they go to system two. That's
your whole script. If now there's an
issue with system one and the API there,
system 3 4 5 6 and everything after that
will also fail. That's where try and
accept comes into play. And how it works
is like this. We do try, then we do a
colon, then we do an enter, and then we
get to the new line, and we have the
indent, similar to an if statement. And
now you can still see there's a red
squiggly line in here because it says a
try statement must have at least one
except or a finally clause. So um other
than like an if statement that you can
also use isolated with a try we need to
have an except clause in here as well.
And then we do it like this. So first we
say look try to divide this by zero.
This will fail but we will still print
hide there. And we can also do it like
this and it will work. So that's a basic
try and accept structure and that will
already get you a long way. Now you can
also capture multiple errors. So
depending on whether it's a file not
found error, a value error, a serial
division error, but in the beginning
just focus on when you need to have some
type of error handling, starting with a
simple try and accept will really get
you a long way. This is already more
advanced. We can also add else clauses
to it and even a finally clause which
will always run. All right, and that
brings us to the last section of the
building program sections, and that is
classes. And now, this is going to be
the most advanced topic that we're going
to cover in this Python course. And at
this point, it will probably still be a
little bit tricky. So, don't worry if at
any point during my explanation of what
classes are, how to use them, and even
what inheritance is when you feel like,
oh, this is going a little bit too fast,
and I don't fully get it. That's also
not the full goal right now. is just to
get exposure to it so that you are
familiar with the concept you've seen a
couple of examples so that later down
the line in your own learning journey
whenever in another tutorial or another
course that you're following when people
mention classes you at least know oh
yeah this was this thing that Dave was
talking about that was a little bit
tricky a little bit more advanced but I
know conceptually what it is and I can
also now use something like JBT to help
me get a better understanding of what it
is and fill in my knowledge gaps and
what we're going to do right now is I'm
going to introduce you to objectoriented
programming which is really just a way
to organize code by grouping related
data and functions together. So one of
the most important things to understand
about classes is that everything that
we've done up until this point you can
totally do that without classes and you
can solve almost any programming
problems without classes but they are
very useful but most specifically from
an organization point of view. So you
can actually create really clean code
files by structuring classes in a good
way and putting functions in there. And
that is what I will show you in this
section. But also just know that for
example for the first like 3 to four
years when I was working with the Python
programming language, I didn't really
use classes at all. So if you think
about organizing your code like as a
toolbox, if it's without objectoriented
programming, tools are scattered
everywhere. So, think of your project as
like a big toolbox. And here you have a
hammer, there you have a screwdriver,
and there you have some some nails. It's
all in there. You have all the tools.
And tools really here are functions. And
whenever you need something, you just
look into the toolbox. You're like,
"Okay, let's grab the hammer." And then
you can start to work with the hammer,
right? But when you are working uh with
OOP, object-oriented programming, tools
are organized in labeled compartments.
So you have the toolbox, but within the
toolbox you have specific sections. So
you know that when you need the hammer
that is over there. So you don't have to
look, you don't have to grab in there.
You can just grab the hammer. That's
probably the best reference or mental
model that I can give you about using
classes. Can you still like grab the
hammer and and jag in the nail without
without the the organization? Yes. But
it's easier if you need to grab it more
often. Okay. So what is a gloss? First
of all, a class is a blueprint for what
we call creating objects in Python.
Objects is just a place where we can
store all kinds of information under and
it defines attributes. So what kind of
data we store in there and methods which
are functions. So what the class can do.
Now when you're learning classes right
now, one of the most tricky things again
is syntax and methodology. You have
these things like attributes which is
data, methods which are the functions.
And then if I go over here, here you
have the class definition. And similar
to how I introduced you to functions,
right, where we need function name,
parenthesis arguments colon classes
also have a peculiar way of like a
Python specific way of doing things. And
it's just one of those things. Is it
logic? I don't know. This is just how
they came up with it. There are some
things to it where it's like it's a
little bit weird, but once you get used
to it, that's just the way you do it. So
you'll see things like these underscore
inits. You'll see this notion of self.
These are all things that I will explain
to you and you just need to be aware of
I need to remember this. This is the
syntax. So why use classes? So classes
can help you write more understandable
programs specifically as they grow as
they grow. So here's the typical
progression as a Python developer. This
is literally what I I went through and I
want to I want to shortcut this for you.
So we all start with single files. So
that's what we've been working on,
right? So I want to do something. I want
to get some data. So okay, this is what
I do. And then at the end I have my
report and I export my CSV one file.
That's where everyone starts because
it's the easiest. And then you get into
functions. This is where you are right
now. So remember we created the helper
functions, right? So now instead of
doing these, so so actually this example
was already Oh, sorry this what the
helper function and the analyzer. So now
instead of having everything in one
file, you can see this is now already a
little bit shorter because this contains
the uh functions that we can import. So
that's level number two. And we have
level level number three. That is where
you get organized with using multiple
files. So what I just showed you and
then in classes level four, that's where
we'll get to right now where we're going
to wrap certain functions even we're
going to nest that inside classes one
level deeper. Now, in the context of AI
that you want to probably be working
towards, you can build really clean
interfaces, manage complex data
pipelines, and create your reusable
components and organize stateful
operations all using classes. So, let's
dive into building your first class.
Let's actually come over here. We're
going to hello.py. And now we're going
to use our hello.py to create a class.
So, to create a class, the syntax is
first we type out clause. So, with the
function, it's defaf. Now, it's clause.
Let's say we want to create a class for
a dog. So let's say we're creating a
game and in the game we have a dog and a
dog can do stuff and it has some
attributes. So we do that and then we do
a colon over here and then we get to the
next line. This is the starting point
and then we need to add an initializer.
And this is just one of those things.
It's a it's a Python function and in a
class we call it a method but everything
is exactly the same. This is just one of
those things where whenever I explain it
to new students, it's like look, you
just need to see it a couple of times.
This is just how we do things in Python.
Does it look a little bit weird? Yes, it
does. But just follow along with me. So,
if I take just this snippet, let me show
you what what this looks like. So, we go
inside the class and then we have the
function the init and then we always
need to put the self in here and then we
can plug in parameters there as well. So
for example every dog when when we want
to have a dog object it should have a
name and it should have a breed and
similar if I for example want to create
a let's say a class we want to do with
cat I can do I would do the same thing I
would do dev I do underscore in it in it
and then I would plug it in a self and
let's say uh a cat has a name and in
this case I want to do a caller and then
we set those attributes on the class
itself by setting that equal to what we
want to plug in there. So, self.name and
self doc color is color. And then here
you can see there's an error because I
forgot the colon. Okay. So, this is how
you create classes fundamentally. But
now let's understand what what they're
doing because now you might be like,
yeah, okay, but what is this and what
can we do with it? So, now we have a dog
and a cat. And similar to how when we're
working with functions, we first create
the function and then we use the
function. With classes, you need to
initialize them. So remember classes are
objects and objects need to be created
and this is a blueprint for creating a
dog and within our program within our
application let's say this is a game we
can have multiple dogs. So we can for
example get I don't know Jerry and Jerry
is a dog and then in there we give him a
name and the name is Jerry and the breed
is let's say Jerry is a Labrador and now
we have Jerry over here which is the
claw's dog and what we can now do and
now we can for example take Jerry and we
can say what's Jerry's name and you'll
find that it's Jerry and what's the
breed it's a Labrador so you can see
that Now by creating a class creating
that blueprint we have now created an
object and that object is an instance of
the class dog and right now that class
has attributes. It has a name and it has
a breed and similar to how we need to be
specific about what we put in there with
functions. If I now for example try to
create another dog. So let's say we have
a dog and we call that dog Tim and we
don't specify the breed. we'll get an
error because it says look it's missing
the breed. So this now all everything
that we learned from functions is still
does still apply here. So we can also
for example put a default value in here
of none of unknown for example and now
when I create a dog now we can create a
we can create a dog with just the name
Tim. So that's how all of these things
that we've learned so far stack on top
of each other. So let's go a little bit
deeper to understand more about uh what
we can do with classes. So here you can
see I walked through this example. We
have the initializer. We can then create
doc objects using positional arguments
or with named arguments to be specific.
And then we can access the the
attributes and then here like this is
what it comes out. Yes in it looks weird
with those double underscores. This is
in Python this is actually called a
dunder method. you have like you in the
beginning you won't encounter it that
much but there are other places in
Python where you use this as well double
underscores it's just how Python works
you'll need to type it exactly like this
don't worry after writing it a few times
it becomes second nature like okay
clause defaf in it and then we do the
self in there it's just it's just Python
special's way of saying look this is a
setup method right so now let's
understand the other weird thing in
there the self thingy because what's
that why do we need to have self like
three times in here in order to just
create a class. So self refers to the
current object. It's how an object keeps
track of its own data. So remember
classes are blueprints and we can have
multiple dogs. And because of this, this
is just Python's way to say look when we
are working with Tim in this case
self.name is just stored like look this
is on the instance itself. This is Tim.
This is not the class level. That's why
we need to plug self in there. That's
how Python works under the hood. So you
can see over here if we have the class
dog and we just have the name in there
self.name belongs to this specific dog.
So you can create buddy it's dog one and
you can also have dog two call it max
and then you have the different names in
here. So you can try this out for
yourself. Come in here and then you can
see we have m we have buddy and we have
max. So that's kind of like a a mental
model that you can use to remember why
is why we have the self in here because
we can have multiple instances. Okay,
let's continue to a real world example
because also I remember when I was
learning classes and you go through
examples like this. You're like it's
very hard to like make the connection
between okay nice that I can give my dog
a name and a breed but like what like we
want to build AI programs, right? What
what does it have to do with dogs? So
here we go. In the real world when you
are working with classes, a very common
use case is to for example build an
entire class around an API config. So
when you're for example working with
open AAI you can say look I want to
store everything that we have. So the
API key I want to store the model the
max tokens the base URL I want to store
that in a class. So let's actually come
over here and just get rid of everything
and plug this in. So now we can see we
have a clause and we call it API config.
So now we're getting a little bit more
serious. So this is not just a dog. And
now we have that same defaf in it and a
self key in here but now we say look we
need an API key. We want to have a
model. We want to have max tokens. And
again, don't worry about what this all
is. This is just a way of look. We can
now store information in here. So if we
now have this class config and we have
that over here, we can now for example
say look, we can now create a dev
configuration where we now have this
class over here. And then if we want to
have a production configuration with
another production key, another model
and other settings, we now have that.
And I can now access all of this
information by looking over here at the
dev config. So in the dev uh config, we
have GPT3.5 turbo which really cheap,
it's fast, we can use to test it. But in
in the production version of our app, so
in the real world, I want to use a
beefier model. So I want to use that. So
now you can start to imagine like coming
back to the example of the toolbox.
Right? Now I have a very clean container
called dev config which is an instance
of the class API config. And in there I
have specific settings. And if I want to
have something else, I just create a new
instance of this, a new instance of that
class. And now within my code, I can
just do oh this is proconfig. Oh, this
is that dev config. And I can access the
attributes from from there. Remember
attributes are the data the data that
you store on there. So hopefully by now
you already start to get a little bit of
a better understanding to where classes
can fit in. And also you don't need
them. you could perfectly like write
everything out in variables and in
functions and you would still be able to
get the uh production configuration and
the dev configurations. This is just a
cleaner way a cleaner interface of doing
it using objectoriented programming. And
one important thing to keep in mind here
is the difference versus a class and the
instance because this can get tricky
when you're working with it. So the
class is the the specification the
blueprint itself and then the instances
are for example the dev config and the
pro proconfig that you have over here.
So a common mistake is that you for
example have this and let's say you want
to uh you want to use this and you do
API config and you want to have the
model and you want to do it like this
and it's like look there is no model in
here. I was like, wait what? But I I
plugged the model in here, right? So I
have the API config and I have the
model. But this is the class. This is
the class blueprint and you need to
create an instance of that. Let's see.
Let's get back. So you first need to
create the instance of that. So that is
the dev config. So this is now the
instance. And now the def config has the
model. So that's how that works. And now
working with classes in the beginning
can be tricky. So common mistakes
forgetting the init. So not having that
in there. uh not using the self,
forgetting parentheses, and modifying uh
the clause instead of the instance.
Those are all things that you may run
into. So those that was the introduction
to classes. So really starting with the
blueprint and then the attributes in
there. So that's what we covered.
Attributes are there to store data. But
now class you can also add methods to uh
methods to to classes and methods are
simply functions. Again this is
methodology. In clauses, we call it
methods instead of function. That's just
the way we do it. And they always have
the self as the first parameter. So this
is also something that's in there. And
you can pretty much do anything with
these functions in the in these methods
inside a class. But you can also do
outside of a class. So let's have a look
at an example over here where we have a
data validator. So now this is going to
get pretty pretty cool. Again, it's a
lot of code because I want to show you
like I want to teach you this by reverse
engineering this because typing all of
this out right now is is still a lot. So
you can see we have a data validator
class over here and we do a very simple
init. It only inits itself with errors
and there's a list. So in the beginning
there's nothing in there. So let's see
whenever I start this up I have my data
class and I create the instance of the
class. Now we have a data validator and
I can check inside this validator what
it has. It has the errors which is
currently an empty list but now instead
of the attributes that we have in in
here we also have methods. So you can
see with this data validator we can
validate an email we can validate an age
and we can get the errors. So here is
specific logic to see look how do we
validate an email? Well, if the ad
symbol is not in an email, we can say
look, there is an error and we append it
to our list and we return true or false.
And in a similar way, we can validate an
age to say look age needs to be between
needs to be lower than zero uh it needs
to be between zero and 150 and we check
that. So what we can now do is we can
use our data validator and then we can
call a method. So we use validator and
then what we can do is we do validate
email which is the method in here and
that takes email as a parameter and we
say look this is a bad email. So what we
can now do you'll see that this is
returning false because it got in here
if it's not in email we say look this is
false and we can also validate the h. So
this is now also false or using
positional arguments where we do so this
is here we say specifically we do email
is age is we can also do it similar to
how we saw with functions right uh where
we don't specify the email or the h but
I often say look you don't want to do
this and then in here we can print the
errors so now every time we run this and
we validate the data and an error
happens it goes into the if statement
and it uses the errors and it appends
that to the list that is a lot of
information going on over here I'm fully
aware of that. But it just goes to show
that we can use our data validator and
we can run it and then we can perform
certain actions. And every time I run
this, okay, I can say this is another
bad email, another bad email. And I run
this a couple of times. And now when I
print the errors, you can see that this
list is now getting very long because
it's storing all of that information.
And now you can see that this data
validator on its own becomes a really
powerful this validator becomes a really
powerful object because it can save
information. We can send new information
to it and we can build entire data
pipelines by just spec specifying
functions over here and then calling the
methods later down the line. So that was
showing a pretty extensive example. And
first I want to show you like look here
is what the end could look like and now
let's do a little bit of a simple
example where we say look let's get back
to the dog and we do the defaf init and
this is also good for you as an exercise
to follow along. So we init we remember
we had selfname let's just give it a
name uh we say is the name and then we
need to plug that in as a argument as
well and then also don't forget the
self. So you see also as I'm doing this,
it's it's one of those things. I know
the syntax. I know what a clause looks
like. But like even as you go through
this, you're like usually you forget
about these things because you think
about the about the logic, right? Oh, I
want my dog to have a name. But luckily
that's where our uh editor comes in and
says like look remember you need the
self in there. And now we can have a dog
and let's say we have uh let's let's
have Jerry again. So we have Jerry. So
we create the instance of the dog name
is Jerry. So there's Jerry. We now know
that Jerry has a name and we can print
that and it's correct Jerry. But now
let's give Jerry um a method. So Jerry
can also do something. So now what we
can do is we can come in here and we say
look we also want a method and we say
bark and we just plug in self and then
let's see we do like this and then we do
print and then we do bark. So now if we
do this and we initialize Jerry one more
time Jerry still has a name but look
Jerry can now do stuff. Jerry can bark.
So now Jerry can bark. All right so that
is how you can add behavior to classes.
So remember attributes for storing data
and then methods adding behaviors that
you can add to this as well. Now then
the next section over here is class
inheritance. Now here I want to like
briefly touched upon this because this
is definitely a more advanced uh an
advanced thing in Python. But whenever
we have a class we can use the concept
of inheritance to take a class and to
add more functionality to it. So let's
have a look over here. So let's say we
start with a basic clause of an animal.
So let's come in here. So we have a
clause. We have an animal. An animal has
a name and an animal can eat and sleep.
That's the that's the default class in
here. Now what we can do is we can
create a child class to create a
specific animal. So this is now the
parent clause and now we create the
child class. So let me actually copy the
whole example over here. It will make
more sense. So in the beginning you're
probably wondering like why would I use
this? But with this you can make your
code even more modular. So let's say you
have an animal and now here over here
you have the clause and you specify the
dog similar to how you normally would
specify the dog class that we just saw
but you put animal in here. And what
this tells Python is it says look take
the animal class take everything that is
in here but also add these specific
functions. So if we have this here in
memory, we can create a dog buddy and
now we have the dog and we can add
create dog number two and that is Max.
And now you can see without specifying
this at the at the dog level uh buddy
and both uh Max can also eat. So they
are eating, they are sleeping, but they
can also bark. So that's class
inheritance and that is really when you
get to the level where your codebase
grows and you have more and more uh
modular bits and pieces and you want to
extend the capability in there and
that's all that I want you to know for
now. So just the concept of inheritance
let you create a new class based on
existing ones. You have basic examples
you can add attributes in the child
classes that we saw right we added the
bark to the animal class. You can also
override methods. So you have the base
class which already might have a method
like make sound but then for a cat you
really want to change that you can
override things. So to summarize this
section when to use classes and this is
all about the programming paradigms. So
functional programming is all about
using functions to transform data.
Object-oriented programming is using
classes to bundle data and behavior. So
here's the recap for now. So both
approaches achieve the same result.
Classes don't unlock new features in
Python. They're just a different way to
organize your code. Choose based on what
makes your code cleaner and also uh the
level that you're confident with when
you're writing and working with your
Python files. As a general rule of
thumb, you want to use classes when you
want to keep track of a state between
operations. So you want to save data, h
group related data and functions
together for organization, create
multiple instances with similar behavior
or model real world objects or concepts.
And you can use functions when it's
about simple transformations, input,
output, stateless operations, so it
doesn't need to remember anything.
one-off calculations or small scripts.
And I recommend just start with
functions. They're simpler often and
often sufficient. And only you add
classes when you find yourself passing
the same data between multiple functions
when you need to maintain information
and a state. All right, and that wraps
up the section on building programs. So
in this section, we really accelerated,
right? We went from getting started the
Python basics, the fundamental building
blocks to now putting some scripts and
programs together where you can actually
see how everything starts to working
together and we even got into more
advanced concepts like using classes.
Now the final chapter of this course is
all about common tools that you'll work
with on a day-to-day basis when you are
working with the Python programming
languages. So instead of like going
deeper on Python specific syntax and
showing you more examples there, I'm
going to give you really like the full
toolkit that you need to go out in the
real world, build things, follow
tutorials, and you'll literally be
prepared for everything. So that also in
the follow-up recommendation courses
that I have for you on either like the
data science track or building agents,
like you can exactly follow along.
That's really the goal. So let's dive
into the tools. All right, so let's dive
into the final chapter of this full
Python beginner course. And here we are
going to cover some tools that you need
when you are working in the day-to-day
with the Python programming language. So
I can guarantee you when you go out in
the world, you'll follow tutorials,
you'll thank me for this because beyond
the Python basics, building programs,
everything that there is to the
language, these are also really things
you need to understand and they are the
following. So we are going to cover git
and github. Now these are also things
like if you're already familiar with
them feel free to skip for all of these
but you definitely need to understand
them. So if you're completely new to
this make sure to watch everything and
understand it. We're all I'm also going
to show you how to work with environment
variables. What they are how to use them
and then I'm going to introduce you to a
modern Python tool that can replace pip.
You remember the thing that we used to
install packages? and I'm going to
introduce you to UVI which is really
awesome and it will make our life as
Python programmers a lot easier. So
let's dive in and we are going to start
with Git and GitHub an absolute
must-have when you start your
programming journey. So for the very
basics if you have never heard of git
what it is it's like a safe uh system
for your code. It can track all the
changes that you make and let you go
back to previous versions. So that when
you are for example working over here,
you create a snapshot and then when the
next day you continue and you all of a
sudden realize that you made a mistake
and you want to go back. That's all
possible through Git. Because if we're
here on our file system, we can just
save the file, right? But that saves the
snapshot right now and it's not really
easy to go back or to work on different
versions or even to work with a team. So
that's a problem that git uh solves. It
allows us to go pre to go back to
previous versions and also collaborate
with others. You can really think of it
like Google Drive or Dropbox with
version control. So why you need it?
save your progress, experiment with
things knowing that you can always go
back, share your projects with others,
and also to use other AI tools, codes,
and repositories from other people
because whenever someone shares code
online in a tutorial, 99% of the time
they'll share it via GitHub. And knowing
how to use git and then also what GitHub
is will make all the difference. So,
what we are going to cover is we're
going to cover the fundamentals of git
and then also I'm going to show you how
you can set up GitHub. So, there's a
difference between the two. That's also
something you should be aware of. So
we'll go over a basic setup all very
quick. This is by no means like a full
dedicated course on how to use GIP. But
the thing is in the beginning you only
really need to understand the
fundamentals. There are only a hand few
uh handful of things that you need to
understand. So that's the nice thing
about this and that's why I really
wanted to be part of this course as
well. Right. So let's start with what is
version control. So like I've said, one
of the main problems that GitHub can
help solve is that we don't have to do
something like this where we have your
script and then you want to try
something, you go to F2 and then you
want to try final actually final and oh
it still broke and then you need
to create a backup. So that is the
problem Git solves. So how it works, you
can think of Git as taking a snapshot of
your code. So you write your code, you
make your changes, and then you can save
the the snapshot. I'm going to show you
how to do that. In Git, this is called a
commit. So we commit our work and git
remembers everything every snapshot who
made it uh when and why. So when you
when here's your code like for example
on Monday you calculate the price over
here and then on Tuesday your client
says look we want to add a 25% markup.
So you change it. So then on Wednesdays
like hey can we actually go back to 20%.
And in this case that's easy. You just
go back to Monday's version. Now with
this like tiny little change like
changing one digit, it of course
wouldn't be that big of a difference,
right? But imagine you're having a big
codebase, you are working on lots of
stuff and then all of a sudden it
doesn't work. Let's go back to the
previous version. So that's really the
key concept and a key problem that git
solves. Uh let's go through a few
fundamental concepts. This is really
all. So this overview, this list, this
is all that you need to know for now. So
let's start with a repository. That is a
project tracked by Git. That's why I
also said that in the beginning when we
were creating our projects right so when
we were here when we were let me show
you this in the finder when we were
creating this project I already said
look I recommend to do it with all like
lowercase and dashes to make sure that
also when we push this to GitHub that we
can use the same repository name so
that's where the repository come from
that is simply a project tracked by git
staging is a concept which allows you to
choose which changes is to save. It's
like selecting items to put in the box
before shipping. So let's say you have
all of your project and you say I want
to save this, this, and this and this
and maybe I would don't want to save
that. Then we have the commit which is
actually creating the snapshot of your
code. Then we have pushing. This is
uploading things to GitHub. So then now
they also really become available
online. Then we have pulling which is
downloading the changes back. So let's
say you are working with someone else on
a team. They made some changes. They
committed the changes. They pushed the
changes. Now they are online. Now you
can pull those changes and now you are
at the same uh version again that they
are. And we can also clone projects. So
we can clone repositories from GitHub
which I will also show you how to do. So
this is if you're completely new and
someone shares their work, you don't
have it on your computer right now, you
can clone it. So you'll find that Git
has this like specific vocabulary, but
it's nothing more than saving files,
uploading them, and downloading the
codes to your computer. That's all
really there is to it. It just has its
own specific vocabulary. All right, so
first we're going to install Git on your
computer. And first you need to check if
you already have Git. So you can check
out the Git version. So you can open up
the terminal either on Mac or on
Windows, doesn't matter. You do get git
d- version. And here you can see on my
laptop that it already says, look, you
already have this version. So if it says
something like this for you already, you
can skip the installation phase. Now
here are instructions for Windows, Mac,
and Linux. It's pretty straightforward
to go through. For Windows, you can just
go to this website and you can download
kit over here. You'll have the
installer. And for Mac OS, there are a
couple of options. So you can install it
via homebrew if that's installed. You
can also alternatively install it via uh
Xcode command line tools. So if you do
get desk version Mac, your MacBook will
probably ask you, hey look, do you want
to install git if it's not found, you
can click install. You can wait a few
minutes and otherwise you can also go to
the official download page over here to
find more information. So that should
all be relatively straightforward by
now. Oh, and then just make sure that
git- version in the terminal uh works
for you after the installation. All
right, so now that git is installed on
your system, let's go through how you
can actually use it and work with this.
And here is a very simple example, the
most basic example of how git works. And
I'm first going to show it here just
conceptually. We're not going to work
through it because we first need to set
up GitHub uh on your end so that you can
actually push the changes. But high
level this is how git works. Whenever
you're working on a project we first
need to initialize the project. We need
to create the project in git and we do
this through git init. So this is these
are terminal commands. So this is not
something you run within your python
script and then execute over here. This
would be something where you open up a
terminal and where within your folder
you run these commands. Now the nice
thing about uh VS code is that we can
also do this through the UI. So there is
a source control menu over here where
you can for example see right now
initialize repository. That would be the
same as running git in it. So throughout
this tutorial in this section I want to
show you first how to use the commands
because that's what's going on under the
hood. But I always use the visual
interface from Visual Studio Code
because it's just a lot easier and you
don't have to remember the commands. But
you initialize the repository. That's
step one. Then you're going to stage
your changes. So you can say at get add
and then the dot signals that you want
to do everything. In most cases, this is
what you want to do. You just want to
save all your progress. So this is
staging. That's how that works. Then we
commit it. So then we can add a comment
in there as well. So let's say we added
or changed the price calculation. We can
give a little comment for us later or
for your team to know okay these are the
changes that I made and then we push it
to GitHub. So that's how git works. But
now before running all of this let's get
into the setup and then we can really
run the end to end example because like
you see you need to connect your GitHub
first. All right. So let's get into the
GitHub setup. Okay. So, if you don't
have a GitHub account yet, go to
github.com and sign up. So, let's see
over here. I'm already logged in, but in
here you can do sign up for GitHub.
Create your account. You can use your
Google account. Very straightforward.
And then from there, pick a username,
email, password, verify your account,
you can set up your profile, profile
picture, edit your name, image, whatever
you want. This is one of the most
important profiles as a developer. So,
if you don't already have one, make sure
to set it up. This is also literally
what can get you hired. This this might
be what people ask for on during
interviews. Clients might want to see
it. So this is going to be one of your
most important profiles as a developer.
More important than your LinkedIn
profile for getting jobs and getting
work. Okay. Now step number four is we
now have your account created and we now
need to create a link between your local
computer and the uh online uh GitHub
account really so that we have a link so
that when we push the changes our
computer knows where to send it. So you
can first set up your name and your
email. So this is a one-time setup that
you need to do. So you can say get
config global and you say your username
and then you can say for example your
name and also your email. This is a way
for git to store all of the information
so that when you do things on uh your
computer, git essentially adds this
information to all of the changes that
you make. Okay? And then you so you do
this in the terminal. So one more time,
you open up the terminal and then it
would be get config and then you would I
would for example do eilar. That's how
you set it up and you also do that for
the email that you've used. That's a
onetime setup. All right. All right. And
then next we need to set up your
authentication method to let your laptop
know where your account is and how to
authenticate so we can actually push
changes. So the re my recommended
approach is to do this through the
GitHub CLI. So there are three ways to
do it. You can do it with an HTTPS token
SSH key, but the CLI is really the
easiest one uh to do it. So on Mac OS
you can do brew install um gh and then
you install it. And on Windows, you can
if you go you go in here, you can
download for Mac and then you'll see
download on Windows. And then you should
that got get that up and running because
what you can do then it's super easy.
You can just open up a new terminal. I'm
already uh authenticated. So I can for
example do I can check this GH version.
What's in here? Yes, you can do GH to
see all of the commands that are in
here. And then what you can do is you
can do simply do gh login and there you
can see where do you want to login. We
do github.com. We go in here and then
you can choose what's your preferred uh
protocol. So you can choose uh SSJs
which I recommend or http and then you
can authenticate via the browser. So
just follow the steps over here. And
here's what that would look like. So you
select SSH then you need to select the
public key. So you can select one of the
keys that is available there. Then you
can give it the name. So for example,
you can do MacBook Pro like 2023. That's
my MacBook that I'm running this on. And
then you select authenticate via the web
browser and then a pop-up will open in
the browser. You can log in. You'll find
a one-time code that you then need to
paste into the browser and then you just
need to log in, select it, and then you
should be good to go. You see all of the
info here right now. This might be a
lot, but when you go through it, it will
just be step by step. Just so just to
follow what the terminal is saying, hit
enter and select what you want. So and
otherwise always you can troubleshoot
with uh JGPT in order to make the
connection work. Okay. So now we have
git installed. You understand high level
how git works. We have authenticated
your local git with your GitHub account.
So we now have a sync. So now let's get
into cloning and creating repositories.
So two things you'll often do when
working with git is downloading someone
other someone else's work cloning and
creating and starting your own projects
and saving the progress. So let's learn
both in this case and we are going to
start by cloning a repository. So this
means we can take a look at someone
else's work and we can download that. So
let's say we have the following file
over here. So, you're looking online,
you're following a tutorial, and you
stumble upon the OpenAI quick start in
Python from OpenAI. There are a bunch of
examples in here, and you want to try
and experiment with this. Now, what you
can do with Git now that you have set it
up correctly, we can easily take this
project and download it to our computer.
So, let's go through how you can do
this. And we can execute a command
through the terminal to download a
repository. And in order to do that,
what you want to do is you want to open
up a terminal in the folder where you
want to store the project. So if we come
over to our Python projects, which is
what I recommend to uh you have a single
folder, right? What you can do on Mac,
you can rightclick and if you go all the
way to the bottom, so you can see
services over here, you can see new
terminal, add folder. What this will do,
this will open up a new terminal. And
we're already within this project. So
you can see right now we are in this
folder over here. So, we're already at
the right uh point. And with in this
case, we don't have to like do the CD
and navigate through the terminal, which
I always find a little bit annoying. On
Windows, you can do the the same thing.
If you right click a folder, file
explorer, open in terminal. So, that's
how to do it. And now, here's how you
clone uh a repository. So, you don't
have to do these commands over here. We
can just go to the repository that we
want to clone. We click on code over
here and we copy the URL. So we copy
this URL. Then we go to our folder over
here and then make sure we have the
terminal in here. And then you type in
get clone and then you paste in the URL.
Now when we do this, what you'll find is
that now you can see OpenAI quickart
Python is now downloaded in here. So now
I can check in here and I can see
everything that is within this
repository. Now if you get an error over
here, some type of a authentication
error, just follow what is set in the
terminal over here. It might be that
because of how you set it up either
through HTTP, SSH or GitHub CLI, there
may be one additional authentication
step that is required in here and this
is just a first time setup that you need
to do and if it then works you can also
for example later use the SSH or the uh
GitHub CLI uh command. So in my case
this would also work. So let's see if I
go in here and I for example remove this
come in here one more time like this
would do exactly the same thing and it
would also download the repository in
here. All right. So now the next thing
that you can do here's quick little
shortcut. So we now have the folder,
right? And we could just as well like
open up Visual Studio Code, create a new
project, but there's actually a shortcut
for this. And you can use the code uh
command. And the first time you do this,
you need to uh set this up. So in Visual
Studio Code, if you are in here on Mac
and you do commandshiftp and then you do
shell command. So let's see in here. I
do command shiftp and I do shell
command. So I do install code command in
path. If I click this, this is a
one-time setup. Then what we can do now
uh and with Windows it's uh it's the
same thing during installation. Now what
we can do is I can actually see the so
let's see I mean change directory into
this folder. So into the open AI and I
can tap to autocomplete. I can now go
into this folder. So now within the
terminal I am here and then I can do
code and I can do a dot and then this
will open up this folder over here that
you'll see. So in Visual Studio Code
everything is in here and now I can
simply just save it as a workspace. Save
it in this folder. Everything that we
learned right and now I can just close
this out come in here and now I have
this project and I can start working
with this. So this is just also one of
those things in the beginning you need
to see it a couple of times. Uh you
don't need to use the dot code. You can
also what you can also even do like look
I'll give you one more tip. You can also
just take this you can on Mac you can
drag it in here also works. So just
click a little ways to make the whole
process of getting projects onto your uh
machine easier. So then coming back to
the setup you now have it on your
computer. What just happened? Get
downloaded all of the files. get
download all of the history and you can
now run modify and learn from this code.
So this is a very fundamental skill. So
if you'll find that this might this
video might be the starting point for
you to do anything with Python. If you
for example go to my you to my YouTube
channel to my other videos, you'll find
that every video where there is a an
example I'll say look the link to the
GitHub uh repository is in the
description. You now know exactly what
to do in order to get that codebase on
your computer. and then to also follow
along. Okay, so that was step number
one, cloning, but we can also now create
our own repository remember and now we
can come back to the project that we
were working on. So over here, so let's
say we want to push this to we want to
push this to GitHub. So let's see how
that works. And before we're going to
push everything, there's one more thing
that we need to add and that is a git
ignore file. And this is very important
because there are some files that we
just don't want to store to GitHub. And
primarily this is going to be our
virtual environment. So if I come over
here and look at the virtual environment
for example and I look at how big this
is we can see this is uh this is 270 MBs
and because this is because there's a
Python installation in there and we
don't want this in our version control
and similar there are other files uh
environment variables secrets cache the
Macds store file we don't want that in
there and git has a feature for that
through git ignore how you can do that
is you can come in here do create a new
file and you do dot and you do get
ignore and you hit enter. Now you can
see VS code will also recognize this. So
you will see a little icon in here for
this file. And now what you want to do
is you can copy paste this in here and
save this. Now this I would say is the
most minimal setup that you can go
through. But there's actually a better
way and something that we use for all of
our projects. This is kind of like the
starting point to set it, forget it. You
never have to worry about it ever again.
And that is available in the resource
hub. So if you come in here and go to
the resources, you see the git
ignore.zip. So if you download this and
then come let's see to the downloads. So
let me actually show you what that looks
like. We download that open in finder.
What you can then do is you can unpack
it. And this is also a hidden file. So,
if you don't see it, what you can also
do is just drag and drop the zip file
into your project and then open it up in
here. And then let's see. Let me
actually get rid of this. And I'm moving
a little bit quick in here, but I'm just
like moving moving the file. And I'm
going to unpack it here. And then you
can see you have the zip. And then all
you need to do because this depending on
your system, this can be a little bit
annoying because it will automatically
give it a name. It doesn't need it needs
to be exactly ignore. So it needs to be
like this. And now you can see we have a
whole list of all the files that we do
not want in our version control. All
right. So that's the get ignore file.
It's one of those things in the
beginning you set it, forget it, but if
you don't do it, uh you'll run into all
kinds of issues. So very important uh
and useful feature and useful uh
resource from the resource hub. So make
sure you download that uh and we can use
it for all your projects. So now we have
our project locally, right? We have
everything over here. But now what we
need to do is we want to create a
project on GitHub that we can send this
to. So we now go to github.com and we're
going to click on new. So over here I do
new repository and then I'm going to
call this Python for AI. So you can you
can name it whatever you want but I
recommend just following along with
this. And let's see. You can decide if
you want it to be public or private.
This is really important. Not for this
tutorial. You can totally set this to
public. But this simply means that
everything that you're going to save and
store like everyone is going to see
that. So of course when you are working
on projects that are private projects
that you don't want to share, I
recommend always setting it to private
as like a default and then only setting
it to public if you really want to share
stuff with like the world. Because if
you want to share stuff with just your
colleagues or friend, you can also
invite them through a private
repository. So this is important. So you
can select you can select the owner if
you have multiple uh repository names or
like I I already have one. So let's for
me I do like Python 4AI 2 just to show
you what that looks like. And then you
can leave everything as is. Make sure
you set it to private. Uh this doesn't
matter. And then you do create
repository. Okay. So now we see this
interface over here. And what we can now
do is let's see we can do this. We can
now connect our local project to this
project that we just created. And how
you can do that is through the following
process. So we can come over here to our
project. And I'm first going to show you
how to do this via the terminal. But
Visual Studio Code actually has a very
nice interface for this where we can
just initialize publish to GitHub. So
this will make our life a whole lot
easier. But I want to show you like the
first time right now how to do it via
the terminal just so you know what it is
doing uh behind the scenes. So let's
take things step by step and open up a
new terminal over here. And then what
you want to do is first we want to do
the git init. So when we come in here we
do get init. And this will create a
github project with in the folder that
we are working in. And you will see
output in the terminal, but you won't
see anything here in uh the explorer.
But that is the thing there. This is
also because it's a hidden file. So if I
go in here, you can now see that there
is a git folder with all of the
information that git is going to keep
track of. You don't have to do anything
with this folder, but it's good to be
aware that this folder is created
because when it's hidden and you are,
for example, moving files back and
forth. If you make copies and you make
changes and you forget to copy the uh
git the git folder, then you can lose
track of those changes locally. So
that's important to keep in mind. Okay.
Then what we are going to do is we're
going to stage your files. So we're
going to run the command get add. And
add simply means we're going to stage,
we're going to select which files we
want to add, but we want to send to our
online GitHub project. And the dot
simply means that we want to do all the
files. So this is very common. Um,
unless you really know what you're
doing, generally what you want to do is
just want to add all the files. So let's
do that right now. So we come in here,
we do get add, and then we do a dot, we
hit enter. So now it's going to add all
of the files, and it's they are staged.
And now the next step is we're going to
commit our files. So this is actually
where we're going to save save the
snapshot. And the syntax for this is get
commit. Then we do dash m and then we
can leave a comment. So we can we can
put anything in here. Initial commit
would be fine. So I'm simply going to
copy paste this. Come over here. And
then I'm going to do let me actually
clear this up. Uh get a fresh slate
again. And then we do get commit-m
initial commit. And now you can see look
we have created all of these snapshots
in here. All the information is now
stored. everything that's in here except
for the files that were in git ignore.
All right. And now the next thing we
want to do is rename our branch to main.
So this is just a naming convention in
git. So we make sure that we are on the
main branch. You can see that over here
in the lower corner right now. And then
the final thing we want to do is we
connect GitHub to our local project. So
if you come over here to GitHub again
where also all of these uh settings are
depending on where you do HTTPS uh or
SSH you can now add your remote. So you
can also follow just the instructions
over here. So I'm going to add the
remote for my it will probably say it's
already there but for you it will
connect it and then the final thing that
we can do is we push it. So get push u
origin main. Now I know these these are
a lot of steps right and when you do
this for the first time let's see if it
works. There we go. So, we have now
pushed all of these changes to GitHub.
So, congratulations. If you followed
along, if I now uh when I'm here and I
do a refresh, you can see look, we now
have our own private project, our
private repository with all of our
files. And again, now coming back to
this, this is a lot, right? So, but
remember in this section of the course,
if you if you've been following like
from the start for GitHub, there were so
many things like we created account, we
did the setup to authenticate, we
created the repository, now we do all of
these commands. And you might feel like
Dave, like working with Git, I'll rather
just duplicate the folder and have a
backup like that than doing all of this.
But you don't want to do that. And like
80% 90% of what we just did is just a
one-time thing. And I'm actually going
to show you how easy it is on a
day-to-day using terminal commands and
then later in the next step even how
it's even easier if you just a visual
UI. But now that everything is set up
really the 90% of what you'll do with
git in the beginning is look right now
over here let's say we have our file
over here and we say we have the
analyzer and we make some changes. So we
for example say I don't know let's let's
even just put like a small comment in
here. So we we make a change there and
we do another print statement to print
the uh grand total one more time over
here. So we made some changes to our
code. We can save it locally in here.
But now what we can do is now if we can
come into our terminal we can check
using a git status to see what the what
the changes are. So you can see look we
have we have made some changes to this
file over here which are not tracked. So
now what we can do let's say it's you're
working on a feature and you like want
to save it right now or it's end of day
and you just want to save your work
right now we can do we can just follow
the process over here. So we can just
say look we want to do get add we want
to add everything in between you can
also always do a get status. So now you
can see it's green. We stage the
changes. Now you can do g commit-m
and then we can say uh made some changes
and we commit that. And now we do a g
push and now it will send all of that
information. And if we come over here
you can now see that right now you can
see it's updated right now. And the cool
thing about git is now that everything
is tracked because I can now come in
here and I can see look made some
changes and I can see exactly what I
updated to these files. So that's on a
day-to-day now that you have set it up
all really that you need to do with git.
You just work on your project and then
whenever you want to save things you do
a simple update. But there is an even
better way to work with git and that is
just using the UI in VS code. So this is
how I always work with Git. It's super
easy and it's available from this
section over here. So you can click on
the little version control icon and let
me show you what that looks like. So now
let's say I want to refer to changes. So
I want to go back to let's say we want
to read data and we also don't need this
in here. We made some changes. You can
see right now here on the left you can
see one there's one change meaning we
changed one file. And if I change other
files as well. So let's say in the
helper functions uh I do like um let's
say I put functions here just just to
make a quick change you'll find that
look this is now updating to a two. So
you can now see git will now tell us
like look you have two changes in here.
And now one of the cool things that you
can do right now is uh you you don't
really have to stage changes in here or
uh even come up with your own commit
messages nowadays in Visual Studio Code.
You can just click on this and then if
you uh sign up with GitHub Copilot, you
can even let AI create messages for you.
So if you want that like let's see, we
can skip that for now. That's a very
nice feature to do because what it will
do is will look at your files. it will
look at the changes and it will just
create a descriptive message for you
because coming up with commit messages
is probably one of the hardest things to
do as a developer and this just make the
whole process easier. So I recommend to
check out GitHub copilot. But let's uh
for now say we made some updates. And
what I can do right now I can simply
come in here and I can either hit
commit. So this would be the same as
running the git commit with the message.
But I can also click on here and I can
do commit and push. And if I do it like
this you can say look there are no stage
changes. And I can say look I want to do
this always. So I want to before doing
this I want to stage all changes. So
when this is set up now you can see we
have successfully let's see made another
commit with the updates and now all you
really have to do is come in here make
changes and whenever you uh and whenever
you want to push them let's see let's
come over here whenever you want to push
them so now the function or the comment
here is is gone whenever you want to
push them says removed comment come over
here get push done okay so hopefully Now
it's a little bit less scary, right? We
went from a lot of controls to like how
do we set this up and all kinds of
commands to now literally giving a
comment and then clicking on one button
in order to save our work and make it
available uh via git. So this is one of
those things I know we spend uh quite
some time here on working with git but
if this the is this is the first time
that you're working with code the first
time programming like this look took
really long for me to actually figure
out how to do properly because there's
just so much that you can do if you go
over to the resource hub uh and actually
go to the git cheat sheet you can find
everything that you can do with git
there's really a lot to it but what I've
just shown you on a day dayto-day is
really like the 80 80 90% that you'll uh
that you have to do. Now there's one
more quick thing that I uh want to show
you and that is also you can also very
easily create GitHub repositories from
within git. So for example, if I'm here
in my Python projects folder and I want
I have a new idea and I want to start a
new project, I can create a new folder
here. Let's say I want to build an AI
agent. So we we create an AI agent
folder and now what I can do is I can
open up uh this folder in a terminal. So
I go to services, new terminal over here
and then I can do code to open it up or
just simply drag and drop it in here. So
now this is a completely empty uh empty
folder, completely empty repository. But
what I can do if I come in here I can
click on here and look I can now say
look I can initialize a repository. So
we create an repository and then uh
let's say I just do a first file in
here. So let's say we do just do a
readme.m MD which is going to be uh my
awesome agent project. Now we save that.
So you can now see we have a file in
here. And now what what the nice thing
is about Visual Studio Code, we can just
say do like first init. And then I can
do commit and push. And what it will now
do, it will say look there is no remote
in here. Do you want to add a remote?
And if we actually cancel out of this uh
just cancel it, it will say publish. Do
you want to publish the branch? And then
we say yes. And then we say there is no
uh GitHub extension. Do you want to sign
in? So we do yes. Follow uh with this.
And then let's see, we open it up again.
So this is also just a one-time sync.
And now what you can see now we can say
look do you want to push this to a
private or to a public repository? Say
okay, I want this to be a private
repository. And now we are going to
create. Now we have everything set up.
So now we have really like boiled it
down to we can just open up a folder
within Visual Studio Code like try to
make a change save something uh and then
it it will like essentially error and
then we can create the project or we can
also do it from uh from the beginning
but this is the process. This is how I
always do it. So whenever I have a new
project and now we just come in here and
if I now like uh make changes in here
say let's start git will now simply like
kick into action and say look now we
have changes in here. Uh so we do the
quick update do commit push and that's
all there is to it. So now you can see
it automatically updates. So for now let
me go in here. You can also manage all
of your settings. You change your
visibility and I'm going to actually
delete this repository. just get out of
this. Uh so that's also how you can do
that if you have a project that you
don't want anymore. Just wrapping this
up, but I'm just showing you from like a
more practical point of view how this
works in the day-to-day because again
for me in the beginning it was so weird
like all of these commands that you
needed to remember. This is really what
it comes down to. So this section was
really important. And that wraps up the
Git and GitHub portion. You now know the
90% that you need to know to confidently
follow all of my tutorials and tutorials
from other people as well. You know how
to create repositories, what they are.
You know how to use Visual Studio Code
and the terminal in order to manage all
of this. So now let's get into
environments and secrets. All right. So
now that you understand how you can work
with git and how you can also send
information and store information to
GitHub, it's important to understand how
you can keep your own data safe because
when you are working on serious
applications, there are always going to
be credentials in the form of API keys,
passwords, and connection strings for
example to a database. And from a uh
security perspective, you want to keep
this as private as possible because for
some applications with just a single API
key, someone can have like full access
to a system. It can pull all your
customer information. It can send
messages to customers. For example, if
you have an API key for a CRM system. So
you want to be really careful with that
and you don't want to push that to your
version control. So you don't want to
have that in in GitHub really or in git.
you don't but you just want it to be
locally only where the application needs
it. So this is something you'll
literally find in almost all of my
tutorials when we are working with AI
models like OpenAI or Entropic and we
need the need the API keys. So in order
to um have a workaround so that we can
use these these files without pushing
them to our version control let's
understand environment variables and
then the env file in Python and how we
use them. So let's start with the
environment variables. So environment
variables are stored outside of your
code. So you can think of these as a
configuration that your program can
read. So let's say you have an API key
or a database. We don't really want to
hardcode that within our file. So let's
say for example, let's come over here.
We don't want to do this. So let's say
let's come over here. We do get our
data. Uh we don't want let's say this uh
this this API would need an API key. We
don't want to do this because right now
anyone that would have access to this
file could simply like take this extract
it and then use it in their own files in
order to access your system. So this is
hard coding private or secret
information. We don't want that. We
don't want this showing up in our Python
files. So we want to store them in
environment variables. And how they are
how they work behind the scenes is they
live in your system, not in your code.
And we can use the OS library in order
to safely read the API key from our
environment. So it's only within the
environment that we need it. And we use
that by with the following syntax. So we
import the OS library and then we say
osenvironment.get
and then we do for example API key and
then we can print it. But before we can
do that, we first need to uh set up the
environment variables. So let's show how
this works. So let's see. We can come in
here. Let's go to our hello.py file.
This is currently empty. If we come in
here and let's say I come over here and
just import this. We can now see look we
have an API key and we have a database
and that is the API key, database name.
We can even set default values. If I now
run this, what you'll find is that
database will have a default value of
DB. But if we go to the API key, it is
going to say look this is none. There is
no API key because it is not set.
Similar if we let's say we remove the
default value in here and we say again
the database same thing it will say look
there is no uh there is no database
connection key over here. So how do we
then set that? Well we can do that
coming over here using an export
statement. So if we come over here and
we export this in the terminal session.
So coming over here to our our project,
we can actually open this up and then
set this and we can export it. So we've
now exported the API key. So this is now
available within our system. But let's
have a look at if we now change this to
API key and we run this file. So we do
run the Python file. We run it over
here. You'll still see that it prints
none over here and the API key is not
available and similar if we do that over
here and just for the print statement.
So no API key also not here. But now if
I do the export one more time. So I
export it here in the terminal session
and I use that same session to run this
Python file one more time. Now you can
see that it correctly has access to the
API key in here. Let me actually save
that. So now if you save that it should
also uh print. So now it should also
print the API key. So you can see that
through this we can actually make
information available within our
terminal session in our system and then
our Python files can use this. But you
can see this is a little bit cumbersome
because now if I like X out of this and
remove this it's now it's not available
within the interactive session. And if I
start a new fresh terminal in here, you
still find that it prints none because
now we would have to export it one more
time. So we export it one more time and
then it's available. So now if we
continue to work in here, it's
accessible. So this is fundamentally how
it works and how we can make it
available. But there is a much better
way to to do this in a much more
effective way that like takes care of
this process. We need to set it up once
and then never have to worry about it
ever again. And that is through using
the env file and environment variables.
So you you'll notice that I follow a
simil similar uh approach over here as
to git and github. I first show you kind
of like the more complex way how it
works under the hood and then I'm going
to show you the the simple way that
makes it really easy to do this. So
instead of doing the exports all the
time, what is very common in Python is
to use files. So what we can do is if we
come over to our project I can do new
file I I can do env and you'll find that
in the git ignore file that we created
the env file is also already excluded
out of here. So uh this will not be
tracked in version control. So you can
see that even though I added the file it
only says look only the only changes we
have is to hello.py Pi and if I create
for example an env let's say txt file
and I come over here you'll find that
this is tracked. So now if I push this
it will be added to to GitHub which is
not the case for the file because it's
in the git ignore. So what we can now do
over here is we can come in here and
this is a place where we can now store
secret information. So all of the stuff
that we don't want to include in our
version control which can be things like
let's see like API keys database URLs
app settings file paths all stuff that
we can dump in here. So now what we can
do let's take this over here this line
plug that in. So we create our env file
and now we can use a Python library
python. M which we can pip install to
safely load this information. So what I
can do right now pip install and coming
back to let's say let's get to hello.py.
So remember right how do we install
packages we go to terminal we open up a
terminal or we do a new terminal and
then make sure our virtual environment
is selected which should already
automatically applied. If you have any
errors make sure that is selected over
here and then we can do a pip install.
So let me actually show you what happens
if you don't install it correctly. So
it's going to be from.f
import load.nf.
So if I now try to run this, it will
tell me look there is no module. And the
way you install this is by doing pip
install python.n. So now it's installed.
And now if I run this, we should find
that we now get the green check mark and
we can import it. Okay. So what we can
then do is let's see we can now use this
simple uh function over here that we
import from the module and this is going
to load our env file and it will
automatically try to find it. So let's
see what that looks like if we do that
over here. You can see it says true if
it cannot find the env file. So for
example, if I put this in here and move
this and I do this one more time, it
will probably say that it cannot find it
in here. Look, it says false. So this
has some capabilities behind it where it
will automatically look in the current
folder and it will also it's some
sometimes it's a little bit annoying.
Sometimes I feel like it works also in
uh folders above, but sometimes it can
also get confused. So uh sometimes you
have to specifically select the path but
out of the box when it's in the same
folder over here in the root of your
project it generally tends to work but
this is a check that you need to be
aware of you can also do it explicitly
right so if I let's say I have another
folder and I'm just showing this because
this is stuff that you can encounter in
the real world and for example you want
to have your env file here if I now run
this it will say false and then what I
can do is I can specify the path so I
can say look it's in folder and then
forward slash env. And now you can load
it. So if you ever run into an issue
where it cannot find it, just point to
it specifically. And this just works the
same way that we learned with the path,
right? So you can also go up. So let's
say you were in some folder and you need
to go up one level. This is how it
works. So generally you want to keep
that at the root of the project. Let's
move that over here. And now from within
this hello.py Pi file we can simply run
this. So what it is now doing behind the
scenes now it is loading these variables
that we have in here this secret
information into our session and we can
now use the OS library to retrieve
those. So now if I look at this you can
see look we can now access our API keys
and now it works regardless of whether I
am using the interactive session or the
terminal. So let's see I think the
database we need to database let's do
database let's actually change this to
database URL because it is a URL in this
case and now we can simply say look we
have the database using database and
then database. So now I can run this and
it will load the API key it will load
the database and if I run this in here
you can also see that also through the
terminal you will get the correct
information. So, this is the
introduction to working with
environments and secrets. It's a little
hack that you need to know, a little
trick that you'll see in almost any AI
video where they're using the Open SDK
because you need an API key. And this is
how you do it. You don't want to
hardcode that. Make sure you create an
ENV file. You have the right git ignore
file and then you plug in your API key
in here. And then in your file, you just
import the load. Mf library. Make sure
it's installed via pip. you call the
load.n function and now you can use the
OS library to retrieve it from your
system. Now next up in the Python tools
section I want to introduce you to rough
which is a really cool tool that we can
use to format and lint our code. So this
is going to make making writing pretty
code and adhering to certain standards
and style guidelines super easy and
convenient. So with Rough you'll get
three tools in one which I will show you
right now. that is linting, formatting
and import sorting. So let's go through
this. First we have to do a quick setup.
So we can actually install it in visual
studio code and what we can do is we can
go to the extensions one more time
remember. So go over here extensions. We
can search for rough. So in here it will
be this one from Astral. You can click
here on install. So I I'm also going to
do a fresh install in here. So that
takes a couple of seconds. Okay. Now it
is installed. Let's see. Come back over
here. So that's the installation. Super
simple. And now what we can do is we can
enable formatting on save. And this lets
FS code automatically format your code
when you save it. So this is available
from the settings. So we can open it up
and then search for format on save. So
you can either go to the gear icon over
here, go to settings or do command or
control and then a comma which will open
it up. And I will search for format on
safe. And then you want to check this
box over here. And then the next thing
you want to search for is the default
formatterater. And we're going to set
that to rough. So here you find default
formatterater. You'll find a list and
rough should be in here if you installed
the extension. So we'll plug that in
there as well. And now you've
successfully installed and configured
rough. So now let's see it in action. So
what does formatting mean? Well, in the
beginning we talked about syntax, right?
So spec there are certain rules in
Python that you just need to follow
otherwise you'll run into errors. But
there are also style guidelines. And
remember how I talked about the PEP 8
style guidelines, right? Rough is a tool
that can help us to adhere to these
style guidelines. So if I have this
messy code over here, you can follow
along by simply copy and pasting this.
Like we know right now this doesn't look
good, right? There are too many spaces
in here. There should be a white line in
here. Here we have a bunch of here we
have a dictionary where it's pretty hard
to read what's in here. So we can
improve that. and even the spacing in
between. So, let's take a look at this.
Now, if I come over here to our editor,
I plug it in. You'll find that this is
now a little bit messy, but watch what
happens when I do command save.
That's cool, right? The first time that
I saw that was like, oh, this is so
awesome. So, now you can just go about
writing your writing your code and then
whenever So, let me see. So whenever you
like make a mistake, you don't add
whites space, you don't add a space or
the indentation or anything that is
weird that is not a syntax error, boom,
rough will fix that. So that's the auto
formatting. And you'll find that if you
use this, all the files that you are
working with will have a similar setup
and your code becomes very readable
because it will also take care of line
length. will make sure that when you
have a list with dictionary items in
here for example that it places nice and
neatly on new lines. We can even go as
far and adding adding a comma over here
and then what it will do it will even
split that and make it even more split
up into more new lines. So these are all
things that you can play with and we
never have to think or worry about the
formatting ever again. So let's see
formatting and that's the most obvious
one and also the most convenient one.
But let's have a look at linting and
import sorting. So linting is finding
issues and potential errors. So these
are the yellow squiggly lines that you
sometimes see that our editor already
gave through pylons. But rough takes
this a step further. So if I now for
example come in here, you can see that
we have these yellow lines over here
under OS. And if I scroll on that, for
example, you can see pilance is giving
an error or giving a suggestion over
here. But then rough is also saying look
we have an uh we have OS but it's
imported but we don't use it. So these
are little hints where it doesn't break
your code but it's overall much cleaner
if we get rid of this. So now one of the
cool things that you can do with rough
is we can form auto correction. So we
can do this through the command pallet
and that is command shiftp or control
shiftp if you're on windows. You can
type in rough and you'll find that there
are a couple of actions that you can
select from here. So one is for example
fix all autofixable problems. So if I
click on this you'll find that it
automatically gets rid of the import OS
that was in there and now we have
cleaned up our Python file in just two
actions. We save it and we also try to
fix all autofixable problems. Now
there's one more thing and that is
import sorting and this is just if you
like want to really be nice and neat
when you're working on your Python
projects. So if I come over here let's
see if there's an example in here where
we have some imports. So you can see the
helper. Let's see. Let's actually get
into the get data. Yes, over here. Here
we have a bunch of imports. If I open up
the command pallet one more time,
control shiftp or command shift P and I
do rough. I can also do format imports.
And what you'll find over here is it
will it will format the imports in a
very specific way. And this will just
help to make all of your code and all
your imports very similar so that
whatever file you open there is a
similar structure and everything reads
very similar. So that's what Rough can
do and I absolutely love it. Linting,
formatting, and import sorting. All
right. And my final gift to you in this
full Python beginner course is a tool
called UV. And this is a more modern way
to to work with Python and to manage
your packages and environments. And it's
a super awesome tool. And all the top
developers and top teams are adapting
it. So I really want you to learn it as
well. And I wanted to start with the
fundamentals, right? Because as a Python
developer, you need to understand what
pip is. You need to know how to work
with the requirements.t txt. You need to
know how to manage virtual environments.
But UI can do all of that and you can do
it much faster with just a few simple
commands. So, I won't go into all of the
details as to why it's so awesome. You
can read more about this, what it does,
but I'm just I'm just going to show you.
And if you want to learn more, they I
even have an entire video on Yui on my
channel. And you can also in the
handbook check why it's so awesome and
what you can do with it. But let me
scroll down. I'm going to go to
installing UVI. So this is what I want
you to do right now. So on Mac or Linux,
you can use a curl command and you can
actually open this up in a terminal. So
you can copy this and then uh create
open up a terminal session and just plug
that in and run it. And on Windows you
can do it via the PowerShell. So after
uh instart restart your terminal and now
here's what you can do with UV. So first
of all it can help you to go all the way
from starting a project to creating
environments to managing packages and to
even managing versions of Python and
running scripts. So let me show you uh
if you have UV now installed and let's
see let me open up another terminal. you
know, you can check if it's installed
correctly by simply doing UV and then it
should not give you an error, but it
should give you all of the things that
you can do with it. If you go, let's
see, UV Python, you can also just go to
the official website. So, they have a
whole bunch of documentation that you
can go through as well. But as with
everything in this course, I'm going to
show you the only thing that you need to
only things that you need to know right
now. So all the way back to the
beginning. If we go to our project
again, project folder and let's say I
want to start a new project. I can even
do that with UI and it's also going to
allow me to set up a some boilerplate
that I can use. So I don't have to
create the same files over and over
again. So let me show you how to do it.
I can come in here and then I can go new
terminal at folder. Again, you can do
this on Windows and on Mac. And I can do
UV in it as a starting point. And then
say, let's say my new project. And what
you'll find is now it will create this
project. And in here, there is already a
bunch of information, already a bunch of
files. So now what I can do, I can do cd
into my project. So now we're in this
folder. And then I can open up, I do
code, and I do dot. So now I open up
this project in here. I say I trust it.
And now you can see that out of the box
we already have a bunch of files in
here. So we have a git ignore file which
is very minimal. I still recommend using
the one that I shared from the resource
hub. It puts a python version in here.
You have a simple main.py file uh that
you can use to start working in. We have
a piprotoml in here. And we have a
readme file. And this piprotoml file is
is a really interesting file. And next
uh before I showed you how you can use a
requirements.txt, txt right where you
can keep track of everything that you
installed. So if you go over here to the
getting started was the packages and pip
scrolling all the way down. So we went
through a look we can have a
requirements.txt or an environment and
then we can do a pip freeze and it will
create a snapshot like this and then we
can install that. Now the tricky thing
is that every time you install something
you manually have to make sure to freeze
it and pip freezes also it adds
everything in all the packages and also
all of the dependencies. So it can
quickly get messy. This is something
that has always annoyed me about using
pip and working with Python. But let's
have a look at how UV handles this. So
if I come in here and I scroll all the
way down to okay, we have it installed.
We're now inside our project. What we
can now do I can now simply come in here
and let's say I am working on this
project and I want to add pandas and I
want to add numpy. I can do uv at I can
do pandas I can do numpy and what it
will do look at what's going on behind
the scenes. It's first going to install
everything and it's automatically
creating a virtual environment and it's
storing it in the dependencies here as
well just with one simple command and
super fast. So now we took care of
virtual environments of dependencies and
whenever I want to remove something I
can do u remove pandas and you can see
pandas is now gone and I can add it one
more time uv pandas and it's there again
and now within here I can simply come in
here and I can now run this file and it
will now use the virtual environment
that is created in here called my new
project or I should select that. So
that's one thing you should make sure
make sure you select it and now we can
use this in here. So now I can come in
here and I can import pandas and this
will now run totally fine. This will not
give an error. Now if you try this in
the interactive session it will give an
error because it will say look we need
to install the kernel. So that's
something that you remember right? We
also need to add yui at ipi kernel. This
is something you always want to have in
your project whenever you want to use
the interactive session. But it's super
easy to manage and everything is taken
care of. Now, one of the other awesome
features of UV is is let's say you add
this to uh a GitHub repository. So, you
push this and someone else wants to work
on this. So, let's say they clone this
as a fresh repository. We remove this
and they open up this project completely
from scratch. UV even even has a
command. It's called UV sync to come in
here do UV sync and it will look into
the project and it will just like
instantly create everything that's
needed in order to run over here. So now
we come back to Python, we can select
our project and we're good to go again.
So this is really why you should use UV
and why it is so awesome. So there's a
lot more that you can do with it but at
the most fun fundamental level is you
can create a project with it. You can
then use it to uh manage your project
with the pi project autotoml file and on
a day-to-day basis this is all you have
to do. You add packages and you remove
packages. That's really all that there
is to it. And you can completely forget
about pip. You can completely forget
about like how to set up uh virtual
environments. Everything will be managed
in here. So that's why I love using UV
and why a lot of Python developers are
adopting this. So as you go through
tutorials online also from like the big
firms from OpenAI and Tropic. If you
look into their tutorials there, they're
also using UV to like run code and to
add packages. So that was the
introduction to UV. And like I've said,
there is actually a lot more that you
can learn about it if you go to the
official documentation, but this is what
you need to know for now. And then if we
zoom out a little bit again, right, and
look at uh all the tools that we've
learned to use right now, Git and
GitHub, working with secrets and uh
within your environment and then also
using UV in a very short period of time,
we have covered so much information that
right now it may feel a little
overwhelming. Um meaning that as you go
through as as you go through this with
me step by step, you can follow along.
But then if you zoom out and like try to
understand the bigger picture, you might
be like, "Oh, it still feels like I need
to understand how to connect the dots."
And that's why I created this final uh
page over here which is a complete
workflow. And I want you to go through
this right now as an exercise because
let's see there's step one all the way
to let me scroll down all the way to the
final tips and strategies in there and
step nine to create a new project to
install the packages to manage your
virtual environment and to get it to a
GitHub repository. Everything is
explained and repeated one more time and
this will really bring everything
together. Also knowing that if you can
follow this step-by-step page here and
you successfully do this once, you can
literally always come back to this page
and follow it one more time to create
your new project. And the the setup in
the beginning is the most annoying part.
Once you have it set up and you have the
Visual Studio Code workspace file, so
you just save save the workspace file.
Once you have that, it's literally as
easy as double clicking the file. You
can go and you just need to push your
changes to GitHub. But I know like so
well in the beginning if you don't
understand this you always feel like you
have to you have you you're missing lots
of pieces that connect the dots whenever
you're following a tutorial online. So
you'll see someone writing out a Python
snippet. You understand what it is but
then you cannot like translate that to
wait but I'm working on my own project.
Where do I put this? How do I open that?
How do we manage that? What is GitHub?
And this is really really what I wish I
learned in the beginning because what
you see right here, the tools, this
setup, this complete workflow, this is
the exact process that we use for our
agency for literally all of our clients
and these are typically like five figure
deals. So these come in, clients ask us,
hey Dave, can you build us this AI
automation? Can you integrate with our
CRM and automate our customer support?
This is the starting point and a lot of
tutorials and courses on YouTube don't
show you that because they are just
working on toy projects and just doing
it for to create a quick and easy
tutorial. But I really wanted to give
you the full playbook to see what it's
like when you work with Python in the
real world when you're working on
serious projects. Congratulations. You
made it to the end of this course. I
know this was a big one and really this
is how I'm feeling for you right now and
I hope you share that feeling. So let's
quickly go over what we've learned and
then let's wrap up this course and show
you how you can continue your learning
path and learn all about building AI
agents. So when you started this course
you were new to Python, maybe even new
to programming overall and by now you
know how to install the Python
programming language, how to work with
variables, data types, control flow. We
looked at organizing your code with
functions and data structures. Then you
uh understand right now how to set up
Python environments and manage
dependencies. We first saw that how we
can do that with pip and vans but then
later I showed you how to do it with UV.
You know how to use git, GitHub and the
professional developer tools. And you
can also connect to APIs, handle errors
and work with files. You are now no
longer a beginner. You now have the
skills to really confidently work with
Python and tackle real projects. That
was really my goal with this entire
video. So you can now go out on your own
and it doesn't I can guarantee you it
doesn't matter which video on YouTube
for example you'll dive into there will
be no surprises. You'll be able to
follow along confidently. So
congratulations. Like I said I'm really
excited about this that you made it to
this point and the fact that you are
still here. You should really be proud
of yourself because this is a long video
and there are going to be a lot of
people watching this video that like
only watch half an hour, an hour and
then they think, hm, you know what? This
is not for me. But you made it all the
way to the end and I congratulate for
you for that. You are awesome. Now,
there's one thing that I'm going to ask
from you. So, this course is completely
free. So, if you enjoy if you enjoyed
it, what my goal with this course is to
like share my knowledge about Python
with as many people as possible. And
now, here's how you can help more people
discover this course. What I want you to
do right now, this is the only thing
that I ask from you is scroll down below
below this video and please leave a
comment about your thoughts about this
course, what you learned, what you are
going to build next. just make sure that
you leave a comment. So, because this
will let the YouTube algorithm know that
people are interested in this and this
will really help to push it. Now, while
you're doing that, also make sure to hit
the like button and also subscribe to
the channel like if you're serious about
learning AI because I'm dropping new
videos all the time. That's really what
I ask for you because through that we
are going to spread this video and we're
going to push it really far and
hopefully it will reach a lot of people.
So, that's all I ask. And then to really
wrap it up, really this is the last
thing over here. What's next, right?
What do we want to do right now? So, if
you're serious about AI and specifically
about Genai and you want to learn how to
build AI agents using the skills that
you've just learned, I have something
really cool for you. So, you made it to
the end. Are you ready to build AI
agents? when you downloaded the
resources which by now you should have
downloaded and if not this is another
reminder to do it because as a bonus by
just through downloading the resources
over here. So you can see download this
over here. You can plug in zero or any
tip you want but with zero it's
completely for free. You download the
resources and you also get access to the
data luminina academy and in here I have
a special surprise for you because if
you go in here and if you go to the
courses you'll find the Python course
over here. If you click on that, you'll
find bonus videos as well as the
follow-up course on building AI agents
and you can access that completely for
free and you can continue learning here
inside the data luminina academy. All
right, and with that we have come
officially to the end of this full
Python for beginners course. I hope that
you found it useful. I'm really glad
that you are here at the very end which
shows your commitment again. Now go make
sure to check out the data luminina
academy. then I can catch you there and
you can continue your learning journey.
And then I hope that you're off to doing
great things with these skills and
hopefully you can use them to make the
world a little bit of a better place and
we can all navigate this AI revolution
together because it's super exciting and
this is only just the kidding.
Loading video analysis...