Learn Python - Full Course for Beginners [Tutorial]
FULL TRANSCRIPT
In this course, I'm going to teach you everything you need to know to get started programming
in Python. Now, Python is one of the most popular programming languages out there. And it's by far
one of the most sought after for jobs. And so if you're trying to get a job or you're trying to,
you know, automate your life, or you're trying to write awesome scripts to do a bunch of different
things, then Python's for you. Honestly, more and more developers every day are moving their
projects over to Python because it's such a powerful and it's such an easy to use language. A lot
of programming languages out there just aren't very beginner friendly. There's a lot of syntax.
There's a lot of like little things that if you get wrong, the program will yell at you. Python
is the complete opposite of that. You basically just type out what you want to do and Python does
it. It's that simple. There's not a whole lot of syntax to learn. The learning curve is literally
zero. You jump in, you can start writing your first program in seconds. In this course, I'm
going to teach you guys everything you need to know to get started in Python. I designed this
course, especially for Python. And each lesson has been specially designed with examples that
will help you along the way with so many people starting to learn Python. The question isn't,
why should you learn Python? The question is, why shouldn't you? And I think for a lot of people,
the reason they might not want to learn Python is because they're intimidated or they're afraid
that it's going to be too hard. Trust me, I am going to hold your hand through this entire course.
We're going to talk about all the core concepts in Python. We're going to look at everything you
need to know to start programming in Python and start being confident and start writing scripts
and start writing programs that are awesome and doing cool things in your life. Anyway,
I'm super pumped to be teaching you guys Python. I can't wait to get started in this course. And
I hope you guys stick around and follow along with the course and learn this amazing programming
language. In this tutorial, I'm going to show you guys how to install Python onto your computer.
And we're also going to install a text editor that we can use to write our Python programs in.
So the first order of business is to actually install Python on your computer. So what we want
to do is head over to our web browser. And you want to go over here to this page, it's just
www.python.org forward slash downloads. And on this page, there's going to be two buttons down here.
And it's just going to say download Python 3.6.3. And at least that's the version that I have right
now or download Python 2.7.1.4. So here's the thing about Python, there's actually two major versions
of Python that are in use currently. There's Python two, and there's Python three. So you can see over
here, this is like 2.7.1.4. Basically, any Python two version we would just refer to as like Python
two. And here's like the latest Python three version. And so there's these two versions that you can
download. And the first thing we have to decide is which version do we want to download. And here's
the basic difference, Python two is a legacy version of Python, which basically means that
it's like an old version, and it's not being like actively maintained or supported like officially
by Python anymore. Python three is like the future of Python. It's like the newest version. It's the
one that's getting actively maintained and supported. And so there's pros and cons to picking either
one. A lot of people will say like Python two has been around longer. And so there's more like
libraries, there's basically just more Python code written in Python two. So therefore, like
there's a few more things that you can do in Python two, then you can do in Python three. But
then again, Python two is not supported. And you know, five or 10 years from now, probably no one's
going to be using it. Python three, like I said, is like the future of Python. It's the newest
version. It's the one that, you know, is sort of being maintained going forward. So for the
purposes of this tutorial, I'm going to be teaching you guys Python three. So we're going to want
to download Python three. Now here's the thing, like, if you learn Python three, or you learn Python
two, the differences aren't very big. There are a few differences in like some syntax and just some,
you know, little things here and there. But if you learn Python three, you'll be able to jump right
in and code in Python two as well. Don't let anyone, you know, tell you that you have to learn one
or the other, we're going to learn Python three just because it's I think probably the best for a
beginner to learn. And so that's the one that we want to download. So I'm just going to click
download Python three. And we're going to go ahead and download this version. And when that's done
downloading, I'm just going to head down to my downloads folder. And you'll see here, we just
have this file, I'm just going to double click. And we get this like Python installer. So I'm just
going to click through here. And we'll be able to install Python three on our computer. All right,
when that's done installing, we can just close out of this and we should be good to go. So now we
have Python three installed on our computer. The next step is we need to choose a text editor. So
we're going to be writing a bunch of code. So we're going to need some, you know,
environment or some program that we can write all that code in. And really, you can write Python
in any text editor, you could write it in like notepad or text edit, it doesn't really matter.
But there are special text editors that are designed just for writing Python code. And these are called
IDEs. And that stands for integrated development environment. It's basically just a special
environment where we can, you know, run and execute our Python code. And it'll, you know,
basically, like tell us how we're doing. So if we're, you know, if we write something that's wrong,
or we have errors, it'll kind of point us in the right direction of what we need to do to fix that
stuff. So in this course, we're going to be using an IDE. And one of my favorites, and one of the
most popular IDEs for Python is called pie charm. So I'm over here on this website, it's called
jetbrains.com forward slash pie charm. And this is the IDE that we're going to be using in this
course. So I'm just going to come over here and click this big download now button. And we'll go
ahead and download this pie charm program. Alright, so I'm getting this page over here. You can see
there's two versions of pie charm. One is a professional version, and that one's like paid,
so you have to pay for it. But then there's this community version down here, and that's
free and open source. So I'm just going to download this one. And this should have everything we need
to get started using Python. Alright, when that finishes downloading, I'm just going to pull that
up in my downloads folder. And again, we can just sort of run this guy. And I'm on a Mac. So I'm
going to have to drag it over to my applications folder. Alright, so we now have pie charm. And we
have Python three installed on our computer. So we're ready to start programming in Python.
And this tutorial, we're going to create our first Python program. And we're going to be able to run
it and see how everything works. The first thing I want to do is open up pie charm. So pie charm was
the IDE, the integrated development environment that we downloaded in the last tutorial. So I'm
just going to go ahead and search for it on my computer. And I can just open it up. And when we
first open up pie charm, we should basically just get a window prompting us to create a project. So
you can see that down here. Actually, the first thing I want to do though is come over here to
configure. And I'm just going to click on preferences. And I actually want to change the appearance. So
I'm going to go ahead and give this a theme. So I just went over here to appearance and behavior,
appearance. And I'm going to change the theme to dark. Just because I think it's a little bit
easier to use and look at. But you don't have to do that if you don't want to. And so down here,
I'm just going to click create new project. And it's going to give us this little window right here.
So I can just name my project. And I'm just going to name this draft. And then there's this other
option down here where it says interpreter. Now, what we want to select is Python version three.
So if I click down here, you'll notice that there's a couple different options. So I'm on a Mac
computer. And by default, Mac has Python version two installed on it. If you're on a Windows machine,
I'm not sure if it does. But in this tutorial, we're going to be using Python three. So you want
to make sure that you have Python three selected as the interpreter. Otherwise, you might not be
able to fully follow along with what we're doing in this video. So now I'm just going to come down
here and click create. And this is going to go ahead and create our project for us. All right,
so now we have our Python project up and running. And I want to show you guys how we can create our
first Python program. So over here in this draft folder, and this is just that project that we
created, I'm just going to right click and I'm going to say new. And I'm going to come down and
click Python file. So we want to create a new Python file, this will be our first Python program.
And I can just give this a name. So why don't we just call this app? And I'm just going to click
okay. All right, so now we have our first Python file open. And we can just start typing in some
Python. So I want to show you guys just a very basic Python program. And this is just going to be
a hello world program. So we're just going to print something out onto the screen. So what I
want to do is I want to type out PRINT print. And I want to type in open and closed parentheses.
Now inside of this parentheses, we can make some quotation marks. And inside of the quotation
marks, I'm just going to type out hello world. So this is like a very basic Python program. And
what this is going to do is it's just going to print out hello world onto the screen. And so now
what I can do is I can actually run this file and we're actually getting a little error here.
It wants us to create a new line at the end of the file. And so now in order to run our Python
program, I'm just going to come up here to the top. And I'm just going to click run. And we'll
click this run button right here. And now we'll see what happens. So it's asking me what I want
to run. I can just click app. And down here at the bottom, you'll notice that we have this text
that just got printed out hello world. And so whatever I print, or whatever I put inside of
this print statement is going to get printed out down below in the console. So as long as you're
getting that print statement to work, then you've actually written your first Python program. And
as we go through forward in the course, we're going to be writing all sorts of awesome Python
programs. And we're going to be doing all sorts of cool stuff.
In this tutorial, I want to talk to you about writing a basic Python program. So we're going to
write just a very simple Python program. We're going to look at how we can go ahead and write
our programs, we're going to talk about how our programs are getting executed by Python.
And we're also going to draw out a little shape onto the screen. So this is going to be pretty
cool. I'm excited to talk to you guys about just the bare basics of Python, and really just sort of
give you an introduction. So over here, we have our basic text editor. And so any of the Python code
that I write inside of this file is actually going to get executed by Python. So this Python file
has a bunch of Python code inside of it. And basically what we're doing when we're programming
in Python is we're just giving the computer a set of instructions. Right, that's really what
programming is, it's just like we're specifying a set of instructions. And the computer is going
to go off and execute those instructions that we give it. And so the more complex the instructions
we give to the computer, the more complex the tasks the computer can carry out. So I want to
show you guys how we can use some Python instructions or some Python code in order to draw out a shape
onto the screen. So I'm actually going to draw out a little triangle. So over here, we can actually
print something out onto the screen and Python using something called print statements. So I can
just type out print. And inside of these parentheses, I can just type some quotation marks and inside
the quotation marks, we can put whatever we want to print out onto the screen. So I'm going to
copy this and I'm actually going to make a few of these little print statements. And we're going
to actually draw a shape. So we're going to draw like a triangle type shape. So down here, I'm just
going to draw a forward slash. And then up here, I'll make a space and we'll do another forward slash.
Here, we'll make two spaces, we'll do another forward slash. And over here, we're going to make
three spaces and a forward slash. And now I'm going to draw like vertical bars going all the way down.
And actually, we can do some underscores here as well. That'd be kind of cool.
All right, so now we have our triangle looking shape and you can see it looks kind of like a
right triangle. So I can save my file. And I can either come up here to run and click run app.
Or if you're using PyCharm, you can also just come over here and click this play button.
And when I click the play button, I want you to notice what happens down here. So down here,
we have this little window and this is called the console. And basically, the console is just a
place where Python is going to output some information. So when we use this print statement,
we're basically telling Python like, Hey, can you print something out onto the console? So there's a
lot of situations where we're going to want to see what's going on in our code. And we can use
that print statement and print things out to the console. And we can basically like have a little
window into what our program is doing. In our case, we're just going to be printing out this
triangle onto the console. So I'm going to come over here and click this play button. And you'll
see down here, we're actually printing out that triangle. So mission accomplished, we were able to
print out our shape. So I could really draw any shape I wanted as long as I was able to specify
it inside of these print statements. So essentially, what Python's doing when I click that play button,
is it's going into this file and it's looking at all of these instructions in order. So the first
thing it does is it goes to this instruction, and it says, okay, they want me to draw out these
like little forward slash in this vertical bar onto the screen. Once it does that, it goes down
to this instruction, it says, okay, they want me to print something out again. When it prints that
out, it goes down to this instruction, etc. So Python is actually going to execute the lines of
code that we write in order. So the order in which I write the instructions matters a lot. So for
example, if I was to take this last line here, and put it up here at the top. Now when I save the file,
instead of just drawing out that triangle, it's going to draw out this little upside down bottom
thing at the top, and then the rest of the triangle down here. So the order of the instructions
actually matters a lot. So this is basically how your Python programs are going to go. As we go
through this course, we're going to learn more and more of these little lines. So here, we're
just using this little print function, basically, it just prints something out onto the screen. There's
a bunch of little things like this that we can learn as we go through, we'll learn more and more
instructions that we can give to the computer. But in this lesson, I just want to kind of show you
guys the basics, right? We can essentially just define these little instructions for the computer,
and the computer will execute them in order. In this tutorial, I want to talk to you guys about
using variables in Python. Now in Python, you're going to be dealing with a lot of data. So a lot
of times in our program, we're going to be working with all types of information and data and values.
And sometimes that data can be difficult to manage. So in Python, we have this thing called a variable.
And it's basically just a container, where we can store certain data values. And when we use a
variable, when we put those data values inside containers, it makes it a lot easier for us to
work with and manage all of the different data inside of our programs. So variables are extremely
helpful. And it's sort of like a core topic in Python that you're really going to want to know
in master. So in this tutorial, I'm going to teach you guys everything you need to know to get started
with variables, we're going to look at what they are, why they're useful, and we'll look at the
different types of variables and the different types of data that we can store inside of variables.
But over here in my Python file, I just have a very simple program. And it's basically just
printing some text out onto the screen. And so this is actually a little story that I wrote. It
says there once was a man named George, he was 70 years old. He really liked the name George,
but didn't like being 70. So this is a valid Python program. I could go ahead and run this program.
And you'll see down here in the console, everything prints out and it looks great. So
we have an awesome little Python program here. But let's say that inside of our story, I wanted
to change the character's name. So instead of naming the character George, let's say I wanted
to name the character John. Well, I'm going to have to go through and I'm going to have to manually
change the name George to the name John at every place inside of this story where it's mentioned.
So I'm going to have to go over here and I'll say, okay, John, I'm going to have to manually
change that. And then, okay, down here, we're gonna have to again, manually type in John and
change it to John. So in order to change the character's name, I had to manually go in and
change it in both places. And now let's say, okay, maybe we want to change the character's name age
also. So in addition to changing the name, we also want to change the age, let's make
them a little bit younger. So why don't we say John is going to be 35. And so you'll see here,
I had to come here and manually change the age and then come down here and manually change it
again. Right. And now that we changed it, like it's going to work, it's going to be updated in
our program. But you'll notice that in order to make that change, like I had to look through the
entire program, find it where that value was and change it. And this was only with four lines of
the story. Imagine if I had a story that was like, thousands of lines long. And we mentioned the
character's name and age, like hundreds of times. I mean, I'd have to look through each one of those
lines and manually change the character's name. And that is not a very good way for us to do this.
And it's really not a good way for us to manage the data in our program, right, the character's
name and the age. And so we can actually use a variable in order to store the character's name
and the character's age. And when we use that variable, it'll make it a lot easier for us to
put the character's name and age inside of our little program here. So I'm going to show you guys
how we can create a variable for the character's name and age. And I'm just going to go up here
above this print statement. And up here, I want to create a variable. So when we create a variable
in Python, we actually need to give Python a couple pieces of information. The first piece of
information we need to give Python is the name of the variable. So I need to actually assign a name
to this container where we're going to be storing information. And so all I have to do is just type
out the name of the variable that I want to create. So I'm going to create a variable called
character name. So I'm just going to say character. And I'm going to say underscore name. And so
generally, when you're creating a name for a variable in Python, you want to separate different
words with an underscore. So I have two words here, and I'm separating them with an underscore.
And now what I need to do is I need to put a value inside of this variable. So basically,
what I can do is I can just say equals, and over here, I can type a value. So I'm just going to type
out the character's name inside of quotation marks. So we're just going to type out john.
So now we have a variable for the character's name. And below this character name variable,
I want to create another variable called character age. So we're just going to type out character
underscore age. And I'm going to set this equal to the character's age, which is 35.
So we now have two variables, one representing the character's name and one representing the
character's age. So what I can do now is I can replace the character's name inside of the story
with this variable. And I'm going to show you guys how to do that. So if we want to put a variable
inside of this print statement, I'm actually going to have to do a couple of things. The first thing
I'm going to have to do is end off this text in here. So I'm going to have to put a quotation marks
here at the end. And you can see I'm basically wrapping this whole thing into a single like quoted
line. And now I also have this text over here at the end. So I'm going to have to put a quotation
mark here. So now I have a bunch of text here in quotation marks. And then I also have text over
here in quotation marks. Now inside of here, instead of saying the character's name, I want to refer
to that character name variable. So I'm just going to say plus character underscore name.
And I want to say another plus sign. So I basically am saying I want to print out all of this text,
plus the value that's stored inside of character name, plus all of this text. And what this will
do is actually print out the character's name. We print this. So I'm going to go ahead and run
this program. And you'll see down here, and actually let me put a space over here. And we'll run it
again. So you can see down here, we still are printing out there once was a man named John.
And so what Python is doing is when it executes this line of code, it's coming over here and it's
seeing, okay, they want to print out the value inside of character name. So it Python is going
to go up here, it's going to get the value. And it's going to insert it inside of there. So I can
basically do this for every instance of the character name in my file. So I can, you know,
I'll just copy this guy right here. And I can just paste it down here where the character's name is.
So I'll paste this. And you can see now we're doing exactly the same thing that we did up there.
I can also do this for the character's age. So I'm going to come down here and I'll paste in what
we just paste it in. And I'm going to change this instead of character name, we'll change it to character
age. And I'm going to do this in one more spot. So we're going to do this over here where we're
mentioning the age. And again, I'll just change this to character age. And so now when I run this
program, it's going to print out the same story that we were printing out before. But now I don't
actually have to type in the character's name and the character's age. I can just refer to this
variable. And the cool thing about variables is if I wanted to change the character's name,
now all I have to do is change it up here. So I could change the character's name to like Tom
or something. And we could also change their age. So I could change the age to be like 50.
And now that's going to automatically update throughout our entire story. So when I click this
play button, you'll see now we're using the name Tom. And he's 50 years old. So that's where variables
can come in handy. And this can be a really awesome way to control and manage the data that's inside
of our programs. With variables, you can also modify their values. So for example, let's say that
halfway through the story, I wanted to change the character's name. Well, I can just make some new
lines over here. And down here, I can actually assign a new value to one of these variables.
So I could say like character underscore name. And in order to give it a different value,
all I have to do is just say equals, and we'll give it a different value. So let's say halfway
through the story, we want to change the character's name to Mike. Well, now when I run on my program,
you'll see here in the first part of the story, it says there once was a man named Tom. And in
the second part of the story, it's referring to the name as Mike. So I actually updated the
variable inside of my program over here. And that's a really awesome way. You know, a lot of times
in these programs, you're going to want to be modifying the values of variables. So you can do
it just like that. So finally, I want to talk to you guys about the different types of data that we
can store inside of these variables. So over here, I have the character name and the character age.
And I'm actually storing what are called strings. So a string is basically just plain text,
right? So the name Tom, that's just plain text. Down here, these guys are strings as well. So
there's a data type in Python called a string, and it's just basically going to store plain text. So,
you know, any text that you would have like in a story, or you know, someone's name, you can store
inside of a string. There's also another type of data that we can store inside of Python,
which is numbers. So in addition to storing text, we can also store numbers. So if I wanted to store
a number, for example, let's say we want to store the character age instead of inside of a string,
we could store it inside of a number. So I could just type out 50. And when we're storing a number,
we don't need these quotation marks, you only need that quotation mark when you're storing a
string. So if I want to store a number, I can just write it out like this. And in addition to
using whole numbers, I could also use decimal numbers. So I could say like 50.5678213. And
Python's going to be able to store that number as well. So you can store all different types of
numbers. And we can also store what's called a Boolean value. And a Boolean value is essentially
a true or a false value. And there's a lot of instances in programming where we're going to
want to represent true or false data. For example, I could have a variable called like is mail. And
this would tell me whether or not someone was a male. And in my case, it would be true because
I'm a guy, right? So I can actually this needs to be capital. So I could sort of value of either
true or false. And actually, if we were naming this in Python, we want to use an underscore. So I
would say is underscore mail. And this can be either true or false. So true or false values is
maybe not something that you're used to dealing with like in the real world. But in programming,
we're going to be using true or false values all the time. They're super important. So like I
said, there's three basic types of data that we can work with in Python. There's strings,
which is just like plain text numbers, which would be like either decimal numbers or whole
numbers like this. And there's true or false values. And these are the basic types of data. I mean,
there's a bunch of other types of data that we can use. But I would say 99% of the time as a new
user and a new programmer to Python, you're just going to be dealing with these three types of data.
So those are the basics of the data and also with variables. And you're going to be using
variables all the time in Python. So you definitely want to practice up and get comfortable using them.
In this tutorial, I want to talk to you guys about working with strings in Python. Now one of
the most common types of data that we're going to be working with in Python is going to be strings.
And strings are basically just plain text. So any text that we want to have inside of our program,
we can store inside of a string. So I want to talk to you guys about all the cool things we can do
with strings. And we're basically just going to get a full introduction into why strings are
awesome. So over here, I'm just going to actually print out a string. So I'm just going to say print
and inside of these parentheses, I can type out a string. In order to create a string, I need to
use quotation marks. So I can make an open and close quotation marks just like that. Now inside
of the quotation mark, I can put whatever text I want the string to have. So we could say like,
draft Academy. And so now we have a string with the text draft Academy inside of it. So if I run
my program, now down here, we're going to print out draft Academy, as you can see. And when we have
these strings, there's actually a bunch of cool things that we can do with them. So one thing I
could do would be to create a new line inside of the string. So if I want it, I can come over here,
and I could say backslash n. And you can see it got highlighted in a different color. And what this
is going to do is it's actually going to insert a new line into the string. So now it's going to say
draft on one line and Academy on another line. So I'm going to click play. And you'll see down
here, we just get draft Academy. In addition to the backslash n, I could also use a backslash
quotation mark. So if I wanted to put a quotation mark inside of my string, I can't just put a
quotation mark like that, because Python is going to think that I'm trying to end this string. So if
I want to include a quotation mark, I can just use this special backslash character. And that's
called the escape character. And it basically just tells Python that whatever character comes
after it, we want to render literally. So when I say backslash quotation mark, it basically means
like, Hey, Python, I want to print out a quotation mark. And so now we'll be able to print out a
quotation mark right there. I can also use this to print out a backslash. So if I needed to print
out a backslash, I could say backslash. And it'll just print out a normal backslash now. So you can
see just like that. So if you want, you can use that backslash to make new lines or print out
quotation marks, or you can just use it as a normal backslash. In addition to just typing out a string
here, I could also create a string variable. So I could come down here up here, and we can create
a variable and we'll just call it phrase. And I'm going to set it equal to draft academy. So I can
store this string value inside of a variable called phrase. And then when I want to print out that
variable, or I want to access that string variable, I can just type the name of the variable. And
you'll see that it's going to print out the value that was stored inside of it. I can also use
something called concatenation. And concatenation is basically the process of taking a string and
appending another string onto it. So I could come over here and I could say phrase, and I can say
plus. And now I can add in another string. So I could say like, is cool. And now this is going to say
draft academy is cool. So I'm basically appending another string onto another one, they call that
concatenation. And so in addition to doing all that stuff, we can also use special things called
functions. And a function is basically just a little block of code that we can run. And it will
perform a specific operation for us. So we can use functions to modify our strings. And we can
also use functions to get information about our strings. So I'm going to show you guys a couple
common functions we can use with these strings. And they're actually going to do awesome stuff. So
in order to access one of these functions, I can just say phrase, and then I can say dot. And I'm
going to show you guys a function that we can use to convert this string entirely into lower case.
So I could just say phrase dot lower. And I'm going to want to type an open and close parentheses.
And this will take this phrase, it'll take my string and it'll convert it to lowercase. So you
can see now we just have draft academy down here, but it's entirely in lowercase. I could also do
the same thing for uppercase. So I could say phrase dot upper. And this will convert the entire string
into uppercase. You can see now it's entirely uppercase. So in addition to converting the string
into uppercase and lowercase, I could also check to see if a string is entirely uppercase or
entirely lowercase. So for example, I could say phrase dot is upper. And this is going to give me
back a true or a false value. So it's going to be true. If the string is entirely uppercase,
or false, if it's not. And you can see here, we're getting a false value because this is an
uppercase. I can also use these functions in combination with each other. So for example,
I could say phrase dot upper, and then parentheses. And then after this, I could say dot is upper.
And now what it's going to do is it's going to run this upper function, it's going to convert it
into uppercase. And then it's going to run this is upper function right after that. And you'll see
now we're going to get a true value back because it will have converted the whole thing into uppercase.
So you can see now we're getting a true value. So you can use these functions one after another,
it can be really useful. And there's a few other ones that I want to show you. So we can also
figure out the length of this string. So if I wanted to figure out how many characters were
inside of this string, I could just say LEN. And I'm actually going to make an open parentheses.
And I'm going to make a closed parentheses. So I'm essentially saying LEN. And this is another
function, it's the length function. And inside of this LEN function, I'm actually passing this
phrase variable, I'm basically giving the length function this, and it's going to spit out a number.
So it'll tell me how many characters are inside of this string. And so you can see here we get 15
because there's 15 characters inside of draft academy. So that's how we can get the length of
a string. And that's going to be really useful as we go forward and Python, we can also get
individual characters inside of a string. So imagine if I wanted to just grab one of these
characters, like imagine if I wanted to figure out what the first character in this string is,
we can actually use an open and closed square bracket, just like that. And in here, I can
specify the index of the character that I want to grab. So if I wanted to grab the first character
in this string, I can put in a zero. So if I say phrase square bracket zero, this is going to give
me that capital G. And you can see down here, that's what gets printed out. Now I want to point
something out in Python, when we're working with strings, a string gets indexed starting with zero.
So you'll notice that in order to access this G, I had to put a zero in here. And that's because
in Python, when we use indexes on a string, we start with zero. So if Python is counting the
characters, or it's indexing the characters in a string, it's going to start with zero. So it's
going to say G is zero, i is one, r is two, a is three. So we would say that G is at position zero
in the string, i is at position one, r is at position two, a is at position three, etc. So we start
counting at zero. So if I wanted to access the first character in the string, this G, I have to say
phrase zero, and that's just a special thing in Python, and actually most programming languages
do that. So they'll start with zero. So for example, if I wanted to access this a, I'm going to have
to put zero 123. So if I put three inside of here, now we'll be able to access that first a,
as you can see, we get an a right here. So this is actually really useful being able to grab a
specific character inside of a string, and you're going to be using that a lot as we go forward
in Python, there's also another really awesome function that we can use and it's called the index
function. And basically what the index function will do is it'll tell us where a specific character
or string is located inside of our string. So I could say phrase dot index. And I can actually
give this a value. So sometimes when we use these functions in Python, we can actually give them
information. So I can give this a value. And we would call this passing a parameter. So I would
call a value that I would give to a function a parameter, and you'll hear that word a lot. So for
example, I could say inside of here capital G. And now this is going to return the index of the
capital G inside of our string. So it should give us back a zero because G is at the zero of index.
And you'll see down here that we get a zero because that's where G is. So for example, if I put an a
inside of here, like a lowercase a, it should give me 0123, because that's where the first a is inside
of this string. So I can click the play button. And you'll see it's giving us a three, you can also
put actual words in here. So for example, I could put like Academy in here, or I could even just put
like a C A D. And this is going to tell me where this starts inside of my string. So when I click
play, it's going to give me an eight because that's where Academy starts right 012345678.
If I put something in here that wasn't in the string, so for example, if I put a Z in here,
that's not in here, it's actually going to throw an error. So when I play this, you'll see that we
get this error down here because Z is not found inside of our program. So that index function,
again, can be really useful. And we'll be using that a lot. There's also one more that I want to
show you, which is called replace. So we can say phrase dot replace. And in here, I can actually
give this to parameters. So I can give this replace function to values that it can use. And the first
thing I want to you to give it is what I want to replace. So for example, I could say draft.
And then I'm going to put a comma in here. And I want to put in here what I want to replace
draft with. So I could just say like elephant. And now instead of saying draft Academy, this is
going to print out elephant Academy. So you can see down here, it just says elephant Academy. So
this replace function can be really awesome because we can replace certain words or even certain
letters inside of our strings with other ones. So those are just some basic ways that we can work
with strings inside of Python. And there's a lot of these different functions that we can use with
strings. These are some of the most common the ones that I just showed you right now. But if you
just do a Google search, you can find all sorts of Python functions that you can try out and use and
you know, see what they do. But you definitely want to get comfortable working with strings in Python,
because you're going to be working with them a lot. In this tutorial, I want to talk to you about
working with numbers in Python. Numbers are one of the most common data types in Python. And any
Python program you write is most likely going to be dealing with numbers at some point. So I want
to talk to you guys about just the basics of using numbers, we're going to talk about the
different types of numbers that we can represent in Python. And I also want to show you guys some
awesome functions that we can use with numbers. So we can do certain like mathematical operations,
we can do some awesome things with numbers. I'm going to show you guys all of that in this video,
it's going to be awesome. So first thing we can do is we can actually just like print out a number.
So if I want to write a number in Python, it's really easy. I basically just write the number.
So I could say like print two. And this is just going to print the number two out onto the screen.
So I can use a whole number like this. I could also use a decimal number. So I could say like
2.0987. And Python is going to be just fine with that. We'll be able to print that out under the
screen as well. I can also make numbers negative. So I could make this 2.0987 a negative number. And
Python will be able to handle that just fine too. And Python, we can also use basic arithmetic. So
I could do things like addition subtraction multiplication. So I could say like three plus
four. And now this is going to print this out. Or why don't we say like three plus 4.5. And this
will be able to print out the answer. So we get 7.5. And we can do addition, we can do subtraction,
we can do division, or we could do like multiplication as well. So you can pretty much do whatever you
want inside of Python. And if you want to make more complex mathematical equations, we can also
use things to specify order of operations. So I could use parentheses. So for example, if I said
three times four plus five, what Python's going to do is it's going to multiply three and four
together. So it's going to be 12. And then we'll add five to that. So we should get 17. But let's
say that we wanted to change up the order, right? I could actually put a parentheses around four
and five. And now it's going to add them first. So it'll add four and five. So we'll get nine,
and we'll multiply that times three. So we should end up with 27. So you can see we can use those
parentheses to specify order of operations. So I could make like a very complex, a little
equation here just by using numbers and parentheses. We can also use one other operation, which is
called the modulus operator. So I could say like 10, and use this percent sign, three. And this is
actually read 10 mod three. And basically what this is going to do is it's going to take the first
number divided by the second number, and it's going to spit out the remainder. So it's basically
going to say, okay, 10 divided by three, that's going to be three with a remainder of one. And so
this is going to give us the remainder. And you'll see down here, we get one, because that's the
remainder of 10 divided by three. And that modulus operator can come in handy quite a bit. We can
also store these numbers inside of variables. So we can store them inside of these variable
containers. So I could come over here, and I could create a variable called like my underscore num.
And we'll just store a value of let's say like five in here. And so I could come down here,
and I can just print out my underscore num. And we'll be able to print that number out to the
screen just fine. So in addition to doing things like a basic arithmetic, we can also do some other
stuff. So one thing I could do is I could actually convert this number into a string. So if I wanted
to convert this number into a string, I can just say str, and I can put the number that I want to
convert inside of these parentheses. And so what's going to happen now is this number is going to
be converted into a string. So when I click the run button, you'll see it's just printing out five,
but now instead of being a number, this is actually a string. And this is going to come in handy when
you want to print out numbers alongside strings. So I can come over here and I can say like, my
favorite number, and we'll be able to print this number along with this string. So I can
click play. And it says five my favorite number. If I was to get rid of this string right here,
and I wasn't converting the number into a string, now Python is going to have a problem with this.
It's not going to allow us to do that. You can see we get an error. So anytime you want to print
out a number next to a string, you got to make sure that you use that little string function.
So there's a bunch of other stuff that we can do with numbers. And a lot of times in Python,
you're going to want to be using specific, like more advanced math operators. So there's a bunch
of different math functions that we can use on our numbers. And a function is basically just like
a little collection of code that does something. So a function could perform an operation like
a mathematical operation on our number. It could also give us information about our number. So
I'm going to show you guys some of the most common functions that you'll be using in Python
related to numbers. The first one is called ABS, and it stands for absolute value. So we can get
the absolute value of a number. And so over here, I'm just going to make this variable negative five.
And basically, I can just say ABS, and I can make an open and closed parentheses over here.
And this is going to give me the absolute value of this number up here. So when I click the play
button, you'll see it's just giving us five, because five is the absolute value of negative five.
I can use a few other functions. There's another one which is called pow. It's so it's just POW.
And I'm actually not going to use this variable. We just use a normal number. And this function is
going to allow us to pass it two pieces of information. So I can give this function two pieces of
information. The first is going to be like a number. And the second is going to be the power
that I want to take that number to. So I could say like three comma two. And this is basically
just going to be three raised to the power of two. So it's just going to be three squared. So we
should get nine down here. You can see that we do. So I could pass in like, you know, some crazy
number, like we could pass in a four and we could raise it to the power of six. And so we should
get like a pretty big number here. And you can see we get 4096. So this pow function is really
useful for taking numbers to specific powers. So in addition to using this pow function, we can
also use another function which is called max. So I can say max. And what this is going to do is
it's basically going to return the larger of the two numbers that we pass into it. So right now I'm
giving this a four and a six. And this should tell us which number is higher. So over here,
it's just printing out six because that's the bigger number. I can also use another function
called min. And this is going to do the opposite. So now instead of printing out the max number,
it's going to print out the smallest number. And it's going to print out four down here, as you can
see, another cool function is called the round function. And this is going to allow us to round
a number. So it's just going to follow like standard rounding rules. So if I said like 3.2
inside of here, now it's going to round it down to just normal three. But if I said like 3.7,
it's going to round it up to four. So that'll allow you to round a number.
So there's a few other functions that I want to show you guys, but in order to get access to them,
I'm actually going to have to do something called importing. And in Python, we can actually import
external code into our files. And so if I want to access these specific math functions,
I have to import something called Python math. So I can just say from up here, math, import,
and this star. And basically, what this is going to do is it's going to go out and it's going to
grab a bunch of different math functions that we can use. So I already showed you guys a few of
these different math functions, but there's actually a bunch more. And in order to access them,
we're going to need to include this line of code inside of our file. So once we have that,
now I can access a few others. So there's another function which is called the floor method. And
it's just FLOR. And what this will do is it'll basically just grab the lowest number. So it's
essentially just going to chop off this decimal point. And you can see over here, now we get a
three, because we're using that floor function, there's also another one called seal. And that's
just going to do the exact opposite. So that's just going to round the number up no matter what. So
no matter what, we'll always get four with we have a three point whatever here. And there's
another one which is called square root. So it's just SQRT. And essentially, this is just going to
return the square root of a number. So I could say like 36. And now we should get six back. So
those are awesome, very interesting functions. And inside of this math module, so we would call
this a module. And you don't have to worry too much about what that is right now. Just know that
when we put this line of code into our program, it gives us access to a lot more math function.
So it basically allows us to do a lot more things with math inside of our program. And there's a
bunch of different math functions in Python. And if you want, you can basically just go online and
search for different math functions. I showed you guys a few here. And I would say the ones that we
looked at here are probably the most commonly used, but there's a bunch more. And like I said,
you know, there's tons of documentation on all this stuff. So you can just look up, you know,
math functions in Python. And there's going to be tons and tons like lists of these things
that you can use inside of your programs to perform different math operations. But for now,
that's just been a little bit about using numbers a little bit about doing, you know,
certain math operations and using functions with those numbers.
In this tutorial, I'm going to show you how to get input from a user. So we're basically going to
allow a user to input information into our program. And I'm actually going to take the
information that the user inputs, I'm going to store it inside of a variable. And then we're
going to be able to do something with that variable. So I want to show you guys how we can actually
go about getting input from someone. And down here in my Python file, all I have to do to get
input from someone is I just have to type out input, and then an open and closed parentheses.
And what this is basically going to do is it's going to tell Python like, hey, we want to get
input from a user and Python will allow the user to type in some information. And inside of these
parentheses, we can actually type a prompt. And so generally, when we want the user to enter in
some information, we want to tell them what we want them to enter. And that's what we can put
inside of here. So I'm just going to say, enter your name. So we're actually going to create a
little Python app, which is going to allow the user to enter in their name. And then we'll store
the name inside of a variable, and then we'll basically just say hi to the user. So we'll say
like, hi, and then whatever the name they entered was. So what I want to do is I actually want to
store whatever the user inputted into our program inside of a variable. So I can actually go over
here, and I can create a variable, I could just call it name. And I could set this variable
equal to input. So what I'm doing now is I'm taking the value that the user inputs,
and I'm storing it inside of this variable container called name. And so now I have access to whatever
they entered in. So what we can do is we can say print. And inside of the parentheses, I can just
print out hello. And then I'm just going to add on to this name. And actually, why don't we put
an exclamation point? So essentially, we're getting a name from the user, we're storing it inside of
the name variable. And I'm printing it out onto the screen. So I'm going to go ahead and play this.
And when we run the program, you'll notice down here, Python is giving us this little prompt. So
it's saying enter your name. Now in here, I can actually type in a value. So I can type in Mike.
And then I can just hit enter. And Python's going to go ahead store this value inside of that variable.
And then it's going to print out hello, Mike. And so this is kind of cool. And actually, what we
could do is we could prompt the user to enter in more than one piece of information. So I could
actually copy this line of code. And I'm going to paste it below here. And why don't we have the
user enter in another variable? So we could have them enter in, for example, their age. And over
here, we just want to say enter your age. And so now we're able to get two pieces of information
from the user. And I could say hello, name. And then I can say, you are, and I'll print out their
age. So I'm just going to print age. So now I'm basically able to get both of these pieces of
information from the user, I can get their name. And I can get their age. So when we run this program,
I can type in both of those pieces of information. So to say, answer your name. So we could just
make up a name like john, and then enter the age, let's say john is 25. And now it's going to say,
Hello, john, you are 25. So the program is able to get input from the user. And then it's able to
use that information in order to say hi to the user and tell them how old they are. And so
getting input from the user is a great way to make your programs more interactive. And here,
we're just scratching the surface, right? We're just basically getting to, you know, a piece of
information from the user, but you can use these inputs to do awesome things in Python. And as we
go through this course, you're going to see some really cool things that we can do with these inputs.
In this tutorial, I'm going to show you how to build a very basic calculator in Python,
we're basically going to build a calculator where we'll get two numbers from a user,
and then we'll add those numbers together and print the answer out onto the screen. So let's
look at some practice in getting input from users. And we'll also talk about getting numbers from
users as opposed to just strings. So I'm really excited to get started on this calculator. The
first thing we want to do is create two variables and inside of those variables, we want to store
the two numbers that the user wants to add together. So down here, I'm just going to say,
num one. And that's what we'll call our first variable is equal to. And now I need to get
input from the user. So I'm going to say input. And inside these parentheses, I'm going to say,
enter a number. And now I basically want to do the same exact thing, but for the second number.
So we'll paste this guy down here. And I'm just going to say num two. And then we'll just say,
enter another number. So now we're able to store the user's input inside of these two variables.
So now all I want to do is just print out the result. So I'm actually going to create a variable
called result. So I'm just going to say result is equal to, and basically, we just want to add
these two numbers together. So I'm going to say num one plus num two. And now all we have to do
is just print out the result. So I'm just going to print result. So we basically have our entire
program written out here. And this will allow us to enter in two numbers, and then we'll add them
together and print out the answer. So I'm going to click play. And down here, we can enter our number.
So I'm going to enter the first number, we'll say five. And I'm going to enter my second number.
So let's say 8.0, or let's say 8.3. So now we should print out the answer. But if you look over here,
that's not quite the answer we were looking for, right, five plus 8.3 is definitely not 58.3. And
here's what happened. When we get input from a user by default, Python is just going to convert
it into a string. So it doesn't matter what we put down there when we're entering the numbers,
Python's always just going to be like, okay, this is a string, that's it. Sounds good, right? It's just
going to make it a string. In order for us to make these num one and num two variables numbers,
we're actually going to have to convert the strings that we get from the user into numbers. And so
the way that we can do that is by basically just coming over here. And when we add the two numbers
together, I can use a special Python function in order to convert those strings into numbers.
And there's actually two Python functions that we could use for this. The first is called int.
So I could say int, I can make an open parentheses and I can make a closed parentheses. And basically
what this is going to do is it's going to convert whatever is inside of these open and closed parentheses
into an integer number. And an integer number is basically a whole number. So it's like one,
two, three, four, five. So it's a, it's a whole number. In other words, it can't have a decimal
point. And so what I could do is I could say, int, and I could put these inside of parentheses.
And now what this is going to do is it's going to convert both of these numbers into
integers. In other words, into whole numbers that don't have decimals. And it's going to add
them together and we'll print out the results. So why don't we do that? And we'll just see if
this works. So I'm going to click the play button. And down here, we'll enter our first number. So
I'm just going to enter in a four. And then I'll enter in a five. And now you can see it's
printing out the correct answer four plus five. And so that's essentially our basic calculator.
But we do have one problem with this calculator. So I'm going to run it again. And I'll show you
guys with that problem is if I wanted to add in a decimal number, so we'll add 4.3 and 5.5.
You'll see that we're getting this error. And that's because I'm using this int function
up here. And this int function is looking for a whole number. So if I put decimal numbers
inside of there, it's going to break the program. There's another function we can use
instead of an int, we can say float. And a float is basically just a number that has
decimal. So it's a decimal number. So I can convert both of these guys into floats.
And now I'll be able to use decimal numbers. So I can click play. And I can just enter in some
numbers. So I could say like four. And I could also say like 5.5. And now it's going to be able
to add both of those numbers, even if one of them is a decimal. And that's the beauty of using that
little float function. So in certain circumstances, you might want the user to only be able to enter
in an integer to a whole number. But in a lot of cases, like in this calculator, we want them to
we want them to be able to enter any number that they want. So we're going to use this float
function. So I'm just going to recap what we did one more time, we created two variables,
num one and num two. And we basically stored the two numbers that the users input into those
variables. And then we converted them into numbers. And we added them together and we printed out
the results. So this is a pretty cool calculator. I'm happy with it. Hopefully you guys learned
something. And as we go forward in this Python course, you're going to learn all sorts of ways
that we can make this calculator more complex. In this tutorial, I want to show you guys how to
build a madlibs game in Python. So a madlibs game is basically just a game where you can
enter in a bunch of random words. So like, you know, verbs, nouns, names, colors. And you basically
take all of those words and put them into a story randomly. So over here, my web browser,
I just have an example of a madlibs. So, you know, down here, we're basically just taking random
words, like an adjective or a noun or a plural noun, and entering them in side of a story. So
like you read through the story and then like some random word pops up and madlibs can be pretty
fun. And I'm sure you guys have, you know, at least heard of madlibs before. But if not,
that was just a little introduction. So I want to create a madlibs in Python. And this is going
to be really cool. So down here, I have this like little poem, which just says, roses are red,
violets are blue, I love you. And so, you know, this is kind of like a classic poem. But I think
we should mix it up a little bit and create a madlib for this poem. And so instead of having this red
color over here, I want to have the user enter in their own color. Instead of having violets,
why don't we have them enter in a plural noun? And instead of saying I love you, why don't we have
them say I love and then some celebrity? So celebrity. So this is the basic structure for our little
madlib, right? I want to basically have the user enter in a color, enter in a plural noun,
and enter in a celebrity. And then I'll take all of those words and put them inside of our
madlib. And hopefully we end up with something that's pretty funny. So how can we actually go
about building this in Python? Well, the first thing we're going to have to do is we're going to have
to be able to get these words from the user, right? That's pretty obvious. So I think what we should
do is we should create three variables called color plural noun and celebrity. And we'll store the
user's inputs inside of those variables. So up here, let's create those variables. The first
variable I want to create is going to be called color. So say color. And I want to set this equal
to the input. So I'm going to say input. And inside of here, I'm just going to give them a prompt. So
I'll say enter a color. And we can basically just copy this line of code. And I'm going to paste
it down below. And we'll paste one more time. So now we're going to create our plural noun
variable. So I'll say PL, URL, plural noun. And we'll just say enter a plural noun. And actually,
over here, I want to make a colon as well. So then finally, we'll create our celebrity variable
and we'll say enter a celebrity. And so now basically, we're creating three variables called
color plural noun and celebrity. And inside of them, we're storing the color that the user enters,
the plural noun, the user enters and the celebrity, the user enters. So last thing we have to do is
replace these guys with those variables. So I'm basically just going to add in the color over here.
And we're going to add in the plural noun. So I can just go like this.
And finally, we'll just add in that celebrity. So now we have a pretty cool mad lib. And it should
be functional. Everything should work. It looks like it's good to go. So let's come over here and
we'll run our program. And then down here in the console, we can play our little mad libs game.
So let's say enter a color, I'm just going to say magenta, enter a plural noun. Why don't we say
microwaves and enter a celebrity, let's say Tom Hanks. So when I click enter, what should happen
is we should get our mad libs printed out. So I'm going to click enter. And now we have our mad
libs. So it says roses or magenta, microwaves are blue. I love Tom Hanks. So it's a pretty good mad
lib. And obviously, you know, the more the more print statements we put over here, you know,
the better the mad lib could could be. And we could store as many, you know, variables up here as we
wanted. This was just a very simple mad lib. We only took in three inputs, but you know, you can
see how you could take something like this and create a little mad libs game. So your homework
for this lesson is to go off and make your own mad libs game. Hopefully one that's a little bit
funnier than mine. But that's the basics of creating a mad libs game in Python.
In this tutorial, I want to talk to you guys about working with lists in Python. A lot of
times when you're programming in Python, you're going to be dealing with large amounts of data.
And when you're dealing with large amounts of data, you want to make sure that you can
manage it and organize it properly. And the list is essentially just a structure that we can use
inside of Python to store lists of information. So we can take a bunch of different data values,
we can put them inside a list, and it allows us to organize them and keep track of them a lot
easier. So generally, you would create a Python list, and you would put a bunch of related values
inside of that list, and then you can use it throughout your program. So I'm going to show you
guys just the basics of using lists, we're going to look at some of the common use cases and
really just get you up to speed with what lists are and why they're useful. So over here in my
program, I'm actually going to create a list. And we create a list a lot like we create a
Python, just like a normal Python variable. So the first thing we want to do when we're making a
list is give it a name. So we want to give it a descriptive name, something that's going to
basically describe what's inside the list. In my case, for this example, let's make a list of
like friends. So we could say like here's a list of all my friends. So I can say friends.
And I'm going to set this equal to the list of values that represent my friends. And we want
to create a list, we can use these open and closed square brackets. So whenever you use these open
and closed square brackets, Python basically knows, okay, they want to store a bunch of values inside
of this list. So why don't we go ahead and start adding some values? So we can say like my friends
are like Kevin, Karen, and let's say Jim. So here I have three values inside of my friends array.
So when we're creating a normal variable in Python, generally, we just give it one value,
right? So I would create a variable, I would give it one value, maybe a string or a number or a
boolean. But when we make lists, we're able to store multiple values inside of the same like little
item inside of the same object, right? And then what I can do is I can access these individual items
inside of my program. So inside of this friends list, we're basically storing all of these different
values, and that can be really useful. So now that we've stored some values, let's talk about what
you can put inside of a list. So you can really put any piece of information in Python, we could
put things like strings, numbers, or booleans. So if I wanted, instead of just putting strings
here, I could change like one of these to a number, and I could change one of these to a boolean,
and it wouldn't matter, Python is going to be fine with that. But let's change these back to
strings just for the sake of the example. So now that we've created our list, how can we access
individual elements inside of this list? It's a good question. I'm gonna go ahead and print
something out to the screen. The first thing we can do is we can just print out the actual list.
So I can just say print friends. And then when I run this program, if we look down here in the
console, you can see it's just printing out the actual list of friends, right? But let's say that
I wanted to print out a specific element inside of this list. Well, I can actually refer to elements
by their index. So each one of the elements inside of this list has a particular index. And the index
is actually start at zero. So we would say that this first element in the list, Kevin has an index
of zero. We would say that this other element Karen has an index of one. And this third element
Jim would have an index of two. So even though Jim is technically the third element inside of the
list, it has an index of two. And that's because we start indexing index position one. And so that's
just kind of how lists work. And that's how list indexes work. So if I wanted to access a specific
element inside of my friends list, I can just refer to it by its index. So over here after we
type out friends, I'm just gonna type an open and closed square bracket. And inside of this open
and closed square bracket, I want to put the index of the element inside the list that I want to
access. So if I wanted to access this guy right here, this element, I can just put a zero. So I can
say friends zero. And now when I run my program, it's just gonna print out that value, it's just
gonna print out Kevin. If I wanted to access this Jim value over here, I can put a two. And that
will give us that Jim value, you can see right there. So we can access elements based off of
their index, we can also access elements based off of their index from the back of the list. So
for example, if I said negative one inside of here, this is gonna give me access to this value
right here for Jim. And you can see that sort of does so when we use negatives, it starts indexing
from the back of the list. So if I said negative two, this is gonna give us Karen. And so you can
use the negatives to access items at the back of the list. Just keep in mind though, and this is kind
of interesting. We would say this first element here is index position zero, right? But we would
say the first element at the back of the list is index position negative one. So just keep that
in mind. So there's actually other ways that we can access like just portions of this list. So for
example, let's say I just wanted to select the last two elements in this list. And I didn't want
to select the first one. Well, in here, I can just say one with a colon. And this will grab the
element at index position one and all of the elements after that. So now we should just grab
Jim and Karen or Karen and Jim in that order. I could also specify a range. So actually,
let me add in a couple other elements just so we can illustrate this better. So I'll add in two
more names. So if I wanted to, I could select like the element at index position one up to the
element at index position three. So it's going to grab this one. So that's one, two. And it's actually
not going to grab the third one. So it's it's going to grab all the elements up to but not
including three. So it'll grab Karen and Jim. So let's go ahead and run this. And you can see
it grabs Karen and Jim. So you can either just put like one with a colon after it, and then it'll
grab that index position and everything after it, or you can specify like a range that you want to
grab from. So that's how we can access elements inside of this list. But we can also modify elements.
So for example, I could come down here, let's say I want to modify this value right here, Karen.
Well, all I have to do is just access that element. So I could say friends and Karen is at index
position one. And I can change this to whatever I want. So I could say friends index position one
is equal to Mike. And now when we print out friends index position one, that value is going to be
updated. So it's just going to say Mike instead of saying Karen. So it can be really useful to
modify values inside of arrays. So it can be really useful to modify values inside of Python lists.
So those are kind of the basics of working with lists in Python. And actually in the next tutorial,
I'm going to show you guys even more ways that we can use these lists, we're going to learn how
to like add elements to a list, delete elements from a list, copy lists, and a bunch of cool stuff
with these lists. But for now, this has just been sort of like the basics of working with lists.
In this tutorial, I want to talk to you guys about using functions with lists in Python.
And Python, a list is one of the most important structures where we can store our information.
A list basically allows you to take a bunch of different values and organize them and store them
inside of their own little list structure. So down here in my program, you can see I have two lists.
I have a list of lucky numbers. And this is just a bunch of different numbers. And then I have a
list of friends. So this could be like a list of somebody's friends. And it's basically just a
bunch of strings with names in them. So these two lists are storing a bunch of different values.
And what makes lists awesome is not only could I store like this many values, I mean, I could store
hundreds or thousands or millions of values. And when you're working with a lot of data and a lot
of information in Python, these lists are invaluable. So because lists are so important, there's actually
a bunch of different functions that we can use with lists. And those functions allow us to do
things like modify the lists and get information about the list. So in this tutorial, I just want
to show you a bunch of those functions, we're going to talk about some of the most common and
the most popular functions that you would use on lists to make them more powerful and make them
more easy to use. So the first thing we can do is obviously just print out a list. So down here,
I'm printing out this friends list. And when I want to run my program, you see down here,
we're just printing out all the elements in the list. So it's always good to be able to print out
all the elements inside of a list. But there's a lot of other functions that we can use. So
one function is called the extend function. And the extend function will basically allow
you to take a list and append another list onto the end of it. So I could say like friends dot
extend. And inside of these parentheses, I can pass in the list that I want to add on to the
friends list. So I could say like lucky numbers. And now when I print out this friends list,
not only is it going to have all the elements in the friends list, it's also going to have all
the elements inside the lucky numbers list. You can see down here, we have all of our friends.
And then we also have the numbers over here. So that's a really easy way to essentially just like,
add two lists together. We can also just add individual elements onto a list. So I could come
down here and I could say friends dot append. And this will actually allow me to append another item
onto the end of this list. So over here, I could just add another name. So we could say creed, for
example, and now I'm like adding another friend onto the end of this list. So when I run the program,
in addition to having the original items that were in the list up here, we also have this
additional item at the end, and that append function is always going to add the item onto the end of
the list. But let's say that you wanted to add an item into the middle of the list. So like right
here or right here, there's another list function called insert. And insert is going to take two
parameters. The first parameter is going to be the index where you want to insert the item. So
why don't we insert it here at index position one? I'm just going to say one. And then I'm going to
type in the name of the element that I want to add. So we can just say like Kelly. And now at index
position one, we're going to have the value Kelly, and all of these other values are going to get
pushed up, they're going to get pushed to the right one index position. So now I run the program,
you can see we have Kevin, this new element Kelly, and then all the other elements have been pushed
off to the right, we can also remove elements. So for example, let's say I wanted to remove this
gym value, all I have to do is just say friends dot remove. And I can just type in whatever element
I want to remove. So I want to remove this gym element. And so now when we run the program,
Jim is going to be gone from the list, and we just have these four guys left, you can also remove
all of the elements from the list. So if I just wanted to completely like reset the list and get
rid of everything, I could just say friends dot clear. And this is going to give us an empty list.
So it'll get rid of every single element inside the list. There's also another really cool method,
which is called pop. And you can just type out pop like that. And basically what pops going to do
is it's going to pop an item off of this list. So when I click the play button, you'll see here
that we're left with Kevin, Karen, Jim, and Oscar. So we basically got rid of the last element inside
the list. And that's what pop does, it pops an element off of the list. So it basically removes
the last element in the list. So we have our list, but let's say that we wanted to figure out if a
certain element was in this list, right, if a certain value is in here, so let's say I wanted
to check to see if my friend Kevin was in this list, well, I could say friends dot index. And in
here, I can just pass a value. So I could say like, Kevin, and this is going to tell me the index
of Kevin. So it'll tell me if Kevin's in the list. So now when I click play, you can see down here,
it says zero. If I was to say like Oscar in here, for example, now it'll give me the index of Oscar.
So that's it three. But if I put a name that wasn't in here, so like, if I put Mike, that's not in the
list. And so now we're going to get back in air, because it says Mike is not in the list. So that's
an easy way to figure out if a specific element is inside of the list. And if it is, then you know
what index it's at, you can also count the number of similar elements in the list. So I'm actually
going to come down here, copy this gym element, and we'll add another gym element into here. So I
could come down here and I could say friends dot count. And we'll pass in gym. And this will tell
me how many times the value gym shows up inside of this list. So it should say two, which it does
down there, because we have two values with the text gym. And so we have all these values, we could
also sort this list. So I could come over here and I could say friends dot sort. And what this
will do is it'll sort the list in ascending order. So we have a bunch of text here, and you'll see
what it does. Basically, what it's going to do is it's going to put it in alphabetical order. So
you can see we have J K O and T. So the list went from being just random order, like whatever we
had up here to now it's in alphabetical order. And that's also going to work for lucky numbers. So I
could say like lucky numbers dot sort, and then we'll print out lucky numbers. And these should
now be in ascending order, which I'm realizing they already are up here. But if I was to take this
42 and put it down here, now it will figure everything out and put it in ascending order. Anyway, we
can also reverse a list. So if I wanted, I could reverse this lucky numbers list, I could say
lucky numbers dot reverse. And now when we print it out, instead of being 4815 1623,
it's going to be 2316 15 842. So it's basically just reverse the order of the list. And finally,
there's one more list function I want to show you guys which is copy. So I could actually create
another list, and I could make it as a copy. So I could say like, friends to, and we could set this
equal to friends dot copy. And now friends to is going to have all the same attributes as friends.
Because it's basically just like copied it basically took a copy of that friends list. So
you can see it's the same exact thing and copying lists can come in handy in a lot of
different situations. So those are just some basic list functions and just to kind of like,
you know, get your feet wet and working with lists. Obviously, like the more you work with them,
the more you practice using these different functions, the more comfortable you're going to get.
But that's just kind of like a basic overview of what you can do.
In this tutorial, I want to talk to you guys about tuples in Python. Now a tuple is a type of data
structure, which basically means it's a container where we can store different values. Now if you're
familiar with lists in Python, a tuple is actually very similar to a list. It's basically a structure
where we can store multiple pieces of information. But a tuple has a few key differences from lists
that we're going to talk about in this tutorial. First thing I want to do is I want to show you
the how to create a tuple. So I can come down here and I can create a tuple. And one of the most
common examples of tuples is coordinates. So let's say I had a series of like XY coordinates, I
could create a variable called coordinates. And I'm just going to set this equal to an open and
closed parentheses. And whenever we want to create a tuple inside of Python, we're going to use
these open and closed parentheses. And inside of here inside of these parentheses, we're going to
put the values that we want to store. So I could, for example, put a list of numbers. So this could
be like my coordinates, I could have like four and five. So this would be my tuple with coordinates
inside of it. And one of the things about tuples that makes them unique is a tuple is immutable.
And you'll hear that word a lot when we're talking about tuples immutable, it basically means that
the tuple can't be changed or modified. Once we create our tuple up here, you cannot modify it,
you cannot change it, you can't add elements to it, you can't erase elements from it, you can't
change any of the elements inside the tuple. What you see is what you get, once you create the
tuple, it's, it's as it is, it can't change. So I could come down here and I can print out
some of the attributes inside the tuple. And the way I can access them is just by using an open
enclosed square bracket. And in here, I can pass in a index. So tuples are indexed starting at zero.
So technically, we would say that four is at index position one and sorry, four is at index
position zero and five is at index position one. So if I said coordinate zero, now it should print
out four. And down here, we're printing out four. So if I wanted to print out that five,
we can just put a one inside of those square brackets. And now we'll print out that five. So I can
access the elements inside of a tuple, I can easily access each, each entry. But if I was to come
over here and try to change one of the elements, so if I said coordinates, and coordinates one,
and I tried to set it equal to something else like 10, Python's actually going to give us an
error when we run this program. So I'm going to run the program, you see we get this error, it says,
type error tuple object does not support item assignment, because it's immutable, because we can't
change it. So that's basically what tuples are how we can create them. And you know, a tuple,
like I said, it's very simple, you just create it, and then you have it. And now I want to talk to
you guys about a common topic in Python, which is the difference between tuples and lists. So
remember, a list is basically doing the same thing a list. If we wanted to create a list, we would
just use square brackets instead of those parentheses. And if I was using a list, I could assign
different values to it, I could I could basically mutate any of the elements I could add delete,
modify change, whatever I wanted with the list. But with a tuple, we can't do that. And that's
sort of the basic difference. And really, Python doesn't say anything more than that. It basically
just says like one of them's immutable, one of them's not. But in practical use cases, generally,
people will use tuples for data that's never going to change. So you'll you'll store data inside of
a tuple, when that data doesn't need to be mutated. That's why something like coordinates
is such a good candidate for a tuple, because once you have coordinates, like, we're probably
not going to modify them, right, we might add other coordinates. So for example, if I wanted,
I could create a list of tuples. So I can say like 4567, maybe we have some other coordinates,
like 8034. So here I have, you know, I have a list and inside of it, we have these tuples.
But remember, these tuples can't be modified, the values can't be changed. So really, it's just a
case by case basis, I would say by a huge majority, you're going to be using lists most of the time.
Tuples are more of a niche, you know, they're using more special situations. But there are
plenty situations where you want to use tuples. So if you're in a situation where you want to store
data that can't be changed or mutated, then tuples are the way to go. So yeah, that's just some basics
about tuples, some basics about the difference between lists and tuples. And so a tuple is a
great thing to add into your Python repertoire. In this tutorial, I want to talk to you guys about
using functions in Python. Now a function is basically just a collection of code,
which performs a specific task. So I can take a bunch of lines of code that are basically doing
one thing, I can put them inside of a function. And then when I wanted to do that task or do that
one thing that the function was doing, I can just call the function. And so functions really
help you to organize your code a lot better, they allow you to kind of break up your code into
different, you know, little chunks that are doing different things. And they're really just awesome.
So functions are like a very core concept when we're talking about programming in Python. So I'm
going to show you guys how to create an awesome function today. Let's say for the purposes of
this tutorial, we want to create a function that says hi to the user. So the one task that our
function performs is basically just saying hi to whoever is writing the program. So over here,
if I want to write a function, the first thing I have to use is a keyword in Python,
it's called death. So D E F. And basically, when Python sees this keyword, it's going to say,
okay, this person wants to use a function. So after we type out death, we need to give this
function a name. So just like when we are creating variables, we give them descriptive names,
we also want to do the same thing with functions. So if I'm creating a function, I can just give
it a specific name, which basically says like what it's doing. So we're going to create a function
that says hi. So I'm just going to call this function, say hi, just like that. And once we type
out the name of the function, and so also I could type out say hi with no underscore or I can type
out say hi with an underscore, both are considered like good practices in Python. A lot of times,
if you just have a simple like two word function like this, you don't need to put an underscore,
but we could put an underscore there. If we wanted, after we type out the name of the function,
I'm just going to type an open and close parentheses. And then I'm going to type a colon. And basically,
what this is doing is it's saying to Python, like, all right, all the code that comes after this line
is going to be inside of our function. And in order to write code that's going to end up being
inside the function, we actually have to indent it. So over here, you'll notice that when I clicked
enter, my text editor automatically indented the text. So it's automatically like, using this indent
here. And that's kind of like one of the rules in Python is like, the code that goes inside of
this function needs to be indented. So if I was to write some code, like out here, this is no longer
going to be considered inside the function. So you can see, as I type out code, and obviously,
this isn't real code, but as I type text over here, that's indented, this little like marker over
here is basically saying like, Oh yeah, that's inside the function. But then when I write code
over here, that is like not at the same indentation level as this stuff, it's no longer considering
it inside the function. So that's just a little thing. Any code inside this function needs to be
indented. Alright, so our function is just going to say hi to the user. So I'm just going to have it
print out some text, it's going to say hello, user. So this is a very simple function. Obviously,
we just have one line of code and inside of a function, you could have, you know, as many lines
of code as you want. But for our purposes, we only need one line in order to perform our function.
So now all we have to do is call this function. So if I want to execute the code inside of this
function, I have to do something called calling it. So if I was to just run my program as it is
right now, I'm just going to run it, you'll see that nothing happens over here, right? Even though
this function is printing out hello user, when I run the program, it's not doing it. And that's
because the code inside of a function isn't going to get executed by default. The code inside of
a function is only going to get executed when we specify that we want to execute it. And in order
to do that, we're going to have to do something called calling the function. So in order to call
the function, you basically just type out the functions name and those open and close parentheses.
So I'm just going to type out say hi, and open and close parentheses. And now when we run this
program again, you'll see that it prints out hello user. So we're executing the code inside of the
function. And I want to show you guys just one more thing here, just talking to you guys about
how these functions actually work. So up here, I'm going to print top. And then down here, I'm
going to print bottom. So I want to show you guys the flow of these functions inside the program. So
when I run this program, you'll see we print out top hello user, and then bottom. So essentially,
what's happening is when Python goes through and executes this program, it goes over here to
this first line, it says, okay, we want to print out the word top. And then it goes down here and
it says, okay, we want to execute the say hi function. So Python actually jumps up, and it goes
over to this say hi function, and it's going to execute all of the code inside of this function.
So it's going to go through execute all this code. And then once it's done executing all the code
in the function, it's going to jump back down here, and it's going to move on to the next line,
which is bottom. So that's kind of like the flow of functions. Again, with functions generally,
when we're naming these functions, you want them to be named in all lowercase. And usually,
when we're naming stuff in Python, if there's two or more words, we're going to use an underspace
or an underscore in between them. So I could write this out as say underscore high. But in a lot
of situations, though, if I have a function like this where the name is really short, I might just
be easier to leave it without an underscore, but why don't we just put an underscore in there just
to be a super Python official. All right, so now we can actually make these functions a little bit
more powerful. And what we can do is we can give them information. So a lot of times when we write
a function, we're going to want to have additional information that gets passed in. And these are
called parameters. So a parameter is a piece of information that we give to the function. So over
here, I can actually specify that this function needs to receive some parameters. So I can basically
say like, Hey, if you're going to call this function, you need to give us some information,
you need to give us some parameters. And all I have to do to do that is just type out the name
of the parameter that I want to receive. So why don't we allow the code calling this function to
tell it what name to say hi to up here, I can just say name. And basically, what this means is it
means whenever I call this say hi function, we have to give it a name. So down here, if I was to call
this, I have to include a name in here. So I can say like, Mike, and what we can do now is we can
actually access this parameter or this variable inside of our function. So I could come over here
and instead of saying hello user, I could say hello name. And basically, what this is going to do is
it's just going to say hello to whatever name got passed in here. So I'm actually going to copy this
and we will do this twice. So I'll say hello, Mike hello, Steve. And now when I run this program,
you'll see that instead of just saying hello user, it's saying hello to whichever name I passed
into the function. So that's why this can be really useful, right? We can give the function
information, and depending on the information we give it, it'll perform its task a little bit
differently. I could also include more than one parameter. So you can, I mean, technically, you
could have like as many parameters as you want. So I can put another one in here, we can say age.
And now I'm going to have to pass in an age along with these. So I'm just going to pass in age and
pass in an age for down here. And I'm just going to pass in strings. So we can say hello name.
You are age. So I'm passing in two pieces of information. And now when we run this program,
it's going to call the function. And it's going to use both of those pieces of information. So I
say hello, Mike, you are 35. Hello, Steve, you are 70. So essentially, we're writing out this one line
of code, which just prints out like hello to someone. And we're allowing this function to receive
two parameters. So the name and the age, and depending on the name and the age, the function's
going to print out hello a little bit differently. And that's kind of the beauty of using function.
So you could pass anything you want into a function. So for example, I could pass in a integer instead
of a string for the age. So like I could pass in the number, the only difference is over here,
we're going to have to convert this into a string. But it's going to work just the same. So you
could pass in strings, numbers, bullions, arrays, you can really pass any type of data into a
function. And it's going to work. So you can see here, we get the same result. So that's the basics
of functions. And as you go through with Python, you're going to be using functions more and more.
And generally, it's a good idea to break your code up into different functions. So whenever you
have like a grouping of code that's designed to perform a specific task, that's usually a good
candidate to be put inside of a function. In this tutorial, I want to talk to you guys about
using the return statement in Python functions. Now Python function is basically just a collection
of Python code that performs a specific task. And when we want to perform that task in our Python
programs, we can call a Python function. And a lot of times when we're calling a Python function,
we just kind of call it and it does its thing. And then we move on with the program. But sometimes
when we call a function, we're actually going to want to get information back from that function.
So when I call the function, I want it to basically execute its task, I want it to execute all the
code. And then I want it to like give me some information back. So that could be like a particular
value, it could be something like telling me how the task went. Basically, the function can just
communicate back to us like, Hey, here's some information. And that's what the return keyword
can allow us to do the return keyword can basically allow Python to return information
from a function. So I'm going to show you guys how we can do this, I want to create a very simple
function. And it's just going to cube a number. So when we cube a number, we take it to the power
of three. So if I was going to cube like two, it'd be like two raised to the power of three.
Right, it's just pretty basic math. So we're going to make a function that will cube a number.
And I'll show you guys how we can use that return statement. So first thing we want to do is make
a function, I'm just going to say death cube, we'll call the function cube. And over here,
we can specify any parameters that this function will take. So basically, any values that we want
to give this function. So we're going to give it one function, which is just going to be a number.
So I'm just going to call it num. And then over here, I'm going to type out a colon.
And then down here, we can put any of the code that we want to be included in this function.
So basically, this function is just going to cube this number. So we could just type out num,
times num, times num. Right, that's going to give us num cubed. So that's all well and good. And
if I come down here, I can execute this function. So I can basically just call it, I'll say cube.
And why don't we pass it a three? So I'm going to go ahead and pass it to three. And I'm going
to save this. And now let's go ahead and run this code. So we're on the code. And you'll see down
here, nothing happened, right? So nothing got printed out in our program. In fact, when we call
this cube function, it didn't really do anything. So let me try to print this out. So maybe if we
print this function out, it'll give us back the information that we want. So type out print,
and we'll print out the value of the function, I'm just going to click play. You look down here,
it says none. So when I printed out the value of this function, it told me none. But really,
this function is supposed to be cubing the number. And here's how we can use the return statement
in Python. I can come over here and I can just say return. And now Python is going to return
whatever value we put over here to the right. So when I call this cube function, and I give it a
three, Python is going to go off, it's going to execute this function. When it sees this return
statement, it's going to say, okay, I want to give a value back to whatever call of this function.
And it's going to return this value. Now, when I print out cube three, it should print out the
answer because we get an answer back. So when I play this, you'll see down here, it says 27. So
we got the correct answer. So I could do this with anything, why don't we cube like four or
something? So now we're on this. And you can see we're constantly getting the correct answer back.
So that's the basics of using this return statement, it allows us to return a value back to the
caller. In other words, back to whatever's calling the function. So I just want to illustrate this
point a little bit further, I'm going to come up here, I'm actually going to create a variable.
So I'm going to create a variable called result. And I'm just going to set it equal to cube four.
This variable result is going to store the value that gets returned from the cube function. So it's
not going to store like cube four, it's going to store the value that gets returned from executing
that function. So now if I came down here and printed out result, it's actually just going to
print out 64 again, because that's the value that it's storing. So this return statement can be
really useful for like getting information back from a function, we can use something like a
parameter, like this four as a parameter, and we can give information to a function. And then we
can use the return statement to get information back from a function. And I do just want to point
out one more thing, I'm actually not able to put any code after this return statement. So if I
came down here and tried to put like a print statement, this actually isn't going to work. So this is
never going to get printed out. So if I just typed out like code right here, when we run this program,
it's not going to type out code. That's because when I use this return keyword, it breaks us out
of the function. So Python's going to go through, it's going to execute the code in the function,
whenever it sees this return keyword, it's just going to break back out and we're going to be
done. So we'll never be able to reach this line of code. So that's the basics of using return
statements. And you can return any data type you want, we could easily return like a string or a
Boolean or an array, it doesn't really matter, you can return whatever you want. And a lot of times
in a lot of situations, you're going to want to get a value back from a function.
In this tutorial, I want to talk to you guys about using if statements in Python. Now if statements
are a special structure in Python, where we can actually help our programs to make decisions. So
by using an if statement, I could execute certain code when certain conditions are true. And I can
execute other code when other conditions are true. So basically if statements allow our programs to
respond to the input that they're given. So depending on the data that we're using in the program,
our programs will be able to respond. So when certain data is certain values, we can do certain
things. And when other data is other values, we can do other things. So you know, your programs
are essentially becoming a lot smarter. And actually, if statements are things that we as human beings
deal with every day. So you know, as you go throughout your day, generally, you're dealing with if
statements all the time. So I want to kind of give you guys a brief introduction into if statements,
and we'll talk about some of the common if statements that you'd see in everyday life.
And then we'll go ahead and do a really awesome example to kind of like show you guys what if
statements can do for us. So over here in my text editor, I just have this little text file.
And in here, I wrote out a bunch of if statements that a lot of people will encounter on a daily
basis. So here's the first one, it says I wake up, if I'm hungry, I eat breakfast. So let's break
this down a little bit up here, it says if I'm hungry. And this is a condition, right? This is
either going to be true, or it's going to be false, right? If it's true, I'm going to go ahead and
eat breakfast, right? If it's false, I'm just going to skip whatever was in here and move on,
right? So that's the basics of an if statement, right? We have a condition, in this case, if I'm
hungry, if the condition is true, then we do the action. So we eat breakfast. Otherwise,
we just skip it. So down here, we have another one, it says I leave my house. If it's cloudy,
I bring an umbrella. Otherwise, I bring sunglasses. And so this one is a little bit more complex than
the one we just looked at. You'll see we have our condition, right? It says if it's cloudy,
if that condition's true, we're going to bring the umbrella. But in this case, if the condition's
false, there's something else that we want to do. So in the case up here, if this condition was
false, we just moved on. But now if this condition's false, then we're going to go ahead and bring
sunglasses. So that's another example of an if statement, it's a little bit more complex.
Finally, I have one more down here, and this one's even more complex, it says I'm at a restaurant.
If I want meat, I order a steak. So if this condition's true, we order the steak. But if this
condition is false, then we're going to move on, and we're actually going to check another condition.
So I'm going to say otherwise, if I want pasta, I order spaghetti and meatballs. This is another
condition. If this condition's true, we get the spaghetti. If it's not true, then finally,
we're going to move on down here, and we'll just default to ordering the salad. So all three of
these if statements are valid if statements, it's just they get more and more complex as we go down
the list. So hopefully that's, you know, it can kind of wrap your head around what if statements
are we're basically specifying conditions. If those conditions are true, we're going to do certain
things. And if those conditions are false, we can do other things, or we can check other conditions.
All right, so let's head over to our Python file, and we'll start writing some actual Python.
So in here in my app dot Python file, I want to create an if statement. And I'm going to show
you guys a very basic if statement in this tutorial. And then in the next tutorial, we're going to
look at some more advanced if statements. And so what I want to do is create a Boolean variable.
So I'm going to create a Boolean variable that's going to store whether or not the user is a mail.
So it's just going to be called is underscore mail. And I'm going to set this equal to true,
because I'm a male. So we have this Boolean variable, right? And right now we have it set
equal to true. I can actually use an if statement to check to see what the value of this variable
is. And if it's true, I can do something. So if I want to use an if statement, all I have to do
is just type out if, and then I just need to type out a condition. So basically, I would have to type
out like something that's going to be true or false. Like, for example, in the other page, we
were specifying like, if I'm hungry, or if it's cloudy, here, we're just going to check to see if
is mail. So in other words, if the person is male. And so remember, this has to be a, like, it has to
be able to be reducible to a true or a false value. So in our case, we're just using a Boolean,
because that's like the easiest thing we can do. So I said if I specified my true or false condition,
and now I want to just type a colon. And what I want to do now is make a new line. And so
anything that I put below this with an indentation is actually going to be executed when that
conditions true. So I could put print. And down here, I could just put you are a male.
And now you'll see what happens when I run this program, it's going to go down here,
and it's going to say you are a male. But if I changed is mail to false. So instead of this
being true, I just change it to false. Now all of a sudden it's not going to print anything. So
it's just going to be blank down here. That's because this condition wasn't true. So anything
that I put below this if declaration and that has an indentation like that is going to be executed
when the if statement is true. We can also use another keyword in Python, which is called else.
And else is basically going to be like otherwise. So you remember before we said, if it's cloudy,
I'll bring an umbrella. Otherwise, I'll bring sunglasses. Well, this is going to be like our
otherwise. So I can say down here else. And now anything that I put below else is going to get
printed out when that condition isn't true. So why don't we just put you are not a male.
So now we're able to cover both of the situations for this variable. If it's false,
we can cover that situation. So it says you are not a male. If it's true,
we can cover that situation as well. So it'll say you are a male. So that's pretty cool.
But we can also make these more complex. And actually before I talk about that,
you can put as much code as you want inside of an if statement. So I could have like,
you know, dozens of lines of code if I wanted. I'm just giving you guys a very simple example.
So why don't we make this more complex? And I'm going to add another variable into the mix. So
I'm going to add another, you know, Boolean attribute into this mix. I'm going to make another variable
called is tall. And we're going to set this equal to true as well. So now I have two variables in
the mix. Let's say I wanted to write an if statement that will check both of those variables. So I
could write an if statement that's like, if they're male and they're tall, or if they're male and
they're not tall, or if they're not male and they're tall, like I could write out a bunch of if
statements to check the combination of these variables. And so the way I can do that is by using
one of two keywords. So the first keyword I want to show you guys is the or keyword. So I can come
over here and I can say if is male, or is underscore tall. And what this is basically going to do is
it's going to say if the person is either male, or if they're tall, then we want to do something. So
this is only going to be true when one of these values is true, when one or both of those values
is true. So down here, we could actually put instead of you are a male, we could put you are a male
or tall or both, that's going to be more appropriate. And then down here, we would say you are
neither male nor tall. And that's because if we execute the code inside of this else,
that means that both of these guys are false. Right, so I'm saying if they're male or they're tall,
we're going to execute whatever is in here. Otherwise, that means they're neither male nor tall. We're
just going to execute this down here. So let me show you guys how this works. I'll click play. And
these are both true. So it's going to say you are a male or tall or both. If I was to set one of
these to false. So let's set is male equal to false. Now, it's still going to say you are a male
or tall or both. But if I set them both equal to false, now it's going to say you're neither
male nor tall. So that's kind of the basics of using the or operator. And there's another operator
we can use which is called and, and basically it's similar to or except both of these conditions
have to be true. So this is basically saying if the person is male and they're also tall,
then we're going to execute whatever is in here. So we could actually change this text to say you
are a tall male, because we know for a fact that if this is getting executed, they're both male
and tall. We can also change this print statement down here. This could say you are either not male
or not tall or both. So you can see it's basically just saying that you're either not one of them
or you're not both of them. So we click the play button and you'll see because they're both false,
it's going to say you are either not male or not tall or both. But if I make them both true,
now it's going to say you are a tall male. If I was going to make one of these false,
though, so let's make is tall false. Now it's not going to execute this first print statement.
It's going to execute the one inside of the else. So to say you are either not male or not tall
or both. So that's the basics of and an or and you'll be using those a lot as you use your if
statements. But there's actually one more thing we can do. We can add in some more conditions.
So up here I'm checking if they're male and they're tall. But what if I wanted to check if they're
and they're not tall? So if they're like a short male, I can use another keyword in Python called
else if or really it's just typed out E L I F and it stands for else if and after this we can put
another condition. So over here I want to check to see if they're male and they're not tall.
So I'm going to say else if male is male and now I can type in not tall and the way I can do that
is by saying not an open and close parentheses and saying is tall. And basically when I use this
not function it's going to negate whatever is inside of here. So if this was true it's going to make
it false and if this was false it's going to make it true. So basically that'll tell us whether or
not they're tall. And then I'm going to use a colon and now down here inside of here we're basically
just going to type out any of the code that we want to happen when this condition's true. So down
here we can just print out you are a short male and there's actually one more condition that we want
to check for which would be if they're not male and they're tall. So we can make one more else if
and I'm actually just going to copy this guy and we'll paste it down here. And so I want to say if
not is male and is tall. So now we're checking to see if they're male and or if they're not male
and they're tall. So here we could say you are a you are not a male but are tall. So essentially
here we have an if statement that's going to cover every single condition for these two variables. So
in every single situation of the two values of these variables we're going to be able to print
something out for it. So if I made both of these true it's basically going to tell us you are a
tall male. If I make is male false it's going to tell us you are not a male but are tall. If I made is
male true and is tall false then it's going to tell us you are a short male. And if I made both of
these false it's going to tell us you are either not male or not tall or both. And actually we need to
change this. So this should say you are not a male and not tall. So that would be a better print out
for that situation. So you can basically see how we can use if else ifs and else in order to help our
program to make decisions and really to respond to the different data in our programs. So I'm able
to respond to every combination of the values for these two variables. And that's really cool. So in
this example we were just using simple Boolean variables but in the next tutorial I'm going to show you
guys how you can actually use something called comparison operators. So we could compare like two
numbers or two strings and we could use those as our conditions. But for now I think this is a good
introduction into if statements. So I'll see you guys in the next tutorial. In this tutorial I want
to talk to you some more about if statements in Python. More specifically we're going to be looking
at using comparisons inside of our if statements. So if you're following along with the course in
the last tutorial I talked to you guys about just the basics of if statements and we used a couple
different Boolean variables. And depending on the values of those bullions we were able to do certain
things. In this tutorial we're going to look at another way to use if statements which is with
comparisons. So instead of just using Boolean values we can actually compare different values. So I
could compare a couple numbers or I could compare a couple strings. And depending on the results of
those comparisons we can do certain things. So this is going to be pretty cool. And this is really
going to give you guys a full understanding of what you can do with if statements. So for this
tutorial I actually want to create a Python function. And this function is going to give us the
maximum number that we pass into it. So this function is going to take three parameters as input and
it's going to print out the biggest number that we give it. So let's create this function. I'm
just going to say death. And I'm just going to call it max num. And inside of these parentheses I'm
going to specify that I want three parameters as input. So I'm going to say num one, num two and
num three. So basically we're passing it three numbers and this function will return the largest
of the three. So in order to figure out which of these numbers is the largest we're going to have to
use an if statement. So I can say if and over here I need to specify a condition right so I need to
put like a true or a false value. And so what we can actually do is we can compare these different
numbers. So I could say if num one is greater than or equal to num two. And num one is greater
than or equal to num three. So basically what I'm doing here is I'm comparing these different
numbers. So I'm saying if the first number is greater than or equal to the second number. And
this is a comparison. So when you think about it, when we compare these two numbers, we're going to
end up with a true or a false value. Right, num one is either greater than or equal to num two or
it's not. It's actually a Boolean value right it's either true or it's false. Same thing over here.
The result of this comparison is either true or it's false. So technically I'm putting a true or
false value in there. I'm just getting that true or false value by using a comparison. And you'll
notice over here we're using something called a comparison operator. And basically this is just
like how we want to compare them. So we're saying like greater than or equal to in this case.
So down here, if this whole condition is true, in other words, if the results of these two
comparisons end up being true, then we know that num one is the biggest. So I can just return
num one, I can say return num one. But we also want to check some other conditions. So I can
say L if, and now I want to do some other comparison. So I'm going to say else if num two is greater
than or equal to num one. And num two is greater than or equal to num three. So I'm doing
essentially what I did up here, except for num two. So again, I'm comparing these different
numbers. And this is going to end up being a true or a false value, depending on the result of
the comparison. So down here, again, we can just return num two, because if all of this is true,
that means we're going to end up returning num two. And then finally, we can just say else.
So if num one isn't the biggest and num two isn't the biggest, then we can pretty much
assume that num three is the biggest. So now we have our function. So down here, I'm actually
just going to call this function. So I'm just going to say, actually, I'll print out the
result. So I'll say max num, and we'll pass in like three, four, five. So we're basically
calling that new function that we just made. And I'm going to go ahead and run this program.
So you can see down here in the output, we print it out five, because that was the biggest.
If I was to change this middle one to the biggest, so we make the middle 140, now it'll be able
to take on that situation. And finally, I can make this like 300. And it'll be able to handle
that. So no matter which number num one num two or num three ends up being the biggest,
it's able to tell us without a problem. So these comparisons are a really, really, really
common way to make if statements. And a lot of times, you're going to want to be comparing
different values inside of Python. So over here, we're comparing numbers, but we could also
compare strings. So I could easily make these both like, you know, I could make this like
dog, and I can make this dog. And I could say a different operator. So I could say like
this double equal sign would mean equal. So I could basically say like, if this string
is equal to that string, so you can basically like compare all different data types, you
know, I could compare numbers, strings, you could also compare bullions. So once again,
these are comparison operators. And this one is greater than or equal to, but there's a
bunch of other comparison operators we can use. So the most basic is just going to be
a double equal sign. And that basically checks to see if the two values are equal. So it'll
say like, if number one is equal to number two, we can also say not equals. And this
and this exclamation point will basically mean not equal. So it's if num one is not equal
to num two, we also have greater than less than greater than or equal to less than or equal
to. And those are kind of all of the operators are all the, you know, the basic comparison
operators that you're going to be using in Python. So using comparisons is really awesome.
And there's going to be tons and tons of situations inside of Python where you're going
to want to compare different values. So these can be extremely useful. In this tutorial,
I'm going to talk to you guys about building a calculator in Python. Now, if you've been
following along with this course, you'll know that in the beginning of the course, we actually
created a very basic calculator. Basically, the user could just enter in two numbers and
our calculator would add the numbers together and spit out the answer. In this tutorial,
I want to show you guys how to build a more advanced calculator. So this calculator is
going to be able to perform all of the basic arithmetic operations. So we can add, subtract,
multiply and divide. And we'll actually allow the user to specify which one of those they
want to do. So the user will enter in a number, they'll enter in whatever operator they want
to use. So plus minus divide, multiply. And then they'll enter in the second number and
we'll perform the operation for them. So this can be pretty cool. And it's going to show
you how we can use if statements in like a practical application. So let's get started.
The first thing we want to do is get input from the user. So I'm going to create three
variables, one for the first number, one for the second number, and one for the operator.
And we'll basically store the values that the user inputs into those variables. So the first
one will just be num one. And I'm going to set this equal to input. And we're going to
give this a prompt. So I'll say enter first number. And here's actually the thing is
whatever the user enters, we're going to want to convert it into a number. So generally in
Python, when we get input from the user, no matter what they put in, it just gets converted
into a string. But we don't want a string, because we're going to have to be adding or
subtracting or multiplying these numbers together. So whenever they enter in the number,
I just want to immediately convert it to an actual like number data type instead of a
string. So what I can do is I can actually say float, and I can surround this entire
input tag with parentheses. And now what this is going to do is it's going to immediately
convert whatever the user inputs into a float. Now that's going to mean that they're going
to have to insert a number, otherwise we'll get an error. But for the most part, this should
work out for us. So I'm actually just going to copy this guy. And we'll go ahead and paste
it over here. And we'll paste it one more time. So we're basically just trying to get three
pieces of input from the user. So the second thing we want to get is the operator. So I'm
just going to say op for operator. And I'll actually get rid of this float, because we
want to keep this as a string. And I'll just say enter operator. So that's going to be
like plus minus multiplication, whatever. And then finally down here, we'll get num two.
And again, we're going to want to convert this to a float. So I'll say enter second number.
So now basically what we're doing is we're getting the first number, we're getting the
operator and we're getting the second number from the user. So what we need to do now is
we need to figure out what is inside of this op. In other words, we need to figure out
what operator the user was trying to do. So are they trying to do addition or they trying
to do subtraction like we want to figure that out. So we can use an if statement in order
to figure that out. So I'm going to come down here and I'm basically just going to create
an if statement. So I'm going to say if, and we want to check a condition. So I'm basically
going to check to see if this operator is equal to a plus sign. So I can say if op equals
plus sign. And now if the operator is equal to plus sign, I'm just going to print out
num one plus num two. But let's say that instead of a plus sign they entered in something
else. So we can say out if op is equal to, and now we'll check if it's a minus sign.
And so here we'll just print num one minus num two. And actually forgot to put a colon
here. So we can just do these for everything. So now we can say L if op is equal to division
and here we'll just divide the two numbers so I can print out num one divided by num two.
And finally, we'll do the same thing for multiplication. So I'll say l if op is equal
to asterisk. And we'll just print out num one times num two. Alright, so basically over
here we have every, you know, condition. But I also want to put in an else statement. So
in the case that the user enters in an operation, that's not like one of the four that we
specified up here, I just want to throw an error. So I'm just going to say op or actually
we just say else. And here we'll just print out like invalid operator. Alright, so this
if statement should give us everything we want. Basically, I'm just checking to see if it's
equal to a plus sign a minus sign a division sign or a multiplication sign. Depending on
which one it's equal to, we'll do a different operation. So let's go ahead and run this
program and see how we do. So I'm going to run this program and it says enter first number.
Let's enter five, enter an operator. So why don't we just do addition and enter a second
number. Let's do 23. So now it prints out the answer 28. So a calculator actually works.
Let's try another operator just to make sure. So we'll do six and why don't we multiply
it by, I don't know, 5.23. So now we go off and we do the multiplication and looks like
we got our answer. So our calculator seems to be working. And so basically all we did was
we got the input from the user and then we used if statements to figure out what they
wanted to do. So we can check to see if they wanted addition. We can check to see if they
wanted subtraction or division or multiplication. And we can also check to see if they entered
in an invalid operator. So actually why don't we try that too. So I'll enter in like five
and now we'll just enter in like an open and close parentheses and a four. So you can
see here it's able to tell like the program's smart enough to know that that's not a
valid operator. So these if statements can be really powerful and we can use them in
combination with getting the user input to create a little calculator. In this tutorial
I want to talk to you guys about using dictionaries in Python. Now a dictionary is a special
structure in Python which allows us to store information in what are called key value
pairs. So essentially I can just create a bunch of these different key value pairs and
then when I want to access a specific piece of information inside of the dictionary I
can just refer to it by its key. So the word dictionary is actually pretty appropriate
here. If you guys are familiar with a normal dictionary you know in a normal dictionary
you'd have a word and then you'd have a definition associated to that word. So in this
situation the word would be the key. In other words the word is what like uniquely
identifies it inside of the dictionary and then the value would be the actual definition.
So this is going to make more sense once we start working with this but I just wanted
to give you guys a quick introduction. So in this tutorial I want to create a little
program that will allow us to convert a three digit month name into the full month name.
So I could convert for example like JAN to January or I could convert M A R into
March. Like I want to be able to have a program that can do that for us and that's actually
a pretty good use case for a dictionary. So we're actually going to create a
dictionary and I'm going to show you guys how we can do that. So the first thing we
need to do in creating a dictionary is just give it a name. So I want to give this a
specific name. So I'm just going to call this month conversions. And now I just want to
set this equal to an open and closed curly bracket. So whenever we create a dictionary
in Python we're always going to want to create it inside of these open and closed curly brackets.
So inside of the dictionary now we can start defining what are called key value
pairs. So I can define a key and then I can give it a corresponding value. And the
way that we can do that is just by typing out the key and then typing out the value.
So in our case we're converting three digit month names into the full month
names. So I would want to type out for example JAN and then I can type a colon
and I want to type out the value I want to associate with it so we could say January.
I can basically do this for every combination. So I could say like FEB and this would be
February. And actually after each one of these we're going to need to put a comma.
So I could do the same for March, say M.A.R.
So essentially what I can do is I can specify an entry inside of this dictionary for each
one of these. So this over here would be the key and this over here would be the
value. And here's one thing is all of these keys have to be unique. So if I was to
come down here and change this to JAN, this is actually going to give us a little
warning. So you can see down here it says dictionary contains duplicate keys.
That's a no no when we're creating dictionaries. So you always want to make sure
that the keys are unique. All right. So I'm going to go ahead and create one of
these for each one of the months and then we'll come back and we'll talk about it.
All right. So I've gone ahead and created an entry for all 12 of the months. So now
we have a bunch of unique keys and each one of those keys is associated with a
different value. So this is the basics of creating a dictionary is we have a bunch
of these different key value pairs. And now what we can actually do is we can
access them from inside of this dictionary. So if I wanted to access a
specific key or a specific value, all I have to do is come over here and I'm
actually just going to print it out onto the screen so we can see it. All I have
to do is refer to the dictionary by name. So it's just called month conversions.
And there's actually a bunch of different ways that I can access these month names.
So I could access different entries inside of this dictionary. The first way is just
by making a open and close square bracket and inside here, I can just type in one
of the keys. So for example, inside here, I could type in NOV. And what this should
do is it should give us back the full name for November. So when I print this
out, you'll see that we get back down here November. So I could do the same thing
for, you know, any of these, we could say like M A R. And now we should get
March back. So I'm able to refer to the key. And it's going to go into the
dictionary and it's going to give me the value that's associated to that key.
There's actually another way we could do this too. So I could say month conversions
dot get an inside of here. I could also pass in the name of a key. So I could pass
in like D E C. And now we'll be printing out December. And what's cool about using
this get function is I can actually specify a default value that I want to
use if this key is not found. So there's going to be certain cases when we're
dealing with dictionaries where you're going to put in a key that might not
necessarily map to a value inside of the dictionary. So you put in an invalid key.
So let's just try to do that. So I'll just put like L U V. That's not a month
abbreviation. So when I try to run this, you'll see we get this none down here.
And in a lot of cases, you're going to want to create some sort of a default
value. So if I'm in this situation where I have a key that's not mapable to any
values inside of this dictionary, I can actually pass it a default value. So
inside this dot get function, I can make a comma. And now I can pass in like a
default value to get printed out. So I can say like not a valid key. And now when I
put in the invalid key, instead of saying none, it's going to go ahead and say not
a valid key. So this is going to give us like a default value that we can fall
back on. So these dictionaries are really awesome. And the keys over here don't
have to be string. So you could also use numbers. So for example, I could say
like zero, one, 10, like I can put any numbers I want over here as keys as long
as they're unique. And then I could access those keys, just like I normally
would down here with this get function or with those open and close square brackets.
So that's sort of the basics of using dictionaries. It can be really useful to
store key value pairs. And honestly, you're going to be using them a lot in Python to
store different types of data. In this tutorial, I want to talk to you guys about
while loops in Python. Now a while loop is basically a structure in Python, which
allows us to loop through and execute a block of code multiple times. So I can
specify like a few different lines of code, and then I can put that code inside of a
while loop. And it would basically loop through that code, executing it repeatedly
until a certain condition was false. So while loops can be awesome. And there's
a lot of situations in Python where we're going to want to loop through specific
lines of code. So in this tutorial, I just want to show you guys the like bare
basics of how while loops work. And then in future tutorials, we're going to use
while loops to create little games and stuff like that. So let's talk about
while loops. When I create my while loop, the first thing I want to do actually is
create an integer. I'm basically creating a variable that's a number and you don't
have to do this for a while loop, but just for our demonstration I'm going to. So
I'm just going to call this I and I'm going to set it equal to one. And now what I
want to do is I want to create a while loop. So I'm basically just going to say
while. And over here I want to specify a condition. And this is what's called our
loop condition or you can also refer to it as a loop guard. And basically we are
going to keep looping through the code inside of the while loop as long as this
condition is true. So whatever condition I put in here, as long as it's true, we're
going to keep looping through the code inside the while loop. So I'm just going to say
while I is less than or equal to 10. And now I'm going to put a colon and I'm going
to make a new line. And so anything that's below this while loop declaration and
that's indented like this is going to be considered code that's inside the while
loop. And so that code is going to get repeatedly executed while this condition
up here is true. So I'm just going to put a very simple line of code here. We're
just going to print out the value of I. And then down here I'm going to actually
increment I so I'm going to add one to I'm going to say I is equal to I plus one.
So basically I'm taking this I value and I'm adding one to it. There's actually a
shorthand that we can use in Python to do something like this though I could just say
I plus equals one and that's automatically going to add one to I. So this is like
a little shorthand and you'll see people in Python using that a lot. So over here
we basically have our while loop. So I have the outline for a while loop. So we're
defining this variable up here and we're going to keep looping through the code
inside the while loop as long as this condition up here is true. So if this
condition is false for example if I is equal to eleven then we're not going to
loop through this code anymore and we're just going to move on. So down here I'm
actually just going to print. Done with loop. So this will kind of illustrate
this for us. Alright so let's go ahead and run this program. I'm just going to run
it. And you'll see down here we get some output. So we're actually going to get
numbers printed out one all the way down through ten and then we're printing out
done with loop. So basically what's happening is we're printing out every
time we go through this loop. So initially I is equal to one right. The first thing
that Python is going to do when it gets to this while loop is it's going to check
this condition. So before it does anything else it's going to check this
condition and it's going to say okay is one less than or equal to ten and that's
true. So as long as this guy is true we're going to go through and loop through
all the code inside of here. So we're going to print out i which has the
value of one and then we're going to add one to i. So now i is going to go from
one to two. Now what we're going to do is we're going to go all the way back up
here to this while loop declaration and we're going to check the condition again.
So remember I checked the condition the first time and then I loop through all
the code then I go back up to the top and I check it again. So I'm going to say
okay is i less than or equal to ten. At this point i is equal to two. So i is
less than or equal to ten. So now we're going to go through and we'll execute
all this code again. So I'm going to print out i so it's going to be two and I'm
going to add one to i so now we have three. Then Python is going to go all the way
back up to the top and it's going to check the condition again. So on every
iteration through the loop before Python does what's inside of this loop it's
going to check that condition. And as long as the condition remains true we're
going to keep executing the code inside the loop. Eventually though in this
program i is going to end up being eleven. So on the last iteration it's going to
end up being eleven. We're going to come back up here and we're going to say hmm
is eleven less than or equal to ten? Nope. And so since it's nope we're going
to come down here and we'll print out this done with loop line. So that's the
basics of how these while loops work. We're specifying a loop condition as long
as that condition is true we'll keep executing the code inside of the loop.
And after every execution of the loop we're going to come back up and check the
condition again. So that's how we end up with this printing out down here. So
that's the basics of a while loop. And we can use these while loops to do all
sorts of things. Like while loops are very very powerful. In this tutorial I'm
going to show you guys how to build a basic guessing game in Python. And in this
guessing game we're actually going to be using all sorts of cool programming
structures that we've learned up to this point in the course. So we're going to
be using things like if statements and while loops and variables and all these
cool things in order to build this game. So the basic idea is we will specify
like a secret word. So we'll have a secret word that we store inside of our program.
And then the user can interact with the program and try to guess the secret word.
And so what we want to be able to happen is we want the user to be able to keep
guessing what the secret word is and keep typing in different responses until they
get the secret word right. So that's the basics of what this game is going to be.
And now let's go ahead and create it. So the first thing we want to do is create a
variable to store our secret word. So I could say secret word and we're just
going to set this equal to a secret word. So why don't we make it draft. That's
a pretty good secret word. And now what we want to do is we want to create a
variable that will store the user's response. So I want a variable that will
store like all the guesses that the user makes. So I'm just going to call this
guess. And I'm just going to set it equal to an empty string right now. So now we
have the secret word and we have a variable to store the user's guess. And
what we need to do is we need to be able to prompt the user to input the secret
word. But here's the catch. What we want to happen is we want them to enter the
secret word. And if they don't guess it correctly, we want to prompt them to
enter it again. So we can't just use a single input statement. We actually have
to use something called a while loop. And we can use a while loop in order to
continually ask the person to guess the word until they guess it correctly. So
let's go ahead and create our while loop. So I'm going to say while. And after I
say while, I need to specify a looping condition or a looping guard. Basically,
this is something that as long as it's true, we're going to keep looping through
this loop. So basically, I want to say I want to keep looping as long as the
user's guess is not equal to the secret word. So as long as they haven't guessed
the secret word, I'm going to keep going through this loop. And inside of this
loop, what we're going to do is we're going to ask them to input the secret word. So
I can take this guess variable and I can set it equal to input. And I'm just going
to say enter guess. And so over here, we're basically telling the user to enter in
their guess. I'm storing whatever they enter inside of this guess variable. And
then what's going to happen is we're going to come back up here. We're going to check
to see if the guess is equal to the secret word. If the guess isn't equal to the
secret word, then we're going to do it again. But if the guess is equal to the
secret word, then we're just going to break out of this loop. And so we can come down
here. And we can just print out a success message. So I could say you win because
they got the secret word. So this is a very, very simple program. But this is
essentially all of the code that we need to be able to build a game like this. So
let's go ahead and play our game. We can run it and see how we did. So I'm going to
click play. And down here, you can see it's prompting us to enter a guess. So I'm
just going to type in some, you know, random text, we can type in whatever we
want. And as long as we're not entering that secret word, it's going to keep
prompting us to enter different information. But if I enter in the secret word, so if
I enter in draft, now all of a sudden the program is going to terminate and it's
going to say, Hey, you win, because we were able to guess the secret word. So that's
like a really cool way for us to be able to do this. And we actually have a fully
functional guessing game. But I think this guessing game could actually be
improved quite a bit. A lot of times when we're making a guessing game, we want to
set a limit. So in other words, I want to set a limit for the number of times that
the user can try to guess the word. So let's say that the user has three tries,
right, they have three guesses in order to guess the word. And if they can't guess
the word after three tries, then they're going to lose the game. But if they can
guess the word inside of three tries, then they'll win the game, right? I think
that would be a little bit more of a fun game. So why don't we try to program
that game? Basically, we're going to set a limit on the number of guesses that the
user can have. In order to do this, we're going to have to create a couple more
variables down here. In other words, we're going to have to store a couple more
pieces of information. The first piece of information I want to keep track of is
how many times the user has guessed, right? So we can just make a variable called
guess count. And we'll just set this equal to zero because initially, the user won't
have guessed down in this while loop. Every time we go through the loop, I want to
increment that guess count, right? So every time we've gone through this while loop,
I want to increment that count because that means the user will have guessed.
So down here, I'm going to say guess count plus equals one. And this is just going
to add one to the guess count, actually whoops. Alright, so after each
iteration of this loop, we're going to go ahead and add one to the guess count.
Alright, so that's the first variable that we're going to need. We're also going to
want to store another variable. And this is going to be called guess limit. And
basically, this is going to tell us how many times the user can guess the word.
So I'm going to it's going to be guess limit. And we'll basically just say three.
So let's say that the user has three tries to guess the word three strikes and
you're out. So the user, if they can't get it in three tries, then we're going to
basically say that they lose the game. And I'm also going to need one more
variable here, which we're going to call out of guesses. And I'm just going to set
this equal to false initially. So this out of guesses variable is going to be a
Boolean. And it's going to tell us whether or not the user is out of guesses. So if
out of guesses is true, that means they have no more guesses, right? They basically
lost the game. And if out of guesses is false, that means that they still have
some guesses left so they can keep playing. All right, so let's use these
different variables in order to make our program more functional. So the first
thing I want to do is when I go through this loop, I want to check to make sure
that the user has more guesses, right? In other words, before I let the user
enter a guess, I want to check to see that they haven't already used up all
their guesses. So down here, I can make an if statement. I can say if and inside
of the condition, I want to check to see that guess count is less than guess limit.
If guess count is less than guess limit, that means that they haven't guessed the
total number of guesses that they have. So they have some guesses left. And if that's
the case and if that's true, then I'm going to go ahead and give them a guess and
then we'll increment the guess count. If this isn't true, in other words, if they
have reached their guess limit, then I'm going to want to set out of guesses
equal to true, because they're out of guesses, right? They have no more guesses
because the guess count wasn't less than the guess limit. And so that means that
they're out of guesses. So they have no more guesses. So there's actually one more
thing we need to do inside of this while loop, we need to add another condition
on to this loop guard. So right now, we're going to keep looping as long as the
guess is not equal to the secret word. But remember, if the user's out of guesses,
in other words, if they've reached their guess limit, we don't want them to guess
anymore, right? So if they ran out of guesses, then we want to basically break out of
this loop and not give them any more guesses. So I'm going to go ahead and add
another condition onto here. And I'm going to say, while they haven't guessed the
secret word, and they're not out of guesses. So it's going to be not out of
guesses. Then we're going to keep looping. So as long as they haven't guess the word,
and as long as they still have some guesses left, we're going to keep looping. But
otherwise, we're going to break out of the loop. And so down here, we're printing out
you win. But actually, when we break out of this loop, there's going to be two
possible scenarios. So again, there's two possible ways that this loop could end,
right? Either the guesses equal to the secret word. So either the user guessed
the word correctly, or the user ran out of guesses. And so there's two situations
down here that we need to account for. And so I'm going to use an if statement to
figure out which is which. So I'm going to say if, and we're just going to type out
of guesses. And if the user is out of guesses, then I want to print out of
you lose. So we're basically going to be like, you lost the game. Otherwise, though,
if they're not out of guesses, that means that they guess the word correctly. So
we're just going to print out you win. Alright, so now we have all the logic for
this little guessing game setup. And let's see if we can run it and play through
it. So I'm just going to run this program. And now it's going to tell us to
enter a guess. So why don't we enter more guesses than we have? So we're going to
try to lose the game. So I'm just going to enter one guess, two guesses. And now I'm
on my final guess. So if I don't get it here, we should actually lose the game. And
you can see it says out of guesses, you lose. So we weren't able to guess it in the
number of tries that we had. Let's run the program again, and we'll try to win the
game. So we'll get a couple wrong. Let's say we're on our last guess. And I'm like,
Okay, I can do this. So I type in the word and bam, we won the game. So that's how we
can basically create a game where we have a guess limit. So this is a lot of code.
Let me walk you guys through this one more time so you can just get a full
understanding of what we're doing. Up here, I created a few different variables. We
created this secret word variable, and we created this guess variable. And then we
also created some more variables. So we created this guess count variable. And this
just keeps track of how many times the user has guessed the word. And you can see
down here, every time we give them a guess, we're incrementing the guess count. We also
have guess limit. And guess limit is telling the program how many times the user can
guess. So before I go through this while loop, the first thing I'm doing is I'm
checking to see that the guess count is less than the guess limit. In other words,
do they still have some guesses left? If they do, then we're going to get the
input from the user. Otherwise, we have this other variable up here called out of
guesses. And out of guesses is going to tell us whether or not they have some guesses
left. So if this is equal to false, that means they have some guesses. If it's
equal to true, however, it means no more dice, no more guesses, they are done. So
they lost the game. The last thing we needed to do was specify an additional
condition up here. So we are going to keep looping through the code inside of this
loop, as long as the conditions up here are true. So as long as they haven't
guessed the word, and as long as they're not out of guesses, we are going to keep
looping through. And so when eventually the user does break out of this loop,
there's going to be two possible situations. The first situation is that they ran
out of guesses. And so we want to check to see if that's the situation. I want to
check to see if they're out of guesses. If they are, we'll print out a lose
message. If they're not, then they must have guessed it correctly. So they win. And so
that is how we can use while loops and if statements and also variables in
combination with each other to build a pretty awesome guessing game.
In this tutorial, I want to talk to you guys about using for loops in Python. Now
a for loop is a special type of loop in Python, which allows us to loop over
different collections of items. So a lot of times we'll use for loops in Python to
like loop through different arrays, or we can loop over like the letters inside
of a string, or we could just loop through like a series of numbers. So for loops
provide a very specific purpose. And the easiest way to kind of wrap your head
around why for loops are useful is just for me to show you guys a bunch of
different examples. So that's exactly what I'm going to do. We're going to look at
why for loops are awesome. So down here in my text file and my Python file, I'm
going to write out a couple of different for loops. So the way that we create a
for loop is just by saying for. And now what I want to do is specify a variable.
And this variable is going to essentially represent a different value every time we
go through this for loop. And you guys will see how that works in a second. But
just know that this variable is going to be used on every iteration of our for
loop. And each time it will most likely have a different value. So in our case,
I'm just going to call this letter. And I'm going to say for letter in. And now
what I want to do is I want to specify a collection that I want to loop over.
One example of this would be like a string. So I could put a string in here.
I could just put like draft Academy. And now I can just put a colon. And so
basically what this is going to say is it's going to say for every letter inside
of draft Academy, I want to do something. And so down here inside of this for loop
and again, we need to indent this. We can put what we want to do with each letter.
So let me just show you guys like basically what this is going to do. So I can print
out a letter so I can print out this letter variable. And it's actually going to
print out a different letter inside of this draft Academy string on every
single iteration of this loop. So I'm going to run this program. And down here
inside of my console, you'll see that I'm basically printing out draft Academy.
So on the first iteration of the loop, I printed out the first letter in draft
Academy, which was G. On the second iteration of the loop, I printed out I
said, third was R, fourth was A. So I'm essentially just looping through all of
the letters inside of draft Academy. So I'm saying for each letter in draft
Academy, I want to print out that letter. And so this is kind of like how four
loops can be used. We can define a variable. And that variable will change on each
iteration of the loop, right? So on the first iteration of the loop, this letter
variable represented a G. On the second iteration of the loop, the letter
variable stored at the value I, etc. Right. So we went through this entire string,
and we were able to print out each letter. So in addition to using this with
strings, we can also use this with other collections, for example, like an
array. So if I created an array up here, let's just call it friends, I'm going to
set this equal to a bunch of different values. So we can put like a list of our
friends in here, we can say like Jim, Karen, and Kevin, instead of saying the
letter, why don't we call this friend? And I can say for friend in friends. And now
we'll print out the friend. So over here, I'm saying for each friend inside of
this friends array, I want to print out the friend. So now we'll actually be able
to print out each element inside of that array. So down here, you'll see we're
printing out Jim, and then on the second iteration, we're printing out Karen,
and on the third, we're printing out Kevin. So I'm able to loop through all of the
values inside of the array. And just so you guys know, like, you can name this
variable, whatever you want. So I could name, I could give this like any random
name. And if I want to access it, though, I have to access it using that same name.
So we can loop through something like an array, we could also just loop through
a series of numbers. So I could say index over here. And again, this can be anything.
I'm just going to call it index. So we could say for index in range, and then in
here, I can pass in a number. So I could pass in like 10, for example. And down here,
I'm just going to print out the index. When I run this program, you guys will see that
it's basically going to print out every number in the range from zero to 10,
not including 10. So starting with zero, it's going to print out zero, one, two,
three, four, five, six, seven, eight, nine. But notice that it didn't print out 10.
So it's essentially just printing out all the numbers between zero and 10,
not including 10. You can also specify a range of numbers. So I could say, for
example, like three and 10. And now this will print out all the numbers between
three and 10, not including 10. So we're on this program. And you can see we print
out three, four, five, six, seven, eight, nine, and not 10. So whatever value that
you put here in this second position is not going to get included in the range.
And ranges can be really useful. So for example, I could use a range to loop
through an array, just like we did before. So if I wanted, I could say something
like four index in range. And now inside of this range, I can pass in the length
of the array. So just so you guys know, if I wanted to get the length of this
array, in other words, if I wanted to figure out how many elements were inside
of it, I could just type out LEN and then inside parentheses, the name of the
array. And so this is going to spit out three, because there's three elements
inside of here, right? Kind of makes sense. So what I can do is I can say down here
inside of this range function, I can just type in LEN and then friends. And so what
this is going to do is it's going to essentially give me a range between zero
and the number of friends inside of this list. So down here, I could actually type
out friends index. And this will allow me to access each individual friend inside
of this list, just like we did before. But now I'm doing it with a range. So you
can see I'm typing out Jim, Karen and Kevin. So actually, for each iteration
through this loop, it's basically going to be printing out friends zero, friends one,
and then friends two, because in here, we're passing in a three. And remember,
whenever I pass in there, it's going to range from zero all the way up to that
number, but not including that number. So that's another way that we could print
out all the elements in the array. And looping through something like an array
is actually a very, very common use case for for loops. But like I said, we can
use for loops to loop through essentially any collection that we have. So we could
loop through like a string, we could also loop through, you know, something like an
array. So this can be really useful. And that's one of the basics of for loops in
Python. And I also want to show you guys one more example, just while we're here.
So why don't we go ahead and print out a range up to five, right? So this will just
be a simple program. You can use all sorts of logic inside of these for loops. So
let's say that I wanted to do something special on the first iteration of the
loop. So I could say, if index is equal to zero. And if the index is zero, then we
know it's the first iteration of the loop. So I could do something special, I could
like print out first iteration. And then otherwise, we could just print like not
first. So this would be an example of like, maybe you want to do something on the
first iteration of the loop and do something else on subsequent iteration. So if I play
this, you'll see only on the first iteration is it printing this out. And
otherwise, it's printing out other stuff. So, you know, don't be afraid to put some
complex logic inside of these for loops, because it can really make your program
is more powerful. In this tutorial, I'm going to show you guys how to build an
exponent function. Now an exponent function is basically going to allow us to take a
certain number and raise it to a specific power. So in Python, there's actually a
really easy way to do this. I can come down here and I can print, we can say like
two, and we can just do two multiplication signs. And I can say three. And this is
basically just going to be two raised to the third power. So you can see here, I can
just print this out and we get eight, because that's too cute. So it's really
easy to do exponents in Python. But I want to show you guys how we can use
something like a for loop in order to create a function like this of our own.
So we'll actually create an exponent function that will basically do this and
we'll use for loops to do it. So let's create a function. So I'm just going to say
def. And now we need to give this function a name. So why don't we call it
raise to power. And inside of here, I'm going to accept two parameters. The first
parameter is going to be the base number. And then the second parameter is going to
be the power number. So we're going to take the base number and raise it to the
power number. So over here, we can just make a colon. Now, inside of this function,
we need to start writing some code. Here's the thing. Inside the function, we don't
necessarily know the value of this pound. Right. For example, if we knew that we
were just trying to like square this number or cube this number, I could just
return like base num times base num, right? That would square the number or I could
cube the number base num. So right. So this would be like the number cubed. The
problem is though, we don't know like right off the bat what this value is going to
be. Right. This could change like the user can decide that. So in order to write this
function, we're actually going to need to use a for loop. And I'm going to show you
guys how we can use a for loop to basically figure this out. So the first thing I
want to do is create a variable. And I'm just going to call it result. And we're
just going to set result equal to one. So we're going to start this off at one. Now
I want to create a for loop. So I'm basically just going to say four. And over here,
I basically want to specify like a index. So I'm going to say index in, and now I
want to specify a range and a range will basically range us through a collection
of numbers. So basically, I want to multiply the base num by itself as many
times as the power num specifies. Right. So over here, I can just make this a range
of power num. Right. And so we'll basically loop through this for loop as many times
as pound them. So if pound them is two, we'll loop through it twice. If pound them
is four, we'll loop through it four times. And every time through the loop will
multiply base num by itself. Or actually, you'll see we're going to multiply it by
this result. So down here inside of this for loop, I can basically say result is
equal to result times base num. This should actually give us everything we need to
take this number to the specific power. So the actual like result, you know, of
doing the math is going to get stored inside of this result variable. So the
first time through the loop, we're just multiplying base num by one. Right. And
so now result is going to become the same value as base num. The second time
through the loop. So this would be if we were squaring the number, we're basically
multiplying result by base num again. So it's essentially just base num times
base num. The third time through the loop, we're multiplying result times base num.
So we're essentially just like cubing the number. So that's essentially what this
for loop is doing for us. So the last thing I want to do is right here below
this for loop, I'm just going to return the result. So we'll be able to return
whatever the result of raising the number to the power was. All right. So let's
come down after this function. And we're just going to call it. So I'm just going
to say, actually, we'll print out the answer. So I'm going to say raise to power.
And inside of these parentheses, remember, we can pass two numbers. So let's
raise three to the second power. So we're just going to square three. And we'll see
how this works. So throw on the program. And you can see down here in the console
we're getting nine. Let's try something a little bit crazier. Let's try three to
the fourth power. And we'll run this and get 81. So that's three to the fourth
power. Let's try two to the third power. And we're getting eight. Yeah, so that makes
sense. So our raise the power function is working just as expected. And I'll just
walk you guys through it one more time. So inside of this function, we're taking
in two pieces of input, we're taking in a base number, and we're taking in a
power number. So that's like, the base number is going to be taken to the power
number. I'm defining a variable here called result. And result is where we're going
to store the actual result of doing the math. Now we specify this for loop. And
I'm basically saying that I want to loop through this range of numbers. When I put
pound on here, it's basically going to range from zero all the way up to but not
including the power number. So we'll basically loop through power number of
times, if that makes sense. And then every time through the loop, we're just
multiplying result by base num. And then we're just returning results. So that's
the basics of building a power function. And obviously, like if you're just trying
to take a number to a power and Python, it's really easy. But this kind of shows you
like the internal workings, right? For a function like that to work, we would have
to actually type something out just like this. And this tutorial, I want to talk
to you guys about two different concepts in Python. It's going to be a pretty cool
lesson. First thing I want to show you guys though, are two dimensional lists. So
down here in our Python file, I just want to create a basic list. So I'm just going
to call this number grid. And I'm just going to set this equal to a normal list
right just like this. Now normally if we were creating a list, I could just put in a
bunch of attributes and bunch of elements, right? And now all of these elements are
inside of this number grid list. Everybody's happy. But another thing I can do is I
can make all of the elements inside of this number grid list lists. So the first item
in the number grid list is going to be a list. The second item in the number grid
list is going to be a list, etc. So I'm going to show you guys how to do that. I
can basically just come down here and where I would normally put the first element
in the list, I can just create another array. So I'm just going to make an open
and closed square bracket. And now I'm just going to say 123. And I can create the
next item in my list. I can say 456. And let's make another item in this list. I
can say 789. And finally, we're going to put one more list inside of the number
grid list. And it's just going to have zero in it. So inside of this number
grid list, right, this high level overall list, we have four elements. And all of
those elements are themselves lists. So essentially what we're doing is we're
creating a grid, right? This grid has 1234 rows, and 123 columns, right, four
rows, three columns. And that's kind of like why these can be useful is we can
create a grid like structure inside of Python using two dimensional lists. So
this is a pretty cool list structure. And I want to show you guys how we can
access individual elements inside of this list structure. So, for example,
let's say I wanted to print out one of these values. Let's say I wanted to print
out this one right here. The way I can access it is by saying number grid. And
the first thing I want to do is make an open and close square brackets. And in
here, I want to put the index of the row that I want to access. So this would be
like row zero, because that's the zero element in the array. This would be row one.
It's at index position one. This would be row two, row three, etc. So I could put
row zero. And now right next to this, I'm going to make another square bracket.
And I want to put the index of the column. So this would be like column one, column
two, column three. So I'm going to put another zero, because this is at the
zero index of this first array. So I just put zero zero here. Now we can
print this guy out to the screen. And you'll see down here that we get that one. So I
could also do this for like for this eight, for example. So this is going to be zero
one two. It's going to be row two and column one. And now we should be able to
print out that eight. So that's basically how we can access elements inside of this
2D list. The next thing I want to show you guys is a nested for loop. And a nested
for loop is a situation, like I said, where we have a for loop inside of a for loop.
And I'll show you how we can use this nested for loop in order to print out all
the elements inside of this array. So I basically show you guys how to parse
through a two dimensional list or a two dimensional array. Alright, so basically
we're just going to create a normal for loop. So I'm going to say four, and I'm just
going to say row in number grid. And I'm using the word row here because I'm kind
of seeing these guys as rows, right? These are sort of horizontal rows in our little
grid. So for each of those rows, for each of the elements inside of number grid, I
want to loop through. So I could actually just print out each row right now. And it's
just going to print out all the rows inside of this list. You can see down here,
we basically just get exactly what we have up there. But I want to be able to
access each of these individual attributes inside of each of these array elements.
So we can actually create another for loop. And I'm going to put that for loop inside
of here. So now I'm going to say for column in row. And basically what this is going
to give us is each individual column or each individual element inside of these
arrays. So for example, inside of the arrays that are the elements of the number grid.
Hopefully that makes sense. So down here, now I can just print out column, and this
should actually print out every single value inside of this two dimensional array.
So let's go ahead and run this program. And down here, you'll see that we're
basically printing out one all the way down to zero. So we're printing out all
of the elements inside of all of the arrays inside of the number grid. So that's
how we can use two dimensional lists and nested four loops together. And both of
those things are actually pretty handy in Python. In this tutorial, I'm going to show
you how to build a basic translator in Python. So essentially, we can take in a
string so we can take in like a phrase or a word, and we'll be able to translate it
into a different language. So over here, I have this little file, and it basically
specifies this language that I made up. So I'm calling it the draft language.
And in the draft language, all vowels become G. So if I was going to translate an
English word into a draft word, for example, the word dog, I would look through the
word dog in any instances where there was a vowel. So in our case, let's just say
A E I O U, I would convert that vowel into a G. So dog would map to D G G, cat would
get mapped into C G T. So those are the basic translation rules. Obviously, this is
a pretty simple language, but you get the point any vowel is going to become a G,
and then we'll get the draft language. So we're going to build a draft translator.
Over here in our Python file, let's start making this translator. The first thing
I'm going to do is just define a translate function. So I'm going to make a function
here, and I'm just going to call it translate. And inside of these parentheses,
we want this to take one piece of information, which is going to be the phrase
that we want to translate. So I'm just going to say phrase. And now inside of this
function, we need to figure out like, okay, how can we translate English into our
draft language? Well, remember, the rules is basically just any vowels become G's.
So the first thing I want to do is create a variable. And this variable is going to
be the translation. So this will be like the final result that we're going to return
to the user. So I can say translation. For now, I'm just going to set it equal to
the empty string. So this is just going to be like a completely empty string. It's not
going to be doing anything. And now what we want to do is we basically want to
loop through every letter inside of this phrase. And if it's a vowel, we want to
change it to G. And if it's not a vowel, we want to leave it alone. And we're
essentially going to be looping through phrase. And we're going to be adding the
letters onto this translation one by one. So let me show you guys how we can do this.
I'm going to make a for loop. So I'm going to say four. And I'm just going to say
letter in phrase. And so now, when I loop through here, I can access each
individual letter inside of the phrase that they passed in. So what I want to
do is I want to check to see if the letter is a vowel or not, right? If it's a
vowel, then we can add a G onto translation. If it's not a vowel, then we can just add
on whatever letter it was onto translation anyway. So I'm going to use this if
statement. I'm going to say if. And there's actually this special thing we can do in
Python, we can check to see if something is in something else. So I can say if
letter in, and over here, I'm just going to type out all the vowels. So we'll say
A E I O U and A E I O U. So I'm basically checking to see if the letter is inside
of this string. And if the letter is inside of here, then we know that it's a vowel.
So down here, we can handle that case. So I can say, translation is equal to
translation plus G. Because if this letter is a vowel, we just want to convert it
into a G. Otherwise, though, I can basically just say, translation is equal to
translation plus letter. So in the case where we have a vowel, I'm adding a G onto
translation. In the case where we don't have a vowel, I'm just adding on whatever
letter it was onto translation. So finally, down below this for loop, we just want
to return the translation. So I'm just going to say return translation. And now we
have a awesome translate function, and it should probably work. So let's come down
here and we'll test it out. So why don't we allow a user to input some
information. So I'm basically going to call this function. Actually, we're going to
print it out. So we'll print this out. And I'm going to call this function
translate. And inside of here, I'm actually just going to pass whatever the user
inputs. So I can just say input. And inside of these parentheses, I can say the
prompt. So enter a phrase. And so now I'm basically combining all these statements
together. I'm saying I want to print out the translation of whatever the user
enters in. So let's go ahead and run this program, and we'll see how we do. So I'm
going to run the program. It says enter a phrase. Actually, let me format this a
little bit better. Okay. All right. So it says enter a
phrase. So let's go ahead and do that. So let's just type in the example we used
before. I'm going to say dog. And so now out pops the answer, which is d g g. So
that's kind of cool. And we can just keep running this as many times as we want.
So I could type in like to be or not to be. So this is like a little bit longer of
a string. And you can see it translates it into our draft language. So that's
basically how we can create our little translator app. And that's pretty cool.
And so you can see we're using a for loop in combination with an if loop. And
that's a really powerful structure. And actually, there's one place over here
where we can make this a little bit more efficient. So instead of saying if letter
in this string, you'll notice here, I'm checking to see if it's in all the lower
case and the uppercase. I could actually just say if letter dot lower in. And now
I only have to type out the lower case letters. And it's still going to do the
same exact thing. So that's a little like hack or whatever that we could use. And
there's also one more problem with this program. So you'll notice down here,
we're setting the translation equal to the translation plus g. But the problem is
if I was to start my word inside of the phrase with a capital vowel. So in other
words, if I said like on, you'll notice that when this gets printed out, it gets
printed out as lowercase g and so it's not keeping our uppercase syntax. And that's
kind of a problem. So we can actually use another if statement in here. And we
can check to see if letter dot is upper. And if the letter is uppercase, then we
can just set the translation equal to the translation plus a capital G. Otherwise,
we can just set it equal to the lowercase g. And so now we're able to control both
of those situations. So let's just test this and see if it works. Enter a phrase,
say on. And now we're keeping that capital letter. So those are just a couple
different ways that we could make this program a little bit better. But the
basic concept is we have a for loop and then we have some if loops inside of it.
And we're calling this awesome function that can translate a word for us.
In this tutorial, I want to talk to you guys about comments in Python. This is
going to be a pretty short tutorial. I just want to give you guys an overview of
what comments are and how we can use them in our Python programs. So over here in
my Python program, I just have this line here. It says print comments are fun. So
if I run my program down here inside of the console, it's going to print out
that comments are fun, right? So any code that I put over here is going to get
executed by Python. But there's actually a special line of code that we can write
and it's called a comment. And a comment is basically a line inside of our Python
file that's just not going to get rendered by Python. So when Python sees it,
it's just going to ignore it. It's not going to execute it. It's not going to
print anything out. It's just going to kind of ignore it. And comments are
going to be just used for us humans. So a comment is used for like me or another
developer to write a little comment, a little like plain text, you know, comment
inside of a file. In order to create a comment, I could use this hashtag symbol.
So I can click hashtag and now I am writing a comment. So I can write whatever I
wanted in here. I could say like this program is cool. And you'll notice when I
put on the program, that doesn't show up anywhere, right? It doesn't matter what
I put after this hashtag. This is just like ignored by Python. It's just for me
or another developer to come in here and use. So a lot of times if you want to
write like a little note inside of your Python files, or maybe you can write
yourself a little reminder, or if you want to write a line to like explain a line of
code. So for example, I could write a comment that would explain this print
statement. So I could say like this prints out a string, right? Now anybody looking
at my file would be able to read this and they'd be like, Oh, that's what this
does. Okay, cool. So comments are useful for leaving little comments in a Python
file. If you want to make comments on multiple lines, you can just create a new
line and use another hashtag. And so then I could type out whatever my next comment
was. And generally, if you're going to put comments on multiple lines, you want to
just use multiple hashtags. But there's actually another way that you can write
out comments. So you can use a triple quotation mark. So I'm typing out the
single quote. And I'm typing it out three times. So you can see here up here, we
have this three times, we have this three times. So in here, I can, you know,
write any comment that I wanted. So I could put, you know, as much text up here as I
would want. And same thing, when I run this program, it's just not going to get
rendered. So this is another way to make multiple line comments. But the, like,
official Python, like style guides or whatever, just recommends that if you're
going to put comments on multiple lines, you use these hashtags. So, you know,
you can really do whatever you want. But if you want to be like super Python
official, then use these hashtags. Comments can also be useful for doing something
called commenting out a line of code. A lot of times when you're writing a
program, you might have a line of code in there that is maybe like potentially
messing stuff up. And so what you can do is you can actually comment out that
line of code. So I could put a comment in front of this line of code. And now when
I run my program, the program is going to run, but it's not going to use this line.
So sometimes if you're trying to see if like a line of code is causing you trouble
or if you want to see what your program would be like without a line of code, or
even like a block of code, you can just comment it out. And that way you're not
actually deleting the code from your file. You're just commenting it out. So Python
is going to ignore it. And those are really the two uses for comments is to write
little notes, write little comments about different things or to comment out code.
And as you go forward in Python, I'm sure you'll find uses for comments.
In this tutorial, I want to talk to you guys about catching errors in Python.
Now, a lot of times when we're running Python programs, you'll encounter different
errors. So different situations could come up and your program might throw an error
or it might throw an exception. And a lot of times when these situations happen,
they'll completely stop your program from running. And what we can actually do is
we can watch out for, you know, certain specific errors that are going to pop up
in our program and we can actually handle them. So instead of our program just
breaking and stop executing, we can actually handle those errors and do things
when they occur. So I want to show you guys an example of using this down here.
I'm saying number is equal to int input enter a number. Basically what's happening
is I'm prompting the user to enter in a number using this input command. And then
I'm converting whatever they entered into an integer. So as long as they enter in
like a valid integer, I can convert it into an integer. So I can convert it into
like a whole number and I can store it inside of this number variable that I'm
just printing it out. So let's try to execute this program. I'm going to run
the program and you'll see down here it says enter a number. So we can enter in
like four, for example. And when I enter in four, everything's going to work and
it's just going to print out four and everything is right in the world. And
actually, I'm going to modify this up here. So it's a little easier to see. But
let's say we run the program again. So it says, okay, enter your number. Let's say
that I break the rules and I don't enter a number. So I just ignore what the
prompt says and I put, you know, some random text in here. Now when I enter this,
the program is going to throw an error. You can see down here, it says value error
invalid literal for int base with base 10. Basically, hey, you didn't enter in an
integer like you said you would, right? We told them to enter in a number. They
entered in text. So it broke the program. And this is a situation that happens a
lot. So there's a lot of different situations where something can just go
wrong in our program. And I'm until now we've just kind of accepted it as a
reality. But if you're writing a real live Python program, you don't want something
like this to trip up your program. You don't want the entire program to break when
somebody just forgets to enter in a number, right? You want to be able to handle
all these different situations. And in order to handle them, we can use something
called a try except block. And a try except block will basically allow your
program to try out a piece of code. And if everything goes well, then we're great.
Like we could try out entering in the number. And if the user enters in the
correct number, great. But if they don't enter in the correct number, then we can
basically like account for that. So they entered in something like a string when
they're supposed to enter in a number, we could tell them like, Hey, you entered
in the wrong thing. So I'm going to show you guys how we can use these try except
blocks. I'm going to come up here, and I'm just going to say try. I'm going to make
it colon. And I want to indent both of these things. So both of these things are
going to be inside of this try block. Now I'm going to make a new line. I'm going
to come down here and I'm just going to type out. Except, and I'm also going to
put a colon here. And down here, I'm just going to print something out. So I'll just
say like invalid input. So now, since I put this code inside of this try block,
if the user enters in something wrong, it's going to be able to catch it. So it'll
be able to catch what the user does wrong and print out invalid input onto the
screen. So let's go ahead and run our program. And it's just going to say enter
a number. So we're going to ignore that. We'll enter this in. And now instead of
yelling at us and breaking the program, it just says, Hey, invalid input. So it was
able to handle the error they got called. It was able to handle the invalid input
error. Basically just told us, Hey, no more input. So this is a really powerful
thing that we can do in our Python programs. Basically just to protect our
programs, right? If I want to have my program running for long period of time,
I can't have something as simple as an invalid input, trip it up. So I can
catch that invalid input using these try accept blocks. But here's the thing.
You'll notice that this little guy down here is getting highlighted. And let's
just see what it has to say. So it's basically saying to broad exception clause.
And this is a message that we're getting because when I just say accept, this
will catch any error under the sun. So anything that can go wrong on my program
for the most part, this is going to accept it. So let me give you guys an
example. Let's say I had in my program, I was creating a variable, right? So I
wanted to create a variable called value. And I set it equal to 10 divided by
zero. Now, if you know anything about math, you'll know that you can't divide a
number by zero. It's not possible. So the program isn't going to be able to
handle this. And you'll see that this is going to throw an error for us. So when I
run the program, it basically says zero division error division by zero. We're
not allowed to do that, right? If I took this and I put this down inside of this
dry block, and I just pasted it in here, it's going to get caught by this
exception that we wrote down here. So it's going to get caught by that
accept. So when I run the program, you'll see it says invalid input. But the
problem is that we didn't input something that was invalid, right? We just divided
by zero, but it wasn't able to handle that. Right. So it could not divide by zero
it through an error. So even though we didn't get an invalid input, even though
it was because we divided by zero, it's still printed this out. And this brings
me up to another point is we can actually catch, or we can accept specific
types of errors. So for example, in this case, we got a division by zero error. In
this case, we got an invalid input error, right? We put something in that was wrong.
You couldn't convert the value. So I can create two different accept blocks to
catch two different types of errors. So down here, I can just make a space and I
can specify the type of error that I want to catch. So when we divided by zero, it
was zero division error. So if I just put zero division error in here, now it's
going to catch that zero division error. So I can say divided by zero. I could
also make another accept. So I could say accept. And we can catch the other
exception that we got. So I think it was a value error, right? It was, we put in
the type of value inside of that input. So now we can print out invalid input. So
with these two accept blocks, we're basically able to handle both of these
situations. So for example, if this ends up breaking the program, this division by
zero, then it'll be able to catch that. So down here, it'll tell us, Hey, you
divided by zero. But if I was to get rid of this line of code, and I just entered
in the number wrong. So I came down here and I entered in a string instead of a
number. Now it's going to tell us invalid input. So we're going to be able to
specify what happens when certain things break. So when we get a valid, when we
get a value error, we can say invalid input, when we get a zero division error, we
can say divided by zero. So depending on what breaks, depending on what happens,
we can actually do different things. And I want to show you guys one more thing
we can do, which is we can actually store this error as a variable. So I can say
accept, I can say as, and I can type in a name here. So a lot of times people will
just say like, e r r. And down here, I can print out the actual error. So I can just
print e r r. And so if we end up dividing by zero. So if I say answers equal to
10 divided by zero, we're going to throw this error, and it's going to print out
what went wrong. So now I can run this program. And it just says division by zero.
So it just printed out the error that we got. So that's another useful thing you
can do is you can actually like print out the specific error that got thrown. Now,
a best practice in Python is to use these specific errors. So you always want to
accept and accept for a specific error. What you don't want to do is just say
accept, and then just accept anything under the sun. It's just too broad. And it's
not a good practice in Python. So you always want to be catching specific errors like
this. In this tutorial, I want to talk to you guys about reading from external
files in Python. Now a lot of times in Python, you're going to want to read from
files that are outside of your Python file. So you might want to read information
from like a text file or a CSV file or like an HTML file. And you can actually use
something called the Python read command. And it will allow you to read a file that
is stored outside of your Python file. So you can use these files to get information
or you can parse through different files and do different things. So I'm going to
show you guys the basics of reading files, opening files, closing files, doing all
that stuff. Over here, I have this file called employees dot text. And it basically
just lists out a bunch of different employees, like these could be employees in
an office or whatever. So it's just listing out all of this information. So let's say
that inside of my app dot Python file, I wanted to read the employees inside of
that file. The first thing I have to do is actually open that file from inside
of Python. So I can use a special command called open. So I can say open. And then
in here, I can type in the name of the file that I want to open. So this is either
going to be a relative path to the file, an absolute path to the file, or just the
files name if both files are in the same directory. So in my case, app dot Python
and employees dot text are in the same folder, like they're in the same directory.
So I can just type out the name of the file. I can just say employees dot text.
And then I want to put one more thing inside of this open function. And it's
going to be the mode that I want to open the file in. So you can actually open
files in a couple of different modes. And the first mode is called read. So I can
just put an R here and that's going to stand for read. And this basically means
that I only want to read the information inside the file. I don't want to modify
it. I don't want to change it. I just want to read it. I just want to see what's in
the file and do some stuff with that information. Another mode is called
write. So I can type in this w and writing basically means that you can change the
file, right? You can write new information. You can change existing information.
There's another one called a and a stands for append. And this basically means
that you can append information onto the end of the file. So you can't modify
any of the information in the file. You can't change any of the information, but
you can add new information. And there's one more, which is R plus. And this
basically means read and write. So this will give you all the power of reading
and writing. So in our case, we're just going to be working with regular R. So
we're going to be reading from the file. Now this open function will essentially
just open the file. So it's going to like go over to that file inside of our file
system, open it up, and it'll allow us to read through it. But generally, we're
going to want to store this opened file inside of a variable. So I can create a
variable and we can just call it employee file. And I'm just going to set it equal
to this open function. So now the open employees dot text file and all the
content inside of it is stored inside of this employee file variable. Now, whenever
you open a file, you always want to make sure that you close the file as well. So
just like we have this open command, we also have an close function. So I can come
down here and say employee file dot closed. And this is essentially just going to
close the file. So we're no longer going to be able to access it. And generally,
it's a good idea whenever you're opening up a file, you want to also make sure that
you're closing the file at some point. So generally, once you're done reading it,
you can just close it. So that's kind of like how we can open and close a file.
Now, let's talk about how we can get information from the file, right? There's no
point in having the file that we can't figure out what's in it. So there's actually
a few different functions that we can use on this employee file to figure out what's
inside of it. And I'm going to show you guys some of those. So I'm just going to
make a print statement. And inside this print statement will basically just print out
some information about the employee file. So the most basic thing we can print out
is just the entire contents of the file. But before I do that, I want to show you
guys how you can check to make sure that a file is readable. So before we do anything
else, generally, it's a good idea to make sure that it's possible to read this file.
And there's a function inside of Python, we can use called readable. So I'm just
going to type out employee file dot readable. And this is going to return a
Boolean value. And it's going to tell us whether or not we can read from this file.
So I'm going to run my program. And you'll see down here, we get a value of true. And
that's because we set the file with a read mode. So it's in read mode, we can read
from it. If I was to put a double you here. So if I put like, right, now readable is
going to be false, because we can no longer read the file, we can only write to the
file. So I'm going to change this back to our so we can just read it. So once you
figure out whether or not the file can be read from, let's actually read it. So
there's another function called employee file dot read. And this is basically just
going to spit out all the information in the files. So when I run the program, it's
just going to spit out all of this information, right, all the information that
was in that file. I can also come down here and we can read an individual line
inside this file. So I can say employee file dot read line. And what this is going
to do is it's going to read an individual line inside of this file. So now when I
run this program, you'll see it's just reading that first line in the file. And
this read line function is actually just reading the first line. And then it's
basically like moving a little cursor onto the next line. So if I was to copy this
code and then print it again down here, I'm saying employee file dot read line. So
it's going to read the first line. And then when I say it again, it's going to read
the line after that. So this is actually going to end up printing out the first
two lines in the file. So when I run this program, you'll see we print out Jim
salesman and Dwight salesman. So if I was to do this multiple times, like I could
technically print out every line inside of this file. And you can see we can do
that. And so that can be pretty useful for reading multiple lines in a file. But
there's actually another function that is better at doing that. And we can say
instead of employee file dot read line, we can say dot read lines. And what this is
going to do is it's going to take all of the lines inside of our file and put them
inside of an array. And so now when I print this out, you'll see we have this
array down here. It says Jim salesman, that's the first item in the array, Dwight
salesman, the second item in the array, it's basically taking each line and putting
it inside of an array. So if I wanted to access a specific line, I can just
refer to it by its index in the array. So if I said one, now this is going to give
us that Dwight salesman line, because that is at index position one inside of the
array. You can also use this read lines function with a for loop. So I can come up
here and create a for loop. I'm just going to say for, and we'll say employee in
employee file. And then for each employee, we just want to print them out. So and
actually started, we have to put employee file dot read lines up here. And so now this
will loop through all of the employees in this employee file that read lines array.
So we can actually just print out the individual employee. And now this will print
out all the employees inside of that file. So it's basically printing out each
line in the file. And that can be pretty useful. So you can use all of these
different functions like read, read line, read lines, readable, you know, there's a
bunch of these different things that we can do to get information from a file. And
so there's a lot of cases where you're going to want to be able to parse through
information in a file. And this is a awesome way to do it. So just to recap,
whenever you want to open a file and read from it, you can just use this open
function, type in the name of the file and then the mode, which in our case is going
to be our, then you can do all sorts of stuff with it. And you always want to make
sure that you close it when you're done. That's just good practice. So that's the
basics of reading from files. And hopefully you guys can use this in some way, shape
or form in the future. In this tutorial, I want to talk to you about writing and
appending to files in Python. So one of the cool things about Python is it allows you
to work with external files. So I could have like an external text file and I could
actually like completely read all of the information in it. I could parse through
it, use that information to do certain things. But in addition to reading a file, I could
also write a file. And that's what I want to talk to you guys about today is writing
new files and appending onto existing files. Over here, I basically just have some
written out and this essentially just reads information from this employees.text file.
So you can see over here, I'm specifying the mode, which is R and that stands for
read. And then down here, I'm just reading all of the contents of the file and
spitting it out on the screen. So I'm going to click the play button over here and
you'll see that this executes. So it's printing out all of the lines of code in
our text file. So over here, I'm in this employees.text file and it just has all
this information, like employees in an office. But let's say that I wanted to
add another employee onto here, right? Let's say that a new employee joined our
company. So we wanted to add them onto this list. Well, I can come over here to
my app.python file. And instead of reading from the file, I want to append to the
file. So I want to say a and appending to the file basically means that you're
adding some text at the end of the file. So wherever the file ends, you're just
going to add some text onto there. So what we can do is we can actually add
another employee into the file. So instead of printing something out, I'm
actually just going to say employee file dot right. And when I say employee file
dot right, I'm going to be able to write something to the end of the file. So I
can basically just write whatever I want. So why don't we add in another employee
into our employees dot text file. So we can add in another employee. Why don't we
say like Toby, and he's going to be in human resources. So now, when I run this
program, it's going to add Toby human resources onto the end of the file. So I'm
going to run my program. And you'll see that nothing shows up down here in the
console. But if I go over to my employees dot text file, all of a sudden we have a
new entry over here, it's Toby from human resources, right? So I was able to
append a line onto the end of this file. But here's the thing, you need to be
careful when you're writing to files, because you can actually mess up a file
very easily. For example, I already added Toby here into my file. But if I was to
run this program again, you'll see that over here in this employees dot text
file, it went ahead and added Toby again, right? So it added this employee here
again. And also, you'll notice that in this case, this employee didn't go on to
the next line. I accidentally ran my file again, and all of a sudden it messed up
this file over here. And so appending, you really need to be careful because if you
actually run your file again, or if you, you know, append something on something
wrong to the file, it's permanent, like it's getting saved inside of the file. So
I want to talk to you guys a little bit more about, you know, appending. Another
thing we could do, let's say we wanted to add another employee. And you'll notice
over here in this employees dot text file. When I appended it on again, it got
appended to the end of the existing line, right? So the first time I had a new
line there, but if you don't have a new line at the end of your file, and you all
and you want to add a new line, you're going to have to add some special
characters. So let's add another employee, and we'll call her Kelly, and let's just
say Kelly is in customer service. So Kelly is going to be in customer service.
And if I want to add this employee onto the end of the file in a new line, I'm
going to have to put a new line character in front of it. So I can say backslash n,
and this will append this entry into the file with a new line. So on a new line.
So now when I run this, you'll see we get Kelly from customer service on her own
line. So you want to make sure that you're aware of like these special
characters that you can use, they call them escape characters. And you know,
anytime you're adding onto a file, you want to make sure that you're adding on
exactly where you want to add on. So in addition to appending to a file, I could
also just like overwrite a file or I could write an entirely new file. So since we
already have this open, instead of appending to the file, why don't we just
write a file? So I'm going to use this W. And now if I say employee file dot right,
because I'm using W and I'm not using a, it's actually going to override the
entire file. And it's only going to put this inside the file. So when I run this,
and we go over to this employees dot text file, you'll see we only have one line
inside of this file. Now it's just Kelly customer service. That's because I was
using W not a when you use W, it's just overriding everything that's in that
existing file. You can also use W to create a new file. So over here, I could say
employee file is equal to employees one dot text. And now what's going to happen
is when I run this, it's going to create another file for me. So I'm going to run
this. And you'll see over here in my file browser, we have this new file
employees one dot text. So if I open this up, it has exactly the same stuff as in
this employees file. But it basically created a new file for us. And so a lot of
times you're going to want to create a new file. And you can use different
extensions too. So if I wanted to create like a web page, I could say index dot
HTML. And I could also add in some HTML code in here. So if you don't understand
HTML, don't worry about it. But if you do, I could put like a paragraph in here,
another paragraph, like this is HTML, basically HTML is like a web page. And
the point I'm trying to make is that you could write out a web page inside of Python
by doing something like this. So now when I play this, we get this index dot HTML
file. And it has some HTML inside of it. So that's one way that writing to files
can be really useful. You can overwrite an existing file, you can write a new
file and create it, or you can append onto the end of a file. And there's tons of
applications for writing to files and Python's a great language for working with
reading, writing, and doing all that stuff with files. In this tutorial, I want to
talk to you guys about using modules in Python. Now, a module is essentially just
a Python file that we can import into our current Python file. So for example, if I
wrote a Python file that had a bunch of like useful functions or useful variables
or, you know, other things like that, I could take that file, I could import it
into the file I'm currently working on, and I could actually access all of those
functions, all of those variables, all the stuff from that external file inside the
file that I'm currently working on. So I'm going to show you guys how we can use
modules, and then we'll talk about like, you know, where you can find awesome
modules and really why modules make Python an awesome language. So over here in my
text editor, I actually created this file and it's called useful tools dot Python.
And basically this file just has a bunch of sort of like useful tools that I might
want to use in one of my programs. So you'll see over here, I have some
variables. This one is telling me how many feet are in a mile. This one's telling me
how many meters are in a kilometer. And then we have this list here, which lists
out all of the members of the Beatles. I also have a couple different functions
down here. So I have this get file extension function. And this basically just will,
you know, you give it a file name, it'll tell you what the extension is. And then we
have this other function down here, which simulates rolling a dice. So you pass
it a number. If I pass it like a six, it would roll a six sided dice. If I pass
it a nine, it roll a nine sided dice, etc. So this is like a Python file that I
wrote. And it has some useful stuff in it. And honestly, there's a lot of stuff
in here that I might want to use in the other Python files that I work with. So
let's say I'm over here at this app dot Python file. And I'm thinking to myself,
huh, I think I could use one of those functions that was inside of that useful
tools file. Actually, yeah, I need to simulate rolling a dice in my program.
Well, instead of having to go over here, copy this function and then paste it
over here into my program, I can actually just import this useful tools file. And
I'll be able to import all of these functions and all of these variables
and attributes. So the way I can do that is just by coming up here. And I'm just
going to go right at the top of my file. And I'm just going to say import. And
then I want to type in the name of the file that I want to import. So I'm just
going to say useful underscore tools. And Python is going to be smart enough to
know that it should go off and grab all the stuff from this useful tools dot
pi file. So once I've used that import statement, I can actually use all of
the functions inside of that useful tools file. So for example, I could simulate
rolling a dice, I could say useful tools dot. And now I'm actually able to access
all of the attributes from inside that file. So when I say useful tools dot,
you'll see down here in my little suggestions menu, it's telling me like
Beatles feet and miles, get file extension meters and kilometers roll dice. So it's
giving the access to all of the stuff that was in that file. So over here, I could
just say like roll dice and we can pass this like a 10 will roll a 10 sided
dice. And now this should actually be able to run that function. So we'll simulate
rolling a 10 sided dice. You can see we got four. And this is a really core
concept in Python, which is importing functionality from external Python files.
And this is like huge in Python. And this is seriously going to change the way that
you create your Python files. So you'll notice like I didn't have to copy any
functions or any variables or anything over into this file. And yet I was able to
use all the stuff that was inside this useful tools file. So that is huge. And
honestly, it's going to save you a lot of time because you can write something
once and then you can import it into your other files. So that's really the
basics of using modules. I mean, modules are very simple. A module is just any
external Python file that you want to use some stuff inside of it. I want to
show you guys a place where you can go to find a huge list of modules. So I'm
going to go over here to my web browser. And I'm on this website. It's actually
like the official Python docs. And basically all I did to get here was just type in
list of Python modules and Google. And depending on the version of Python you're
using, you're going to want to make sure that you click the right one. I clicked
on the version 31. And over here on this page, you can see there's just a huge
list of Python modules. And these are basically modules inside of Python that
you can just access. So essentially, there's like all of this awesome code
that's already been written for you. And so there's some sort of functionality
that you want to have inside of your Python program. Chances are there's a
module in here that has that functionality. So there's all sorts of stuff you can do.
I mean, if you just look through this list, like, you'll see a huge collection of
basically just, you know, a bunch of either Python variables or Python
functions, just, you know, things that you can use to make your programs better.
So here's what I would recommend, like, head over to this page and just sort of
look through a lot of these different modules, you know, see what you can see.
And honestly, like, if you click on one of them, it'll bring you to a page that
talks about like how to use it. It'll tell you like how to import it and just,
you know, some basics about it. And this is sort of like the list that's on the
official Python docs. But here's the other thing. The Python community is huge.
There's tons of developers who use Python. And you can actually find a lot of third
party modules. So if you just go online and, you know, look up, like, Python
module for doing X or Python module for doing Y, chances are somebody out there
has already written a Python module to do what you're trying to do. So if you get
good at using modules and you get good at, you know, looking for them and finding
them, you can actually save yourself a bunch of time because chances are that
somebody's already written a module to do, you know, what you're trying to do or
like part of what you're trying to do. So now that we've taken a look at all these
different Python modules, I want to just kind of dive in a little bit deeper into
how we can actually use these things. So you'll notice here, there's a lot of
files, right? There's a lot of different modules that we apparently have access to
Python. But the question is, where are all these files stored, right? When I was
over here in my program, for example, I was using this useful tools.py file.
Like, I knew where that was. I created that file. I was directly involved in
making it. And I just imported over here. It was pretty easy, right? But what about
all of these files, right? What about all these modules over here? Like, where are
all of these stored? And there's basically two types of modules here. There's built-in
modules, which means they're just built into the Python language. So we kind of
just automatically have access to them. And there's external modules. And a lot
of these external modules are just stored in basically the same folder that we
installed Python on our computer. So for example, let's look at a couple of these,
right? We have, like, base 64, BDB, bin ASCII, like, if I come over here and
you'll see, I'm over here in my little file explorer. I have my Python project,
which is draft. There's also this other folder over here called external
libraries. If I was to click down on this and I come down here, you can see it's
just the version of Python that I'm using. There's a folder here called lib.
And this is a very important folder. This is basically storing all of those
external modules. So you can see if I scroll down here, we have all of these
different, like, modules, right? So here's that base 64. Here's BDB, right? A lot
of these external modules are stored inside of this lib folder. And like I
said, there's external modules. There's also a few modules that are just like built
in modules. They're not going to be stored inside of here. And you can actually
tell. So for example, if I was to come over here and click on base 64, it tells
me where the source code is. So the source codes in lib forward slash base 64.
We were able to see that, right? I was, I saw that inside of my lib folder.
This bin ASCII, though, for example, if I click on this, you'll notice that this
isn't giving me a folder because this is basically just like built into Python.
So we don't have to worry about, you know, locating that file. It's just kind of
like, it just kind of works. So some of these are external. Some of them are
built in. And I want to show you guys, in addition to using these modules,
right? So there's a lot of good stuff here. And honestly, you could spend, you
know, years and years just learning about all these different modules. But a lot
of times you're going to want to use modules that other people have written.
And you know, there's a lot of developers who work on Python and who write different
modules. So there's a lot of useful modules out there that aren't included in this
list, right? They're not going to be inside of this lib folder by default.
And what we can actually do is we can install those external modules, those
third party modules, that don't just come pre installed with Python. And I'm
going to show you guys how to do that really quick. So the first thing you need
to do, obviously, is find a Python module that you want to install that you want
to download. And I have actually used one of the past Python docs. It's an
external module that you can use to basically use Python to create Word
documents, which is pretty cool. So you can like format Word documents inside
of it. So I'm just going to look that up here in Google. I'm just going to type
in Python docs. And look, here's the thing, there's tons of these external modules.
You don't have to use Python docs. I'm just giving you guys an example. But really,
if you just look up like useful third party Python modules, there's going to be lists
of these things online. In my case, though, Python docs has a website, and it
basically just has like some installation instructions. I'm going to come over here.
And it tells me that I can install Python docs using the command pip install Python
docs. So this brings us to something I want to talk to you guys about, which is
pip. And pip is essentially a program. And actually, if you have a newer version
of Python three, it comes pre installed with Python three. And you can use pip
to install Python modules. It's referred to as a package manager. And a package
manager basically just allows you to like install, manage, update, and uninstall
like different Python modules. So pip is extremely useful. And in order for us to
install Python docs, we're going to have to use pip. So I'm going to show you guys
how we can do that. What I want to do is open up the command prompt or the
terminal on my computer. If you're on a Mac, you want to open up your terminal.
If you're on a Windows computer, you want to open up your command prompts. I'm
using a Windows right now. So I'm just going to come down to the search bar and
type in CMD. And this command prompt should come up. So I'm going to click this
inside of the command prompt, we can actually use pip. The first thing we want to do
is just check to make sure that pip's installed. And like I said, if you have a
version of Python three, pip should come just pre installed. And it should just work
in here. If you don't have a new version of Python three, though, you might have to
install pip separately. And there's tons of instructions online on how to install
pip. So I'm just going to check to make sure that I have it, I'm going to type in
pip hyphen hyphen version. And this should spit out the version of pip that I
currently have. So as long as we have pip, then we're ready to install an external
or third party Python module. All I have to do is just say pick install. And now I
just want to type in the name of the Python module. So in the case of Python
docs, it was just Python docs like that. Now again, you don't have to install Python
docs. I'm just doing this for this tutorial. Just kind of show you guys how this is
going to work. But you know, what you want to do is go online and look up some third
party external Python modules. And generally, like I'd say 90 to 95% of the
time, you're going to be able to just install them using pip. In the off case
that you can't install them using pip, chances are there'll be like some detailed
installation instructions. But I would say for the most part, any like legitimate
Python module is going to be able to install using pip. So over here, I'm just
going to say pip install and then the module name. So Python hyphen docs. And
when I click enter, this is going to go off and install Python docs for us. So I'm
just going to enter. And it's going to go off and install everything we need for
Python docs. So you can see we successfully installed Python docs 0.8.6. So I'm going
to show you guys where exactly this got placed. So normally, when we install a
external third party module, it's going to get put inside of this lib folder. But
it's going to get put inside of a special folder in here called site packages. So
site packages is a special folder. And if I open this folder, you'll see now we have
this docs folder and we also have this Python docs 0.8.6 pi 3.6.ag info folder.
So these two folders are basically included in that module installation. So you can
see this docs folder has a bunch of different Python files and has a bunch of
stuff in here that we can use. So if I wanted to use this inside of one of my
programs, I'm just going to refer to the name of the module. So in our case,
it's just going to be docs. So I used Python docs in order to install it. But we're
going to use docs if we want to import it. So I could come up here and I could say
import DOCX. And now I can actually use it. So I can just say like docs,
doc, whatever. And you can see there's a bunch of different stuff down here. There's
like a document document part image part. There's a bunch of stuff that we can use
with this. And obviously, depending on the Python module you install, there's going
to be different instructions. But you can see it got stored down here in this
site packages folder. If I wanted to remove this, I could use PIP to do it. So I could
just say PIP uninstall. And we could just again say the module name. So Python docs.
And PIP will now uninstall this on our computer. So how is to go back over to
this folder? You'll see that those two, that docs folder and then that other
folder disappeared. So they're no longer here. And I'm actually not going to be able
to use this anymore. So that's sort of the ins and outs of using modules. Now again,
there's tons of these modules and I can make dozens and dozens of Python courses
covering each one of these modules. The built-in modules, the modules that are
included by default and external modules, there's tons of these things out there.
And really, as a Python programmer now, what you can do is you can go out and play
around with these different modules. I showed you the ins and outs of installing
them and you can use PIP to install all these different modules. And you can make
sure that you have them by checking the site packages folder or the libs folder.
But really now it's on you to just kind of go out and use these modules and don't
shy away from this because modules are a huge part of Python and you're definitely
going to want to include them in your Python stack.
In this tutorial, I want to talk to you guys about classes and objects in Python.
Now classes and objects are extremely useful in Python programming and they can
help you to make your programs more organized and more powerful.
So when we're in Python, we're dealing with all types of data, right?
And a lot of times when we're writing programs, we're going to have to work with
different types of data. And there's essentially like a few basic types of data
we can do with usually things like strings, so like plain text, numbers, and
Boolean values. Those three are kind of like the main types of data that you're
going to be working with in Python. And we have all sorts of structures we can
use to store that data, you know, things like lists or dictionaries.
But here's the problem is that not all information, not all data and not all
things can be represented using strings, numbers, or Booleans, right?
There's a lot of things in the real world that we can't represent in something like
a string or just a number, right? In other words, something like think of like
something like a phone or a computer or a person, right? You can't really
represent those things in like a string or a number. You know, in other words,
like the data types that we have available to us in Python can't cover that.
And so what we can do with classes and objects is we can essentially create our
own data types. So I can create my own data type for anything I want in Python.
So I could create like a phone data type and it could represent a phone.
So I could store all the information I would ever want to know about my phone
inside of that data type. And in Python, we could create a class for it.
And essentially what a class is is it's just saying, hey, here's another data type
that we want to use in Python. So with a class, you can essentially define your
own data type. And it's super awesome. And classes are extremely useful.
Classes are used in almost every single major programming language out there.
So in this tutorial, I just want to give you guys a basic introduction of classes
and using them inside of Python. So let's say that I'm writing a program
and I want to represent a student inside of this program.
So maybe I'm writing a program for like a college or a university.
Let's say that in this program, I want to model a student.
Like I want to model a real world object and I want it to be a student.
Right, we don't have a student data type and I can't really represent a student
in just a single string or a number.
So what I can actually do is I can create a class for a student.
And I'm basically creating like a student data type.
So I'm going to show you guys how we can do that and create our student class.
So over here, I'm just going to make a new file.
We'll just make it a new Python file. So I'm just going to call it student.py.
And inside of this student.py file, I want to create a student class.
So the way that I can do that is just by typing class.
And now I want to type the name of the class that I want to create.
So in our case, we're going to create a class called student.
So I'm just going to say student and now we're going to make a colon.
Now everything that goes inside of here is going to be inside of our student class.
So everything that's indented like this will be considered a part of our student class.
And basically what we can do inside of this student class is we can define a bunch of attributes
about a student.
So essentially what I'm doing is I'm like modeling a student.
I'm creating our student data type. And I can use things like strings,
integers, and booleans in order to map out what a student should be and what a student should have.
So I want to create something called an initialize function.
And the way I do that is just by typing out def.
And I'm just going to type two underscores.
And then the word I N I T and then two more underscores.
And you also want to type out open and close parentheses.
And inside of those parentheses, we want to type out self.
And then we want to colon after that.
What I can do inside of this initialize function is I can basically map out what attributes a student should have.
So we can essentially define like, hey, here's the student data type in Python.
So what I want to do is add in certain attributes after this self.
So I'm just going to say self a comma.
And now we can start thinking about, you know, what values will represent a student inside of our Python program.
So if I'm thinking like, I'm thinking something like name, right?
So every student in our program should have a name.
They also might want to have a major because they're in college.
They probably also will have a GPA. So they're a grade point average, like how they're doing in school.
And let's define one more thing.
Let me think. Why don't we make a Boolean?
And it's going to be called is on probation.
And this will basically tell whether or not the student is on probation.
So essentially what I'm doing inside of this initialize method inside of this init function is I'm defining what
a student is in our program. And so in this program, a student has a name, it has a major, it has a GPA,
and it has a value that determines whether or not it's on probation.
This right here is the student data type.
So if I'm representing a student inside of my program now, it has all of these attributes associated to it.
And that's basically what I'm doing up here.
Now inside of this initialize function, we actually have to do something. And basically what I want to do is assign some values.
So I'm going to be writing out some stuff and this might not make total sense right now.
But this is going to make sense in a second after we create our first student object.
So just stick with me right now and just basically know that we have to do what I'm doing over here.
So I'm just going to say self dot name is equal to name. And then I'm going to say self dot major is equal to major.
Self dot GPA is equal to GPA. And I'm going to say self dot is on probation is equal to is on probation.
So again, this might not make a whole lot of sense right now, but in a little bit, this is going to make total sense.
So now that I have this student class defined, I can actually use this class inside of my other file.
So I'm going to come over here into this app dot Python file. And this is just my main file.
So I actually want to create a student right so in that student class, we defined the student data type.
And we basically said like hey, a student has a name, a major GPA and it says whether or not they're on probation.
That's like the template for what a student is. But we can actually create a student.
So we can create an actual student and give it some information. And that's called an object.
So this student class over here is basically defining what a student is.
So a class is just like an overview of what the student data type is.
An object is an actual student. So it's an actual student with a name, a major and a GPA.
It's not just this template anymore. It's actually like a student that we're representing inside of our program.
So in order to use that student class and create a student object, I actually need to import that.
So all I have to do over here is just say from student, and this is referring to this student file.
I can say import student.
And basically what this is saying is from the student file, I want to import the student class.
So even though these are both student, they're referring to different things.
So this is referring to the file and this is referring to the actual student class.
So now that we did this, we can create a student. So you can create an object of a class a lot like you would a normal variable.
So I can just give it a name. I'm going to call this student one.
And I'm just going to set it equal to student.
And I'm going to make an open and close parentheses.
And now inside of this parentheses, I want to give this student a name, a major, a GPA, and an is on probation value.
So I'm going to say, we'll just create a fake student. So I'll just say his name's Jim.
And he's studying business.
And we want to give him a GPA. So maybe he has like a 3.1.
And finally, we want to say whether or not this student is on probation. So why don't we say false?
So basically what I'm doing is I'm saying that I want to create a student.
So I want to actually create a student object. And remember, an object is just an instance of a class.
So the class is like an overall template. It defines what a student is.
But an object is an actual student with actual information.
So we could call student one now. This is a student object.
So I just want to show you guys what this student object actually is and what we can do with it.
And then I'm going to talk some more about that in it function from before.
So over here, I'm just going to make a print statement.
And inside of here, I just want to print out student.
And what's cool about this student one object is I can actually access each of the attributes from inside of this object.
So if I wanted to get the name of the student, I could say student one dot name.
And now this is actually going to print out the student's name.
So over here, you'll see it prints out Jim.
If I wanted, I could print out the student's GPA. And it's going to print out the student's GPA 3.1.
So now that I created this student object, I can actually access the information about the student.
So essentially, I've just created a student data type.
So I could create as many of these students as I wanted. If I wanted, I could create another student we could call it student two.
And essentially, we do the same thing. We just give it different information.
So we could say like Pam and her major is art. And she has like a 2.5. And let's say that she is on probation.
So now I have another student, student two. So if I wanted, I could access information about that student.
We could say like student two dot GPA. And now this is giving me the GPA of student two.
So basically, what I did was I created a student data type and I created student objects.
And now I'm able to represent a student inside of my program.
So let's talk real quick about all this stuff over here because I didn't really explain it.
Basically, what's happening is when I come over here and I say student and I'm passing in all of these different values,
those are getting passed into this init function.
Remember, I passed in a name, I passed in a major, I passed in a GPA.
When we create that student, we're actually calling this function.
And when I pass in the name, the major and the GPA, those values are actually getting stored over here in this name,
this major, this GPA, et cetera. So I'm giving this student object all of that information.
And down here, what I'm doing is I'm saying self dot name is equal to name.
And basically what this means is the actual object's name is going to be equal to the name that they passed in.
So self dot name is an attribute of student.
So the student is storing a name, the student is storing a major, the student is storing a GPA.
But that's different from this name, this major, and this GPA.
Remember, these are all just values that I passed in, they're just parameters.
And so I need to take the values that I passed in and I need to assign them to the actual attributes of the object.
So I need to say, okay, the name of the student is going to be equal to the name that we passed in.
One more time, the name of the student object is going to be equal to the name that we passed in.
The name of the student's major is going to be equal to the major that we passed in.
The student's GPA is going to be equal to the GPA that we passed in.
That's basically what's happening here. So when I say self, it's referring to the actual object.
So over here, when I'm creating this student object, I'm giving it all of this information.
It's taking that information and storing it as attributes for the object.
So hopefully that makes sense. I think that's about as clear as I can make it.
But the point is that now we have a student data type.
So now I can represent a student inside of my Python program.
And that's like super powerful. And what's cool about classes and objects is you can do this with anything.
So in this example, we created a student class and then we created student objects off of that.
But I can model any real world entity into this program.
I can model something like a phone or I can model like a water bottle or I can model, you know, a keyboard, right?
I can model anything that I wanted inside of my program.
I could just give it certain attributes, right?
And so that's what's cool about classes is we can model real world objects and we can create our own data types.
In this tutorial, I'm going to show you guys how to build a multiple choice quiz in Python.
So basically, we're going to set up a little multiple choice quiz and we'll have the user take the quiz.
And as they take the quiz, we'll keep track of their score and then at the end we'll tell them how they did.
So this is going to be a pretty cool video. I'm excited to show you guys how to do this.
And we'll get to use things like classes and if statements and loops in order to build this program.
So we're going to use a lot of cool stuff in Python.
The first thing I want to show you guys, I actually did this before I started the video. It's this little array. It's called question prompts.
And I basically just wrote out all the questions that are going to be or I shouldn't say the questions.
I wrote out all the question prompts that are going to be inside of my multiple choice quiz.
So the first one just says what color are apples and the answers are red, purple or orange.
The next one is what color bananas the options are teal, magenta and yellow.
And the third question is what color are strawberries and the options are yellow, red and blue.
So this is obviously a very, very easy multiple choice quiz.
But I just kind of wrote out the questions beforehand so I didn't have to spend time doing that.
So let's talk about how we can build a multiple choice test.
Well, the first thing I want to think about is how can I represent the questions in the test.
Over here, we have the question prompts, but there's actually two parts to a question when you think about it.
There's the prompts. In other words, the actual question itself.
And then there's the answer to the question and both of those attributes need to be kept track of.
I need to keep track of what we want to ask and I need to keep track of what the answer is.
So what I'm actually going to do is create a question class.
So I'm going to create a little data type for questions and inside of that question class,
we'll be able to store the question prompts and we'll also be able to store the questions answer.
So I'm just going to come over here to my folder and I'm going to make a new Python file and we're just going to call this question.py.
So we'll call it question.py and inside of this question.py file, I'm going to make a class.
So I'm just going to say class question and I'm going to put a colon here.
So inside of this question class, we want to define it in initialize function.
So I'm going to say def two underscores in it, two underscores, and then we're going to type out an open and close parentheses self and then a colon.
So after this self, we want to define the different attributes that will describe or that will be included in a question.
So we're going to include a prompt and we're going to include an answer.
So every question will have a question prompt and a question answer.
Now down here, I want to take those values and assign them to the actual class object.
So I'm going to say self dot prompt is equal to prompt and I'm going to say self dot answer is equal to answer.
Now we have a question class set up and we can store all the information we need about a question.
So what I want to do now is go back over to my app dot Python file.
And we're actually going to create an array of questions.
So I have over here these three prompts and I'm actually just going to create another array.
So we'll just call it questions and I'm just going to set it equal to to open and close square brackets and we're going to start creating question objects.
Let's say the first element in this questions array will be a question and we want to pass question prompts zero and the answer to the first question was a red green.
That's the color of Apple.
So I'm going to pass that first question and I'm also going to pass the answer, which is a and you'll see down here, I'm getting an error.
So saying unresolved reference question. So actually what we need to do before we can create these questions is we need to import this question class.
So I'm going to come up in here, come up here and say from question import question.
So now down here in this questions array, we're not getting that error anymore.
So we created one question object and I'm just going to create a couple more for these other ones.
So I'm just going to copy this one and we will paste this down here.
Alright, so now we're creating two other questions and these ones are going to be one and two and the answer to the second question is C and the answer to the third question.
I think it's B. Let me look.
Yeah, so it's C and B. Alright, so essentially what we're doing is we're creating three questions. Each one is getting a different question prompt and they're each getting different answers.
So now we have an array of questions that we want to ask on our test.
So the next step is to actually write a function that will run the test, right?
It has to ask the user the questions and it has to check to see if they got the answer right.
Down here, lower in the file, let's create a function. So I'm just going to say death and we'll call this run test.
And inside of this parentheses, we're going to take one parameter into this function.
We're going to take a list of questions. So I'm just going to say questions.
So this is basically going to be a list of question objects that we want to ask the user.
So once I'm in here, all I want to do is loop through all the questions.
So I want to loop through each question. I want to ask it to the user.
I want to get the user's answer and I want to check to see if it's right.
And we need to be able to keep track of how the user does through the test.
So I want to create a variable called score and I'm just going to set it equal to zero.
So every time the user answers a question right, we'll increment this score variable.
Alright, so what I want to do is, like I said, loop through all the questions in that questions array.
So I'm going to create a for loop. I'm going to say for question in questions.
So in other words, for each question object inside of this questions array, I want to do something.
And basically, the first thing I want to do is ask the user the question.
And I want to store their response inside of a variable.
So I'm going to say, I'm going to make a variable down here called answer.
And this is going to represent the user's answer to the question.
And I'm going to set it equal to input and I need to give this a prompt.
So I'm actually going to give this the prompt for the question.
So I can just say question dot prompt.
So now I have the answer that the user entered in, which is stored inside this variable.
So we want to check to see if they got the question right.
And I can do that using an if statement.
Let's say if answer, and this refers to the student's answer, is equal to question dot answer.
So I'm checking to see if the answer that the student gave is equal to the answer of the current question that we're asking.
And if this is true, then I just want to increment score.
So I'm going to say score plus equals one.
And this basically just means we're adding one to the score.
So the last thing I want to do inside of this function is just print out the result.
So I want to print out how the user did.
So down here, I'm just going to say print and I'm basically just going to print out like,
Hey, you got like two out of three questions or you got five out of six questions, right?
Or you got zero out of 10 questions, right?
We'll just print out how they did.
So I'll just say you got.
And now I want to print out how many questions they got right.
So this is just going to be the score.
And in order to print a number alongside of a string like this, I'm going to have to say STR and then type in the number.
So we'll say STR score.
So we'll basically say you got however many they got right out of.
So we'll use this little forward slash.
And now we'll just say how many questions there were.
So we can say, and again, we're going to have to convert this to a string because it's going to be a number.
So we can say, string, and then we want to figure out how many questions were in that questions array. So I can say LEN.
And inside of here, we can just say questions.
So this is kind of like this long statement just to print out how many questions there were.
So then over here, I'll just say, correct.
All right.
So now we have this print statement.
And it's just going to be printing out how many questions they got right.
So let's see if this works.
The last thing I need to do is just call this run test function.
So I'll say run test, and I'm just going to pass it that questions array that we created before.
So we created this questions array with all of these question objects inside of it.
I'm basically just going to pass that into this run test function.
So now let's run our program and see how we did.
Hopefully we don't get any errors.
There's a lot of code there, so it's possible that we might.
Okay, so it says what color are apples?
Huh, this is a tough one.
All right, let's put A. So we'll get the first question right.
What color are bananas?
Let's get the second question wrong.
So I'm going to say B, bananas are magenta.
And we'll get the third question right.
So it says what color are strawberries?
I'll just put B because that's the right answer.
So now you'll see that our program prints out.
You got two out of three correct.
So not only did it ask us all the questions and get our input for each question,
but it also kept track of our score and it printed it out.
So why don't we run through the test one more time and we'll just like get too wrong this time.
So we'll get this one wrong.
I'll get this one wrong and I'll get this one right.
So now it says you got one out of three correct.
So it's able to actually grade our test.
And what's cool about the program we just wrote is if I wanted,
I could add another question into here and it would automatically be able to ask it.
So it doesn't matter how many questions we put in here.
It's just able to ask it.
So really the point I was trying to make with this whole video was just how we can use something like a class
in order to model a real world entity.
So for example, we created this question class and we were able to store all the information about a question that we could ever need.
So we were able to sort of like the prompts and the answer.
So we created our own question data type and then we were able to create a bunch of different questions,
pass it into this run test method and it was able to grade the test and just run the test really easily.
So that's kind of the point.
So hopefully this makes sense.
You know, this was a lot of code.
So if you don't fully understand what I did, go back, watch the video, you know,
try it a couple of times on your own and see if you can create your own multiple choice test.
In this tutorial, I want to talk to you guys about class functions in Python.
So a class function is essentially a function that we can use inside of a class and it can either modify the objects of that class
or it can give us specific information about those objects.
Over here, I have this student.py file and inside of here, I created a student class.
So I just had a class student and I gave this student a couple of different attributes.
I gave it a name, a major and a GPA.
So I can give this student all of this information.
So when I create a student object, it'll have all of that information in it.
Now over here on this app.py file, it actually created two students.
So student one is Oscar who is studying accounting and he has a GPA of 3.1.
Student two is Phyllis who's studying business and she has a 3.8.
So what we can actually do is we can use functions inside of these class files.
So I could actually define a function inside of this student class and then all of my student objects could access it.
So for example, I can come down here and create a function and let's say we wanted to create a function that told us whether or not this particular student had honors.
So whether or not they were on the honor roll, right?
Basically like, are they like a really good student?
So I could define a function called on honor roll.
And basically what it's going to do, it's going to tell me whether or not this particular student is on the honor roll.
And let's basically just say the rules for being on honor roll is you have to have a GPA of 3.5 or above.
So here GPA is 3.5 or above, that means you're on the honor roll.
Well, over here I could actually write the code for that.
So I could say if self.gpa, and when I say self over here, this is referring to like the actual student's GPA.
And you'll notice over here I have to pass that in as a parameter and that always has to be the first parameter.
So I could say if self.gpa is greater than or equal to 3.5, then we can return true because this student is on the honor roll.
Otherwise else we can just return false because if their GPA isn't above a 3.5 then they're not on the honor roll.
So that's essentially our little on honor roll function. And you can see it's a very small function, but it provides a service to the objects of this class.
It allows the objects of this class to figure out whether or not that current student is on the honor roll.
So over in this app.pa file I could just print out that value.
So I could say print and I'm going to see if student one is on the honor roll.
So now when I run this you'll see it should print out false.
And it's putting out false down here because student one only has a GPA of 3.1.
If I was to do the same thing on student two, student two has a GPA of 3.8 so we should return true.
And that's exactly what happened.
So essentially a class function is just a little function that can be used by the objects of the class.
And I would say this is a pretty good example of what a class function should be doing.
It should be like giving us information about the class or it can also be like modifying information about the class.
So that's the basics of using functions in classes.
I mean this is a pretty simple tutorial, but I really just wanted to introduce you guys to the concept of putting functions inside of classes.
This is going to be really useful.
And if you're making a class you always want to think about what functions can I put inside of here that will essentially help the user to either figure out information about the object or modify different values in the object.
In this tutorial I want to talk to you guys about inheritance in Python.
Now inheritance is basically where we can define a bunch of attributes and functions and things inside of a class.
And then we can create another class and we can inherit all of those attributes.
So I can basically have one class that has all the functionality of another class without having to physically write out any of the same methods or attributes.
So if that's confusing don't worry I'm going to show you guys exactly what this is in this video.
So let me show you about some stuff I have set up over here.
I created this class called chef. And over here we just have this class chef.
And inside of this chef class we have three functions.
So this chef can do three things.
The chef can make chicken.
The chef can make salad and the chef can make a special dish.
And whenever the chef makes a dish he basically just says the chef makes a chicken or the chef makes a salad or the chef makes barbecue ribs for this special dish.
It's basically just saying whatever the chef is doing.
So if I was to come over here to my app.python file you'll notice that I'm importing the chef.
So I'm basically allowed to use this chef class now.
And I can say my chef is equal to chef.
So I'm creating a new chef.
So now if I came down here and I said my chef dot make chicken.
When I run my program now it's going to say the chef makes a chicken.
If I said make special dish and I run the program now it's going to say the chef makes barbecue ribs because that's the chef's special dish.
So I have this really awesome chef class and it works really well.
But let's say that I wanted to create another class to model another type of chef.
So this chef class is just modeling like some generic chef.
We're representing some generic chef in our program.
But let's say that I wanted to create a class that modeled a different type of chef.
For example let's say we wanted to create a class that modeled a Chinese chef.
So instead of just a normal chef this is a Chinese chef.
Well I actually created a file over here called Chinese chef dot python.
So we could actually use this Chinese chef dot python file to create our Chinese chef class.
So I'm just going to say class and over here I'm just going to say Chinese chef.
And basically I just can define everything that the Chinese chef can do.
But let's say that our Chinese chef can do everything that our generic chef can do.
So the Chinese chef is like a special chef right it's a very specific type of chef.
But let's say that this Chinese chef can do everything that the normal chef can do.
So the Chinese chef can make chicken, can make salad and can also make a special dish.
Well if I wanted to give this Chinese chef all of that functionality I could just come over here and I could copy all of these functions and I could paste them in here.
So now the Chinese chef can do everything that the other chef can do.
But let's say that in addition to all of these things the Chinese chef can also make a special dish called fried rice.
So I could say deaf and we could say make fried rice.
So the Chinese chef has this extra function which allows it to make fried rice and over here we can just say the chef makes fried rice.
And let's also say that the Chinese chef has a different special dish.
So instead of making barbecue ribs the Chinese chef makes orange chicken.
Alright so now we have our Chinese chef class set up and you'll notice that it can do everything that the normal chef can do.
So let's go back over to our app.python file and I want to create a Chinese chef object.
So instead of importing from chef we're going to just import it from Chinese chef so I'll say Chinese chef and we're going to import Chinese chef.
So let's go ahead and make a Chinese chef object.
So down here I'm going to make one and I'll just say my and then my Chinese chef we can print out this special dish.
So remember the Chinese chef can do everything that the normal chef could do.
So if I say my Chinese chef make special dish when I run this program you'll see the regular chef is making barbecue ribs and the Chinese chef is making orange chicken.
So everything works out and this Chinese chef also has an extra method called make fried rice and you can also make fried rice.
But here's the problem right when I go over here to this Chinese chef dot pie when I wanted to use all of the functionality that was inside this chef class.
I actually had to copy and physically paste all of these functions down into this file right and that's kind of a drag.
I mean especially like imagine if this chef had like 20 or 30 of these different functions in it.
Well I have to copy and I have to paste all of those inside of this Chinese chef and this is where we can use something called inheritance.
So instead of having to copy and paste all of these functions I could just inherit these functions from that chef class.
And in order to do that I can actually just get rid of all of these and I can come over here and right next to where I'm saying class Chinese chef.
Inside of parentheses I can say the name of the class that I want to inherit from so I can just say chef.
And I'm also going to have to import this so I'm going to come over here and we'll say from chef import chef.
So basically what this is saying is inside of this Chinese chef I want to be able to use all of the functions that are contained inside of the chef class.
So I'll say that one more time. Inside of this Chinese chef class I want to be able to use all of the functions that are inside of this chef class.
So by using inheritance here I'm actually able to come over here to this app.py file and now I'm still able to execute for example like the make chicken function.
Even though I didn't specify the make chicken function in here I didn't write it out. I can still run this program and the Chinese chef will still be able to make chicken.
So when I run this program you'll see down here it says the chef makes chicken.
And that's because I inherited the make chicken method from the chef class.
But here's one thing that got messed up. If I come over here and I say make special dish.
Remember the Chinese chef special dish was supposed to be orange chicken.
But now the Chinese chef is going to be making barbecue ribs because I inherited this make special dish function from this chef class.
And so what I can actually do is I could come in here and I could override that make special dish class. So I could say death make special dish.
And down here I can just override it. So I could say the chef makes orange chicken.
And now when I go over here and I run this make special dish function.
Now the chef's going to be back to making orange chicken. So the Chinese chef will make his special dish.
So that's basically what inheritance does. It allows me to inherit functionality from an existing class into the new class.
And I can actually just inherit all of it without having to write it out.
So I didn't have to write out make chicken and make salad inside the Chinese class.
But the Chinese chef can still make chicken and salad because it inherited that functionality from the chef class.
In this tutorial I want to talk to you guys about the Python interpreter.
The Python interpreter is basically a little environment that we can use to execute Python commands.
So it's kind of like this little sandbox environment where we can test out and try out different Python commands or different Python functions in a very safe and neutral environment.
So it's kind of like a quick and dirty way to write Python and try different things out.
And the way we use the Python interpreter is by opening up our command prompt.
Now if you're on Windows there's a program called command prompt.
If you're on Mac there's a program called terminal. And basically you need to open that program in order to use the Python interpreter.
So I'm here on a Mac but if you're on a Windows you can essentially do exactly what I'm doing just inside of the command prompt.
So on Mac here I'm just going to search for my terminal.
If you're on Windows you can just search for like CMD or command prompt and it should pop up.
So the terminal is basically an environment on our computer where we can interact with the computer and do different things without a graphical user interface.
So normally when we interact with the computer we're using things like buttons or windows or we can move stuff around.
We can basically just like interact with the computer with graphics but the terminal or the command prompt is a place where we can interact with the computer using text commands.
So you know I'm not going to get too much into what the terminal and the command prompt are but that's essentially what it is.
So inside of here we can use the Python interpreter.
And the Python interpreter is basically just a little program that like I said we can write you know sort of Python in.
So generally all you have to do is go over to your terminal or your command prompt and type in Python 3.
And since we're using Python 3 I'm going to type Python 3 but you can also just type normal Python and I'm pretty sure that gives you Python 2.
Now here's a quick disclaimer on Windows you may run into a problem where you can't use this Python 3 command and it's possible that if you're running into that problem Python.
Like Python 3 hasn't been added to your windows path variable.
And basically what that means is your computer doesn't recognize the command Python 3.
If that's the case all you have to do is just go and Google how to add Python 3 to your windows path variable.
It's really easy I'm not going to get into it in this video just because it's kind of outside the scope but if you're running into that problem that's probably it's not on your windows path variable.
So just Google around for that and you should be able to find an answer.
But I'm going to type Python 3 and click enter and it's going to open up this cool little thing down here.
So you can see it basically just has like here's the version of Python.
Now I'm just going to enter so we can have some like new lines here.
But down here this is the Python interpreter so we can write Python code in here and it's going to work.
So I can for example I could write like print and inside of here I could print like hello world.
And when I click enter you'll see that it prints out hello world.
I could create a variable so for example I could say like num one is equal to ten.
I could say num two is equal to ninety and then I could print out num one plus num two and it's going to be able to do that for me.
I could also use some like a function so I could define a function in here.
We could make a function that's called say hi and it'll take a name parameter.
And you'll see here when it saw I wanted to create a function it put these three little dots and I can indent and we can write the code for our function.
So I can say like print hello plus name.
And now I have a function called say hi so I can break out of this by just entering again and I can call the function so I could say say hi and we'll say hi to me.
Hi Mike so now it's going to say hello Mike so I can use a function I could use something like an if statement you know I could say I could use something like a for loop we could use while loops.
I mean we can basically use all of the basic like Python commands and you know inside of this Python interpreter and like I said before it's essentially just an environment where you can test out Python code.
Now this is not a place where you want to write like any serious Python scripts.
So if you're like designing and writing a Python program I would not recommend doing it inside of this interpreter.
It is it's not a very intuitive environment it's pretty much just set up for some quick and dirty tests like we need to test something out great this is a perfect place for it because you don't have to set up a file.
You don't have to use like some ID you don't have to execute a file you just go in here it's quick it's dirty you get it done.
But if you're writing like an actual program you definitely definitely definitely want to use a text editor.
The text editor it's just going to be way more organized everything is going to be a lot cleaner and you're not going to get confused with like where stuff is and what you have so that's the basics of the Python interpreter.
You know in the course I haven't really been using it at all just because I think it's a lot easier to teach when we're inside of a file than inside of here but you know don't count this guy out the Python interpreter is awesome a lot of people use it and it's great if you just
need to test you know little bits of code out without having to set up some huge environment so Python interpreter definitely awesome I would play around with it you know have some fun and take advantage of it.
Hey thanks for watching if you enjoyed the video please leave a like and subscribe to draft Academy to be the first to know when we release new content.
Also we're always looking to improve so if you have any constructive criticism or questions or anything leave a comment below.
Finally if you're enjoying draft Academy and you want to help us grow head over to draft Academy.com forward slash contribute and invest in our future.
UNLOCK MORE
Sign up free to access premium features
INTERACTIVE VIEWER
Watch the video with synced subtitles, adjustable overlay, and full playback control.
AI SUMMARY
Get an instant AI-generated summary of the video content, key points, and takeaways.
TRANSLATE
Translate the transcript to 100+ languages with one click. Download in any format.
MIND MAP
Visualize the transcript as an interactive mind map. Understand structure at a glance.
CHAT WITH TRANSCRIPT
Ask questions about the video content. Get answers powered by AI directly from the transcript.
GET MORE FROM YOUR TRANSCRIPTS
Sign up for free and unlock interactive viewer, AI summaries, translations, mind maps, and more. No credit card required.