Transcripts
1. 00 Promo: In less than six hour
with practical examples, I want to teach you the Python. We start from installing
Python and code on Windows, Mac and Linux, and we start
learning about variables. How to use CMD command prompt
and all of the systems, learn about operations,
everything about strings, list, topple dictionaries,
set, map filters, comparisons, chain
comparison, lots of options with loops like
four loop, y loop, after all of these basics, we go for functions. We will see lots of examples of how we can
create a function. After that, we learn about Lambda expressions,
and at the end, you are ready to completely
do lots of projects. And throughout this course, we will create a
practical example of calculating a
DMI of a person, so we can tell them if they
are fat or if they are not. So without any further
ado, let's begin.
2. 01 Download install pyton on windows: For learning Python, first
thing that you want to do, we want to download the
Python and install it. So let's see how we
can do that first. So for downloading
Python in Windows, I'm using Windows 11, in the Google, just
search for Python. First result that comes
up, just click on it. And in here, go to Download
Hover over Download, and you can see the
Python latest version. The most latest stable version. Of course, there is
newer version as well, but this is the more
stable version. Based on the time that you
want to download this Python, it may be in another version, but if it has three in it, three doesn't matter
what comes after that, you can continue with
this course easily. But if you want to just exactly use the same
version that I'm using, you can click on the
download all releases. And in here, in category of looking for
a specific releases, you can see all the releases
will be here until 2001, and you can see the
download there. No, we want to download
the Python 3.13 0.3. So click on it, and it will start downloading.
And you can it's downloading. With
that download it, click on it to open it. And in here, it's really important to enable
both of these to add the Python to the path environment
path of our Windows. Next, I want to use
customized installation, and in here, make sure everything
is selected, head next. And in here, I want
to tell it to install Python for all the
users, and that's it. We can hit install, and it will start
installing Python. And now you can see the
setup was successful. You can just close it.
No for testing it. Hit Windows key on your
keyword and search for CMD or Command
Prompt. Click on it. And in here, if you write PY
for Python and hit Enter, it should go inside
the Python and it should show the version
that we did in install. Now, let me create
another tab in here. There is other ways to go
into the Python as well. We can just search for Python. And again, you can see
it will go to Python, and it will show us the version. With that, we are
inside the Python. If we want to get
out of the Python, we can say exit, and it will exit to root
path of our user again. No, we didn't install Python and we check if it is
installed successfully.
3. 02 Download Install VSCode on windows: No next thing that we
want to do so we'll be able to write some
code. We need a ID. An ID, it's like a
notepad that we can write some code inside it and it will have lots of options. So we can write our
code more easily, like autocomplete, like color
coding and all of that. So no, let me close
this, close all. And in here, in Google, just search for VS code. Of course, there are a
lot more like sublime, Pycharm and all of that, but the visual do code
is from Microsoft, and I think with the
plugins that it has, it will help us a lot. So just go to
code.visualstudio.com, the first result in
here, and in here, you can click on
Download for Window. And it will start the
download automatically. You can see it over here. It's like 100 megabyte. No, with the VS
code, download it, just click on it, and it will start the installing process. In here, say I accept Head next and leave everything in here to be checked
by defaulted Ts, head next, and head in
install. That's it. It will be installed now. Now, in here, we can just check this launch Visual Studio
code and head finished, and it will open up the
Visual Studio code. In here, if you have this, it says restricted mode is intended for safe code browsing. Trust this window to
enable all features, click on the manage and click on the Trust,
and that's it. The problem will go away. And in here, let me
close everything. You can see this should
be the first thing that you always see when you open
the visual studio code. Now we can click in here, so we'll be able to open
folder and create projects. Next, we have a search in here. After that, we have source
control, like Github, and after that, we
have debugging, and after that, we
have some debug. For example, in here, I want to search for
Python, and you can see, first thing that it's come up, it's from Microsoft and you can click on it and
see the things there. It's called Python extension
for Visual Studio code. So this plugin will help us to write code in
Python more easily. If this extension was
installed before, that's okay. By default for me was installed. I did uninstall it. I wanted to show you
how you can install it. Just in here, head in
install, and that's it. It will be installed. Now everything is ready.
4. 03 Hello world Code in windows: No with VS code in Installed
and Python in assault, let's see how we can write
a little bit of code. So in here, I want to
go to the explorer, and in here, we can just
click on Open folder. But before doing that, I
want to go to our desktop. And in here, I want
to right click Go to New and create
a new folder. And I want to call
it Python Projects. Now with this folder created, we can go to here and
open that up in here. Go to desktop and open up
Python project, head select, and now you can see we
are inside that folder, and we can click on Nu
to create a new file. For example, a Python file. So click in here, it will tell us what do you
want to create? We want to say,
create a Python file. That's it. No, first thing
that you want to do in here, we want to use Control S
on or keyboard to save it. And by default, you can see
at the extension, it says PY. So whatever file that you have, at the end of it has PY, it means it is a Python
file or Python project. So now, in here, what
I want to do I want to call this hello word, right? Just save it with hello word. In here, first thing
that I want to write is a print, right? Just print with open
and close parentheses, and you can see because we did
install the Python plugin, that's why it helps
us what it is. It will bring us the
documentation, right? So always, you can use this
plugin as a documentation. But for now, just
don't mind this. You don't need to be
worried about it. I will explain how you can
use these kind of things. No, in here, I want to
just say hello word, and I need to do it
inside double quotation. Let me zoom in here like this and make this a
little bit smaller. Again zoom a little bit. Now, you can see it better. We have double quotation, and inside that
double quotation, whatever we say, for example, hello word from course, right? It will printed
in output for us. But as you can see, there
is a circle in here that it is telling us that
your file is not saved. You should head
Controls to save it, and now you can see
that circle go away. No, for running this code
that we write in here, there is a couple of ways. Let me zoom again. So you can see everything. Go to terminal and
add a new terminal. Now, in here, we want to
run this Python code, and how we can do that, first, we need to be
inside that folder. Right now, you can
see the path that we are inside is our folder, so everything is okay. No, we want to say PY or
Python and after that, write the name of the
file, hello word. And I don't want to
write the whole of it, I can just put the
first letter of that and hit Tawan or keyword, and it will put the rest for us. After that, if I hit Inter, you can see at the output, it will say hello
word from course. No, we write a code, and we run the code, right? But it's not just this way
that we can run over code. There are other ways that I wanted to show you
in next video.
5. 04 Use CMD to run python code in windows: There are other ways to run
our code inside our Windows. Let me show one of
the important one. So for doing that, I
will head Windows key, and in here, search for CMD, open the command prompt. If you remember, in here, we could just say
PY and with that, if we hit inter,
it will go inside the Python itself and it will
show us the version of it. I want to get out of
it with writing exit, and we are no Go no, you can see the path
that we are inside. We are inside the C user Navid, and we want to go inside
our desktop because we know that our project
is inside or desktop. So how we can do that, we can first check what
is the folders that we have inside this Navid
folder with DIR, right? With that, it will show us all the folders inside
the Navid folder. You can see directory of
this path Navid folder, we have these files. These files that have dot
at the beginning of it, it means they are hidden, and there are some
other folders that we can just use Windows
Explorer to go to them. Now, you can see we
have desktop in here. So we can just say CD
and write the desktop. The first letter is
more than enough. After that, we can hit tab, so it will write
the rest for us. CD desktop, and now you can see the path is C users
Navig and desktop. That's it. We are
inside the desktop. Let's just see what is the folders inside or
desktop with DIR, right? You can see there are
a couple of folders. For example, Python projects, and we want to go inside
that Python project. So we can say CD Python and head tab and it will
put the rest for us. And if you head inter, know you can see we are
inside the Python project. Now, again, I want to use DIR to see what files we
have inside that. So head Inter, you
can see in here, we have a hello word
dot PY and know that we are inside this
folder and we see this file, we can run it with
saying Pi, hello. That's it, head tab, and it put the rest for us. If you hit Inter, no, you can see we run over Python
code with command prompt. No, it's a good time to talk more about this
command prompt. What other things that we need to know about
this CMD, right? So first thing
that I want to do, you can see we have
lots of text in here, and we want to clear them out. How we can do that, we can write CLS and it Inter
and you can see, no we have a clean
slate in here. And again, we can
run toward code. So we can say Pi Hello, write some part of it, head tab, and it will
put the rest for us, and now you can see we
are running our code. Next tip about this
command prompt that you can do it in
terminal in here as well, is that whatever command that you put in here
and you head in, it will be saved as well. So with the arrow
key, up arrow key, and down arrow key, we can go to those
commands, right? So let me delete
this for example, I want to run over
code one more time. I can hit up arrow key. Again, you can see it will put the Pi helloword dot P for
us and we can hit Inter. Again, we can do it really fast. You can see we don't need to write it over and over again. And we can do the same inside or visual studio code as well. In here, we can say CLS,
and now you can see, it will clear the terminal for us and we can use up aro ky or down aro ke to go through the commands that we put in there and head in
there to run it again. No, we learn about CD how we
can go inside the folder, but let's see how
we can go back. Let's say in here we are
in Python projects folder, and we want to go back
to desktop folder. How we can do that, we can say CD double dot just like this. M zoom a little CD, double dot and head Enter, no, you can see we are
inside the desktop. And if you hit DIR, write DIR and head Enter, you can see it will show
us all the directory, all folders inside our desktop. And again, we can say CD Python and use tab to put the in it. And now you can see, again, we are inside Python project, and we can use CLS again to clear the terminal and
everything now works fine. So you can see the
command prompt and terminal are
exactly the same. Just this one, this terminal is integrated with our
visual studio code, and it's much faster. But I think this
way is much cooler.
6. 05 Install python on linux: You are using Linux, first thing that
you want to do, we want to install Python. And easiest way to do
that is with terminal. So open up your terminal. In here, I will
search for terminal. In this Linux that I'm using, it's called TLIx, but you
can open up your terminal. Just hit Windows key on your keyboard and
search for terminal. I will bring up your terminal. In here, first thing first, for installing things,
we can say sudo. Psudo is for administrations
commands, right? So when you want to install
something with psudo, it will be installed. After that, we should say
app in install Python tree. And it will tell us
to put your password. So let me put it there and
know the Python is installed. No, next thing that I
want to do I want to install another
program that is called Python PIP for downloading all the necessary
packages for our Python. This is really
important. In Windows, it will do it
automatically for us. But in here, we need to
say psudo app install. Python three PIP. That's it. Head Inter. And first and
first, it will say, do you want to continue
download this? You say, why or yes, head Inter, and it will start downloading all the necessary files
for our Python PIP. And now you can see the Python and Python PIP is installed. And for checking
that, we can say Python three, Head Inter. No, you can see, it's
showing us the version. Python 3.13 0.3. And it is the same version that we are using in Windows as well. No, for getting
out of the Python because right now we are
inside the Python itself. We can say exit. With that, it says,
use exit open and close parenthesis or
Control D to exit, right? So either we can say exit
open and close parenthesis, and it will get out of it. And no again, say Python T
and go inside the Python, this time, I want to use
Control D to get out of it. So these are some tips that it makes our life
very much easier. Know that we didn't
install Python, let's see how we can
install VS code on Linux.
7. 06 Install VSCode on linux: Now that the Python
is installed, we need to install the VS code. So in there, we can write
some code for Python. VS code is just like any text editor that
have lots of tools, for example, color coding, auto completing and all of that, that we can use to
write some Python code. So we need to install. Go back to Firefox. In here, in Google
search for VS code, and in here, go to VisualStudo code code
editing redefined, right? It's code.visualstudio.com. You can see there is two
types of downloading here, one dot db and one dot RPM. One of them is for Debian Obunto and one of them is for Red Hat, Fedora, and so on. Based on your Linux version,
download one of them. For example, mine
needs a Debian, so click on it to go
to download page. It will automatically
download it for us. Know that it is downloaded. What I want to do, I want
to go back to terminal, and in here, we need to navigate
to the download folder. So for doing that, first of all, I want to see where I am. I hit CD, and now you can see, we are enroute, right? Of course, we could just see it, but I wanted to show
you with CD again, you will see the path. No, I want to check what is the folders inside
the root folder. For that, we say LS, for list all the folders
and head inter, no, you can see we have desktop document downloads,
music, and all of that. No, I want to go
inside to download. How we can do that,
we can say CD down and we don't need to
write to the end. We can just write some
part of it and hit tap Keon or keyword and it will put downloads there for us. Head Inter, no we are
inside Downloads folder. Again, in here, I
want to use LS to see what folders and files we have inside the
download folder. Now, you can see in here, we have the code dot DBN or DEB and we want
to install that for installing that
we should say sudo DPKgI and the name of the file
that you want to install. It's called code, all of these. We don't want to write it
completely by yourself. So we write just the code and hit tab on our keyboard
and it will put it there. And that's it. Now,
if we hit Inter, it will tell us to
write your password. Let me put it there
and head Enter and know it's starting
to install the VS code. No, it is saying the
installer would like to add the Microsoft
repository and sign in key to update VS code
through app, right? So in here, just head Enter and know the install
is finished. Now for opening the
Visual Studio code, we can just say code in here, head Enter, and know it will
open the visual studio code. We can maximize it. Of course, we could just hit Windows key on or
keyboard and search for VS code or Visual Studio code and double click
on it to open it. But this way of opening it
looks more cool, right? So now, with that done, Visual Studio code is install. No, I next video, we will write a hello word code. Let's see how we
can do that. Next.
8. 07 Hello world cose in Linux: Know that Visual Studio
code is installed, let's just write the
first project for us. Before doing that, I want to
go to Visual Studio code. Go here at the extensions and install the
Python extension. If you click on it, you
can see what it can do. It has some autocomplete and some color coding and some
debugging stuff, right? So heating install
to install it. Now you can see the Python and all the necessary
plugins is installed. Now, we can go back in
here on this explorer tab. No we can open a folder, but right now, we
don't have any folder. We can create a folder
inside our desktop. For example, in here,
create a new folder, and I want to call it
Python Projects, right? With that done, no, we can open the top in our
visualsdo code. So in here, click
on Open folder, go inside the desktop, go inside Python Project, and hit Open, right? Now we are inside that
Python project folder. In here, first
thing that it says, it says that, do you want
to trust this or not? To access the desktop
and all of that, we say we click on this, and after that, yes, I trust the Outer and that's it. Now with that done,
we can create some Python files in
this folder, right? So click on this plus, and I want to name
it hello dot py. Why I'm calling it hello dot PY? Because each one of the Python files should
have a extension of dot PY. So Python knows that
this is a Python code. So whatever Python
project that you have, the extension of it
should be dot PY. So Python and Visual Studio code knows about it that
it is a Python code. After that, choosing the
name dot P, head Enter. And now you can see in here
I can write some code. I want to say print
this print function, I will explain it a
lot more in future. But for now, with it, we can show some string
or text at output. And how we can do
that, we can do it we put in a text inside the quotation mark inside a
open and close parentheses. Let me zoom in here a little bit so you can see it
better. Make this small. And no in here, I
want to say hello word from course, right? You can see there
is a circle in here that it will tell us that
this file is not saved. If we hit Control S, that circle will go away
and know it is saved. But no, let's see how we
can actually run this code. There is some way of doing it. Let me show the first one. For the first one, go to terminal in here and
create a new terminal. First thing first that
I want to do in here, I want to use LS to list all the files inside this
Python project folder. Like this, you can
see we Hello dot py. No, I want to run it. We can say Python Tree, hello tab. We will complete it. We don't need to write
it completely by ourselves after that head Enter, and you can see it says
hello word from course. In Windows, we say
PY hello dot pi. But in Linux, we say
Python tree, hello dot py. So if you are using Linux
wherever I'm using PY, you should use Python Tree. That's the most difference
between Linux and windows. Now, you can see we are
running our first project. Now, in next do, I want to
talk more about this terminal. So I will see you in next.
9. 08 Use Terminal in linux: As you saw, we did run a Python code in the terminal
of our visual studio code. No, I want to do it in
actual terminal of Linux. So in here, what I want to do, first of all, we are
in download folder. I want to go back folder
to the root folder. So in here, I can
say CD double dot, right, and hit Enter, and now you can see we
are in the root folder. We can say LS to list all of the folders and files
inside the root folder. No, we want to go to desktop. So in here, I can say D CD desk. You can see I put
some letter of it, and no if I head tab, it will put the rest for us. No head inter, you can see
we are in desktop folder. And in here, I want to use
LS to see what is inside it, and now you can see the Python
Project folder is there. No, I want to go to
inside that folder. I can just say CD. Python and head tab, and now with head in Inter, we are inside Python
projects folder. In there, again,
I want to use Ls. Can see we have hello
dot py. We can run it. We can say Python, T Hello. Just write some part of it, head tap and it
will put it there. Now if I head Enter, you can see it says
hello word from course. Cool, right? Now, next
thing that I want to talk about is how we can
clear up all of these. Right now, it is a mess. We can say clear head Enter. No, you can say
everything is clear. Now, let's say we want to
run our project again. One way is to write Python
three hello dot py. But another way with up
arrow key on our keyboard, we can go through all the codes, all the commands that
we put in there. Up arrow key and down arrow key. For example, if I head up, you can see it will put the Python tree
hello dot py there. And if I head in
there, you can see it's running or code. That's it. Now you know how you
can run your code inside the terminal and
how you can use arrow.
10. 09 Download and install python on MacOS: Now for writing code in
Python inside the Mac OS, let's see first how we
can install Python. So for doing that, open up your browser and
search for Python. And here, let me maximize this. Open up the first
thing that comes up, and in here, go to Downloads. And in here, hover over the download and you can
see the version over here. You can just click in
here to download it. If you don't see the
exact same version, but there is a tree in
here at the beginning of this number, it is okay. But if you want to just install the same
version that I'm using, go down until you see the
looking for a specific release. And in here, you can see you can download whatever
version you want. You can download version one
in view all the releases, right, from 2001 as well. No, what you want to download is just this Python in here. Just click on it. And just with that, the
Python will be downloaded. We can just minimize this. Go to finder in here and
go to Download folder. And in here, you can see
the Python package in here. Don't click on it,
and with that, you can see in here, we can
just click on Continue. Continue again, continue
in here, head agree. No, you can head in astall. After that, it will tell you
to put your password there. I put my password and
head install software. And it will start installing
Python and knowing that the Python is assault and you
can just had close in here. It will tell you,
do you want to put the Python file into the trash? After installing it, you
can just hit move to Trash. And you can see in Download, we don't have it anymore. Let me minimize this,
minimize this as well. And for testing that, I want to go to terminal, click on the terminal
to open the terminal. Now in here for checking if the Python is
installed or not, we can just search
for Python three, head Enter, and now you can see. First, it will show us the version that
we didn't install, and after that, it will
go to Python itself. And for getting out
of this Python, you can see by this tree arrow, it means we are in Python. And for getting out of it, we can say exit, open and close practice, and head enter, and now you
can see we are out of it. Now, we know we did
install Python success
11. 10 Download and install vscode on MacOS: For us to be able to
write some Python code, we need IDE, some type of notepad so we can
write in it, right? We have lots of ID
like sublime, VS code, Microsoft Visual Studio, even
notepad Notepad plus plus, you can use whichever you want. But the one that
I want to use in this course is called VS code, and we want to
install that node. So go to your
browser and in here, I want to search for VS code. Right? The first
thing that open up is visual CDO code
code.visualsd.com. Just click on it, open
it, and that's it. In here, you can just
click on download for MacOS and tell you
download it automatically. And now you can see
it start downloading. And now with VS
code download it, we can go to Finder in here. And you can see it. It
is in Download folder. You can just double click
on it to extract it. Now you can see it did extract
it and put it over here, and we can open it
just like this, but I want to put
it in launchpad. For doing that, I want to
click on over Finder first, and in here, open up
a new finder window. And with this new one, I want to go to applications, and you can see all the
application in here. For example, the Python
that we installed. Now go to Downloads,
hold Command key, select and drag the
visual code and go inside the application
folder in here and release. And just with that, we did install
Visual Studio code. Now, if you go to Launch Pad, you can see the visual
stdio code is in here and you can click
on it to open it. First time that you open
the visual stdio code, it will verify the software, and after that, it will
be much faster to open. Now, in here, it will tell us the Visual stdo code is up
download it from Internet. Do you want to open
it, you had open and no Visual Studio is open. Let me make it maximize
and now you can see in here like we have it
in Linux and Windows, we have all the tabs, for example, in here, we can open a folder. In here, we can
search for stuff. In here, we have
Version Control. In here, we have some
debugging tools, and in here, we have some extensions
and search for Python. Now you can see we have
the Python extension. Just click on it, and it will
give us some color coding, auto completing, and lots more. So click on Install
to install it. It will make our life
very much easier. And know with that,
the Python extension will be installed. That's it. Now, we
can just close it. Close this, close this. You should see something like this when you close everything. You can just click on
this one more time, and you will close everything. Next widow, we will write our
first Python code together.
12. 11 Hello world code in MacOS: No, W O VS code in a
Salt and Python instalt, let's just write or first code. For doing that, I want to
go here, click in here. You can see in here,
I can open a folder. So first thing,
we need a folder. Let me minimize this. Go
to finder, go to desktop. B now there is nothing there. So let's just create
a new folder, and I want to call
it Python projects. It. This is our folder and we can open it in
Visual Studio code. So in Visual Studio code, click on Open Folder
and go to desktop and click on the Python
project, and it will open it. But before it says,
do you want to allow? You say hit Ao After that, it will tell you, do you trust the software or not to
access your desktop? You say, Yes, I
trust this author, right, because it's
from Microsoft. In here, we are
inside that folder, and we can click on this
plus and create a new file. I want to call this
one hello dot PY. Why PY because all
the Python code should be inside a file
that has extension of PY, so it will be runnable. So Visual Studio code and all other software should
know it is a Python file, Python codes inside it, right? After that head
inter to open it. And in here, the only
thing that I want to do, I want to use a print in here, and we open and close parenthesis
and double quotation. Okay? Increase the size a little bit so you
can see it better. Knowing here whatever
I put in there, whenever we run our coat,
it will show it to us. For example, let's
just say hello word from course,
right? That's it. No, you can see because we
add something to this file, there is a circle in here,
white circle, right? If you go to file and head
save or use Command S, it will save it,
and now you can see the white circle is
not there anymore. Now, let's see how we can
actually run this code. For doing that, go to terminal
and create a new terminal. It will open it up here. It's like any terminal
in our MACS, right? We are inside the
Python project folder, so we can just say Python tree. After that, the name of the file that you want to
run that is called Hello, we can just put
some of it in there and it tab and it
will put the rest. Now, if you had in there,
you can see at output, it says hello word from course. And now we can do it again, right? We can run it again. So we create a Python file and we write some
code inside it, and after that, we run it. Now, next do I want to talk
about this terminal more
13. 12 Use terminal in MacOS: No, we want to talk more about
the terminal inside MacOS. First of all, we have a
terminal in here, right? So in here, first thing that I want to do I want to use CD. So it will show us where we are, and right now we are
inside the root folder. And for us to see what folder
is inside this root folder, we can say LS, ok.
And now you can see it will show us all the folders that is inside the root folder. And what do you want to do? We want to go to desktop and run the hello word project
that we create, right? So in here for doing that, we say CD disk. We don't need to
write it completely, put some part of
it and head tab, and it will put
the rest for you. Head ter, no, you can see we are inside the
desktop folder. Again, we can use LS to see what folders is and
it will tell us, do you want a little terminal
to access the desktop? You say Hello, and now you
can see inside the desktop, we have Python project. We want to go there.
So we say CD. Pi. Just put some
of it head tab, and it will put the rest
for you, head inter, and now you can see we are
inside the Python project. And with that done, again, we can use LS to see
what is inside it. You can see we
have hello dot py. And in here, we can say Python. Tree. After that, hello head tap to put the rest of
it there, head inter. Now, you can see it says
hello word from course. You can see we can run Python code just
inside the terminal. We don't need the visual
diode to run it, right? No, next thing that
I want to show you is how we can
get back one folder. For example, go to desktop. We can say CD, double.in here, head Enter. No, you can see we
are in desktop. Let's say we want to
go back one more time. We say CD, dot, dot, and head Enter, right? No, you can see we are in root. Next thing that I
want to show you, you can see right now it is a mess in here and I want
to clean everything. All of these text, we can
say clear and head Enter. No, you can see we
have a clean terminal. No, you know how you can use terminal inside Mac OS.
That's more than enough.
14. 13 Operators and priority of them: No before going and
write some cool code, what I want to do
I want to show you more thing about Python itself. So for doing that, I want to
use Windows key and in here, search for terminal or
Command Prompt or CMD, and this will be open. Let me zoom a little bit
with Holding Control, and with rotating middle
mode, I can zoom in here. Knowing here, I want to
go inside the Python. If you are in
Windows, as you know, we should write PY, and with that, we will
go inside the Python. And with Control D, we can get out of the
Python space, right? If you are in Linux or MacOS, you should write Python three. That's the most
difference between Windows and MacOS
and Linux, right? So remember that for
the rest of the course. Now in here, let's just go inside the Python
with write in Pi and head Enter and know we are inside the
Python, what we can do. Like the Hello Worth
project that we done, we can use a print. Open pranxs with a
double quotation. We can say hello word, and it needs to be wrapped
between two double quotation like this and close pranxs
like we did it in IDE, right? If I head inter, Python will run this code and
give us the result. You can see the
result is in here. But we can do lots
more with Python. For example, we can do
adding, for example, five plus seven and head enter, it will give us the result. It's like calculator. Next, we can do
multiplication, three, multiply by seven, it's
21, you can see it. We can say eight, divide it by two, it will give us four, and you can see whenever we have a operation that has dot
zero or dot something on it, it means it is a float value, and I will talk about
float value more later. No, for example, let's say 8/3. It will give us 2.6
6665 at the end, right? So this is a float number
that is not a hole. This 21 because it
doesn't have dot, you know that it
is a whole number. But Python for showing it to you that it is a float
value or whole number, it will add a dot zero
at the end of it. And we can do lots more. For example, substraction eight minus five. It will
give us three. Next, we can do a remainder
of a division, as well. For example, this 8/3, what it will give us, it will
give us 2.6 6665, right? But if we say 8% three, what it will give us,
it will give us two. Let me show it in paint so
you can understand it better. In here, if we say 8/3, we should say two
multiply by three, give us six in here, right? And the remainder
will be two, right? So with that percent, we receive the two. Sometimes this is
really important. For example, when we want to see if a number is odd or even, for example, we have ten. If we get the remainder, get the person two of it, it will give us zero because
five multiply by two, it will be ten, and there
is no remainder, right? No, if we have 11/2, it will give us 5.1. But if we say 11%, two, it will give us one. So we can use this
percent to check if a number is even
or if it is odd. And it doesn't matter
how big it is, we can check with
percent by two to see if it is odd or even head enter and because
it gives us zero, it means this number
is even number. And again, let me put
another value and person two in here and
it will give us one. It means we know that
this is odd number. Maybe you say where
we can use it, there is lots of places that you can use
something like this. Thing that I want to talk about is the priority of operations. For example, we want
to say five plus two, multiply by seven plus three, right? What will happen? In Python, we have
priority of operators. And if I just search for priority of operators in
Python and go to image, you can see the
first image in here. That it will tell us
that multiplication have highest priority
from D plus and minus, and you can see all
of them in here. It doesn't matter
which one you use. All of them are okay. You can see it over
here by clicking here, you can see more if
I click in here. So you don't need to remember this stuff because you have Google and you
can search for it. So I don't remember this stuff. Now in here, what should happen? As you know, if I go here, let me make this
minimize a little bit. What should happen
in here first, the multiplication should
happen to multiply by seven. Let's see what will happen
with the calculator, two multiply by seven. It will give us 14. And after that, let's say
plus five plus three, and it will give us 22. If I had Enter, you
can see this 22. Why? Because the multiplication will happen first
and after that, this plus and this
plus will happen, as you can see it in here. The highest priority,
as you can see, is the multiplication and
after that plus and minus, and you can see it over here. Let's say at some
point you want to add these two together
and after that, add these two together, and after that
result of these two, we want to multiply
it together, right? So what you want it to happen, let me show it to you in here. We want to say five plus two, it is seven, and we
have seven plus three. It will give us ten. No ten, multiply by seven. I will give us 70, right? So let's say we want to
get the result of 70. So first, this adding should
happen and after that, this adding and after
that, the multiplication. You can see in here the parenthesis has highest
priority than the others. And my recommendation is
to use parentheses always. It is more readable and easy
to develop with it more. Just a big recommendation. Remember that. These parentheses
is really important. So in here, we can
say open parentheses five plus two close parenss after that multiply by
open parents again, seven plus three after
that close parenthesis. And if I enter, you can see it will give us 70. So, in my opinion, and
my recommendation, you don't need to know about the priority of
operators at all. Just use prenss. It will be much easier to read and develop
it more in future. So just stick with prances. You don't need to know about these priorities
at all after that. And that's it. Now you know about operators and priority of
15. 14 Variable and their rules: No it's time to talk
about variables. So why we need to use
variables for saving things, for using them later, right? So let's see how we
can define a variable. So in here, let's just go inside the Python with writing
PY and head Enter. Now we are inside the Python. You can say Mar is equal to 20. Know what will happen
if we head inter, Python will create a space inside the RAM and it
will save 20 in it. And for accessing
that number that is saved inside that
part of the Ram, we can say Mvar and Python
will go to that address in the Ram and get
the number that is saved in there and it
will show it to us. So with the variable, we are saving things so
we can use them later. But we can save lots of things, lots of different variable
type we have in Python. But the naming of them
is really important. The name of variable
should always start with a letter
or underscore. So I can say like
this underscore. My ar is equal to 30. You can see, no, I
can get that value. My ver and it will
give us 30, right? And now, if I say my var
without this underscore, it will give us 20 because it
is two different variables. No, let me show you some type of variable name
that you can't use. Let me clear the screen. The variable name can't
start with a number. So if you say two var is
equal 20, if I hit inter, you can see it will
give us an error, and it will tell us that these two shouldn't
be there, right? Don't mind this error
that it's saying in here. It's not that smart that knows that you want
to create a variable. So just remember this that you can't start a variable
name with the number. You can't start it
with a dash as well. If I say dash R is equal to 20, if I had inter, again, it will give us an
error because you can't start a variable
name with the dash. You can't start it
with equal as well. Equal equal 20. Right? You can't do that. The variable name should always start with a letter
or underscore. Next, our variable name
can't have dash inside it. So if I say my var
is equal to 20, it will give us error as well. So inside it, you
can't have a dash as. You can't have equal plus and all of that. Again,
you can't do that. So I can't say M plus
ir is equal to 20, and again, it will
give us an error. So you can't do that. Now, next thing, you can't have a space inside
a variable name. I can't say my space, is equal to 52, for example, if I had Inter, again, it will give us a error. No, what you can do, you
can do it like this. My var is equal to 20, right? You can say M underscore
var is equal to 20. You can say underscore, M underscore var is
equal to 20, right? You can put all of
them in upper case, my var is equal to 20. Again, you can see
it works the same. And we can put number
inside the variable name. My two var is equal 20. Again, this will work as well. My two. Now if I had enter
it you was 20. Now, you know, what is the rules about
choosing a variable.
16. 15 Variable type: No, we know how we can
create variable and how we can create them
correctly with the right name. But let's see why we need them and how
we should use them. So for doing that,
let's just go to Python with writing PY heading. Now in here, I want to say
my age is equal to 36. I want to say, my name is
equal, double quotation. Write your name and double
quotation for closing it. And Python with this double
quotation around a text, it knows that it is
a string, right, and it can be saved inside
the variable head inter? No, the name will be saved
in the myname variable. We can say, my weight
is equal 92.6, right? So whenever I want
to get my age, I can just say my
age, and that's it. I will give it to us. Whenever
I want to get the name, I say my name and it will
give it to us, right? So variables inside the Python will make our life
much easier, right? But as you can see, variables can store different
kind of value inside them. For example, in here, it's
saving a whole number. And in here, it's
saving a float number. And in here, you can see, it's just saving a text
or a string, right? So this type of coding, it's called dynamic typing, and Python support that. So you don't need
to tell it that this variable can
save a whole number. You don't need to tell it that this my weight should
save a float value. And you don't need
to tell Python that this variable can save a
string inside it, right? Python will do that for you. But the type of things in
Python is really important, and you can get them
with type function. Just write type, open parses. And for example, let's
just put my H there. And now this function
that you can see in here, it will give us the
type of Mg variable. And if I head inter,
you can see it says it is a int or integer. Integer is for saving the
whole numbers, right? So let's just do it for
the other one, my name. So put my name in there and wrap it around prances
like this, head inter. And now you can see it
says the type of it, the class of it is STR, string. No, let's just do it
with my weight as well. So a parochy I will
put the type there, delete these parts, and now
I can put my weight there. And if I had Inter,
you can see it says the class of it or
type of it is a float. And the good thing about the Python is that
whenever we want, we can change the type
of things easily. So, for example, right now, my H is integer. It needs a whole number. Can't save float number. But PyTon help us to
do that, as well. So if I say my age, right now, it will give us 36. But let's say, I want to
say my age is 36.5, right? We can do that in Python. If I had Inter, it won't give us any problem. It will automatically
change my age. That was integer to float, so it can save 36.5. So no, if I use type my age, if I head Enter, no, you can see, Python automatically changed the
type of my age to float. So in that case, Python
is really a smart.
17. 16 Varibales operation: Know that we know about
the variables more, let's see how we can use them. So, for example, in here,
let me go inside the Python. And in here, what I want
to say, for example, A is equal to two, and B is equal to three, right? Now, we can see the
result of A plus B, and it is five, right? We can say A is equal, A plus B as well. No A will be five. And B will be three. Okay? So we can do lots more. For example, we can say A
is equal A plus A, right? And with that, the A is five, five plus five will be ten. No, A will be ten, right? So if we show the result
of A, it will give us ten. And we can say A is equal A
plus ten, as well, right? If you show the value, it should be 20. You can see. So with the variables, we can have all the operations. Example, multiplication,
division, and all of that. For example, if I
say A is equal, A that is 20 divided by B, that right now it is three. Now with that, A is 20, 20/3. What value it will give us? It will give us a float value
that is 6.66 6667, right? No, if we get the type of A
like this, it is a float. At the beginning, it
was just an integer. Now, let me show you
some other operations. For example, let's say
A is equal to two, and B is equal to
three again, right? We can say C is equal
A plus B, right? So with this, C will be five
because as two B is three, so C equal A plus B
will give us five. And no, if we get the type of C, it will give us integer because
C is saving an integer. So Python will decide that. Now, again, A is two, right, and we want to say A is
equal A, multiply by A. What will happen? A
will be four, right? We can do this in a better way. We can use in power of. So we can say A is equal to two. No, A is two, right? We can say A is equal, A, power of two. So to a star, it means
in power of two. If I head inter, if I show the value of
A, it will be four. And we can do it with numbers
more than two, right? We can say B, is equal to B in power of
three, for example. No, B will be 27 because
B was three, right? So three multiply by itself three times,
three multiply by three, it will be nine, nine
multiply by three again, it will give us 27, right? So now, we know that
we can do operations with variables as well and save them inside
another variable. And Python will decide what
the variable type should be. No, it's a good time for a cha
18. 17 (Project) Calculate BMI: No, I want to give
you a challenge. I want you to
calculate the BMI of a person based on the weight
and height of that person. You can pause the
video in here and go to Google and search
for its formula. And I think this is the best way to learn
Python because you need to research a
lot as a programmer. But let me explain
the formula myself. In here, I did search
for BMI formula, and you can see the
formula in here. It is body mass index. Basically, when we
calculated it it will tell us if we are
fat or not, right? So BMI is equal
weight in kilogram, divided by height in
meter in power of two. So we want to do that, right? To calculate the
BMI of us, right? So let me give you an example. If you search for calculate
your body mass index, you will see
something like this. And if you put your
height that is 180 mine and my weight is 92.6. In kilogram, if I
compute the BMI, it will give us 28.6, right? So we want to create this calculation system,
this calculator, right? Pause the video in here,
try to do that yourself. We will do it in a second. So for doing that, we need
the weight and height. So let's just go here, go inside the Python, and in here, I want to say
weight is equal to 2.6, right? And the height is equal to 1.8 because the height
should be in meter, so that's why I put
height equal 1.8. Now we have two variable. One of them is saving weight, and one of them
is saving height. Now we can say BMI is equal to weight divided by height
and power of two, right? But in here, everything
should works perfectly. But for it to be more readable, I want to use prances, right? So in here, I will wrap this
between prances so everybody that want to read or code knows that we want
this to happen first, and after that, the rest
we want it to happen. Now with that, if we had
enter and show the BMI, it will give us 28.58, right? So it's really perfect. This one is calculating the BMI and it will
round the top, right? It was 28.58 as we calculated. O code is much more
precise than this one. You can see 28 that 58, but this one is says 28.6. If you round this up, it will be 28.6, right? We don't want to round
it up. This is good. Now, with that done, you can
see if the BMI was 25-29, it means we have overweight. So you can see, I have
overweight. That's it. This was our first project
to calculate the BMI. And let me put some
other values in here. So for example, with up aroky
I want to go to weight, and I want to change
the weight to 90. And with up arochy, I want to set the
height as well to 1.86. So I did decrease the weight
and increase the height. Now, I want to use
that formula again, head enter with that, and now let's just show the BI, head enter, and now
you can see 26. Let's just go and
put it in here. 186 with 90 kilograms, right? If I compute, it will be 26. So this one is perfect. No, we wrote a code that
can calculate the BMI. No, with that done, I want to do the same inside
ID because this is not cool. This is one line
execution each time. I don't want to do it like this. I want to create
a project for it. Let's see how we
can do that. Next.
19. 18 (Project) Calculate BMI python project in vscode: No, we want to do the
same inside the IDE. This is the fun part. Let's just open up the visual studio code. We are inside the
Python Project folder. And inside this folder, I want to create a new file, and I want to call bmi dot PY. That's it. Now, VS code and Python knows that this
is a Python project. So in here, first thing, we want to create a variable, and I want to call it weight. And let's say the
weight is 92.6, right? Now, let's just save the height. Height is 1.8. It should be in meter, right? If you remember,
if I open this up, can see it is in meter
and know with that, you want to calculate the BMI. So we say BMI is equal weight. Divided by open and close
prances height, right? Now, you can see
the auto complete. You write the first letters, and it will suggest
you the height, and you can make
a power off two. That's it. Now with that done. We can go to the next line. Now with that, we can show
the BMI with a print, right? So put print there and I want to show the BMI
and put the BMI there. Now with that, we want
to run this code. But before doing that,
you can see white circle. It means it's not safe. You can go to file, save or Control or command S on
Mac to save it, right? Now, we want to run this code and how we can
run it, first of all, I want to use DIR to see what is the file that we have
inside the Python project. We have BMI dot PY, right? So we can say PY, BMI and after the head tab, so it will fill it up for you
and know if I head Enter, it will give us
the value of BMI, and you can see 28.58. So we write the code that
can calculate the BMI. And now in here, we can change the values
90, and this one, let's just increase
this like before, and no control is to save it. And in here, now we
want to run it again up arochy to put the
last command there, head Enter, and now
you can see again, we calculate the BMI.
20. 19 Create strings: No, we want to talk about strings and using
them with print. You see, we did use print in here at the
beginning of this course, and we did use print in here. And no, it's good time
to talk about it, right? Let me close this one. I want to create a new file and I want to call it strings, and I will add PY at
the end of it, right? So in here, there is multiple ways that we can
create a string, right? So let's say name, this
will be a variable. And for creating a string, I can either use
double quotation and put my name there, right. And with that, I can use a
print to show that name, just like that. It's safe. No in here, I can
say PY strings, tap and it will put it there. No, you can see it showing the string that we
put in there, right? No, I want to show you another
way of creating a string. If we go down in
here on line three, I want to say name two is equal. This time, I want to use
quotation marks just like this. And I want to call
it David, right? After that, let's just show it. Use a print to
show the name two, that's it with the autocomplete
is really easy, right? No, before running it, head control is to save it, make sure you are saving it. After that, we say PY, strings or we can
use up arrow key to put PY strings PY
there, head Enter. And now you can see first, it will say Navid
this one, first one. After that, it will say
David, the next one. Next thing that we
can do in here, we can use double quotation
and quotation mark together. For example, let's say M string. You can see the S is big. This uppercase and lowercase in the name is really important. Just remember that I want to say my string is equal to a
double quotation string. We want to say he Navid, right? Just like that. So you can see I did use quotation mark inside
double quotation marks, and that is why we have two
type of creating strings. So we can use the double quotation
and quotation mark inside the string as well. So in here, Python knows that all of these are
just a string, right? Whatever that is inside
of it is a string. Let's just show this only
use print my string, right. And with that, if we
use Control to save it and in here of Aoki at Inter, no, you can see we can print
the cutan march as well. No, this can create
some problem. Let me copy and paste all
of these in here as well. This is M string two. In here, if we put double quotation in middle of
these two double quotation, make sure you save and go
here and run the program, it will give us an error because when we start a string
with double quotation, whenever Python see another
double quotation after that, it assumes that your string is finished. It is that, right? So you can't do
this kind of stuff. If you want to use, for example, double quotation,
inside a string. How you can do it. We
can just delete this. Use a quotation mark like this. We can say Navi has double ctation like this
gun, right? That's it. So we are using
double quotation, and Python knows because this string is a start
with cuation marks. I will assume whatever is between the first
quotation mark, and the next utation
mark is a string. Doesn't matter what it is, unless it is another
utation mark. And now with that we want
to show Myastring two. So let's just put
MyString two in here. Head control has to save it. And in here, let's just run
PY strings PY head inter, and now you can see it says NawT has double quotation gone. It has a space at here, Y because we have
a space in here. So if you delete
that, head control is to save it and
run over code again, you can see it says
Navit has gone. You can see now, you know
how you can create a
21. 20 String indexing: No, next thing I want to
talk about strings more. What I want to do
I want to create a new file and I want
to call it strings. Two dot py, right? So with this in here, I want to create a string. You know how to do it. I
want to call it my string. Just remember, uppercase and lowercase in the name
is really important. I want to equal it to a string that it says hello
word inside it, right? Make sure you save always. Now in here, let's
say we want to get a one of these letters, for example, letters on location of four,
for example, right? So this H is on location
zero inside this screen. This is the location one.
This is location two. This is location three,
and this is location four. So if you want to get the
location four of this string, that is O, we can say M
string and square bracket. And in here, we want to say, give me the letter number four. And we know that letters in
a string is start from zero. So zero, one, two, three, four. So this should give
us O on output. Let's just save it
inside the variable. I want to call it A,
for example, right? And now, let's just show this
A. I want to say print A. That's it. Control A to save. And knowing here
I want to use up arrow key strings two
after that head tab. And now if you run it,
you can see it'll give us O on out foot. So
that's good, right? But getting just one letter
is not helping us a lot. Of course, at some point, we need to do that, but I want to show you more
cool stuff with this. I don't want to put these things inside another variable,
what we can do. We can just delete this and put this M string number
four directly in here. Just like that, if
I had control is, what will happen if you run, it will give us the same result. We did use less variables. It's just that, less
variable, right? Now, let's say we want
to get this W, right? How we can get it,
we can say zero, one, two, three,
four, five, six. So if I copy and paste this printer string
down here and tell it to give me the index six of this string, let's
see what will happen. Control is to save and in
here up arochy at Enter, and now you can see this
giving us W. No next thing, let's say we want just the
word part of this string. How we can do that. For
doing that, there is a way. We can say print Okay. And in here, we can
say M string again, use square brackets to
get something off of it. But this time, I want to say from this W that is index six. And after that to the end, give me all the characters. How we can do that, we
can use Colin to do that. So from the left
side of this column, we want to tell it to
start from a index and show it to me to the index that we put on
the right side of it. So, for example, the
W is at index six. So we can put six in here. And for the out foot, you can see it is D, right, six, seven,
eight, nine, ten. So you May 10, and it should be that, right? From index six to ten, show me the letters
in our string, right? So let's see what will happen. If I use Controls to
save it and in here, run, you can see, it's just showing world. It's not showing the D. Why? Because this angle
bracket, what it is doing, it is not getting that last
letter that you put in. It will end at a
letter before that. So if you want to have D, we should put 11 in here, right? So it will start from
index six to ten, and it will show it to us. Control has to save, and
knowing here if I run again, you can see it
will give us word, and that's how a string
indexing is working. Another way to do this, we can use Control
C, put it over here, Control, and in
here, we can say, from six to the end,
show me everything. So we don't need to put this 11. It will put the max index number there automatically,
just like that. It will start from
index six and it will show everything after that doesn't matter what
is after that. So let's just use
Control S and in here, run, and you can see
it showing us word. So you don't need to specify the last letter if you want
to show to the end, right? So, for example,
if I in here, say, from course and again, use Control S to save and after that run,
what will happen? Te print my String six to 11. Again, it's printing word, but this print the S string from six to after that to the end, it will print word from course. Can see we can do it
backward, as well. So let me copy and paste
this one more time. This time, let's just say we want to get
the hello, right? One way is to go from
zero to index five. So zero, one, two, three, four, and we should add one
more to it at the end, so it will put the O
and output as well. So with that, control is to
save in here, let's just run. You can see it is
giving us the hello. But again, for zero, because it is the
start of or a string, we don't need to put it there. So if I use this printer
string down here, and tell it whatever comes before index five,
show it to us. Again, it should play
the hello for us. Control us to save,
and in here, run, you can see, again, it's showing hello
for us, right, cool. So these were some
ways that we can take some part of a string
and show it in output. Another thing that
we can do in here, we can copy and paste this. Let me show you a trick. If we don't put
anything for a start and end index to show
what it fill do, it will give us
the whole string. I assume from index
zero to the end, right? When you don't put anything
there at the left side, it's put in zero
there automatically. And for the right
side of this hold, it will put the last index
there. Now with that done. If you use Control S to
save it and runaway code, you can see it says
hello word from course. So it will show
everything for us. No. Next thing that sometimes you may see is that we have
something like this. Let me use this. You can see. We have double colon, right? If we do something
like this, again, if you run over a game, you can see it will print
everything normally, right? But why we have two colon? For you to see the difference, what I want to do print I want to use a string with
some dots in here, and know if I had Control
S and in here run, you can see from these dots, we are putting some new codes
to explain some new things. So you know all of this
is for all of this, and this is for this one, right? Now let's see how we can use double colon, why we need them. So first first,
with double colon, let's just say we
want to get the word. So we did it like this, print my S string from
index six to 11, okay? Index six to 11 11 that
is in here, right? We can do the same in here. Start 6-11 and don't
put anything there at the right side of
this last column and save and in here, run or code. Again, you can see
it will give us what exactly like before, right? And everything that
you can do in here, you can do it in here
as well. Everything. But this last place that we have in here is for step size, right? So let me show you an
example. Copy and paste. What I want to do, I want
to say the whole string. So at the left side
of first column, and at the right side of it, I won't put anything. So this will give us
all the string, right? But at these step size,
I want to say two. Now, let's see what will happen. Control is to save run again, you can see what it is doing. It will get all of our
strings and the step sizes that it takes to grab
the letters, it is two. So for example, first, it
will print the first index. After that, it will
go to Index two. After that, it will
go to index four. After that, it will
go to Index six. After that, it will
go to index eight. So the step size is two. Each time the index
will be increased by two and it will show
that letter to us. That's what is
happening in here. HLO word you can read it, right? So this is for step sizing. So with this, we know
that the first colon in here is exactly this column
that we have at top here. But the next column at
the right side of it, we are deciding
the step size that this string should go and
grab the letters, right? And that's it. Now
you know about double column inside
a string index.
22. 21 String operations: No, I want to show you how
you can add strings together. Let's see how we can do that. For doing that, I want
to create a new file, and I want to call it strings. Three dot PY. And that's it. Now, in here, I want
to create a string. I want to call it my SDR, and I want to put
hello there, right? Now with that done. First,
let's just show it. Print my SDR. That's it. Now with that done. Let's just run PY. Strings three dot PY. Now if you run it, it
will say just hello. But we can add things together. I want to say my SDR two, is equal my SR plus
my SR again, right? Now, let's show
that with a print. This time we want
to print MSDR two. Let's see what will happen. In here, if I run you can see
it will add them together. Hello. And after that, hello, it will add them together.
No, we can do more. Let me copy and paste
this down here, what I want to do this time, I want to say my string tree, my STR or string tree
for both of these. No, this time, I want to add a space between these two hello. How we can do that,
we can add a plus in here and create a string
between them within a space. So what will happen?
It will say hello. It will add it to
this empty space, a string, and after that, it will add hello again. With that, control S to
save and no let's just run. You can see, no, between
hello and hello, there is a space. No. Let's just give
you another example. In here, we want
to say hello word. So we can just delete this. And inside this, I want
to create a string. I can say Wort, right? We don't need this space, so let's just get rid of that. My STR is hello, and we create a string in
here that you say Wort. No, I want to change
the name of this to four and in here four,
so we can show it. Control is to save. No,
in here, let's just run. You can see it says hello word. No, we want to add a space
between hello and word. You can see I can add a space in here and control is to save. Now, in here, if I run, you can see it says
hello word, right? No, MSTR four is
hello word, right? Let's just add to it. For example, I can
just say print. And inside this print, I want to say MSTR four plus. We want to add a string to it inside these princes.
We can do that, right? I want to say space
after that from course. That set control is to save. Now in here, if I
run, you can see, at the end, it should say
hello word from course. No, we did use adding a
string together, right? We have multiplication as well. So let's just say MSTR five is equal to a
string N, right? So what will happen if we
multiply this by a number? So for example, let's
just say print. And inside that, you want to say MSTR five, multiply by five. What will happen, it will
print MSTR 55 times. So it will have five N
after each other, right? Control S to save and
in here stron you can see it will
print N five time. So you can see, we can
add strings together. We can multiply strings with
the number as well, right? But let's say we want to
do something like this. We want to say print. A
string doesn't matter what, which one you want to use
plus a number, right? Let's see what will happen. We are adding integer two, not a string two, right? So in here, if I run it, you can see it'll
give us an error. So you can add strings together. You can multiply
strings by integer. But you can't add a
string to a number. Float number or integer
number doesn't matter. You can't do that. It
will give you an error. If you want to add two, at the end of the string, you can wrap it around double quotation or
single quotation, right? Save and know if I run, you can see it will say N two, because we add this
myString five that is N, a string of N, and we
have a string of two, so it can add it together. Know you know about how
you can add a string together and multiply them what you can do and
what you can't.
23. 22 Method and documentation: To this point, we did work
with the strings a lot, but I want to show
you another thing. So in here, let me
create another one, and I want to call it
strings four dot PY, right? And in here, I want
to create a string. Let's just say my STR is equal. Hello word, right? Now with that, this
string variable has some methods on it. It has some functions that
you can use with it, right? So for example, if I write my
STR and put dot after that, you can see it will show
us a lot of functions. And whenever you see some box
like this, with this color, it means it is a function or method
that we can use on it. For example, if I want this
string to be in upper case, we can say upper, and
it is a function. So we should add open
and close parentheses. Know what this will do. It will take this string and make all the
letters uppercase. So for showing you that, I want to use a print, open and close parents
and with that, I want to cut this with control and control V
to paste it over here. Now, if I save in here, go to right pie strings. Four and head tab, you can see put it there. I I run, you can see the hello
word no is in uppercase. If you want it in lowercase, again, let me copy this,
paste it over here. And in here instead of upper, we can say lower, right? Again, because it is
a method or function. It needs a open
and close prances. Now with that, if
you run, first, you can see it will write it
in uppercase and after that, it will write it in lowercase. If I change one of these
two uppercase, for example, this L in here and
run or code again, you can see, again, it will put it in lowercase. So this uppercase, lowercase, doesn't care if one of
these are in uppercase, one of them in lowercase, it will change whatever that is uppercase to lower
case in this case, and this one we'll change whatever letter
that is lowercase, it will make it
uppercase, right? No, another thing. Let me paste it one
more time this time. I want to capitalize it, right? Capitalize. That's it. If you want to see
what this can do, you can hover over it, and it show you a version of
documentation that it says, return a capitalized
version of this string. More specifically, make
the first character have uppercase and
the rest lowercase. So let's see what will happen. If in here run, you can see the hello word, the first letter
it's in uppercase, and the rest is
in the lowercase. So we have lots of functions, and if you hover over them, it will tell us what they are. But there is other ways to
read through documentation. And I don't want to limit you to this course or
any other course. Maybe in future,
there is Python four, Python five, and you want
to learn them as well. And how you want to learn them. One way is with course, but when you get to
be a professional, you don't have time to
go through courses. So what you do, you
go to documentation. So if you go into the Google, search for Python you
can find the page of Python and you can open
up the documentation there. So open up documentation. You will see
something like this, or you can just click on
documentation after that Doc. And in here, I can click
on the Python Docs, and it will open up the
documentation in here. There is a lot of
things in here, but the best way and
easiest way to find things that you want is
in library references. So with the middle most, I will click on it to
open it over here. Now in here, I
want to check what these MSDR or String can offer us like this upper,
lower capitalize, right? So what I want to
do I want to use a type function, right, and put the MSTR there to see what is the type
of this string, right? And I want to show this
type with a print with open closed prances like this. And know if I run, you can see it says it
is STR or a string. So we can go here in
Python Standard Library, head Control F to search
for SDR. Now go down. You can see text
sequence type STR. No, let's just open that. Now, this is a documentation
for the SDR or a string or this that
we create over here, right, that we use upper, lower capitalize on it, right? Now, in here, if you go down, you can see there
is a string methods in here that you can see
they capitalize in here, and it will tell
you what they are, and it will tell you if they change in other
version as well. We have as fold full
documentation in here. We have center, count, encode, ends with expandabFin
cool format. We will talk about
this format as well. If we go down, you can see
there is a lot of them. Doesn't matter how many
course you want to take, you need to be familiar
with the documentation. That's why from now on, I try my best to show
you the documentation. So you can work
with documentation. So in future, if you
want to do something new that none other
course is teaching you, you can find it
inside documentation.
24. 23 Format string method: If you remember, we did calculate
the BMI, right in here. But we were just showing
a number, right? Know what I want to do. I want to show it with some other text so it will
be more professional, right? We are making our
BMI project much better every video. So
let's just do that. In here, I want to copy all of this code for
calculating the BMI, and in here, I want to create a new file and I want
to call it format. String dot PY, right? And here, I want
to paste the code. Now I want to show the
BMI in a better way. And how I can do that, I can say print. BMI is, right? We want to say BMI is this number that we
have in here, right? But how we can do that? Can we just say plus BMI? Can we do that or
not? Let's see. If I say Pi formatted
string head inter, you can see it will give
us error because we can't add a float or
any number to a string. We need to first convert
this BMI to a string, and after that, added to
this BMI is a string, right? So for doing that, we can use a function
that is called STR. This STR function can convert whatever you put
in there to a string. So if you put BMI there, no, let's see what will happen. In here, if I run
the code again, you can see it says BMI is 26, but we need a space in here, so we can just add
a space in here, know if it's safe. We can run it, and now
you can see BMI is 26. You can see the difference. From before, we were
just showing a number, but right now we are
telling it what it is. And this is not the only
way that we can do it. There is better ways as well. For example, if we
go to documentation, we have a method for strings
that is called format. And with this format, we can inject numbers and whatever we want
inside a string, much easier, much more readable. So let's just go
here on a say print. This time, again,
I want to say BMI is open and close
curly braces, right? This is a string that has
open and close curly braces, and with that, we can format it. Just a dot after that
and write format. And this is another
method that you can see it in documentation. You can see how they did use it. I will explain it completely. Don't worry about it. You don't need to go to documentation. For this one, if you
forget it in future, you can go to documentation, and it will be much easier to understand it after watching
this course, right? Now, what will happen? This format method search
through the string. And it will replace the curly braces by whatever we put inside
these parentheses, for example, BMI, right? Control is to save. Now, in here, if I run, you can see again,
it says BMI is 26. So either you can use this
version or this version, there is other versions as well, but you can use both of them. This way is much cleaner and more beautiful and
more professional. Now that we know the basic
stuff about the format method, let me explain it more and
give you lots of tips in next.
25. 24 Format string tips: Now let's see what other things we can do with this
format method. I want to copy and paste
this what I want to do. I want to show the height
and weight as well. So in here, I will say weight is equal to open and
close curly braces. And height is equal to open
and close curly braces, and BMI is open and close
curly braces, right? Now what will happen. How we can replace all of these curly braces
with this format. We have just one in here, but no in here, format will check the
string from left to right. First curly braces that it see, it will replace it with
the first input in here. For example, wait. That's it. I didn't write this correctly, so let's
just put it there. No, again, it will
go to right and when it see the
next curly braces, it will replace it with
the next input inside it. So let's just put
height in there, right? And again, it will go to right, and when it see the
next curly braces, again, it will replace
it by its next input, that is or BMI, right? That's it. No, it's just
save and go here and run it. You can see weight is
90 height is 1.86. BMI is this. Now you can see it looks more
beautiful, right? It's not just this to
check from left to right, first curly braces replaced by first input, second
curly braces. Replaced by second input, third curly braces,
replaced by third input. There is other ways as well. So in here, let me
copy and paste it. We can give them indexes. For example, we want to say, this is the first index. This is the one index. This is two index, right? So what we are telling
it to do with this is that replace these curly braces with the input zero.
That is this one. Replace these curly
braces that has one inside with the input index one in the second input, right? And replace these
curly braces that has two it with the zero, one, two variable inside, zero, one, two, input. Okay? No, this will
exactly work like before. Let me run it and you can
see exactly like before. Now, let me give
you another example so you will understand
this better, copy and paste it over here. If I put all of these to
zero, what will happen? This format will replace all of the curly braces that has zero inside it with
the weight, right? Because this is the input zero. So now if we save and run
what will happen, it says, weight 90, height, 90, BMI is 90, right? So you can see how it works. We can give it numbers and we
can do it however we want. Let me copy and paste
it one more time. We can say this is two, this is zero, and this is
one. Now what will happen. It will replace these
curly braces that has two inside with the input
two with VMI, right? Next, it will replace
these curly braces that has zero inside with the
weight that is input zero, and it will replace
these curly braces with the number of one inside with the input one
that is over height. Now, let's just save
that and run it. You can see weight is 26, height is 90, and BMI is 1.86. You can see how you
can work with it. It's not just this. There
is other ways as well. Let me copy and paste
this one more time. The format give us
a lot of options. So for example, in here, I can change this
to W. I can change this to I can change
this to B, right? So what will happen? No format, how does it know which one should be replaced with
which input, right? For format to know that, we can do it like this. W is equal to weight. H is equal to height, and this one B is equal to
BMI. So what will happen? Format will replace
these curly braces with the W. W is over weight, so it will replace
this with this weight. Again, for the height,
it will do the same. So it will replace these
curly braces with H inside it with this height because H is
equal to height, and so on. So let's just save
and run it again. You can see exactly
precise, right? You can see in here, we did replace some integer. We did replace some float value, and again, some float value. It's not just that. We can use whatever type of variable
that you want in here. And this format
will convert it to a sring and it will replace
these curly braces with it. So for example, let me in here. Create a variable
and call it name. Name is equal, David,
for example, right? And know it that we can go down in here and I
can add some string. You can see how easy
we can do that. We can say name is equal to open and close curly braces
and the rest, right? And in here, I can just put N in there,
right for the name. And at the end or
at the beginning, doesn't matter where
you are to do it because you reference
it with a letter. No, in here, I can say N
is equal to name, right? This name is a string, and again, it should
work. Let's see. I did use Control S to
save. Now, let's just run. You can see name is David, weight, height, and BMI. You can see how easy you can
use format to put a string, float, integer, and all of that inside a
string and show it. And you can do the exact
same thing that we did in here with these
numbers as well, right? So let me copy this. Control C to copy it
and paste it over here. No, we want to add this name. We can just copy it like
this from here to here. But instead of
giving it a letter, we want to use numbers.
So three, right? So this time, the priority
of these inputs is important because we did use numbers for referencing
curly races, right? So this time, we need
to put the name at the end at input three because we put
three in here, right? Now, with that, if we hit
Control S and Runo game, you can see name Na weight, 90 and everything
is working fine. But another thing in here, we could just put
this an equal name to middle to the beginning to wherever you put
it, it will be okay. But when you use numbers, you need to be
careful about where you are putting your
inputs inside this format. And that's it. Now you know
everything about formatting a
26. 25 Format with fstring: No, I want to show you a newer method of formatting a string.
Let me show it to you. I think you would love this because it's more
cleaner than this one. What I want to do, I
want to get all of these for calculating the BMI and the name and
everything, right, and create a new file
and I want to call it format string two dot PY. And knowing here,
I want to paste that code with Control
V. Knowing here, we want to show the BMI. So we can just say print, right? You know lots of methods. No, I want to show
you a new method. You say F and after that, to double quotation, and know inside this
double quotation, you can say BMI is this time open and close curly mbaces and inside that, we can say BMI. That's it. Beautiful, right? Yeah, that's cool. Now, let's just run it to see
if it's working. Um, I want to say PY format
string two, right, and Shron. You can see BMI is 26. So you can see this
is working fine. Why I teach the old
way because if you go inside a company and
you are part of a team, they have all their
code as well. So that's why you need
to know about all of these things that
we have done in here so you can work with them, work with your teammate, right? So that's why I did teach those, but this is good as well. No, we can do lots of
things with this, right? We can say F double quotation
mark and knowing here, we can say name is equal curibrass and
inside that name, right. After that, we can say
weight equal weight, right? After that, we can say height is equal to
curibrass height. And after that, we can say BMI is equal curbrass BMI,
right? That's beautiful. You can see how clean it is. So in here, if I run, you can see name is David, weight, height, and BMI. Everything looks perfect, right? I suggest using this, but the older way you need
to know about them as well. Even this one, you need to
know about them as well. There is lots of
code in Internet, even from big companies that are using the
method as well. So you need to know
about everything. That's it. Beautiful,
clean, readable.
27. 26 Create list: No it's time to talk
about the lists. What is a list? We did work with it before, but you may didn't know. So for doing that, let me
create a new Python file, and I want to call it List
one and PY dot PY, right? So in here, we did say my STR, a string of hello word, right? And with this,
what we have done, we did get some
number of it, right? Like this, we say, for example, give me the right? And we did show it
with the print. So let me wrap it
around a print in here. And with that, let me run it. No, you can see it did give us And if you put one in there, it will give us E, right? So let's just save
and Run, you can see, did give us E. This
is called string, but in other words, we can call it list of letters. That's it. List of
letters. Cool, right? So we did work with
list of letters, but there is a
specific variable type that is called list. So let's see how we
can create a list. I want to say my list is equal, for example, one,
two, three, right? So this is a list of numbers. This is a list of letters
or list of characters, and this is list of numbers. And like we get one of these characters and
show it with this print, we can do it in here as well. We can say, My list, right, and we want to get
the index zero of it. Index zero is this one in
here, right? That's it. No, let's just show that. If you run, you can see it give us one in
here. Cool, right? So it is exactly like a string, but we can have things in it. So, for example, let's just
say my list two is equal. For example, at the first
point, we want to say 1.6, and for the other one, let's just say a string, right? Let's just say David. Next, we can add
whatever we want. For example, ten,
an integer value, and after that, another
string like Hello, right? We can do lots of
things with it. We can save lots of things
inside this list, right? So we can go here,
print one of them. Like, let's just say
my list two of these, my list two, the number one. It is a start from zero, one, two, three, right? So if you put one in there, it will give us
the David, right? So let's just run and you
can see it, give us David. If you put two in there, zero, one, two, it
will give us ten. Let's just save and
after that run, and you can see it give us ten. So a list can save whatever
you want inside it, and you can get them and
change them as well. So for example, in here, I can save my list two, and in here, we can say, change the David that is
index one, 01, right? Change it to, for
example, max, right? So Max or whatever, right? No, if we show the
value on that position, let me go here and tell
it to show us Index one, 01, that we change it to max. Let's see what will happen. So you can see, we did
change the value inside it. It's not just you
can change a string to a string or float to
a float or whatever. Let me do something. In here, I want to say print, and in here, let's
just say my list too. Just with that, let's just see what it will show it to us. You can see it will show us the whole list for us so we
can see what is inside it. So this is cool tricks that we can learn the lists, right? So, no, we did change
the David to Max, right? No, we can change
it to a number. Let's see how we can do
that. We can say my list. Two, change the index one, that right now it is max. We want to change it to numbers
like 100, right? Why not? No, let's just run
and show that. You can see it is 1.6 after
that 100, after that ten, after that hello, you can see, we can change things
inside a list. We can take things from
the list and use them, and we can manipulate items
that is in the list, right? Now, you know how you
can create a list, but I want to give you
lots of tips and tricks.
28. 27 List slicing: Now let's just talk
about the list more. For doing that, I want to
right click on this one. I want to say close others, so we have a clean
slate in here. Now, I want to create a
new file and I want to call it last two dot PY, right? Now with that, what I want to
do I want to create a list. I will call it my list is equal. Let's just say one, two, three. That's it. Just like
that, we have a list. At the end, let's
just show it, right? Print. And I want to show
the list, my list, right? Now with that done,
let's just see how we can run it, my list two. And now you can see it's just
showing us one, two, three. Now, let's just say we want to add something to this
list. How we can do it? We can say my list,
add another list. So this square brackets
that we have in here, it's representation of a list. So we can add four to it. List that has just one input
inside it and it's four. Now let's see what will
happen if we run it. You can see I did run it, and again, it's not added there. Why it's not added there? Because these kind of
things when you do it, you need to save it inside itself as well,
so it will happen. So for doing that, we should say my list is
equal my list that has one, two, three inside it
plus four, right? Just like that. No,
if you run or code, you can see, no four
is in there, right? One, two, three, four. And
it's not just like that. We can add more to it. For example, in here, I can say, my list is equal, my list. Okay, you want to
add more to it. Let's just say we want to
add a string, David, right? After that, I want to add another value like 5.6
a float value, right? That's it. Let's
just test that, no. If you run, no, you can
see the David is added, and 5.6 is added to it as well. Now with this list that we
have in here that has one, two, three, four, David, 5.6 in it, we can do lots of things that we
did with strings. We can do it for this, as well. So, for example, you can see this list is started from zero, one, two, three, four, five. Let's just say we
want some part of this list from index
tree. How we can do it? We can say my list, give me with this call and
we did it for a string. We want to say zero,
one, two, three, from index tree to the
end give the list to me. And I want to show that with a print like this,
print it out, right? It won't change the list at all. Now with that, if you run, you can see from four David 5.6, we get it and we did show it. Of course, again, it's not
changing the list at all. If I print it again in here, you can see it's not
changing it at all. If I run, you can see it is
exactly the same as before. We did get some part of
it to use it, right? We didn't change it. If
you want to change a list, you should say it is
equal to that, right? Now, let's just delete
this. What else we can do? We can do whatever we did with the string
in here as well. So we can say my list. For example, we want
to say with the sullen that whatever that comes before David, show
it to me, right? So zero, one, two, three, and four is David. So we put four in there, so it will get the
four, three, two, one. It will get it and it will give it to us, right?
And it will show it. Let's just run, and
now you can see it just gave us one,
two, three, four. No, let's just say we want
to get the David and four. How we can do that. We did
it with strings as well. We say, my list. And with Colin, we can do that. So, for example, in here, we want to get the
four and David, right? So zero, one, two, three. So the start point should be three at the
left side of Colin. And at the end because
we want to get the it is zero, one, two, three, four and five, because when we put five, it will start 4-1 index before that index that we put at the right
side of this column. So if you put five in there, what will happen, it will
give us David and four. So let's just see if it's
working fine or not. Run and now you can see. It did give us four and David. Now, you can see
how easy we can do things in the list like we
did for a strings as well.
29. 28 List methods: Now let's just talk
about the lists more. I want to show you some
method for the list. For doing that, I want
to create another file. I want to call it list
three dot PY, right? And in here, what I want to do, I want to create a list. I will call it my list. Is equal, let's just say
one, two, three, right? Now, with that done, I want
to see the type of it. If you hover over it, it
will tell you a list of end. So this list is
important, right? So let's just go
to documentation, go to library
references, and in here, head Control F to
search for list, right? Go down until you see the list, and you can see sequence
type list topple range. Just the list we want. So click in here and know
in here if you go down, you can see there is lots of
things that we have in here. I will explain the
most important one, like how we can create a list, how we can sort things out. And let me go up, I think here in here, can see how you
can change things. You can append,
clear, copy, extend, pop and remove reverse,
all of that, right? So in the documentation, you can see, there is
lots of things to learn. You can see how big it is. I will show you the
most important ones. Now, in here, we have a list, right? We want to add to it. In last video, I did
show you how you can add to this list by
adding a plus, right? There is other ways. So we say
my list dot append, right? And this append is just
a method or function. So that's why it need open
and closed parenthesis. And in here, let's
just say we want to add four to this list. After that, you want to show it. So with the print, I
want to show the M list. That's it. No,
control is to save, and in here, let's just run it. Pi is three and head inter. You can see with the append
with it at one element, one item at the end of our list, right? We can do more. So for example, in here, I can say my list
dot append, right? It is a method, so it needs
open and close parenths, and I want to add a string, for example, David, right? Control is to save. And after that, I want to show it one more time over here. No, let's just run in here. You can see No, the David
is added to our list. So with the append, we
can add the stuff, right? There is other things as built. So, for example, let's
just create another list, and I will call it my list two. It's equal to the five, three, eight, two, one,
four, six and seven. You can see this is a list of numbers that it's
randomly placed, right? There is a method inside list class that it will
allow us to sort it out. So with the sort method, that's we can sort
these kind of things. So print. And in here, we can show thee my list too. Now, let's see what will happen. If you run, you can see O numbers in our list
is randomly placed. But with this sort method or function, it did sort it out. One, two, three, four, five,
six, seven, eight, right? We can do the same with
the letters as well. So for example, in
here, if I say, I want to randomly put
some letters there. For example, this one, I want it to be A, right? This one I want it to be B. This one I want it
to be C. This one, I want it to be F, this one, I want it to
be L or whatever, right? This one to be G, and this one, just put J. This one, just
deleted at M, right? So we randomly put some
characters inside this list, and let me change the name
of the list to list three, so you have all of them, always. Now with tatonT sort, we'll sort this out correctly
based on alphabet, right? So let's just run. And now you can see ABC FGJLM and you can see it
did sort it out. So this was for sorting. Next, let's just
say we have a list. Let's just call it my list four, and it has some
names, David, Max, and let's just put some
numbers there like 2.6, 8.9, and some big value like
this and like this, right? So we have some
integers, some floats, some text, right
inside this list. And what we want to do, we want to be able to
delete some of them. How we can do that. We can say, my list four dot POP Okay. What this pop two, it will delete one item at
the time from the list, and it will give it to us and
we can save it somewhere. So in here, let's say we
want to delete the max. So Max is zero, one index one, so
put one in there. And what this will do, it will give us a value on output like this and
we can save it somewhere. We can call it popped item is equal to the item that
we popped that is max, right so in here, I want to use a
print to show it. And for doing that, as an exercise, I
want to use a string. So in here, we say Last
is my Last four, right? And we popped open and
close curly braces. We say and we popped popped
item, and that's it. I think that looks beautiful. Now, if you run or code, you can see it says
List is David, 2.6, 8.9 and this, and we popped
Max off of it, right? You can see how
beautiful it is to use print FS strings and how
we can pop something up. As you can see in
here, in this list, we don't have Max anymore because we did
pop it off, right? Ooh. No, we know how we
can add to our list, sort our list, and
delete from our list. I want all my examples
to be practical. I don't want to overwhelm you
with lots of information. As you can see till now, I did teach you integer, float, strings,
and know the list. I don't want to show you all the variables that
we have in there and all the methods because it's too much for you and
it will be boring. That's why I'm using a practical examples
to show them to. So that was about some
important list methods.
30. 29 Create dictionary: The next variable type that I want to talk about is
called dictionary. So let's just go inside
the VS code in here. What I want to do, I want to close everything and in here, create a new file and
I want to call it dict dot py. Dig one, right? Now, in here, first, let's see how we can
create a dictionary. It's like a list, but instead of just an
index that is a number, we can give any value that
is inside that dictionary. A key. So with that key, we can get the value. Let me give you an example. I will say my dict, right, is equal for creating
a dictionary, we need open and
close curly braces. And first, we need to specify the key, for
example, in here. I want to say key one and for the value that
this key represent, we add Colin and we
put the value there. For example, we can
say 100, right? This is or value. And this is our key. So whenever we want to get this value, we use this key
to get it, right? Let's just add more,
and we will talk about how we can
get them as well. The key should be a string. So in here, create a
string either with double quotation or single
quotation, doesn't matter. For example, in here,
I want to say key two. You can name it
whatever you want. Doesn't need to be key,
right? Whatever string. And the value that
it is representing, let's just this time use a
float value 10.6, right? And let's just add another one. This time, I want to use single quotation and I
want to say P three. And in here, the value
that it is representing, I want to use a string, for example, David, right? Just like that, first, let's just show it with
a print midt, right? That's it. No,
let's just run it. We say PY. In Linux and Mac, you should say
Python three, right? In here, we say PY, dit, right? Head inter. No, you can see, there is
a key value pair in here. Now, in here, let's
just see how we can get a value from
this dictionary. For doing that, I will say
my dit and like a list. Again, we need this
square bracket, and we can tell it to
which value I need, right? For example, if I want
the 100, we pass K one. If we want this
value, we pass two, and if we want the David text, we can pass three. So in here, the way that we want to get things
is with a string. So for example, in here, I want to get the key
two, for example. So we put the key two
string in there and know it will return the
10.6 for us, right? And we can save it
inside somewhere. Let's say, for example,
A equal to this. No, with that, we
can print A, right? Print, A, that's it. Let's see what it is giving us. If we run, no, you can
see it will give us 10.6. And for you to see
it better because we know about FS strings,
we can say F, doll quotation in here, and we can say A is
equal to curly braces. A, that's it easy, right? Now with that, you can
understand it better. Let's just run. No, you
can see, A is 10.6. We get it from a dictionary that its key is called
key two, right? That's it. No, we did create a dictionary and
we use it, right? Let me give you more
example in next.
31. 30 dictionary practical example: So now, let's just have a practical example
of dictionary. So in here for that, I want
to create a new Python fall. I will call it dict
two dot PY, right? And in here, I want to
create a dictionary. And this time, I want to
call it David dit, right? David dit. So we want to save all the information about David inside a
dictionary, right? So we say equal to curry braces. And in here first key, I want to save the name, right? So the key will be
name and the value, I wanted to save the
name is David, right? So put David there. Next, we
want to store the weight. So in here, again, the key should be a string. So we say weight, and the value of this key
that is weight is, let's just say 90. And next, let's just
store its height, right? Again, we need the key. The key. If it is height, it will
be more readable, right? So in here, the value that
this height represent, we want to say 1.86,
right? That's it. Now, we have a
dictionary for David, and we can use that dictionary. For example, let's
just say print, David. Dictionary, and we want
to get the name of David. That's it. We get it
like this, right? And if we run dig two, let me go to dig two, and you can see it
give us David, right? Cool. Now, let's just
get the weight of David. So in here, just change the key in here to weight
and pass it there. Or you can just
copy it from here, Control C to copy it and control
V to paste it over here. Now, again, we can take this, copy it and paste it over here. This time we want to show
the height of David, so we can put the
height in here, Control S to save, and know in here, I can run it and you can see it
will show us the name, the weight, and its height. So you can see how
readable it is. Dictionaries is really
good for readability, because everything in
here is self explanatory.
32. 31 Dictionary tips: No, I want to talk more
about dictionaries. So in here, I want to
create another file, I will call it dit
t dot PY, right? And in here first, let
me create a dictionary. My dit, right, is equal to. Let's just say the first
key I want it to be name. And the value that
it represents, let's just say, David, a text or a string of David. And after that, I want to save the things
that David bought, right, bought on a store, right? So, for example, in here, we can give it a key
items bought, right? This is the key,
and for its value, we can use list, right? That looks awesome. So in here, we can
give it a list. So, David, the item
that he bought is this. For example, we can add
a mouse after that, you know the list how it works. He bought a keyboard, right, put some things there. He bought a monitor, so put monitor there,
and that's it. You know how list works. We can put float value in there. We can put integer value, string value there,
and we can do it for the dictionary, as well. So in here, for example, at the next key pair value, I want to say the
price of all, right? So in here, I need a key. You can say total price, right? And for the value,
let's just say $500 for all of them, right? So now, we have a
dictionary that is saving a string that
can save a list, and that can save
numbers as well. So no, let's just see
how we can get them. We did get a string. Let's just see the total price. Oh, I can do it. I
want to use a print. And in here, I want to use a
string. To show it, right? We want to say, David spent
$500 in a store, right? So for doing that,
we can say, first, get the name with curly
braces and say my digged, right, and we want to
get a value inside it. We want to get the name first. So what will happen first, it will print out the name. And after that, we say
pint for example, $500. And how we can get
this $500, again, a curly braces and
inside it, we say, M died and we want to
get the total price. So in here, we add a square bracket to
get the total price. And now let's just run
the dig tree like this. And now you can see it
is say, David spent 500. So if we change things
in here, for example, let's just 700 and
David is Max, right? If we do that and run over code, you can see Max has spent 700. Now, we can add more to this. After saying that
it has spent 700, we can say D item that
was butt W. So again, we need a curly braces, and we want to get my
dictionary item butts so it will give us all the items
in here, the list, right? So in here, we can say, My dit, give me the items bought. That's it. Now, let's see
what will happen with that. We run, you can
see Max spent 700. And the item that was
bought was mouse, keyboard, and monitor, right? Next thing that we can do, we can get this list element one by one if we
wanted to, right? So for getting one of
them, say for example, my dig, right, and we want
to get these item bots. So we get the item bots in here. We put the strings,
item bot either with copying this or
auto complete, right? And what this will give us, it will give us this list, and we can save it
inside the variable. For example, items, right? Whatever name that you
want to put in there. Now, let's just print the item, print item. Items, right? Now, let's just run.
And now you can see it is showing us the items, mouse keyboard and monitor. Let's just say we want to get the first item
that we bought. How we can do it
because this item is just a list because
you see it is a list. We can get items off of it. So we can say like this, give me the item in
index zero, right? So just like that,
control is to save. And now if we run it, you can see it will
give us mouse in here. If you put one in here, it will give us keyboard. Make sure you save
before run no, you can see it is
giving us keyword that is professional and beautiful
and readable, right? You don't need to
save the list that you get from a dictionary
to show it in a print. The easiest way is to get this my dictionary like
we did it over here. And like this, we know that the dictionary for item
boot will give us a list, so we can get a list
item from it, right? So like this, we can say zero. Give me the items bought value that is inside the dictionary,
that is a list. So we know that this
will give us a list. We need the index zero
of it that is over most. So with this, it works as well. If we run, again, you can see it is
giving us mouse. So you can do both, but for it to be readable and
easy to develop in future, it's better to stick
with this version. So just save the list that
is inside a dictionary, inside a variable,
and after that, use that variable, right. This way is much
cleaner to do things, and it's easier for team works. So now, let's just
save, and in here, run, and you can see
it is giving us mouse.
33. 32 Dictionary operations and methods: I want to show you how you can edit a dictionary and add to it. So for doing that, I want
to create a new file. I will call it digT
four dot PY, right? And in here, I want to
create a dictionary. So my digT and equal
to a dictionary. First key, let's
just say name and the value that is representing let's
just say David, right. And after that, another key, let's just say weight and the value that
it's representing, it's 90 integer value. And that's it. That
is our dictionary. First of all, let's
see how we can add to this dictionary
and add height to it. Right now, if you print, let's see what will happen. My digged right? And we want to run it. We say PY digged
four, that's it. Now, you can see there is
just name and weight, right? We want to add the height to it. So we can say, M died in here, we add or key. The new key, for example, let's just put inger and we want to say it is
equal to 1.86, right? After that, let's just
show the dictionary. By a print, my dict. Now, let's see what will happen. If it run, you can see, No, the height is added
to it. Cool, right? Now, let's see how we can change the things that
is already there. Let's just change the name. Or we can do it, we say M ****. The key value that you want
to change is the name. So we say Midik a
square bracket, a string of the key
that is, in this case, name, we can say it is
equal to a string max. We can do it for the integer
float and list as well. We can do it the same, right? Now with that, we are
changing the name inside our dictionary
from David to Max. No, we want to show it. So let's just use a print
in here and show it. In here, if you run, now you can see the name
is changed to Max. Now with that, let's
just see how we can delete one of these
key value pairs. For doing that, there
is two way to do that. We can say Dil or
delete. Just say Dill. After that my dictionary, you should get the item key value pair that
you want to delete. For example, let's just
delete the weight, right? So put the weight there.
So with this line of code, we delete that key value pair. Weight that is 90 completely
from our dictionary. With that, let's
show the dictionary. After that. Let's see
what will happen. If you run, you can see, no name max height is 1.86, and no, we don't
have any weight. You can see with this,
we did delete it. No. There is other ways. Right now, we have
name and height. Let's just see how we
can delete the height, but in another way. We can say my dig dot with
a method, pop, right? With this pop, we can tell
it which one to delete. And here, we should
give it the key, for example, the height
we want to delete. We can just copy it from here to make sure nothing is wrong. And because it is a key
value pair, in this case, we need to pass a value as
well that for deleting it, we should pass none to it. With that, let's just print it to see if it's
happening or not. In here, run again, you can see with the pop, we delete the height
from a dictionary, and we did show it. You can see right now there is just the name there, right? So we learned two methods of
deleting a key value pair. The difference
between these two, when you delete this way, you don't get the deleted value, but with this pop, you get something in return. You can save it inside the
variable, for example, A, and you can show it print A, right? So put A in there. At the end, it should print A, and you can see
the value is 1.86. Why? Because we did
pop the height. So that's why it returned
the height to us. So we can get the value before
deleting it. So that's it. You learn how you can
edit a dictionary, add to it, and delete
from it, and pop from.
34. 33 Tuple: No next thing that
I want to talk about is topple variable. So let's just create
a file for it. In here, I want
to create a file. I want to call it topple dot PY. And this topple, we
don't use it that much, but you should know about them because sometimes it's
really important. So in here, we call it my
topple, create the topple. And for creating topple, it's exactly like a list. But with the difference that you use parentheses for
initiating it, right? For example, inside it, I want to say ten,
a float value, 5.6 and after that, a text like David, right? And that's it. That's your topple. No, I can't tell you
what topple can do, but the most
important thing about the topple is what it can't do. For example, you can't change anything
inside this topple. You can't add to it. You can just get values from it. So, for example, if I want
to get a value, for example, this ten that is in
index zero, right, we say my topple
and like a list, we can get things
off of it, right? Control to save. And in here, let's just run it head inter, and now you can see
it will give us ten. Again, we can do it like this, give me the value that
is in index zero, one, two, put two in there, know if it's safe
and in here run, it will give us David
that is in index two. Next, let's just see
some other stuff. For example, using Colin
inside it like this. Let's see what will happen. If we run, you can see it
will give us everything. Next, we can do most things
that we can do with the list. But the difference
is that you can't add to it and you can't
change anything inside it. And when do we want to use whenever we are
working in a team, and we create a
topple, for example, from the information that
we get from the Internet, and we put it in a
topple so nobody in our team can change that after that accidentally, right? So that's why use topple
mostly data integrity, right? So for example, if
I say my topple, let's just say we want to
change the index zero to, for example, 12, right? Like this, if I save and run
it will give us an error because you can't change the topple and it
says it in here, topple object does not support item
assignment. That's it. That's everything about topple. There are some method
for topple for example, Mtopo we can say dot and you can see there
is two method in here. One is count that will count one of the inputs
that is in here to see how many of it
is there, right? So for example, if
we put ten in there, it will check how many
ten we have in here. So, for example, if I add another ten in here
and another ten, so now we have 310, right? Now, with that, if I put this inside a variable,
we can show it. Let's just use a print. And in here, let's just use
FString and over topple, there is open and closed
curly braces, put a there. This is the number of times that this number ten was repeated
in this topple, right? Of number ten, right? So now, if we save and
run, what will happen, it says, in our topple, there is three of number ten. There is three of number ten. If we change that to four, for example, no, there is
four of number ten, right? So again, if we check it, you can see there
is four of them. We can do it with
the text as well. So let's just say we have two
David in there. Like this. And now, let me copy and
paste this over here. We say B for this one, and we want to count the
time that it repeat David. So let's just copy this, control C to copy it. Control V to paste it over here. Now, we want to show the B
of David like this, right? So we say in our topple, there is B of text or a string, David, right? Like that. Let's just run and no
you can see Our topple, there is two of a string, David. That's it. Let me show you
another one, my topple. If we put dot, you can see we have index. So if you put something
there, for example, 5.6, it will first check if there
is 5.6 inside here or not. If there is, it will give
us the index of that. So let's just show the
index with the print, open and close
branches around this. Now with that, control
is to save and no, you can see it will give us one. And if you put David, let's see what will
happen because we have two David in there. So let me copy this, paste it for this to
get the index of David. Control is to save, run, and you can see
it will give us two. White give us two
and not this one because it go through our
topo from left to right. First time that it see it, it will return the
index of that. And after that, it won't
search for it anymore. That's it. Now, you know
everything about topo.
35. 34 Set: Next thing that I want
to talk about is sits. Set is not a variable, but like a function that's
really important in Python, that we can use it a lot. So in here, let me create
a new file and I want to call it set dot PY, right? And in here, I want
to create a set. I will call it my set is equal set open and close prances
that's it. That is our set. And now with this, let's
just add some stuff to it. We say my set dot add and
with AD we can add things. For example, we can add a
integer value to it, right? We can, let's just say, add a float value there, 5.6, for example, we can
add a string, as well. So again, at this time, which is add David, and after
that, just show it, right? We say print. And with that, we want to show my set. And with that done,
control is to save. Now in here, let's just run it. And now you can see
it will give us something like a dictionary
because it has curly braces. You can see it over here,
but this is not a dictionary because it doesn't have
key value pair, right? So it's for sits.
It's called sits. If you have a curly braces that doesn't have
key value pair, it means it is a sit. And what is important about
set is that you can't have multiple of one number
or one text in a set, for example, if I copy
this, let's just copy it, paste it over here, if we do this with the list, what will happen, I will
add the David four time. But in the set, it won't do it. Let me run. You can see, doesn't matter
how much you say ad, it won't add David
more than once. You can see David 5.6 15. It won't add it, right? Now, when do we want to use it? Let's say we have a list of users and one new user
want to register, right? And we don't want
the new user to choose a user name that is
already used. So let's see. In here, I want after this print my set.
I want to do that. For example, we have a list, and I want to call it
all user names, okay? And it's equal to David
Max and Navid, right? So let's say, these are all users and no a new
user want to register. So how we can do that, we can change this
list to a set. And after that, try
to add the new user. If it was added, it means they can register
with that username. But if it didn't add, it means the user name
is already exist. So in here, what I want to do
I want to change this from a list that you can
see in here to a set. So I will say all
user names set. Okay? I will add set
at the end of it. I can say sit, open and close parenss and pass all user names
that are list. So this function, this set
function that we use in here, either you can use it
to create a set or convert a list to a set. Know with that D if you show
print all user names, sit. What will happen if we
run it, you can see, no, it is converted to a sit. Can see it is a set. But if we print the all user
names that are just a list, let's see what will happen,
how it show it to us, right? All username, if we
run, you can see. First, it show it as
a list because we did show all user name that is just a list
and you can see it. It has a square bracket. You can see and after that, we are showing the set, and set is like this. No, let's just say the new username that you
want to add, try to add it. We can save all user
name set dot at. And this time, I want
to add David again. Let's see what will happen. If we show the set again, you can see if I save and run, you can see it didn't change because it was already there. And that was it. In future, we talk about sets more. It was just some basic stuff that I wanted to
show you about set.
36. 35 Inputs: No, it's time to talk about
the input because right now we are just hardcoding some
stuff inside our code, right? So what I want, do I
want to create a file? I will call it input
dot PY, right? So in here, I want to get
a number in the input. So how we can do that,
we can say input, right, and open and close parents, and we can tell whatever
we want in here. For example, please
write a number, right? And a semicolon and
space after that. Now with that, if we run it, we say PY input, head inter. No, you can see it says, please write a number. We can put ten in there and the program will be
finished, right? We put ten in there
and nothing happened. What happened to these ten? This input, whenever we put a
value there and head inter, this input function will return that value that you put in
there and we can save it. So in here, let's
just say number one is equal to input, right? After that, let's just show it with a print f string, right? The number that you enter is open close curly
braces and show the num one. That's it. No licious testate. No, you can see if
I put 50 in there, it says the number that
you enter is 50, right? Now, let me give you
another example. So, for example,
get this copy it, paste it in here, and change
this to num two, right? And after that, I want to add
these two numbers together. So I will say sum of
number numbers is equal num one plus
num two, right? And after that, I want to
show the sum of numbers. And in here, I want
to say some of numbers that you enter is
sum of numbers, right? Just with that, let's just run. First, it will tell us to
please write a number. We put like five. Okay? Again, it will tell
us to write another number. So let's just put two. So what will happen
five plus 27, so it should show us seven. Sum of numbers that you entered
is 52, what is happening. You see, when you get
something from input, it will give you that
number as a string. So if you want add it, multiply it, or do
whatever you want, you need to first convert
it to a integer or float. And how you can do that, you can do it with functions like Int, or do it with functions
like float, right. So whatever you put
inside these practices, it will convert it to that type. For example, if you put
num one into integer, it will give us
an integer value. It will convert the
string to integer value. And if you put it inside this float open
and close pranxs, it will convert it to a float. Now, let's see how
we can use it. For example, for these two
input, what I want to do, I want to convert the string that it will
return to a float, just like this, put a
float before it with open and close pranxs and we need to do it
for this one, as well. Float. Like this, open and close pranxs and
know with that, if you run, you can see first
number, I will put five, next number two,
and no you can see sum of numbers that
you entered is seven. Next, we can do more. For example, let's say
after doing all of this, again, we want another
input to put their name. So in here, I will say, write your name, right? And what this will give us, it will give us a text, and we can save it inside a variable that we
can call it name, right, because it's
easier to do that. Now, let's show that
with a print F string. So F double quotation. And in here I want to say
your name is open ankles, curly braces and put
name there. That's it. No, let's just run.
First, it will tell us to write a number five head inter. After that, again, it will tell us, please
write a number. Let's just put seven
in there, head inter, and it will tell us the
sum of numbers that you entered is 12 and know the name. Let's just put David there. If I head Enter, you can
see your name is David. Of course, you
shouldn't forget to put a semicolum at the
end and with a space. So it looks more beautiful. And here, again, if you run it, eight, enter, seven, enter. The sum of numbers that
you entered is 15. No, write your name. Let's just say Max this
time and hit Inter. No, you can see
your name is Max. No, I want to give
you a challenge. We have a BMI
calculation, right? So what I want you to
do to get the height and weight and the name of
the client and after that, calculate the BMI and
show it in output. You try to do that. We
will do it in next video.
37. 36 Bmi with inputs: Now we want to calculate the
BMI based on the inputs. Let's see how we can do
that. For doing that, I want to find the BMI. We have so much project in here, and I want to copy this part, Control C to copy it. And now in here, I want to
call this one BMI dot PY, because we are making it better, right Control V to
paste it in here. You see, in here, we hardcode
some numbers in there, but this time, we want
to get it from input. No, how we can do that, first of all, we know
that it can be a float. In here, I will say a float. After that, inside the
parenss we get our input. It needs a parenss as well. Now inside that, we need to tell it what they should enter. We say, please enter a
number and KG garam right. After that, less add
a colon and space, right? Now with that done. We need to copy and paste
it for this one as well, because we want to get
the height as well. So this time, we want to say, please enter a number
in meter, right? Next, we want to get the name, but it's more beautiful to get
the name at the beginning. So in here, let's just create
a variable, call it name. And in here, we want to
get it from input, right? We don't need to convert it to anything because by default, it is a string, and we
want a string, right? So we say input and
in there, we say, please write your name, Colin and space at the end. Know with that the BMI
will be calculated. Now we can use a print F string, F, double quotation, and
in here, show the result. But just say, dear, open and close prances, and I want to put
the name there. Your BMI is open and close
curly braces in here, show the BMI. That's it. No, let's just test that, PY, BMI, two, head tab, head inter. No, it will tell us to
please write your name. I will say Max, for
example, head Enter. After that, please enter
a number in kilogram. I will put 19 there. After that, it says, Please enter a number in meter. So we can say 1.86 and
know if I had enter, dear Max, your BMI
is 26. Cool, right? Of course, it's better
to change these text. Please enter your weight
in kilogram, right? This will be much better. And in here, we can say, please enter your height. In meter. Control is to save. No, it will be better, right. So please write your
name this time. Let's just say
Navit. After that, please enter your
weight in logram. So let's just say 90. After that, please
enter your height in meter, 1.80, right, and head Enter, and
now you can see, dear Navit your BMI
is 27.7, right? Cool. No O BMI application
is much more professional.
38. 37 Bool: No, I want to talk about another variable type
that is called Boolean. And the Boolean variable is really important
in any languages. So for doing that, I want to
go here and create a file, I want to call it boll dot PY. So a Boolean variable
can have just two value, either true or false, right? So, for example, let's just
say Am I taller or not? And I want to say it is true. You can see the true that I'm writing in there
is with big right? So when you want to put true
or false inside a variable, you need to be
careful about this. Don't write it in lower case. It should be in upper
case, the first letter. And for false, as well, it should be exactly like
this, right? But that's it. This is just a variable that
can save true or false, but how we can use it, right? When I'm creating
a woodan variable, a default, I will
put it on none. None is just a placeholder, so we can later change it. So by default, it is none, and it should be like that. Now, with that, I want to
create another variable, and I want to say average
person height, right? And I want to put, like, 170 right in centimeter. No, we want to check if
we are taller or not. For doing that, I want
to get it from input. So I will get the height
and put it inside the variable height is equal
because it can be float. I will say float, open and close prantes and after that,
I will say input. And in there, I
will say, please, write your height,
right? Just like that. And after that, we want
to check if the height of the person that are using our program is bigger
than average or not. And how we can do
that, we can say, Ami taller is equal
to height that was entered bigger than
average person height. That's it. And after
that, I want to show it. Print, just show AI taller, right? Just like that. No, let's just run it. PY, boll head inter. No, it says, please
write your height. If I put 160 head inter, you can see it says false. And let's just run it again. If I put 190, it says, true. You
are taller, right? Now, let's just run it again. If I put 170, it says, false. You are not taller, right? So what is happening in here? These are my toler. We
want to use it as a bull. And whenever we check something like a number is
bigger than a number or a number is equal to a number or a number
is less than a number, it will give us true
or false, right? So we can save it inside
a variable and show it. But right now, it
doesn't look good. So let's just use
AF string, right? I want to say result
of if you are taller than average is calibraces and show
AI taller, right? Control is to save.
No, let's just run. If I put 160, it says the result of if you are taller
than average is false. And again, if you run it and
put 190, what will happen. The result of if you are
taller than average is true. You can see no, we can do the less. We can do the equal,
equal is like this. If these two is equal together, it will return true. But if it is not equal together, it will return false. And like that, if
I put a smaller, if the height is less
than average height, it will give us true. But if the height was
bigger than average height, it will give false. That's how Booleans works, and we will work with
them a lot more. This was an example, so you will understand
the basics of it. And here, let me
change this to bigger, so code works fine. And that's it. Now, you
know the basics of Boolean.
39. 38 Comparison: Last video, we did talk about comparison if it is
bigger, smaller or equal. But in this video, we want to talk about it more because it's
really important. So for doing that, I
will create another one, another file, and I will
call it comparison dot PY. And in here, let's just
give you some examples. So for example, I will say
print to show the result. If one equal to one, what will happen if we run this, let's just say Pi Pi, comparison, a tab, and now you can see
it will give us two. White give us two because
one is equal to one. So it should give us two. But if we put two in here, one is not equal to two. So that's why it
should give us false. Now, let's just
save and in here, run again, and you can see
it is giving us falsen. It's not just comparison. We can check if it
is bigger or not. So if one is bigger than two, right now, it's not, so
it should give us false. If you run, you can see
it will give us false. But if you put t in here because three is
bigger than two, that's why it will give us true. Make sure you save,
and let's just run it again like this and no
you can see this is true. We have a smaller as well. Like this, three is
not smaller than two, so it should give us false. Let's just run and you
can see giving us false. But if you put five in here, because three is less than five, so it should give us true, and you can see, that's cool, right? No, we have more. So, for example, in here, we have not equal. So three is not equal to five. That's correct. So it
should give us true. Make sure you save and run. And now you can see
it did give us true. Again, if we put five in here, five is equal to five, so five is not equal to
five should give false. So no, make sure
you save and run, and it is giving us false. Next, what we have is bigger
or equal than five, right? Let's just say three in here. Three is not bigger
or equal than five, so it should give us false. Make sure you save and
run and no you can see, This giving us false. If you put five in here, because five is equal to five. So these five greater
or equal than five, it should give us two, right? So let's just run and you
can see it did give us two. Now, next thing, if
we put six in here, six again, is bigger
or equal than five. So that's why if we run,
again, it should give us. We have less or equal than
five in here as well. So let's just test that six
is not less or equal to five. So if we run, it
should give us false. If we put five in here, what will happen five
is equal to five. So this statement
that is saying five is less than or equal than five, it should give us true as well. So you can see give us true. Now, if you put four in there, what will happen if we run because four
is less than five, so again, it should
give us two as well. And that's it. You know about all the comparisons in Python.
40. 39 Chained comparison: Next, we want to talk
about change comparison. We want to check two things that if they are
true or not, right? So for doing that, again, I want to use this file. For example, we want to
say one is equal to one, this is true, and we want
to check two este together. Two is equal to
two or not, right? So what will happen?
This we'll check. If both of these are
true, it will return to. And because both of these
are true right now, so that's why if we run
it will give us true. But if one of them was not true, for example, put two in here, it will return false because
one of these is not true. And now, if we save
and run it again, you can see it will
give us false. How you can translate
this is like this. If this is true, and this is true as well,
it will give us true. But if any of these on right side and left
side and was not true, it will give us false,
like we have it. For numbers, we have it
for a string as well. So for example, in here, we say David and in here, we say David, as well. So if this is equal to this one and this is equal to this one,
it will give us true. That in this case, it
should give us true, right? Run, and you can see it is true. But if you change this to Mx, what will happen
if we run because this one is not true
and this one is true, this end will return false. So just run and you can
see it is giving us false. Now I want to talk
about another one or so or what it will do, it will check both of the statement and the left
side and the right side of it. If one of them was true, it will return true. But if both of them was false, it will return false. So in this case, because
two is equal to two, that's why doesn't matter
if this is true or not, it will return true. So if we save and in here
run, it will give us true. So if we change this to one, because both of the statement at the right side and left
side of the is false. In that case, it will give
us false. You can see. But if both of these cases
was true like this, again, it will give us true
because or needs just one statement at the left side and
right side to be true. If one of them was true, it will return true. But if both of them was
true, all the better, right? I will return true, as well. Now, if you save and go here, run, you can see it will
give us true as well. Now, you know how you can
chain comparisons together.
41. 41 Improve BMI program with statment: No, we want to improve
our BMI application. Let's see how we can do that. In here, you can see
if I put the height 180 and the weight like 90, what will happen if we compute, it will give us 27. And based of these categories, we can say if we
are underweight, normal weight or overweight
or obesity, right? So we can check all
of these, right? Let's see how we can
do that. In here, I want to find our BMI
that we have in here. I want to copy it and
create a new file, and I want to call
it BMI dot PY. You can see we are
making it better, right? Control to paste it over here. No, we are calculating the BMI, but we don't want to
show it like this. We want to tell the user if
they are fat or not, right? So we want to check the BMI. So let me put this aside
so you can see both first, if the BMI was less
than this value, let me copy this value. It means we are underweight, and you can see if it is equal
or less than this value. It means they are underweight. So we can say in here, if BMI was less or equal than this value that
we copied after that column, we can say print. I want to use FS string because we know FS
string how it works. Your BMI is clibras BMI, right, and you are
underweight. That's it. Control S to save. Next category is normal weight. If the BMI was between
this value and this value, it means they have
normal weight. They don't need to have
any sort of diet, right? So in here, I want to
check it with LF, right? And in here, again, we want to check the BMI. If it is bigger than
this value, 18.5. And with the end, we check if BMI was
between 80.5 and 24.9. So we can say I BMI was
bigger than this and BMI was smaller
than 24 dot nine, right after that,
we need a column. And in here, we want to print that you have
a normal weight. So paste it over here. I showed the BMI, and in here, we say normal Wait. Cool, right? Next thing that
you want to check, we want to check if
the BMI was 25-29 0.9. If it was this, it means you have a little bit of
overhight, right? So again, we can
use another F. You can do this kind of LIF
as many as you want. Again, we check BMI is
greater or equal than 24.9 and BMI was less than 29.9
after that a colon, right? So in here, we want to print that you have
overweight, right? We can change this
to overweight. And if none of these
cases was true, we can use as after
that a colon, and we can say
print for example, let's say danger, B city, right? That's it. No, with that,
let's just test it. If we run or code, the name max, for example, the weight, let's just say 70
and the height, let's just say 1.9. That's it. It is a
meter. Head inter. No, it says, You BMI is 19
and you are normal weight. That's cool. Let's
just test that. In here, if I put
190 and in here, put 70 compute, you can see, we did calculate it correctly. Now, let's just test it
with some more variables. For example, Max.
Again, in here, the weight, let's just
put 120 this time, right? And the height 1.7, right? And you can see it says
danger obesity, right? Again, let's just run
it one more time. Max. Let's just
say 92 this time, and the height 1.86 head inter and you can
see your BMI is 26 and you are overweight. Again, if I check this,
it should be correct. The height 186, the
weight 92 compute, and you can see 26.6
correctly and 26.6, you can see it is in this
category of overweight. So no BMI program is working
flawlessly. Congratulations.
42. 42 For loop basics: Now it's time to
talk about for loop. In Python, when we have
a list and we want to check all the
item inside it and, for example, show it, we use for loop. So let me create a
new file in here. I want to call it
for loop dot PY. And in here, I want to
first create a list. So I will say my list. Is equal to some stuff, right? For example, a text,
David, after that, a integer number, after
that a float number, after that really big value. After that, another
text, for example, Max. After that, let's just
say another text as well, like Apple, right, like that. So you know that how we can get one of these items and
show it in output, right? So we say my list
and after that, with a square bracket in here, we can say, for example, give me the item in index zero, one, two, three, right? Give me that and with that, we can save it
inside a variable. For example, A is equal to my list item three of our list. And after that, we can show
our variable A, right? With that, let's just run it. We say PI four head tab, and it will put the
rester head inter, and now you can see
it is giving us that exact item in index, we save it inside a variable, and after that, we show it. No, I want to show you how
we can check all of them and get each one of them one by one and
show it in output. For doing that,
we use four loop. For four loop, we say
four, and in here, like we define a
variable in here, I want to define a
variable B, for example. After that, we say NO
list, my list, right? After that, colon and no, what will happen with
this line of code, it will go through
our list one by one. And each time from
the first index, it will get that item. It will save it inside the B variable that
we create in here, and no, we can show
that variable, the B variable in here. So I can say print B, right? And again, like I
that we talk about, this indentation is
really important. So until you have
indentation in here, it means it will run that code
for that variable, right? So, no, let me run
it and after that, I will explain it more. If I run, you can see first, it will print A that is this, and after that, it will
start from index zero. It will get that item. It will save it inside B, and after that in here, it will show it like
you see it in here. Next, this four loop
goes to the next index. Next index has ten in it, right? So it will save that ten
inside the B as well, and again, run this code
with that variable. And you can see show it in here. Again, this four loop will go to the next index that is 5.6. It will save it inside the B variable and let
you do things with it. For example, printed, right? So you can see it did write 5.6, and it will do it to the end, that is over Apple, right? This is the four loop. And for you to understand it better, I want
to delete this, and I want to change
this variable to, for example, item or whatever. And I want to show that item. No what will happen. This four loop will
get each one of these items inside this list from the beginning to
the end one by one, it will save it each time
inside this variable, this custom variable
that we create in here and let us do
things with it. Beneath the four loop
if it has indentation. Now with that done, make
sure you save and run, and now you can
see it is exactly like before I start
from Index zero. Get the item, save it inside the variable that
we create in here, and know we can do things
with that variable, right? That's it. That's our for loop. Let me give you more
example in future videos.
43. 43 For loop challenge: No, let me give you
another example. For example, in here, let
me create another list. I want to call it
my num list, right? I will call it my num list. You can call it
whatever you want. And put some random
value there, right? Like this, like
this and like this, some really random value. And this one, right? Doesn't matter what
you put in there, we want to just have
some fun with it, right? Let me some more, and that I think is good. No, you see, we have
a four loop for showing all the items
inside our list, my list in here that
has everything inside. But now we have a list
that has just numbers. I want to give you
a challenge to just print the odd numbers
in this list. So you need to use
a four loop to check which one of
them is odd and which one of them
is even and show just the odd ones
with if statement. So before doing that, I want to give you some tips
so you know how to do it. As you know, whatever
line of code that you put with four indentation
after four loop, all of those codes will
happen to that item, right? But if you want to go to a new
line and do something new, apart from this four loop, you can go down and delete the four indentation
space in here, right? So for example, in here, I can create another four loop. I can say for num
in my num list, with a colon at the end of it, no, we can't print it,
right? Print everything. Print the number inside it, right? Control to save. No, first, this four
loop will happen, and when it's finished, it will go to next
line and after that, it will go to this four loop. And what this four loop do each time it will get
one of these items, and it will put it inside
this num variable, and it will run all
the code benefit if it has indentation, right until it
reached a line that doesn't have a space
indentation, right? Now with that control is
to save and if you run, you can see first four loop will happen to the apple,
as you can see here. And after that, it
will print everything, all the numbers, right? No, if I want to do
something after all of this and I want it to be
apart from the four loop, I can just delete the for space indentation and for
example, use a print. And in here, I can
say, print, finish. Execution, right? That's it. Control is to save
and no if you run, you can see the
finished execution will happen at the end of these
two for loop, right? No next thing for you
to do the challenge of this video is to
use mod or person. For example, if I
use a print to show 25 mod or person two, what this will give us. If it give us one, it means it is odd number, and if it gives us zero, it means it is even number. So in here, if I save and run, you can see it did give us one, so it is a odd number. But if I change this to 26, no, it will be even number, and if I run, you can see
it is giving us zero. So with knowing all of these, you need to solve the challenge. Just print the odd
values inside this list. You try to do that. We
will do it in next video.
44. 44 Challenge soulotion: No for printing the odd numbers. First of all, what I want to do I want to use printing here. To create a line
between these 24 loop. So you can see it better. In here, I will say showing
all odd numbers, right. So with that down, if we
use Control S and run, you can see, first, it will run the first four loop,
and after that, it will go for the
next four loop, and before that, it will say showing all odd
numbers, right? Now in here we don't want
to print everything. We want to check if
with the IS statement, if the number mod or person
two was equal to one. Colon after that. We go to next line. Disenentation is
really important. If num mod two or
person two was one, it means it is odd number. So with that, we can
print the number. But if num person two was not equal to one,
nothing will happen. It won't print, right? This print num won't
happen anymore. So control is to save. If I run nu, you can see
showing all odd numbers, and no it's just showing the
odd numbers in our list. Now I want to give you
another challenge. We are showing the odd numbers. I want you to create
another four loop. Don't use this one,
create another four loop, and with that, show
all the even number. Like we did it in
here, we say show in all odd numbers and we
show the odd numbers. And after that, you
should say show in all even number and show
those even numbers. You try to do that, and
we will do it in next
45. 45 Get all even numbers: No, for showing all
the even number. The only thing that
you want to do, we can just copy all of this, Control C, go down, delete all of this indentation because we need a new line. We need a new four loop, Control V to paste
the code in here. This time, we want to
show the even number. So change this to even number. And after that, change this one to zero. So
what will happen? This four loop will get
each one of these items one by one and put each one of
them inside this num variable, and it will run all
the codes beneath it if it has four space
indentation, right? So for each one of these, this if will happen. And if that number
person two was zero, it means it is even number,
and we can show it. But if it's not equal to zero, it means it is not even number and we don't do anything
about it, right? Control is to save
and know if I run. First, it says, showing
all odd numbers. I will show all the odd numbers. And after that, it says, showing all even numbers
and no you can see, it's showing all the
even numbers for us. Beautiful, right? So we can
use I a statement inside for
46. 47 Total price calculation: Now again, another challenge. These challenges will help
you to remember everything. So in this challenge, create a list of
numbers five number, for example, don't
make it too long. And with a four loop, get all of them and add
them together, right? So what do you want to do? We want to get the
sum of them, right? So you try to do that. We will do it in a second. No for creating that
list. I want to go here. I want to say item, but prices, for example, right, it's equal to a list. So list of the item that you bought the
prices of them, right? For example, one of them is $5. One of them is $7, one of them is $10, and one of them is $20, right? And right now, we want to get the sum of them to see how
much we did spend, right? So 20 by ten is 30 by 737, and by five, it
will be 42, right? So we don't want to do it like this with a calculator
or or mind. We want to write a code.
To calculate that. So for doing that, again, we can use a four loop. And before doing that, I
want to use another print. And in here, I want to
say total price, right. And after that, use
a four loop for, let's say, item in item
but prices, right? And a column after that, know we are getting each one
of these values one by one. And it will save it in item, and now we can add
them together. But when we are
adding them together, we need to save it somewhere.
Where can we save it? We can go up here and we can
create a variable for it. So we can say, total
price is equal, by default, none, or you can just put zero in
there, right? That's it. Now with this total
price that by default, it is zero in here, head tab. So we make sure we have
the right indentation. So all the codes will happen when these four
loop happen, right? In here, we want
to say total price is equal the total price plus the item item that we know will be the prices that
we put over here, right? It will give it to us one by one in here and we add
it to the total price, and we save it inside
the total price, right? So now with that,
we can show that. You can say print and show
the total price. That's it. Control S, add a space in here, so it will be more
readable and know if we run you can see it says, total price is 42, exactly. Let's just add one more to it. For example, $8, right? Control to save. Now in here, if you run, you can see 50. So everything is working
fine with this total price. You can do other things as well. For example, if I put
this total price print, Control X to cut, and put it beneath the four loop with the
right indentation for a space indentation and
show it what will happen each time that this four
loop go through any item, each item one by one and save it inside the item and add
it to the total price, it will show it right
away each time. So what will happen, we have one, two, three, four, five
of them, right? So five times the print
total price will happen, and first time it
should five plus zero. By default, the
total price is zero. So five plus zero, it should show five. After that five plus seven, it should show us 12. After that 12 plus ten, it should show us 22. And each step on the four
loop, it will show it to us. Control is to save, and know in here,
let's just run it. And now you can see the
total price first item is five plus zero. I will give us five, five plus
seven, it will give us 12. And after that, you
can see it reached 50. But I don't want to
do it like this. I just wanted to show
you that you can have multiple line of code
beneath the four loop. After that, we can
use a print a string, right, to show it
in a better way. So we can say total price is with curly brackets
or curly braces in here, we can show the total price. That's it. Control is to save. Now, in here, if
you run at the end, it says, total price is 50. This was another example
of using four loop.
47. 48 For loop on string and tupple: Like we did the four loop for the list that we mostly
do it for the list. We can do it for the
other things as well. Let me show you something. In here, let me
create a new file. I want to call it four
loop dot PY. That's it. Now in here, I want to use
four loop on a string. So we need a string. I will call it my STR is equal to let's see, hello word, right? With that done, we can use
a four loop like this four, whatever you want, you
can call this variable. I want to call it car or
character or whatever you want to call it in my CR, after that, a column, right. After that, if you put whatever code beneath
the four loop, if it has four
space indentation, it will happen for each
one of these characters. For H, it will happen, for E, it will happen, for
L, it will happen. So what will happen, these four will get each one of
these characters, and it will put it inside
this char variable, and we can do whatever we
want with that character. So for example, let's
just say print, right, and show the
car or character. That set control is to save. In here, I want to
run for loop two, so head tab, so it will put for loop two in
there, head enter. Now, you can see it
says HELLO space. We have a space. The space is another character,
right, WORLD, right? So with the four loop, we can loop through a string
with its characters, right? So now with that done
on to go to next line. Here, go to beginning, so I want to create a topple. So in here, I will call it my topple is equal to
open and close parents. And in there, let's just
put some values there. Like 2,055.6, a
text, for example, max, and after that, some value, one,
two, three, right. Whatever you want,
you can put in there. Now with four loop, we can loop through
all of these like we did it for list and
we did it for text. So in here, I can say four we need to declare
a variable in here, whatever name that you
use, it will be okay. I will call a for
example, in this case, in after that my topple, right, and after that, a colon, don't forget
the colon there. And with that, let's
just show it, right? With the print, we want
to show the right? So what these four loop it will get each one of
these items one by one, and it will put each one of
them inside this variable, and we can decide what
we want to do with that item that we get
from the topple, right? Control is to save. No. Let's just run it again. You can see it will
start from 25, 5.6 max one, two, three, and it will show
everything for us. That's it. So you know we can use four loop with
strings and topple
48. 49 Tupple unpacking: No, I want to show
you topple unpacking. This is a neat trick
that we use a lot. So our code will be efficient and much easier to work
with and readable, as well. So what I want to do in here, I want to create another
one, another file, and I want to call it topple
unpacking, right? Dot PY. While I'm not calling it for loop because we want to
use for loop in here. But while I'm not using for loop name for it because
it's really important. And in future, I want
you to be able to find this really easy because
it's really important. Let's say, we have a list, and I want to call
it my list is equal to a list that has some
topple inside it, right? For example, one and two. Next topple, again,
let's just say three, four, and next topple. Let's just say five
and six, right? Now with that done,
let's just see how we can use for loop on it. So we say four item, for example, in my list, right? Column after that and in
here, let's just show it. Show the items. We show printing item. Control S to save. In here, I want to say PY, topple packing and head inter. No, you can see it is giving
us tree topple, right? But W saying that
it's really cool. Right now we are just
getting some topple, right? Nothing cool about it. But we can get the items inside this topple with a four loop directly. Let me show it to you. Can you see to copy this and let's just paste it
over here like this. Know what I want to do. I want to say, with
this four loop, get the least items, and because I know that or
least has topples inside it, unpack it and save it
inside these two variables, for example, A and B. That's it. And we
can put it inside a parentheses so it looks more
readable, just like this. I love to use it
in a parentheses. This way is much better. So know it that this follow,
what it will do first, it will check all the item, and it will give us
all item one by one. But how it's saving it, it will save the one, for example, in A
and two inside B. When you doing that, we
can just show the A, for example, for no,
of course, right? Next in next loop, what it will do it will
give us this topple and three will be saved
inside A this time, and four will be saved inside B. And because we are
showing just A, it will show us three and so on. Let's just test that
I run it in here. No, you can see it is
giving us one, three, five, one, three, five, all
of the A's, right? But what I'm saying it's cool because let me
show you something. In here, we can
directly say A plus B. So what it will do, it
will give us the sum of the numbers inside
the topple, right? So with that control
S to save and run. No, you can see one plus two. I will give us three
and three plus four, it will give us seven
and five plus six, it will give us 11. You can see how much we can do with just one line of code. We don't need to go
through each topple. Just with that, we
are unpacking topple. But you may say, This is
not that cool, right? But in next video,
let me give you another example that
makes more sense. I will see you in next.
49. 50 Tupple unpacking tricks: Now, let's see what is brilliant
about topple unpacking. For doing that, I want
to create a new file. I want to call it topple
unpacking to dot PY, right? And in here, I want to create a list of topples that
has key value pair. I know it's confusing, but you will understand
why we are doing this. So in here, I will call
it temper chars, right? And in there, we want to create
a list of topples, right? And I can put whatever I
want inside this topple, but what I want to do I want to create some key value
pair, like dictionary. But this is not a dictionary. It's just list of
topples, right? So in here, I can say
London and after that, the next input, I want to say, it is 35 degree,
for example, right? After that, let's just
add another one in here. We want to say Texas, right? And it's temperature. Let's just say 58 degree, right? After that, just
add another topple. This one first, I want to name
it NY for New York, right? And it's temperature. Let's just say 25. And with this know what we have, we have a list of topples of
city temperatures, right? So with that, we can use a four loop with topple
unpacking, for example, in here, I want to
tell the four loop to grab each topple and put it
inside these two variables. For example, let's say
this is city name. So first item of topple, put it in city name. And after that, the next
item inside the topples, put it in temp or
temperature, right? And after that, we want
to say in temperatures. After that, a column,
and know with that. We have access to all the
cities and the temperature, and how these four
loop doing it, first, it will get this
first topple and it will put London inside
the city name variable, and it will put 37
inside hemp variable, and now we can do
whatever we want with it. So we can say print. We can use a F string,
right, like this. And in here, I want to say temperature of open and
close curly braces. In here, I want to
show the city name. And after that, I
want to say is, again, another curly
braces and show the temp. And after that, I can save
degree, for example, right? And with that done, let's
just save and in here. I want to run topple
packing to head inter. No, you can see the temperature
of London is 37 degree. The temperature of
Texas is 58 degree, and you can see
it, it's correct. Temperature of New
York is 25 degree, and you can see it over here. So, now with this
topple packing, we are creating a key
value pair that with it, we can loop through every city and show its
temperature with its name. So that is what's brilliant
about topo unpacking, but we are not finished yet. Let me explain some
more in next video.
50. 51 Use for loop on dictionary: Topple packing is important
in other places as well. For example, using it with dictionaries because
actually from Internet, you get a dictionary
first, and with that, there is a method
on that dictionary that will give you
a list of topples. That's why the topple
packing is that important. So for doing that, in here, I want to create a new file
and I want to call it for loop with dictionary,
right, dot PY. So in here, let's just
create a dictionary, and I want to call it
my dict. Is equal, too? If you remember, we should use curly braces for a dictionary, and it needs to be
a key value pair. So, for example, in here, I want to say, like before, London, the value of London, let's just say 28 like this. And after that, let's
just say Texas, right? And the value of
Texas key is 54, for example, this
time, and after that, let's just add another
one N Y or New York. And the value of it, I want to say 23, right? And with that, we
have a dictionary, and let's just print it to see what will happen if
we print it, right? We say my dict in here, control is to save and
in here for run it, we say PY, four and four
bit dictionary head Enter. Now, you can see it is giving us a dictionary
on output, right? But let me show you something. Let's just use a four
on it, four loop on it. So in here, we say four, for example, let's
just say item. In my dictionary, right. And with that, let's just show
the item that four loop is getting from the dictionary
each time and put it in item. So in here, if I show the item, just like that, let's
see what will happen. Control is to save run. And now you can see it
is giving us the keys. London takes us and N Y. So when we are using four
loop on a dictionary, it will give us just
the key, right? As you can see in here,
London Tis us and New York. So it is giving us a key, but how we can get the values. So in here, if we
put dot after that, you can see we
have some methods. So for example, if we say keys and add a open and
close prances, let's see what it is giving us. In here, run, and
you can see again, it's giving us
keys again, right? But we are not
interested in the keys. So let's see how we
can get the values. So in here, we say values
and open and close prenss. Now, let's see what will happen. If we run, no, you can see it is giving
us just the values. But in one for loop, we want to get both of these
key and the value with it. So before that, we show the
temperature of city is, for example, 25 degree, right? You want to do the same in here. How we can do it, there is a method in there that
is called items, right? Now with these items, method, let us see what it is giving
us each time in item, right? So in here, let me run. Now you can see it is
giving it to us as topples. You can see open and
close parentheses. So with these items method, we are converting our key
value pair to a topple. And the cool thing
about it is that we can unpack this because we
know that this is a topple. So in here, we can save
it inside the variable. Open and close, prances,
you don't need it, but it will be more
beautiful to do it this way. First, input, I want
to put a city name. So first variable
will be city name. And the next variable, I want to say Temp, right? So what this will do now
because of the items method, it will convert this to a topple and it will put the London
inside the city name, and it will put the
temperature inside the temp. Now we can show it. For example, like before, use
F string, right? So in here, you want to
say temperature of city. For example, curly braces, city name is again, curly braces, and in here, put temp in there, right? And after that, let's just
say degree like before. Rule is to save no you run. And now you can see
the temperature of city London is 28 degree and you can see
it, it's correct. No temperature of City
Texas is 54 degree, and you can see it is correct. So no with that, if we get
a dictionary from Internet, we can convert it to a list of topples with
these items method. And with that, we are getting the key value pair in
just one for loop, right, just like that. So that is what's brilliant
about topple unpacking.
51. 52 While loop: No, it's time to talk
about while loop. So for doing that,
I want to create a new file and I want to call it whileloop dot PY, right? And here, for
creating a Vile loop, you say Wile, I will explain
everything. Don't worry. You say just while
and after that, a condition that can be
either true or false, right? So in here, let's just say true. So if we do that
after that column, you can go to next line. And while it is true that
it will be always true, we can run some code. So for example,
we can use input. For example, let's just
say write your name. Right. And after that, this input will give us
a string that we can save it in name variable,
right, like that. And after that, we want
to show it, right? So print and show the name. That's it. Control is to save. Now in here, we want to say Pi, while put WH there, head tab, and it will put the
rest for you, head inter. Now, you can see. It
says, write your name. If I say Max, it
will show the max and after that because
this while is true, that's why it will go back
and it will run it again. So in here, if I say
David again at Inter, it says, David, and after that, it will tell you
write your name. So while this is true, all of this code
will happen over and over and over again until we use Control C to break the
execution of code, right? That's it. Control C. So this while true will always
happen because this is true. So while it is true, get the name and show
the name, right? But if you put false in here, let's see what will happen. If you put false in here, run, you can see nothing
will happen because this is false and none of
this code will happen, and you can see it is grade
out a little bit, right? So we as code knows that
because it's always falls, this won't happen at all. But what we can do, we can
create some other conditions. For example, I want
to say, for example, count is equal to zero,
right? That's it. And we want to say, while count is less than
ten, for example, right? Run this code. But what will happen, the count is zero and
zero is less than ten, so this will be true. And this will always happen
over and over and over again. So each time that this
code is happening, we need to increase the count, so it will happen
just ten times. So in here, we can say count
is equal count plus one. That's it. Now, the
count will be increased. And with that done,
I want to use a print to show
the count, right? And I want to use FS string, and I want to say count
is curry brackets. And in here, show
the count, right? That's it. No, let's just
save and run and no, you can see if I
put max in there, you can see count, no is one. At default count was zero, no, the count is one. And again, let's
just put some value. Count no is two, put some
more value, count three, count four, six, seven, eight, nine, and ten. And after ten, you can see it did exit out of or code, right? I don't want to get an input
always, what I want to do. I want to delete
this, so it will be easier to test it.
So what will happen? Count start from zero, and it will reach ten, right? So let's see. In here, let's just run, and you can
see count is a start 1-10. If you want it to
start from zero, because it is
starting from zero, you can just cut this print and paste above this count
plus one in here, right? So now with that, it
will start from zero, and it will reach nine. And when it reached nine, it will add one
more to this count, and the count will be ten. And again, this i loop check, know that the count is ten, ten is not less than
1010, is equal to ten. So that's why this
won't happen anymore. So let's just run one more time, and you can see the
count is 02, nine, ten times it did happen because the count is
starting from zero. That's it. Now you know
how you can work with L
52. 53 While loop challenge: No, it's time to give
you a challenge. You know how to work
with a VI loop. I want you to create a Vil loop that say hello 20
time, for example. You try to do that.
We'll do it in a second. So for doing that, let
me create a new file. I want to call it Wil
loop two, that PY. Now, for a Vil loop, we need a counter, right, a variable, so we can
increase it and check it as a condition of
our illoop, right? So for example, in
here, I can say, counter is equal to zero, and no, I want to
use a Vil loop. So while a empty space, and after that, I want
to check the counter. If it is less than 20, we want to print hello. So I should say print Hello. Knowing that we need to
increase the counter each time that the print
hello happen by one. So we can say counter is
equal counter plus one. Or you can do it in
more efficient way, you can just say plus
equal to one. That's it. This will do the
exact same thing to say counter is equal
counter plus one, right? So this line of code, it will increase
the counter each time that it's to be run by one, and it will say
that inside itself. Now, let's just say Sato. No in here for running. Just head tab, head tab again, while loop two, head inter. No, you can see it's
print hello to time. I don't want to write it. Let's just put five in there to see if it's happening
or not control. Es save. And here, run, can see one, two,
three, four, five. So if you put, for example, 100, it will happen 100 times. So let's just run,
and now you can see 100 times we print Hello. No, I want to give you
another challenge. Create a V loop that will get all the even value 0-1
thousand and show it. So again, create a
Vi loop that it will print all the even
values 0-1 thousand. You try to do that. We'll
do it in next video.
53. 54 While loop even challenge: Know for printing all the
event number 0-1 thousand. Let's see how we can do that. I want to create a
new file for that. I will call it Wild
ooptre dot PY. And with that done,
again, we need a counter. You can call this counter
whatever you want, but this way, it's much
cleaner and readable. I want to put it to zero, right? And now with that done, I want to use a Wil loop, and I want to check the
counter until the counter is less than 1,000
after that a column, know what you want
to do in here. We want to check it
with the If I counter. Mood or person two
was equal to zero. It means it is an
even number, right? So we can print it, print. Counter. That's it. No, after doing this, we need to increase
the counter, right? We don't want to do
it inside this I. We want to do it after the I. So it's always happen,
doesn't matter what. Every time that
this y loop happen, we need to increase
the counter by one. So plus equal to
one, and that's it. Control is to save. Now if
I run the Wy loop tree, you can see it is writing all the event number
0-1 thousand. And if you check it,
you can see there is no odd number in it at
all, real easy, right?
54. 55 While else: Wil has another thing
that is called s. So I L and s that
we talk about it, we have s for le as well. So let's just create
another file, and I want to call it
Wil Loop for that PY. Now in here, let's
create a counter. I want to call it I and I
want to equal it to zero. After that, I want to say
Wile I is less than six. After that, the colon, and I want to show the I, right? Show I. After that, we need to increase I,
so I plus equal to one. And with that done, what it will happen
if I clear here and run this wild head inter, and you can see it is a start
0-5 because after five, it really is six and six
is not less than six, so that's why it
won't print the six. But when it reaches six, the else will happen, right? So let's just see
how we can use se. In here we say se
and after that, the colon, and in here,
we can say print. And let's just say I is six, and it is not less
than six at all. It is equal to it. It's not less than that. Now with that, if you run, you can see it will print 0-5. And when I reach six, this six is not less than six, it is equal to six. So these codes won't happen
and the else will happen. So at the end of you, when this statement is
not true and it is false, the else will happen. So if you want to look through something and at the end of it, you want to do something with all of those informations
and variables, you can do it inside else.
55. 56 Continue and break in loop: There are two things
about any loop doesn't matter y loop or for
loop that you can do, but we don't use it that much. But in some codes, when you are in a team, you may seize those, so I want to explain
them as well. So in here, let me create a
new file and I want to call it break continue dot PY. So it doesn't matter
what loop you are using, you can break out of that
loop whenever you want, and you can ignore
one of the loops. So let me give you an example. For example, in here, let's just say I
is equal to zero. And we want to say, while I is less than six and a column, we want to show the I with
print, right? That's it. And now, in here, I want to say if I
is equal to three, what I want to happen, I want this loop to break completely and get to of
the loop completely, right? But if it's not three, increase the I by
one, just like. And after that, let me add a
print at the end of or code, and I want to say finished
Execution, right? So this print finished
execution is outside of the le. It's outside of the I. It will happen always at the
end of our code execution. Now with that done,
let's just run it. We say Pi, break. Head inter, know
what will happen. It will start from zero. Zero is less than six, and it will print zero. You can see it did print zero. And zero is not equal to three, so it won't break, but it will increase
I. Noi is one. So it will go back to the loop
and it will run the code. Noi is one. One is less than six, so it will print one and
one is not equal to three, so it won't break the loop. And after that, it will increase it again by one until
it reached three. No tree is less than six, and it will show that
it will show the tree. But in here, it check. If the tree that right now it is tree is equal to it
will break the loop, and it won't increase
this at all. It won't run this
line of code at all. So it will break the
loop completely, and it will go to the next line, and it says finished execution, and you can see it in here. It will start 0-3. After that, it finished
execution. That's it. That is about break. No, another thing that I want
to talk about is continue. Like the break, we
have continue as well. So in here, what I want
to do I want to say finished break execution,
know what I want to do. I want to show you
how continue works. In here, I want to
say, for example, J is equal to zero, like we did it in here,
we did call it I in here, I did call it J, right? In here, I want to use
Wil and I want to say. While J is less than six
exactly like before, after this, what I wanted to happen in
here, first of all, I want to increase J by one, and after that, I want to
check if J is equal to three. In that case, continue. I will explain what continue do. But after this F, what I want to do, I want
to show the J. So print J. And in here, I want to copy this print so you know when
it's completed, right? Like here, and I want to say finished continue
execution, right? Now, if I run,
what is happening, it's showing one, two, four, five, six. What does happen? Let me explain. J at
the beginning is zero. Zero is less than six. So this code will
happen and no J is one. One is not equal to three, so this won't happen, and it will print one.
You can see it over here. No, again, it will
go up in the loop, and because the J
right no is one, one is less than six. So that's why this
code will happen. And J will be two after this. Two is not equal to three. So that's why this
continue won't happen, and it will show two, right? You can see it over here. No, it will go up. No, J is two. Two is less than six. So this code will happen. With this code, J is three no, and three is equal to three, and the continue will happen. And what this continue will do, it will prevent the codes
after that to happen. Make the loop to ignore
the line after it inside the loop and it go at the beginning
again. No J is three. Three is less than six. So this code will happen
three plus one will be four, so no J is four and four
is not equal to three, so it will print four. And you can see it is ignoring three because
when the J is three, we say continue or ignore the codes after
that in the loop. That's it. That's how
break and continue works.
56. 57 In operator: No, I want to talk
about in operators. It's really a neat thing in Python that you
should know about. So in here, let's just
create a new file. I want to call it
in operator dot PY. So what this will
do, for example, we have a text, my STR, right? We have a string, and let's just say it is hello word, right? And with that, we
want to check if there is W inside
this string or not. So how we can do that, we can say W in o my SR. So what will happen in here, this in operator will check. If there is W in
the MSTR variable, it will return true. So in here, let's
just use a print to see what it will
return like this. Control S to say in here, let's just run the
in operator run, and you can see it is giving us true because there
is a W in here. But if you change this to A, let's see what will happen. If we run, you can see
it will give false. We can do this
with list as well. So if I say my list
is equal to a list, for example, let's just say Max. After that, let's just add some integer number,
some float number, and some more value like this value and more float value and some other
texts, David, right? So know that we can use this in operator
with a list as well. So in here, we can say print because we want to
show the result. We want to say if there is ten, number ten in my list, right? So with that done,
control to save. Now, if you run, you
can see it says true. So there is number
ten inside in here. But if you change this to 11, control to save in here, run, and you can see
it is giving us false. Now, let's just do
it with the text. For example, if I in here say max control is to save and run can see
this is giving us true. And if I change this
to Hello, for example, what this will give
us because there is no hello in this list,
what will happen. If you run, it will give us false because there
is no hello in there. But let's just check something. If there is inside this list, there is max in here, but there is no
inside this list. So what this will give us, it will give us false. Let's just run and you can
see this is giving us false. So we can check if there is something in
other things. That
57. 58 Librarys: No, it's time to talk about the libraries that
is inside Python. Let's see how we can use them. In here, I want to
create a new file. I want to call it
library dot PY. And in here, first thing
that you want to do, we want to import
the library and tell which one of the library
function we want to use. I know we can do
that. For example, I want to create
a random integer, and there is a library for that. For doing that,
we say from after that name of the library
that in this case, I want to get a function
from random library, and from this library, we can import a function. For example, random integer. You can see we have
random integer. That's it. Know we have
access to random integer or Rad Int insight
or random library that is a part of Python. Know with that do,
we can use it. So, for example, let's
just use a print and use this rand Int in here. It is a function. You can see it says, return random integer in range A and B, including both end points. So, for example,
I want to tell it to give me an
integer value 0-100. And now let's see
what will happen. If we run the library file, you can see it is giving us 57. And again, if you run it, again, it's giving
us another value. Again, if you run, again, it is giving us another value. Again, we can do lots of
things with the functions that is inside this random
library or random module. So we say from random, right, Import, after that, you can use controller space to see all the functions
that is inside it. For example, if you go down, you can see shuffle in here. If you hover over it, it will tell you what it can do. It can shuffle list in
place and return no. So what it will do,
if we have a list, it can shuffle it around, right? So for example, if
you have this list, my list is equal, let's say, one, two, three, four, five, six, seven, eight,
nine, and ten, right? You can see if I print this, my list no luscious run. And now you can see, our list
is really in order, right? But if we use shuffle on it, let me take this, grab
it and put it up here. Because I want it to
be more readable. Now, if I use this
shuffle function, I can do shuffle, and inside it, I want to
say shuffle my list, right? So know what will happen. It will shuffle up the list, and it will save
it inside itself, right? It will return none. If you are already,
you can see it is shuffle least g in place, and it return none. So just with that, if you print the
last is my list, and control is to
save and run it. You can see first,
it is ordered, but after that, we
did shuffle it. So that's how you can
use the libraries or methods or modules
that is inside Python.
58. 59 Librarys from internet: We did use the library or modules that is already
in Python, right? Like Random module
or random library. But there are a lot
of libraries in Internet that we should be
able to download and use them. Let's see how we can do that. For doing that, I want to create a new file
and I want to call it library two dot PY, right? And in here, the module
that I want to use, first, we need to install it, download it and install it. And how you can do it, you
should go to terminal and say PIP, right, Install. It's called Pi Piglet. Okay? Just paste this and Python installer will go and download and
install it for you. Head Inter. If you are getting
a row with PIP Pi Figlet, you need to use PIP Tree, right? If you are on Mac or Linux, use this PIP tree in install. Pie fig let, right? They like this, Pip tree. If the pip itself is
not working for you, just add a tree at the end
of it and it should work. And now in here,
you can see it is successfully
installed pie figlet. Now let's see how we can use it. For using it, we can
just use import, Pie figlet like this. And with that done, we can use the functions
or method inside it. Let's just test some
functions inside. Pie, Figlet jot. It won't show anything,
but it's okay. Sometimes these kind
of things happen. The function that you
want to use inside it is called
FigletUnderscore, format. And we can add the open and close prances
and put something there. For example, hello word, right? So what this will give us, we can save it
inside a variable. So let's say A is equal to this. Now, let's just print A, print. A, right? Let's see
what will happen. Control S to save and in
here, let's just run it. And now you can see it did write hello word in a graphical
sense. You can see. So whatever we change
in here, for example, if we change, my name is Max. Control S to save and run, you can see, my name is Max. Now I want to give
you a challenge. Use this library or
package or module. To get the text from input
and show it this way, right? So you try to do that. We will do it in a second. So for doing that, what
I want to do in here, I want to add an
input, right, input. And in here, say what
you want, right? After that, we want to save
this inside the variable, so I will call it a string
to show equal to this, and no I want to
use this variable instead of hard
coding things there. So in here, I put a
string to show, right? With that control is to save. Now if you run, let's
see what will happen. It says, say what you want. I will say David, for
example, head tab. No, you can see
it's showing David. Again, if you run and put
Max in there at Inter, and now you can see
it's showing Max. Congratulations. Now
you know how you can download and install
packages from Internet. But where these
functions is coming from it's like our VS code
can't auto complete that. Sometimes these kind
of things happen. You can use Control
C to copy it. Now go to Google and in here, paste the name, right? If we search for it, and the first result,
let's just open that. And now you can see it is giving us all the information
over here, how to install it. You can just copy it from here. And if you go down, you
can see it will tell you how you can do
other things with it. It will show you the functions
that you can use in here. So you can see everything
is inside this page. So whatever you want to do, just search it in Google, find a library for it, and go to its page, and you can just copy
and paste some code. For example, these ones, right? And you can see, we can
change the font as well. We can do lots of things
with these kind of packages. And in future, we will
learn about more packages. I just wanted to show you a simple library to use so you have
experience as a beginner how to download and install
a package about the problem that we had that autocomplete
doesn't work sometime. When you install something, you can just close VS code
and open it up again and now you can see it can
autocomplete for us. For example, if I use Pi
Figlet and a dot after that, you can see it will give us
all the method inside it, for example, Figlet format. And if you hover over it, it will tell you how
you can use it, right? That's it. Just close
and open VS code one time and the
autocomplete will work.
59. 60 Use from import on pyfiglet: No, I know you
noticed something. In the last library, we say from random
module or library, import a function, right? But in here, we
didn't install and download a package or
module from Internet, and we just say
Pi figlet, right? We say import Pi
Figlet. That's it. But I want to show you how you can do it exactly like here, because right now,
because it is an assault, you can use it exactly
like we use it for the modules and functions that is already
in Python, right? So in here, in a sing like this, we say from Pi
Figletimport a function. What function? Let me
head control space and you can see it show us
all the functions inside it. And the one that I
need is FigletFmat. That's it. Now with that done, we don't need to say Pi
figlet dot FigletFmat. We can say FigletFmat.
That's it. That's all you need
to do. So you import a function from the
module or library, and you can use that
function wherever you want. This way, I think
it's much cleaner, but I wanted to show you
the other way as well. So know it that if
I run and in here, let's just say hello head Enter. Now you can see it is
showing hello for us. So now you know how you can download and
install packages or libraries or modules and how you can use functions inside
them in both ways.
60. 61 Create Function: No, it's time to talk
about functions. First, why we need functions. Let's say we have a task that we do it a lot in our
code over and over again. So that's the first
reason that we can put it inside a
function, a wrapper. So wherever we want to use it, we just put the name of
that function in our code, and it will do
everything for us. Next reason is for it
to be more readable. So our code will be more compact and readable
and categorized, right? So, for example, we did say hello word a lot in
this course, right? So let's just create
a function for it, so we can always use that. In here, what I want to do I
want to create a new file, and I want to call it
function one that PY. And in here, you remember we say print hello
word a lot, right? So now, we want to create
a function for it. And for creating a function, you always should say deaf
or define after that, the name of functions, for example, say
hello, that's it. Every function needs a
open and close prenss and after that, a colon. After the colon, whatever code you put beneath the function, if it has four space
indentation on it, it will be run whenever we
try to use this function. For example, in here,
I want to say print. And in here, let's
just say hello W and we can have
multiple line of code. For example, let's
just say print and say from function, right? So this is our first function. And the important thing
about Python is that Python will execute code
from top to bottom. So first, this line will happen next this
line, next this line. So this is really
important to create your function at top of
where you want to use it. Know with that done, I don't want to do anything. I want to just save
and in here, run it. So we say PY,
function one dot PY, and you can see,
nothing is happening. Why nothing is happening. We have some code in here. Because we have a function, Python won't run it unless
we tell it to run it where, for example, in here, we
want to use that function. That's it with open
and close parenss. Know it if we run, you can see it says hello
word from function. No, in here, I can use
it multiple times. So again, I can say that
hello word and in here, run, and now you can
see this function caused these two lines of code to print hello word
from function twice, and you can see it
over here because we call the function twice, right? That's it. That's how we
create a function and we use.
61. 62 Function with one input: Functions can have
input as well. So let's see how we can use
the input of a function. So in here, what I want to do, I want to create
another function. So for doing that,
I will say death, say hello to, right? This is a new function
that you want to create. I need open and close prenss. And inside these prances, we can tell Python that this function needs
an input, right? For example, I want to
say hello to somebody, so I will put name there. Create a variable,
whatever, it will be okay. After that, a column, know in here what I want to do. I want to use a print
F string, right? And in here, I
want to say hello. Calibraces and put the
name there. That's it. Now, we have a function
that has an input. For using it, I don't want
to use this say hello. I want to use say hello to. Now with that done. If you create a open and
close parenthesis, you can see in pop up, it says, put a input there, right? If you do it for say hello, what will happen, it
says, put nothing there. That's it. And it is returning nothing.
It will return none. So how you can read this the function doesn't
need anything for input, and it won't return
anything, right? But in here, if we put the open and close
parents there, again, you can see it will tell us it needs an input and the
name of input is name, and it doesn't return anything. We will talk about
the functions that will return something,
but for no, you can see it need an
input that is called name, and it won't return anything. So in here, if I, for example, say, Max, let's see
what will happen. I don't want to say hello. Control is to save. No, if we run you can
see it says hello max. And if I use this function
one more time and in here, say, David, what will happen? This is the input, and this input will
be saved in name. And in this function,
it will be used. Control is save, and in here, we can run, and
you can see first, it says, Hello max, and after that, it
will say hello, David, and you can see it how
it works over here. Cool. Now you know
how you can create a function that can take an
input and use that input.
62. 63 Function input default value: We create a function
that gets an input. But what if we don't put
anything on its input? Let's see what will happen. In here, we did
use say hello to, and if we just delete
this and just use say hello to and don't
put anything for input. Let's see what will happen.
If we run, you can see, it's a say hello missing one required positional
argument name. So if the function
needs an input, we should provide it, and we can't pass
an empty in there. So when we want to create a function that if they put
an input, it should work. And if they didn't put
the input as well, it should work as well. So it will be more
bug free, right? So for doing that, we can decide a default value for
input function. So in here, we can just say
name is equal to something, for example, an
empty string, right? Just like that. No, if we run, you can see it will
just just say hello. We can put whatever
we want in there, for example, Max by default. And know if we run, what will happen because we didn't put
anything inside it, the default value will be
there will be inside the name, and it will say hello, Max. But if you put something
inside it, for example, David, now let's see what will happen. Control is to save
and in here run, you can see it will
say hello, David, because this name
is equal to Mx, it's just a default value
that we put in there. So if the user that want
to use this function, don't put anything as input, it won't give us
any error. That's
63. 64 Function with more than one input: Create a function
that has one input. Let's see how we can
create a function that has more than one input
because we can do that. So for doing that,
let me delete this. We create two function. I want to create another function. For example, I will say death. After that, the name
of the function, I want to call it sum, right? After that, open
and close prances. And in here, I want to show
sum of two value, right? So as an input, I can put two variable there, for example, A and B. Just like that, with a colon, we can write or code. For example, we can say, C is equal A, plus B. And after that, we
want to show that. We say print C or we
can use FS string. So for example, in
here, we say F, after that, double quotation
mark, and in here, we want to save some of open
and close curly bracket, A, and another curly bracket, B is another curly
bracket, C, right? So now with that, let's just see how we can use
this function. We say sum, and we
want to use it. As you can see, it says
it needs to input, A, it can be anything, and B, it can be anything as well, and it won't return anything. So if you put two
and three in there, it will sum up these two value, and it will show the result
for us. Now, let's just run. You can see it a sum
of two ent is five. If you change these values
to five, and eight. Let's see what will
happen, control S and run. You can see sum of five
and eight is 13, right? Now, we know how we can create a function that has
multiple input. It can have more than that. For example, in here, I
can say another input, let's just say F, right? So now it has three input, and we want to sum of all of these three
input, just like that. C is equal A plus B plus F, and we can change stuff in here. You can see, and F is C, right? And in here, if you run, it will give us a error. It says, missing one required
positional argument, F. So it says, you did put A there, you put B there as well, but you didn't put
F. So if you put F, for example, let's just say two. Now, let's see what will happen. Control S, and know
in here, if you run, you can see sum of five
and eight and two is 15. Everything works fine. So
now we know how we can create a function that
has more than one input.
64. 65 Advance way of passing input to a function: No, I want to show you a better way of using
inputs of a function. So, let's see. In here,
I want to delete this. I want to create a function, so I will say div and I want
to call it say hello fool. Right and open and close
prances like always. Full, I mean first
name and last name. So I will say first input is
first name and second input, I will say last name, right? And a col and after that, no, we can write or code. We can say print. I
want to use Fstrem. So in here, I will say hello to open and close curly
braces, the first name, and after that, space, and after that, another curly
braces, last name, right? A easy one. No, we
want to call it. We have some way to call it. We can say say hello fool, and in here, we can
pass, for example, Max. And for the next input, we can say Tin, for example, Max Tin, right? Now, with that done,
let's just run. You can see it says
hello to Max Tin, right? No, another way to use
this input is like this. For example, say, first
name is equal, to Max. Okay? And last name is
equal to TIM, right? Just like that control is, it will be exactly
the same like before. But this way of using it, you will see it a
lot in team works, and it's really
easy to work with. And the good thing about it is that you don't need
to put them in order. So what I can do in here, I can just say, last name
is equal to TIM, right. And after that, pass
the first name. We say first name
is equal to Max. It should exactly
work like before. So let's just run
hello to Max team. You can see with this type
of calling a function, it's much easier to
work with a team, and it will prevent
lots of bugs. So this was another way
of using a function.
65. 66 Function return: Till now, we had some functions
that can take an input. But we didn't talk about how
we can return some stuff. Let's see how we can
do that in this video. In here, I want to delete this. I want to create
another function. For example, let's
just say death, sum of two number, right? And for the inputs, I will say num one
and num two, right? Just with that, we need a colon. And in here, first way is
to just show it, right? We can just say show the
num one plus num two. That's it. It will
show it to us. Now, let's just test that. Say sum of two number, for example, two and three. What will happen control is
to save in here if you run, it says five, right? That's it. But I don't want to print
anything inside this function. I wanted to be
able to return it. So in here, we say
return num one plus num. Two, that's it. Now this function.
When we use it, it will return
something and we can save it somewhere inside
the variable, right? With this, if we use it, say sum of two number
open and close parenss now you can see there
is two input in here, num one, num two, and as
a return, there is any. It can return anything, right? So it's not none, so we know that it will
return something, right? So no, let's just test that out. For example, num one, we want to say it
is equal to two, and num two is equal to
five, for example, right? Now, what this will do
if we hover over it, you can see it needs two input, and it will return something. So we can put it
inside the variable. We say A is equal to
sum of two number. No, we can show DA
print. Hey, that's it. Now, we have a
calculated variable that we can show it
outside the function. Now, if we run, you can
see it is giving us seven. That's cool, right? No, we did talk about all
type of functions.
66. 67 Function practice: No, I want to give you
some practices so you get more familiar
with functions. And with this practice, I want to show you
that you can do whatever that you want
inside the function. You can have I in it, you can have for loop in it. You can have I loop in it. You can have lists in it. You can check the list. You can create a list. You can return a list. You can do all sorts of things. So for example, let's
say we have a list of numbers and we want
to check if there is an even number inside that list. So what I want you to do
create a function that can take a list as its
input and with a for loop, check if there is
even number in it. If there is even
number, return true. But if you check everything
inside that list and there is no even number in after
everything finished, return false. You
try to do that. We'll do it in a second. So for doing that, I want to create a new file, and in here, I want to call it function
two dot PY, right? So in here, first, we want
to create our function. So for creating a function, we say DF or Dev. I want to call it
has even number. Right, open and close
parenss and as it's input, I want to get a list, right? So that's why I want to create
a variable for its input, and let's just call
it num list, right? And after that, a column
and with some indentation. After that, in the next line, we will write our code. So in here, what I want, do I want to use a four
loop and I want to check all the numbers
inside our num list. Okay. And after that, a colon and in here, we want to check if the number percent or mod
two is equal to zero or not. If it is equal to zero, we can return two, right? Because we know that
there is an even number there because the num person two or mod two is equal to zero. Now, if we check all the numbers and we didn't return anything
after the four loop. When the four loop is finished, we'll go to this line, and with just four
indentation in here, I will say return false, right? That's it. No over
function is ready, but let me explain it. We have a function that check a list if there is any event
number inside it, right? And it will check it
with a four loop. It will check all the
items inside that list. And if M two of them was zero, it will return true. And when we return something, it's like breaking the function. It will get out of the function and it
will return the result. And these return falls won't
happen anymore because this return return will break
the function completely, and it will return to the line
that we use this function. Now with that done, let's just see how we can use
this function. For example, I want to say, my list is equal
to, for example, let's say, one, five, nine, seven, three, right? And this is our list. Now if we check D
has even number. So let's just say
has even number. It is a function, so it need
a open closed parenthesis. And now you can see
it needs numlist. And what I want to do in there
on a pass or list there, my list, and the
return is boolean, and we can save that
inside somewhere. I will save it inside A, and let's just show
it with a print. We say print A, right? That set control is to
save in here, PY function, head tab, head tab
one more time, so it will go to function
two and head Enter. Now, you can see it
did give us false why? Because first and
first, it will put one inside the num one
mod two will be one, so it won't be true and
it won't return anything. So again, it will go back
and check the next item. The next item is five. Again, five M two
will give us one, so this won't be true and
it won't return anything. So it will go to next
item and next item. And because we don't
have any event number, that's why this four loop will finish without
returning anything, so it will go to the
return falls at the end. No, I don't want to
do it like this, put it inside a variable
after that show it. I can just say print just like that wrap it around open and
closed parenss that's it, I will give us the
same result, right? Now, if I put, for
example, a two in here, so no list has an even
number inside it, right? So with that done, if we run, what will happen? I
will give us true. Why? Because it will check one, five, nine, and none of
them will return true. But when it's checking two, two, MT will give us zero, so it will return true, and it won't reach
this line at all because it's returning and
breaking the function. That's it. Now, we have a
function that we can use it to check if there is an
even number inside our list. No, I want to give you
another challenge. What I want you to do, I want to create a function that get all the
even number that is inside a list and put it inside another list and return that
list. You try to do that. We will do it in next fiddle.
67. 68 get all even number from list: No, we want to create a
function that will get a list, and it will get all the event number from that
list, and it will return. So for doing that,
what I want to do, I want to just delete this part. I want to create a
function in here. It exactly like this one, so we can copy and
paste some part of it, but I want to do
it from scratch, so you will
understand it better, and I will explain
a one more time. In here, for creating function, I will say DF or D for
creating a function, and I want to call it get
or even numbers, right? This is the name of function. I need open and closed prenss and the name the input variable, again, I want to
call it num list. It's okay, right?
And after that, colon and after that, let's just create or logic. For every number in
our num list, right, we want to check if num person two is
equal to zero or not. If it is equal to zero,
what we want to do, we want to create a list and add this number to that list so
we can return it at the end. So for doing that, we need to create a placeholder variable. So in here, I want to
call this variable or even number list. Okay? And what I want to do I want to equal it
to a empty list. That's it. Now with that done. If the number person
two is equal to zero, we can add it to this list
that is empty right now. We say all event number
list dot append. With this append
method or function that is inside a list library, we can add numbers
or items to a list. So in here, we want to
add the num, right? So if the number person
two is equal to zero, we will add it to all
even number list, right? And at the end of this function, when these four loop get
to be finished completely, we can return the all
even number list. And that's it. This
is our function. Now, in here, we have this list that has just one even number. Let's just add more,
for example, eight. After that, nine, after
that, let's just say six. Now we have three even number. Let's just see if our function
is working fine or not? The name is get
all even numbers, and we pass the list. It's called my list, and what it will
return, it will return. If you hover over it, you can see it is
returning a list. We can say that inside a
variable, but we don't need to. We can just print a tot, right? So just like that, we are printing it out.
Control is to save. Now if you run, you can see it is getting all
the event number, it will put it inside a list, and it will show it to us 286. Cool, right? Now if you
add more, for example, 12, 25, 89, 36, 48. And just like that, control
is to save and run. Now, you can see it is giving us all the event number as a list.
68. 69 function return more than one variable: I want to show you a
way that you can return more than one thing with a function. Let's see
how we can do that. In here, I want to
create a new file, and I want to call it
function dot PY, right. And with that done, I want
to create a function, and I want to call
it, for example, get Ale info, right? So this function needs
open and closed parenths, and with that, we need
the column and now we can return the Als info, right? I will give you more
practical example after this. But I want to show you the basics first so you
will understand it. After that, we will go through
more complex stuff, right? Let's say the name of Alice
is Alice, right? That's it. Now, the age of Alice
creates a variable, and it's equal to 30. And let's say the country
that Alice is in is a string, so we can save USA, right? So we have some
information about Ale, and we want to return these informations that maybe we get from Internet
or wherever. We did hardcode it in here, but in future, we get it
from Internet, for example. And now we want to
return this stuff. So we say return name
and age and country. So return all the information
about the Ae back, right? So now, let's see
how we can use. We say get Alice info,
right? That's it. This is a function. And
what it will return us, you can see, it is returning
us a topple, right? So we can use topple
unpacking to save the information that these
functions gives us, right? So in here for saving it, we say Alice name, and Alice H and
Alice count tree, right, is equal to
the informations that these functions gives
us, that is a topple. So what we are doing in here, we are unpacking the data
from return of this function. How we can use
topple unpacking in here because when we
return multiple stuff, Hiton will make it inside a topple and it will
show it to us, right? So for you to see it better, first, let me show it
just like this print. And use the FS string. I want to say curly brackets, show the Allie's name. After that, I want to say
has another curly brackets, show the Allie's age. Years old, for example, and she lives in another car bracket and show
the Alice country, right? With that control is to save, I want to say PY function, head tab, time, head inter. No, you can see Alice
has 30-years-old, and she lives in USA. So you can see,
we did unpack it. But what if we did
use this function in here and use a print
on it, just like that. We are not unpacking it, right? So what this will give us
if we run it will give us topple of all the
information about As, right? So now we know whenever we
are returning multiple stuff, Python automatically put it inside a topple and
it will return it. And we can use topple unpacking to get the
information that we need.
69. 70 Function tupple unpacking Practice: No, I want to give you more complex
examples so you will understand the returning
of a topple, right? So in here, I want to create
a new file and I want to call it function
for that PY, right? And in here, let's
say from Internet, we are getting some stuff. For example, a list
of topples, right? So in here, I want to call
this user infos, right? Is equal to a list of topples. For example, I want to say
the name is Alice, right. Another item inside
this first topple that is inside this list. I want to show the age
that is 30, right. After that, the country, let's just say USA, right? This is our first topple. Next topple, I want to
say the name is Max. After that, the age is
45 and the country, let's just say U. You see, I'm using
double quotation and single quotation
inside one topple, so you will understand that we can use both of
them, right? It's easy. Now, let's just add another
one, another topple. Let's just say the
name is David. After that, the age,
let's just say 25. After that, the country, let's just say Egypt that's it. Control is to save. Now, let's say we did get all of these
informations from Internet. Now, we want to create
a function to find a person and get all of
its information, right? So in here, I will say death, get user info, right? Open and close parentheses. And for its input, I want to put name in
there, no in here. We want to use a for loop
on these user infos, right? So we say for
person, for example, you can call it whatever you
want in user infos, right. So what this person
will give us, it will give us a topple of
a string, int and a string. You can see it will give
us a topple and with that, we know that this person
will be a topple of a string int and a
string because you can see a string int a string. Python is smart in
that way, right? So now we want to
check if the name that user gives us is equal
to one of these names, you can return that topple. So in here, I will
say if person, and we want to get the name. The name is first input inside this topple so
we put zero in there. We know that for sure. If it is equal to the name that is coming from the
input of this function. In that case, you can
return the person, right? And what this will do, it will return that person
if it exists, right? So let's just see
how we can use it. In here, I want to say get
user info and inside it, I want to say, please
give me the Max info. Put Max in there and know
what this will give us. It will give us a topple
of SDR int and SDR, a string integer,
a string, right? Let's just save it inside
for example, max info right. And with that, I
want to show it. So with a print, we can show Max info, right? Just like that
control is to save. We want to use PY function
at tab three times. So we go to function
four head inter. Now, you can see it is
giving us max 45 UK, right? So we can use topple unpacking
in here because we know that this function is giving
us topple SDR in SDR, right? So for unpacking the topple, let's just say person
name and person age. And after that person
count t, right, is equal to the data that this function
will give us, right? So we are unpacking the return of a topple inside
some variables, right? So now, let's just use a print of a string
in here to show it. In here, we say
F, the quotation. First, I will add
a curly bracket, show the person name, right? Person name has
another curly bracket, person H. After that, I want to say yours old. And he or she lives in another curly bracket
person country, right? Control as to safe. No, in here, if we run it
says Max has 45-years-old, and he or she lives
in UK, right? So now, if we
change this Max to, for example, David, let's
see what will happen. Control is safe and no Run, you can see it says
David has 25-years-old, and he or she lives
in Egypt, right? So everything is working fine. But what if we put some values there that we don't have
it inside this list? For example, Tom, right? Control is to save. Now, if you run,
what will happen, it will give us error. It says, cannot unpack nonterable non type object because it didn't return
any topol, right? So that's why it
gives us a error. So for fixing that, what we want to do after this four loop happening and it didn't return anything, we
can do whatever we want. For example, we can return none, none and another none. So what this will do, it will return a tuple of none. So in here, if you run, it says none has none years old, and he or she lives
in none, right? So it will return in none. But we don't want to show
it like this, right? So what we can do, we
can check one of them. So in here, we can check if person name was
not equal to none. In that case, you can
show all of this. So Control X to duplicate this, add for space indentation and Control V to
paste it, right? Control to save no. If you run, you can
see nothing has happened because
the person name, because it is Tom and
Tom doesn't exist, it will return none. So with that, we prevent printing something that
it shouldn't be there. But in here, we can
add else there. So in here, use an se after
that colon and in here, you can say print person, is not there, right? That's it. Control S to safe. Now, if you run, it says person is not there
because Tom is not there. So if we change this to Alice, let's see what it'll
happen. Control E to save. If you run, it says
Alice has 30-years-old, and he or she lives in USA. Now, we have some codes
that it is working.
70. 71 Adding comments: Know that where functions and codes is getting
more complex, we need to know
about commenting. Let's see how we can
add some comment. First, what is the comment? Comment is a line of strings that Python
will ignore always, and we can explain things there, and it won't be executed at all. We can add it to
wherever we want. So for example, in here, I want to add a comment
on top of this. I will use hash tag, and after that, I can
explain what this is. I can say this is a
list of topples of users informations that we
can get from Internet, right? That's it. This line is for just explaining what we
have done in future. So in future, whenever
we get back to our code, we know what we have done
with this explanation. And after that, let's just add another one in here at hashtag. I will say, with this function, we get user info. After that, for example, at front of this four
loop, I can add comment. So in here at the hashtag, I can see with this, we loop through all of the items inside user
info's list, right? After that, in here, add another comment,
I want to say, we check the user name, right? After that, in here,
add another comment. If the user name was equal to the name on input, we return the user
or person, right? And in here, I can
add another comment either at top of it or
in front of it, right? So in here, the indentation is not important for comments. So in here, we return none
if we didn't find the user. That's it. Now, we add some comments that
explain our function, and it's really good to
always add comments.
71. 72 Limitless inputs args: Now, I want to show
you another thing. We can have multiple input, but always we have a limitation. Let's see how we can
pass this limitation. For doing that, I want to create a new file and I want
to call it function. Five dot PY. So with this, we want to create a function that can
have multiple input, but there is no limitation on
how many input it can have. So for example,
we want to create a function that say hello
to everybody, right? So I will call it say hello to all and open
and close prances. For input, we use
star names, right? Star names. So what these star
names do it will tell PyTon compiler
that this function can have as many
input as it want. And with this star, PyTon knows whatever
we put as input, it converted to a topple
and give it to us, right? So let's see how it works. A colon after that and in here, I want to use for
loop and I want to say for name in names, right? And after that, a colon, we want to print F string. Hello to open and close curly
bracket and use name there. Now, let's see how
we can use it. For using it, let's
just go here, go down a little bit, so it will be easier to read it. And in here, on say hello to all and open
and close prenss, you can see it can
have multiple names, how we can pass them. In here, we can add, for example, Max, after that, add another one,
for example, David, and after that, pass another
one, for example, Tom. And with that, control
as to save in here, we want to say pi funk
tab multiple times. So we reach function
five dot PY head Inter. No, you can see it says hello
to Max, hello to David, hello to Tom, and we can add
more input to here, right? For example, let's just
say Navi it, right? And in here, let's just
run and you can see, no, it's saying hello to Max, David, Tom, and Navid, right? Cool. Now let's
see another thing. For example, we have
a function that can add numbers, right? We did this before. We
add numbers together. We create an input
for just A and B, at some point, we
add C and D to it. But no, we want to use
this star for telling Python that whatever number they put as input,
add them together. So in here, we can say
star numbers, right? So you can add how many
numbers that you want, and these function should
add them together, right? This is a challenge for you. Try to create this function
yourself and test it, and we will do it in a second. So for doing that, I want
to first create a variable. I want to call it total is equal to zero
at the beginning. After that, we use a
four loop on numbers, number in numbers, and
a column after that. And we want to say
total is equal to total plus number, right? Number itself. That's it. Now, after that, when
this four loop finished, it will go to this line. Just four space
indentation, right, as same line as this four loop. So when the four
loop is finished, it will go to this line. Either we can return
it or print it, right? We can say return total, or we can just print
it. We say print. And with the FS string, we say total is open and close curly brackets
and show the total, right? Now, with that done, let's
just go and use this function. For using this function, I will call it add numbers, and let's just add some number. One, two, three, four, five. It doesn't matter
how many you want. You can add there.
There is no limitation, no let's just run, and
you can see it says 36. If I change this, for example, to one,
two, three, again, it should give us the result, please run, and you can see
it is six, and it is correct. So this way of passing
inputs with a star, it helps us to create
functions that there is no limitation of the number
of inputs that it can take. But one other thing that
I want to show you in here is that when we
pass these numbers, what Python do with them? If, for example, in here, just print the names, right? Just the names, right? Let's see what will
happen. Control to save. Now if we run you can see it is giving us a topple of
all of the inputs. That's why four loop
is working on it. And again, for these
numbers, again, we can print the numbers, and you will see that
it's returning topples. And that's why we can use four loop on it and do
whatever we want with it. You can see it's just a top
72. 73 limitless inputs with kwargs: No, I want to show you
another way that you can pass as many input as you want inside the function with a
clearer way, right? Cleaner way. So for doing that, let's just create a new file. I want to call it
function six dot PY. Right? And in here, I want to create a function, and I want to say print
user info, right. And with that, we need to
specify a input, right? But this time in one star, I want to use to a star. And with two a star, we tell Python that the inputs is limitless and they can add
as many input as they want. And I need it in a key
value pair way, right? So let's just name it user or whatever you
want to call it. After that, a colon. And in here, first thing first, let's just use a print
to show the user, right, to see what
it is giving us, what Python gives us. And now, let's just use it. Print user info and
for the inputs. No, it has two star right. So what we can do,
we can say name is equal to Alice, right. After that, we can
say, for example, age is equal to 35, for example. After that, we can say country
is equal to USA, right? So we are passing
some stuff there. And Python what we'll do, it will convert all of
these to a dictionary, right? Let me show it to you. In here, if I say PY, function, a tab, sometimes, so we reach the function six, head inter, and now you can see, it's giving us a key value
pair of a dictionary, right? So with this user, it is just a dictionary, and we can convert it to
a topple and after that, unpack it and use it, right? So for doing that,
what I want to do I want to use a four, and in here, I want to say, we have a key and
we have a value inside user dot items, right? Just like that. And know
with that what we can do, we can print everything. So we say print. Use a FS string. We say curly bracket, key. Is, for example, another curly bracket and show
the value, right? That's it. Control Save. Now, if you run, you can
see the name is Alice, age is 35, country is USA. And if we add more things
to it, for example, if I add height is
equal to 173, right? Control TSAve and
run it in here. Now, you can see it says
name is Alice, age is 35, country is USA, and
height is 173, right? We can add more. So for example, let's just add weight as well. Weight is equal to 58, right? Control is the safe. And now you can see,
we can access that. So with these two star in there, what it will happen, it will convert all the
key value pair that we pass as input of a
function to a dictionary, and we can use that item on it so we can change it to a topple and we
unpack it like this, and we use the data inside it to show all the information
about a user. That's
73. 74 using both args and kwargs in one function: No with that done, we can have both one star and two star input both
together. Let me explain. In here, let me
create a new file. I will call it function
seven dot PY, right? And in here, I
want to do both of things that we done in
here inside here, right? So what we can do, for example, we want to print user info. So for doing that, let's just copy all
of these to here. This is our function that is getting a key value per input, limitless key value
per in input. Now, I want to add
workover as well. So in here, I want
to see how many our she or he works
in a day, right? So for example, I
can add a star, and I can call it hours,
right? That's it. Now with that done, we are showing the information
about that user in here, and now in the next line, I want to use another for
loop hour in hours, right? What do you want to
do we want to have a total over in here at top, so we say total ho and
it is equal to zero. And now we want to
say total over is equal to total over
plus hour, right? When I give you an example, you will understand it better. So in here, at the end, when both of these four
loop get to be finished, I can print the total O. And in here, let's
just use the FS ring. I want to say total O is curly bracket and
show the total ho there. Now with that do, let's
see how we can use it. Go to the next line and in here, use print user
info, and in here. The important things,
you see, first, we should put the hours there
and after that, user info. So in here, let's see, she works like two
hour first day, three hour next day, eight hour next day, and four hour next day. And after that,
we give the name. Name is equal to Ale. Okay. And we want to
say company position. Is equal to CO, for
example, right? And let's just say salary is equal to 100,000 just like that. Know what Python will do. It will convert all of
this number at beginnings that is not a key value pair
inside these s as a topple, and it will put all of these key value pairs and
convert it to a dictionary, and it will put it inside the user variables,
user input, right? Now with that done,
control is to save. And in here, if we run
the PY, function seven. Let's just go to function
seven, head inter. Now, you can see
the name is Alice, company position is CO, salary is 100,000, cool. Total hour is 17. We did use both of one
star and two star or arcs and Kw arcs inside just
one function. That's it.
74. 76 Map: Now, I want to show you
how you can use maps. There is a map function inside
Python that we can use to do one function on a
list items of that list. Let me show you an example. You will understand it better. In here, I want to
create a new file and I will call it map dot PY, right? So in here, let's say
we have a function that convert lower as to
upper Ks, right? Really long name. And in there, we have a string. I will call it MSTR, right, or whatever.
After that colon. And in here, we can return
the mystr dot upper, right? This is a method or a function that we can do it
with a string, right? And know with this function, let's just give it something. For example, convert, lowercase to uppercase and
in here, give it a string. For example, let's just say max. And what this will give
us if we hover over it, you can see it is returned
something for us, so we can save it
inside something. We can say A is equal to whatever that this
function return. And no if you print it,
let's see what will happen. A, print A. That's it. Control S to save in here. I want to say PY, map, tet inter. No, you can see. We did give it max
in lower case, but it did give us
max in upper case. But no, let's say we have a list of names that
we want to convert. So in here, let's
just create a list. I will call it my names is
equal to a list of names. For example, Max, after that, David, and after that, Tom, and so on, right? Now, one way to do it, we can just create a function, loop through everything and do this upper method
on all of them, but there is the
fastest way to do it, and it's called map. This map gets a function
that in this case, we want to pass the
convert lowercase to uppercase, right? And without any open
and close parentheses, we need to give it the list. Or list is my names, right? What this will do, it will get all the item one by one from this list and pass the item
as input of this function, and it will do for all of them, and it will return
something for us, right? It will return a map for us. We can't show a
map with a print. That's why I want to use a list function in here to
convert this map to a list. And this list, we can
save it inside somewhere. For example, inside
a variable, right? Now, if you print
what will happen, print A, control is
to save and no run, you can see it did give us
a list of all the names, but it is in
uppercase, right? No. You know how map is working. Get in all the items
and put it as input of the function that we provided there and give us
a result in map, and we convert it to a list, and now we can show
it, right? That's it.
75. 77 Filter: Like we have a map, we have a filter as well. Let me show you an example. If I create a new file, I will call it filter dot PY. And in here, let's say we
have a list of numbers and we want to check which one of
them is even number, right? We can do it with a four
loop and with a function, and you know how to do it
we done it in this course. But I want to show you how you can use filter for
doing that, right? So for doing that,
let's just create a function Dev is even. Right? And this
function gets an input, for example, num or number or whatever
you want to call it. After that colon, we check if number person two was equal
to zero after that colon. And in here, we can just
say return true, right? Just with that, if
a number is even, this function will
give us a true. So let's just test that out. In here, we can use a print and use is
even function in it, and let's just pass a number. For example, two, two is even, so it should give us true. So we say PY, filter, head tap, head inter. No, you can see it
did give us true. If we put five in there,
what will happen? I will give us none, and you can see it over here. Why giving us none because at the end we
didn't return falls. So in here, we can have an s.
If it's not equal to zero, we can return false,
right? That's it. No, if we check that out, you can see, no, it's
giving us false. If we put a tender,
what will happen, it should give us two
because it is a even number, and you can see it
did give us two. But we have this function and
we have a list of numbers, and we want to check each one of them if it is even
number or not. One way is to use
for loop, right? But I want to show you filter. So in here, we say
filter, and inside it. We want to check the
function is even on a list. So let's just create a
list as well in here. I will call it my nums numbers, right, is equal to a list. For example, one,
two, three, four, five, six, seven, eight, nine, and ten, right? This is our list, and we can
pass it in here, my numbers. So what this filter do, it will check all of the
numbers inside that list, and it will put those numbers as input of
this function is even. Inside this number, and
it will check everything, and it will give us on each one of them
either true or false. If it's even, it
will give us true. And if it's not even, it will give us false, right? But this filter is giving
us a filter as well, right? So that's why print
can't show it, and that's why I want to
convert it to a list in here. I say list. So open and
close prances over here, right? Just like that. No, we can save it
inside the variable. Let's just say A
is equal to this. No, let's just show DA, print A. Control S to save. Now, if you run or code, it is a list of two, four, six, eight, ten. So it did get all the
numbers one by one, check it with this
is event function. And if it was true, it did return it to us and
we did convert it to a list, so we can show it and you
can see it over here, right? So that is how filter works. And for beginners, you don't
need to use this a lot. It's better not
to use it at all. I want you to be able
to read others code. There is more readable way
to do this kind of stuff, and you know that with
a four loop, right? That's set for filter.
76. 78 Lambda expresion: Did learn how to create a
function multiple ways, but I want to show you
a new thing how you can create a function with
Lambda expressions. I don't recommend to use
Lambda expressions too much because it makes or
code not that much readable, but you need to know
about it as well. So without explaining it. Let me give you an
example in here. Create a file in here.
I want to call it lambda dot PY, right? So in here, let's say we have a function that whatever
value we give it, it will return its
square two, right? For example, let's just say Div. Let's just call it number, square, and it gets
a number from input. So let's just put
a number variable there after that the colon. And in here, we just return
Number square by two, right? What this will give us,
let's just test it. In here, I want to use it. Use number square
function in here, let's just pass two and
what it will give us, it will give us a number. We can save it inside A,
and let's just show it. We say print. A, that
set control is to save. Now in here PY, lamb head tab, and it will put
the rest for you, head inter, you can see it
is giving us four, right? If we put four in
here four by four, let's just see what will happen. It will give us 16, right? So we have these functions
and it's readable, right? But I want to change it. I want to convert it to a amta expression.
Let me delete this. How I can do that, I
can say Lambta first, after um Do key in here, we need to tell it the input. In here, we had
the number input. So in here, we can
say number, right? So this is the input of the um do expression
or Lambda function. It is um do expression, but I will call it um do function so you will
understand it better. So when you say Lambda, it's like you are saying death that you want to
create a function, right? This is the input of that
function that in a function, we put it in prances, but in here, we don't
need to do that. After Lambda, we can
define the inputs. And after that a colon
and in that exact line, you don't need to go
to the next line, you can write whatever
you want to return. For example, number square two. That's it. So this function is exactly this function
that we have in here, right? H, this is like the depth
that we have in here. This is the input that
we have over here, and this is the return value
that we have over here. But we don't need to
tell it to return it. It knows because it is at the
right side of this colon, I need to return the
result of this and it know the variable at the left side of this colon is the input
of our function, right? Know how we can use
this um do expression. This umd expression,
it will create a umd expression as well that
you can save it somewhere. For example, let's just
call it num square, right? Equal to this, right? Now with that done, let's
just see how we can use it. We say num Square. After that, we give it an input. For example, let's just
put two in there and know it is returning
a value for us. We can say that somewhere. Let's just say A is equal
to num squared two, and let's just show it. Print A. Control is to save. Now if you run it, you can
see it is giving us four. The difference between
these two is that, with Lambda expression, or
code will be just in one line. We don't need to define
a function like this, have indentation, have
two line of code. We can just do it in one line. Create a Lambda function or Lambda expression
in just one line, and you can see it or here. Now, with that done, let me
give you another example. For example, we have a
function that at two number, right, add two number. And for inputs, we
have A and B, right? After that, colon, and we
are returning A plus B. And that's it. This
is our function. Let's just go to next
line and use it. We say, add two number. Let's just add two and three. No, let's just save it
inside the variable. Let's just say A is
equal, add two number, and after that, let's just
use a print to show it, show the A, control is to save. Now, if we run, it will give us five because two plus three is five, right? This is one way to do it, but we know we want to do
it with Lambda expression. For doing it with the Lambda
expression, we say Lambda. After that, the input
the input is A and B, so we say A and B. After that, we need
to add a colon and Lambda or Python knows
that when we say um Da, after that, we have a colon. At the left side of that colon, we are defining the variables
and the right side of it, we tell it what to return. We want to return
A plus B, right? And now for using that, we can save this Lambda
expression inside the variable. Let's just call it
sum of two number, is equal to this
Lambda expression. Now, let's just see
how we can use it. We say sum of two number, we pass two number A
and B, for example, two and three, and it will
return something for us. We can save it inside, for example, a variable, right? And let's just show that. We say print, A, that set control is to save. Now if you run,
you can see it is doing exactly what this
function is doing. But the cool thing about Lambda expressions
is that you can use them with a map
and filter function. Let's see how we can
do that in next.
77. 79 Lambda expresion with map: Now let's see how we can use Lambda expression inside a map. First, let me find
the map in here. So with the map,
what we have done, we create a function and we pass the name of that
function to the map, and we give it a list of
text in this example, and we make all of them
in upper case, right? And we pass that list at the second input of
this map function. After that, we convert it to
a list, so we can show it. Let's see how we can do the same with Lambda expression, right? For doing that, I
want to copy all of this and I want to
create a new file. I want to call this
one map, um dot PY. And know with this control V to paste all of
this code, right? Now with that done, I don't
want to use this function. What I want to do, I want
to create um Do expression to use instead of this
function that we have in here. So let's see how we can do that. We say Lambda, after
that, we need the input. So let's just pass my
STR. This is our input. And after that, a colon at
the right side of that colon, we return whatever we want. We do whatever we want with
this input, and we return it. We don't need this
return key at all, it will return it automatically. So we say my SDR dot upper, right? Just like that. No, we need to save this Lom Da expression inside a variable so we can use it
over here, right? No, with that done,
let me save it inside. Two upper, for example, right? It's equal to this
Lambda expression. Now, instead of
using this function, we can just delete that
function. We don't need it. We can use this two upper Lambda expression
and put it over here. Just like that, if we run no, say PY map, Lambda, head inter? No, you can see all
of the items inside we list converted to upper case. So you may say, why we need to use this
Lambda expression. The function is more readable. The reason that we use it it's because we can do
it in one line. We don't need to just save
it inside the variable. We can just copy this and
paste it over here. That's it. So we create our Lambda
expression inside over map, and it will do the job for us. If I just delete this, just like that, just delete
that. Now with that done. If we run, again, it will give us the same
result because Lambda expression we can just use it
inside the map. That's it. But I don't recommend
to use it because it is not that readable to
have something like this, but I will explain
it one more time. When you see Lambda, it means we want to create
a Lambda expression, a function in type of
Lambda expression, right? Now after this um keyword, we have a column. At the left side of this column, we have the variables, inputs of our function, and at the right
side of this column, we have all the calculations or things that you want
to do with that input. And when we put it at the
right side of this column, it will return it automatically. So what it is doing in
here, this map function. Gets all of these items one by one and put them
inside this MSTR. And because at the right
side of this expression, we have dot upper. It will make everything
to uppercase, and it will do it for
all of these items, and it will return it as a map. We change it to a list, and now we can show it and
you can see the result. We can do it for the filters exactly the same use
Lamda expression. Let's see how we
can do that. Next.
78. 80 Lambda expresion with filter: Let's just do the
filter much faster. In here, let's just
create a file. I want to call it
filter Lambda dot PY. And I here, if you remember, we have a filter. So let's just copy all of these and put it
inside filter Lambda. We want to convert this function to Lambda expression, right? We have a list of
numbers that we want to filter just the
event numbers, right? For doing that, let's
just say Lambda. And after that, the inputs
in here, we had number. Let's just call this one num. After that, a colon, no, we want to return something. We can just return
this equation in here. We say num, percent
two is equal to zero. Now, what this will do
this is input function, and this is what
we are returning. If you put one for
input, one person two, it will give us one because
it is not equal to zero, I will return false, right? So this will return false. If you put inside this num
two person two will be zero. So this will return true, right? Now with that um do expression, we can just put it
inside a variable, for example, check even. It's equal to this
um do expression. And now with that, we can just delete this. We don't need it. Just like that. And
instead of this is even, we can use heck even
this time, right? That is Lambda expression. Now, in here, let's
just say pipe, filter, headter No, you can see it is giving us
all the even numbers. Of course, we don't need to
put this inside the variable. We can just delete this and use this Lambda expression
directly in here. That's it. Control save. Now, if we run again, it will give us the same result. And again, the reason that we are using Lambda expressions is because it's more cleaner
for pro users, right? So this kind of one line codes. It's more cleaner to
use Lambda expressions. But my recommendation
is to use functions. This is exactly a function
that is just in one line. That's it. Now, you know about filter with Lambda expressions.
79. 81 Variable scope: In this do, I want to talk
about variable scope, and this is really important. So let's begin. In here, I want to create a new file. I will call it
variable scope dot PY. And in here, let's
create a variable. For example, name is
equal max, right? No, we have access to this variable and the
lines beneath it. So we can print it, right? We can say print name, right? Control is to save. Now, in here, PY,
variable scope. We can see we have
access to Max. That's why we can use it, right? But what if we cut this and put it after this print?
What will happen? First, we say print name, and after that, we
declare the name. It's obvious, right? It
should give us error. But see, can see it says the
name name is not defined. So for you to access variable, variable should
be first declared somewhere so you
can use it, right? No. What about a function? So let's say in here, we have a function
def, say hello. And just like that, we want to print
Hello, read FS string. Hello to open and close curly brackets
and put name there. In this case, this variable
is a global variable, and we can access it in all the functions
and we can use it. Now if you use this function, say hello, what will happen? It will print hello to Max. You can see hello to max, right? Now, let's say we want to
declare this not above in here, we want to declare it in here. You see, after this function. Control is the save and know if you run,
you can see, again, it can find the variable
name and it is using it. Now if you cut this and put it after this function and run, again, you will see that it
says name is not defined. Why? Because this function
we'll call first, and after that, we are
declaring the name. So this name, we can't use
it inside that function. It needs to be there in here, or it needs to be in here
at top of everything. So no with that we have
access to the name. And if we run again, it should show hello to
Max. No, next thing. Let's say we want to declare the same variable
inside this function. We want to say name is equal to David, for example, right? Now with that, Don, if
we run what will happen, I will say hello to David. What just happened? When we want to use a
variable inside the function, Python will first check. If that variable exists
inside that function, it will use that variable
that is inside that function. But if it doesn't exist
in that function, it will reach in global
space in our code, and it will get that
variable and use it. And know because we are declaring the name
inside that function, that's why it will
print Hello to David. But what if we use print and print the name
outside of this function? What will happen if we run, you can see first, it says, Hello to David because
in this function we did declare the name
variable to be David, and we use that in here because it is inside
the function. That's why it tried to find that variable first
inside that function. And because it is there, it
won't go to this one, right? But when we say print name in here outside
of this function, this print see the variable that is in global space, right? This name is equaled David is called local variable
to this function, and this name is equal to max is a global variable
that we can access it beneath that code
wherever we want, right? So this is a global variable. And this is a local
variable to this function. And let's say we delete this, what will happen in here, right? So we have a function. We did declare a variable
inside this function. We call it David, and we did use it and
it should work fine. After that, in a global space, we want to use that
variable, but we can't. If we try to run this it
will give us an error. It says, name is not defined. But before that, it
says hello to David. So it will run this properly. But because this variable is a local variable
to this function, we don't have access to
it in a global space. So now you know, if you declare a variable
inside the function, it will be local to that
function and you won't have access to it outside
of that function. But if you declare a variable outside the function
at top of everything, you have access
to it everywhere. Now, let me show
you another thing. Let's say we declare a variable. For example, again, name
is equal max, right. And with that, we
want to be able to change this name variable
inside the function. Let's see how we can do that. For changing this name
variable inside this function, first, we need to tell it
that it is a global variable. And how we can do that, we say global name and with
this global name, Python knows even though
this variable is defined, it's setting inside
the function, it knows it should change
it in global space as well, because we say global name. So with that, what this
print hello to name do, it should say hello to David. And because we set global name the name in global space will be changed
to David as well. So in here in global space, when we say print name, it should says David because we declare the name that
is global, right? Now, if you run, you
can see it says, Hello to David inside this
function and outside of this function because we did declare it global and
we set it after that. That's why the global
variable name will change. And that's why we say print
name in global space, it will return David. That's it about the
variable scope.