in this course I will teach you everything you need to know to start programming
python.
Python is one of the most popular programming languages,
and he is one of the most sought after jobs
and so if you are trying to find a job
or are you trying to know
automate your life
or try writing great scripts to do a bunch of different things from
python is for you
honestly
more and more developers are switching their projects to python every day because it's so powerful
and the surgeon
easy to use language
many programming languages ​​out there just aren't very beginners
a lot of syntax has a lot like little things that if you get confused the program will call you.
python is complete
on the contrary.
basically just write what you want to do in python does that it's just that it doesn't
a whole lot of syntax to learn the learning curve is literally zero.
you can start writing your first program in seconds.
in this course
I'll teach you guys.
everything you need to know to get started in python.
I created this course
especially for python,
and each lesson is specially designed with examples
this will help you along the way.
with so many people starting to learn python, the question is not,
why do you need to learn the trumpet on the question is why not?
and I think for many people the reason they don't want to learn python is that they are scared or afraid it will be too difficult.
believe me, i will hold your hand throughout this course, we will talk about all the basic concepts in python that we will look at
everything you need to know
start programming in python and start being confident, write scripts and start writing programs that are great at doing cool things in your life.
anyway
I'm super pumped to teach you python boys.
I can't wait to start this course
and I hope you guys stick around and fall along with the course and learn this amazing programming language
This tutorial is dominated by three guys on how to install python on your computer.
and we will also install a text editor that we can use for writing
our python programs
inches
so the first business order is actually to install
python on your computer.
so we want to make a transfer to your web browser
and
you want to go here, this page is simple
www dot python dot org
front slope
downloads
and on that
page there
will be
two buttons here
and it will say download python three point six point three.
and at least
the version i have at the moment or download python two points seven point one four.
so here's something about python there are actually two basic versions of python
which are used
at present
there is
python two
and there are theories about pythons that you can see here,
it's like two point seven point one four
basically every python two
a version we would simply call python two
and here's the latest
python three version.
and so there are these two versions that you can download and the first thing we need to decide is which version we want to download.
and here is the main difference
python
two
is an inherited version of python, which actually means that
this is like
old version and is not as actively supported or supported
as officially already by python.
python
three is like the future of python, it's like the latest version, it's the one that is actively maintained and maintained.
so there are pros and cons to choosing a win or
many people also say like a python
is around and longer, so there's more
libraries actually have just more python code written in python two
so, for example, there are a few more things you can do in a tube then you can do in python three
but then again
python 2 is not supported and you know
five or ten years from now
probably no one will use it.
python three as a site is like the future of python, this is the latest version, this is it
you know it's kind of keeping going.
so for the purposes of this tutorial i will teach you guys
python
three
so we will want to download python
three
now it is unclear here
if you learn python three or learn python the differences are not very big.
there are a few differences as in some syntax and just like that
you know little things here and there,
but if you learn python three, you will be able to jump right in
code in python 2 also don't let anyone
let me tell you this
you have to learn from each other.
we will learn python three, just because I think it's probably best for a beginner to learn.
and this is the one we want to download so just click to download python three
and we will continue to download
this version.
and when the download is complete, I will head down to my download folder
and you will see here, we just have this file
double click
and we get this as a python installer, so click here,
and we will be able to install python three on our computer
just when it's done and we put it off, we can just end it and we have to start getting us out now we have python three
installed on our computer.
the next step
do we have to choose text
editor so
we're going to write a bunch of code, so we're going to need an environment that you know in some program where we can write all that code.
and you can really write python in any text editor where you can write it as a notebook or a tax on it,
actually it does not matter
but there are special ones
text editors that are designed
just for writing python code.
and these are called
id ease and does not mean
Integrated development environment.
in fact, it's just a special environment where we can know the ron and execute our python code.
and you will know basically by telling us how we do it
only if we write something that is wrong or we
there are bugs you will see
point us in the right direction what we need to do to fix these things,
so in this course we will use an idea
and
one of my favourite
and one of the most popular amenities for python is called pi charm.
so i'm here on this website.
is called
reactive brains
dotcom
front slope
pi charm.
and this is the idea we will use in this course
so i'll just come here and click on this great download
now button
and we will move forward and
download this
pie tea program
right, i get this page are here, you can see there are two versions of pi charm
one as a professional version and this one is as paid, they have to pay for it.
but then here is this version of the community here which is free and open source so i will download this
and this should have everything we need to start using python
just when this download is over, i just go
drag this to my download folder
and
again we can just sort
let this man go
and i'm on mac so i will have to move it to my applications folder.
well, so now there is pi charm
and we have a python
three installed on your computer.
so we're ready to start programming in
python.
in this lesson we will create our first
python program.
and we will be able to do it and
see how everything works.
the first thing I want to do is open up
pi charm, so the charm charm was this
idea he
Integrated development environment
that we downloaded in the last lesson, so
I'll just go out and look it up on my computer
and I can just open it
and first we opened a high charm, we actually just need to get a window
which prompts us to create a project.
so you can see this down here
in fact the first thing I want to do is come here to configure
and I just go
click on preferences.
and I actually want to
change of appearance
so
I will move on and
give this topic so i just went here for looks and behavior
and will change the subject to
darker color
because I think it's a little easier
use and look,
but you don't have to do it if you don't want to
so
down here
I'll just click
creating new projects
and it happens
give us this little window here so I can just name my project.
and I'll just name that
graphics.
and then there is another option here where it says translator.
now
what we want to choose
is
version three of python
so if you click here, you will notice that there are several different options.
so i'm on a mac computer
and
by default mac has
python
version 2 installed on it.
if you're on a Windows machine, I'm not sure if there is, but
in this tutorial we will use python three, so you want to make sure you have python
three selected as translators,
otherwise
you may not be able to fully follow along with what we are doing in this video.
so now come here and click
create.
and this will continue and we will create our projects
about us
that's exactly what we have now
our
python project
up and running,
and I'm sure, boys, how can we create our first
python program,
so here in this draft folder and this is exactly the project we created.
You will just right click
and I say new
and I will go 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 that name,
so why not just call it that
ap
and you will click ok
ok 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.
makes a very basic python program.
and it's just going to be a robust global program, so we're just going to print something on the screen.
so when i say
what i want to do is i want to write pr iyer anti prince
and I wanted to have
open and closed
brackets
now
inside these brackets we can make some
quotes
and I'll just stick tape inside the quotes
Hello
world.
so it's like a very basic python program.
and what I will do is
you will just print a hello world
on the screen.
and so now what can i do
can i actually run this file and we actually get a little arrow here it wants to create a new line
at the end of the file.
and so now to start our python program, i'll just get here to the top
I'm at the movies too
and we will click on this play button here
and I will see what happens
that's right
ask me what i want to run, i can just click add
and
at the bottom you will notice that we have this text that has just been printed
Hello world
so
whatever I am
seal
or whatever I put inside this press release will be printed
down in the board so
as long as you get the print reports working, then you do
actually wrote his first python program.
and as we pass through it
forward in the course can be writing
all sorts of great python programs.
and we will do
all sorts of cool stuff
in this tutorial i want to talk to you about writing basic python programs so we will write it as a very simple python program.
we will look at how we can move forward and write programs that we will talk about
how our programs run from python
and we will also draw a little shape on the screen so that
it's going to be pretty cool, which excites me
talk to boys about just that
bare python bases,
and it really just presents you with an introduction
here
we have ours
basic text editor and so on
someone from
python code that I write inside this file
will actually be performed by a python, so
this python file
there are a bunch
python code inside it.
and actually what we do when we are
programming in python is that we just give the computer a set of instructions.
That's right, this is really what programming is, it's just like we define a set of instructions
and the computer will come out and follow these instructions we give it.
and so
the more complex the instructions we give to the computer, the more complex they are
tasks
the computer can
carry
so i will show you guys how we can use some python instructions or some python code
to draw a shape on the screen so
I'll actually take it out
small triangle.
so here
we can actually print something on
screen and python, using something called printing so I can just write
seal.
and inside these brackets.
I can just write
some quotes
and inside the quotes
we can
put what we want to print on the screen.
so i will go
copy this and i will actually do
several of these statements of the little prince
and we will actually draw a sheep.
so
we will draw as
triangle shape
so down here it will draw a front line
and then above I will make a space and we will make another foreign wings
here we will do
two intervals
with a unified front slope.
and here
we can make three spaces and afford wings.
and now i will draw so that the vertical bar goes down.
and we can actually make some emphasis here as well.
I think high school
that's right now we have our triangle looks shaving and you can see
looks like a right triangle
so i can save my file.
and I can either come here to run and
click launch the application
or use the magic pie, you can also just come here and press this play button
and when I click the play button, I want you to notice what's going on down here
so
below here we have this little window and
this is called a consul
and basically the tip is where the python goes
display some information like this
when we used this press release, we basically said python late
hey, can you print something for the consul.
so
there are many situations in which we want to see what happens in our code.
and we may use this press release and
print things out to the board.
and we can generally have a small window into what our programs do
in our case we will just print this triangle on the consul.
so i will come here and press this play button
and you will see below
we are actually printing this
triangle
so that the mission was accomplished, we were able to print our form.
so I can really draw any shape I want, as long as I've been
able to
specify
inside these press releases.
so
essentially what does python do when it clicks the play button
enters this file and looks at all these instructions in order.
so the first thing it does is go to this instruction and it says
Okay,
they want to subtract that small slope in that vertical band on the voltage,
after doing so, it comes down to this instruction that says
Okay
they want me to print something again
when you print a dot, it comes down to this instruction
etc.
so
python will actually execute the lines of code we write
in order.
so
the order in which I write the instructions
it matters a lot.
for example, if I want to take the last line here
and put it up here.
now when i save the file.
instead of just drawing this triangle.
will be downloaded
this little upside down
bottom something on 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 will run.
as we go through this course, we will learn more and more from these little lines, so here we just use this little print feature in general and just print something on the screen,
there are a bunch of things we can learn as we go through
we will learn more and more instructions that we can give to the computer.
but in this tutorial I just wanted to show you the basics correctly.
Oh
we can essentially just define these little instructions for the computer
and the computer will execute them
in order.
and this tutorial i want to talk to you about using variables in
python.
now in python and you will be dealing
a lot of data
and so many times in our program that we will work with
all types of information and data and values,
and sometimes this data
can be difficult to manage.
so in python we have this thing called variable.
and it's actually just a container
where we can store certain
data values.
and when we use a variable, when we set them
data values ​​inside the containers.
that makes it a lot
easier for us to work and manage
all different
data inside our programs.
so variables are extremely useful and it's kind of like
main topic in
python that you will really want to know and master.
so in this tutorial each of you omelets.
everything you need to know to start with variables, we will look at what they are
why they are useful and we will look at the different types of variables
and different species
data that we can store
inside the variables,
which here in my
python file I just have a very simple program.
and it's actually just printing some text on the screen.
and so this is actually a little story that I wrote that he wrote.
there used to be a man named George
he was seventy years old.
he really liked the name George
but he didn't like being seventy.
so this is a valid python program that i can go ahead and
run this program
and you'll see down here in the board
everything prints and
so it looks great
here we have a great little python program.
let's say that inside our story I wanted to change
the name of the hero
so instead of naming the character George, let's say I wanted to name the character
Jon
good
I will have to go through and have to manually change the name George to the name John at every point in this story where it is mentioned.
so I will not go here and say
Okay, John will have to change that manually
and then
OK, this is here, we will re-enter manually in john and change it to John.
so to change the character's name, I had to manually log in and change it in both places.
and now let's say
Okay
maybe we want to change the characters
age also in addition to the name change.
we also want to change the age, let's make him a little younger, so why not say that John will be thirty-five.
so you see here I had to come here and manually change the age and then go down here
and change it manually again
right.
and now that we have changed it that way
will work, will be updated in our program.
but you will notice that in order to make this change so I had to look at the whole program, find it where
value was and you changed it,
and
it was just with
four lines
from the story, imagine if I had a story that was like
thousands of rows long.
and we mentioned that the name of the character and
age as
hundreds of times.
we don't need to look at each of these lines and
manually change the character's name
and that's not a very good way to do it
and
this is really not a good way to manage the data in our program exactly
name of the character in the age.
and so we can actually use a variable
for storage purposes
the name of the character and the characters.
age.
and when we use this variable, it will do a lot
it is easier to put the name and age of the character.
inside our little program here.
so i'll show you guys how we can create a variable for the character's name and age.
will now climb here above this press release.
and
above i want to create a variable.
so when we create a variable in
python, which we actually have to give
python
some information.
the first information we need to give python is the name of the variable, so I actually need to name it
in this container where we will store information.
so you just have to write
the name of the variable
that I want to create, so I will create
a variable called a symbol name.
so i'll just say
character
Adam, they emphasize
name
so usually when you create a name
for a variable in python.
you want to separate different words
with underlining, so I have two words here and I separate them with underlining.
and now I have to do what I have to do
set value
inside this much more
in fact, what I can do is just say
equal to
and here I can write a value
so i'll just write the name of the character from
quotes.
so we'll just write John.
so now we have a variable for the character's name
and under this symbol with a variable name I want to create another variable called
character
a certain age will just go to the bar
character
underscore
age.
and I will do that
equal to the age of the characters, which is thirty-five.
so now we have two variables,
one that represents the name of the character and one that represents the characters
age.
so what can i do now as i can replace
the name of the character in this story
s
this variable.
and I'll show you how to do it
so if we want to put a variable inside this press release.
In fact, I will have to do a few things.
the first thing i will have to do is
complete this
text here, so I'll have to put
a
quotes here end
and you can see that I am basically
wrapping the whole thing in a
single as a quoted line.
and now i also have this tax eavesdropping end so i will have to put a quote here.
so now i have
a
bunch of tax year in quotes.
and then I also have tax here in quotes
now inside here
instead
says the name of the hero
I want to refer to the name of this symbol
variable
so i'll just say plus
character
underline name
and I want to say one more plus sign, so basically I say I want to
print all this text,
plus
the value stored in the symbol name
plus
all this text.
and what we're going to do is that they're actually going to print the name of the character we're printing this
so i will go ahead and run this program
and you will see below
and actually let me put a place here,
and we will release it again.
so you can see here
we still print a man's name at the time
John.
and so what python does is when it executes this line of code
comes here and sees
OK they want to turn out.
the value inside the symbol name
that's right
python will go here, get the value,
and it happens
put it inside
so basically I can do this for each instance of the character name in my file so I can
you know
r is copying this person here
and
I can just stick it here where the characters' names are
so that's enough
and now you can see exactly what we did up there.
I can do the same for the characters
age someone to come down here
and I'll insert what we just put it in.
and i will change this instead of the symbol name was changed to a symbol
age.
and I will do that
in another spot we will do this here, we mention the age
and again I will just change that
I wear your age.
and so now that I'm running this program, I'm going to print the same story they printed before,
but
now you don't really have to write the name of the character and the age of the characters.
I can just refer to that
variable.
and cool about the variables, like I want to change the character's name.
now all i have to do is change it here so i can change the names of the characters you like
Tom or something.
and we could also change their age so that I can change
age to be like fifty
and now this will be automatically updated throughout our history
when you click this play button, you will see
we now use the name tom
and he is fifty years old.
so variables can be useful to you as well
this can be a really great way to control and manage
the data that is inside our programs
with variables you can too
change their values
so
for example, to say that in the middle of the story I wanted to change the name of the character
why can new lines just be made here,
and down here
In fact, I can appoint a new one
value for one of these variables.
so i can say like
character
underline name,
and to give it a different value, I just have to say it is equal
and will give it a different value and so on
to tell half the story we want to change the name of Mike's character
one now as I run my program
you will see here in the first part of the story that tells
there used to be a man named
Tom.
and in the second part of the story,
refers to the name as
Mike.
in fact
update the variable
inside my program
here
and it's a really great way
you know many times in these programs that you will want to change the values ​​of the variables
so you can do it
just like that.
and finally I want to talk to you about the different ones
types of data that we can store inside these variables.
right here
I have the name and character of the character
and
I actually store so-called strings,
so the string is actually simple
ordinary tax straight
so the name tom is just plain text
down here these guys are strings too.
so there is a data type in python,
called a string and it's simple
will basically be stored
plain text
so
you know any text you might like in the story you know someone's name early on
you can store inside a string.
there is another type of data
that we can store inside a python
which is numbers
so additional text storage that we can also store
numbers.
so if i wanted to start a number for example
let's say we wanted to keep the age of the characters instead of inside a string.
we can store it inside a number so that
I could just write fifty.
and when we store a number, we don't need those quotes
you only need this quotes
when I store a string, so if I wanted a number, I just could
write it like this
and in addition to using integers, I can also use decimal numbers.
so I could say like fifty point five six seven eight
to one three
and pythons will be able to store
and this number.
you can store all different types of numbers
and we can store
what is called a Boolean value
and a Boolean value is essentially a true or false value.
and there are many cases in programming where we will want to present ourselves
truth or lie
data
for example I can have a variable called like
his mail
and that will tell me if someone is a man or not,
and in my case it will be true, because I am the right person to be able to
in fact it should be
capital
so
I could sort the value of any truth
or
incorrect.
and actually, if we called it in python
we want to use underlining or so I would say
is an underlining mail
and this can be either true or false
so
true or false values ​​may not be something you are used to dealing with in the real world
but
we will use true or false values ​​in programming
they are super important all the time
like i said, there are three main types of data we can work with in python
strings, which is like plain text,
numbers that would be as decimal numbers or integers like this,
and there is true or false
values.
and these are the main types of data, I mean, there are a bunch of other types of data that we can use, but I'd say ninety-nine percent of the time.
as a new user
in a new python programmer,
you will just deal with them
three types of data
so
these are the basics
from the data
and
also with variables
and you will use variables
all the time in python, so you definitely want to train and feel comfortable
using them
in this tutorial are conversations, boys, about working with strings in python.
now what the most common species
the data we will be working with in python will be string
and the strings are basically fair
plain text, so any text
which we want to have in our program
we can store inside a string.
so i want to talk to you guys about all the cool things we can do with strings and basically just take
a complete introduction to why strings are great.
so here
I'll just print it out actually
strings that will just say print
and
inside these brackets.
I can write a string
To create a string, I need to use quotes so I can do it
open and closed quotes
just like that
now
inside
quote I can put any text
I want the string to be there so we can say like
slop
academy.
and so now we have a string
with the text
the draft academy in it
so if i run my program.
now here and there we're going to print a draft academy, as you can see,
and when we have these strings, there are actually a bunch of cool things we can do with them like that
one thing I could do is create a new row inside the string, so if I wanted I could come here,
and I could keep the slope
n
and you can see that it is highlighted in a different color.
and what I'm going to do is actually insert a new row into the string, so now it's going to say draf a row
and an academy on the other line.
so you will click to play.
and you'll see here, we just get a draft academy,
addition to the slope of the back line
n
I could also use
reverse tilt
quotes so
if I wanted to put a quote in a string.
I can't just put a quote like that
because the pythons will think I'm trying to finish
this force.
so if i want to include a quote, i can just use it
special character of the reverse line
and this is called the character of the escape and that
actually just telling python this
whatever character comes after him that we want
do literally so
when I say the opposite line
quotes.
actually means like
her python
I want to quote.
and so we will now be able to print
from quotation marks
right there.
I can also use it to print a slash, so if I have to put a backslash.
I can see the opposite line,
and
it will just print a normal back line now
so you can see just like that
if you want and can use this slash
new lines or quotes,
or you can just use it as normal
slash
besides just entering a string here I could also create a string
variable.
so I can get down here
up here and we can create a variable and let's call it
phrase.
and I will adjust it
equal to
dropped out of the colony,
I can
store this string value in a variable called a phrase
and then when I want to print
outside this variable,
or I want to access this variable on a string.
I can just
enter the name of
the variable.
and you will see that it will print the value that was stored inside it.
I can also use something called concatenation
and binding is the basis of the process of making a string and
add another string to it.
so I can come here and say a phrase,
and I can say
plus.
and now i can add in another string so i can say like
is cool.
and now it will say
druf academy
is
cool,
so I'm basically adding another string
on another, which they call this compound.
and so in addition to doing all these things, we can also use a special thing called
functions
and the function is actually just a small block of code that we can run
and it will perform specifically
operation for us,
so we can use functions to change our strings.
we can also use functions to get information about our strings.
so
I'll show you guys some common features we can use with 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 gonna show you guys a function that we can use to convert this
string entirely into lowercase 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 will convert it to lower case.
so you can see now we just have a draft academy done here but it's entirely in the lower case.
I could also do the same thing for the upper case so I could say phrase dot
upper
and this will convert the entire string into
upper case so you can see now it's
entirely upper case.
so in addition to
converting the string into upper case
lower case
I can also
check to see if a string is entirely upper case are entirely lowercase.
so for example I could say phrase dot
is upper.
and this is going to give me back a true or false value
so it's going to be true
if the string is entirely upper case
or false
if it's not.
and you can see here we're getting a false value because this
is an
upper case
I can also use these functions in combination with each other so for example,
I could say phrase
dot
upper
and that a
parentheses
and then after this I can 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 upper case.
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 an upper case.
so you can see now we're getting a true value so
the news 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 close parentheses.
so I'm essentially saying
alien and this is another function it's the length function.
and inside of this alien function I'm actually passing
this
phrase variable
basically giving the length function this and it's going to spit out a number so tell me how many
characters are inside of this string.
as you can see here we get fifteen because there are fifteen characters inside of draff academy.
so that's how you can get the whole length of the string and that's going to be really useful as we go forward in 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 the string is
we're going to actually use
and open a closed
square bracket
just like that.
and in here I can specify the
index of the character that I want to graph.
so if I wanted to grab the first character in the 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.
that's because in python.
when we use indexes on a string we start with zero
so if python is counting the characters
or its indexing the characters in a string it's going to start with zero so it's going to say
g is zero
I as one are as to
a history.
so we would say that g is at position
zero in the string.
I is at position one
r is at position two
a's 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
no,
I'm going to have to put
zero
one
two three so I put three inside of here
now will 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 gonna be using it
that a lot as we go forward and python
is 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 strings like unsafe 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
of 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 can say inside of here
capitol g,
and now this is going to return
the index
of
the capitol 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 in
a inside it here like the lower case
a
it should give me zero one two three
because that's where the first
aid 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
aca 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
a
because
that's where
a cat and he starts right
zero one two three four five six seven
eight
if I put something in here that wasn't in the strings of for example if I put az 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
the z is not found inside of our program so
that the next function again can be really useful we'll be using that a lot.
there's also one more than I want to show you which
is called replace
so we can say phrase not replace
and in here I can actually give this
two parameters so I can give this replace function
two 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
giraffe.
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 draff 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 springs with
other ones.
so those are 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 in use and you know,
see what they do,
but you definitely want to get comfortable working with strings and python because you're going to be working with them
a lot.
in this tutorial I wanna talk to you about working with numbers in
python
and numbers are one 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 the basics of using numbers we can 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 number so we can do a certain like mathematical operations mechanism
awesome things
with numbers I'm going to show you guys
all of that in this video it's gonna 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
too.
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 can say like two point
oh nine eight seven
and pythons are gonna be just fine with that we'll be able to print that on
the screen as well.
I can also make numbers
negative so I could make this
two point
oh nine
eight seven
a negative number and
python to be able to handle that just fine too.
in python we can also use
basic arithmetic so I could do things like addition subtraction multiplication
fancy like
three
plus four
and now this is going to print this out or was he like three plus
four point five.
and this will be able to print out the answer so we get seven point five.
and we can do a dish in 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 is gonna do is it's gonna multiply three and four together so it's going to be twelve and then we'll add five to that so we should get seventeen.
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 social ad foreign fighters will get nine and will multiply that times three so we should end up with twenty seven,
so you can see we can use those parentheses to specify order of operations so I could make like a very complex
little equation here and just by using numbers and parentheses.
we can also
use one other operation which is called the modulates operator so I could say like
ted,
and use this percent sign
three and this is actually read
ten
mod
three
and basically what this is gonna 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
ten divided by three.
that's going to be three
with a remainder of one.
so this is going to give us the remainder.
and
you'll see down here we get one because
that's the remainder of ten divided by three
that modulates 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
we underscored gnome
and we'll just store a value
of let's say like five and here
is why have you come down here and I can just print out my underscore numb.
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 strings 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
we
favorite number,
and we'll be able to print this
number along with this
string so I click play,
and it's as 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 pythons gonna 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 gotta 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 gonna want to be using specific
metabolic more advanced math operator 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,
we can also give us information about our number,
so I'm sure 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 going to
make this variable negative five
and basically I can just say
abs
and I can make an
open enclosing 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 is another one
which is called
pau at so it says pow
and I'm actually not going to use this variable we just as a normal number
and
this function is going to allow us to pass it
to 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 two 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 can raise it to the power of six,
and so we should get like a pretty big number here
and you can see we get four thousand ninety six so
this
power function is really useful for taking numbers to specific
powers.
so in addition to using this power function we can also use another function which is called
max.
so I can say max.
and what this is gonna do is basically gonna 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 pulling out six because that's the bigger number.
I cannot see is another function called
men 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 turn out for 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 three point to inside of here
now is going around it
down to just normal three
but if I said like three point seven.
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 gonna 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 the zipper 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 other so
is another function which is called the floor method and it's just
flor
and what this will do is it'll basically just grab
on
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 around the number up
no matter what so
no matter what will always get for
which we have a three point whatever here
and there's another one which is called square root so it's just asking you our team
and essentially this is just going to return the square root of a number so I can say like
thirty six.
and now we should get six back.
so
those are also 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 is right now,
just know that when we put this line of code into our program
it gives us access to a lot more
mathematics
function so though
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 why 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
your 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 has been a little bit of by using numbers a little bit about doing it on certain math operations and using functions
with those numbers
in this tutorial I'm going to show you how to get
input from the 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 gonna 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 in 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 will store the name inside of a variable and then we'll basically just
say hi to the user so we'll say like high
and then whatever the name they entered was
so what I want to do is I actually want to store
whatever the user and put it 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 it.
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 want to put an exclamation point.
so essentially we're getting a name from the user.
restoring it inside of the named variable
and I'm printing it
out onto the screen.
so I'm gonna go ahead and play this?
and when we run the program you'll notice down here
python is giving us this little problem
so it saying
enter your name.
now
in here I can actually type in of values ​​so I could type in might.
and then I can just hit enter
and pythons going to go ahead and 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 can actually
copy this line of code.
and I'm an
pasted 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.
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 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 as they enter your name.
so
we could just
make up a name like john
and then enter the age that say john is twenty five
and now is going to say hello john you are twenty five.
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,
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 word just basically getting to 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 to numbers from a user,
and then we'll
add those numbers together and print the answer out onto the screen.
so this will give us 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,
number 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 piece this guy down here,
and I'm just going to say gnome
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 results so
I'm actually going to create a variable called result somebody's going to say a result
is equal to
and basically we just want to add these two numbers together something as a number one
plus
numb
too.
and now all we have to do is print out the results 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 could play.
and down here we can enter our number,
so I'm gonna enter the first number we'll say five,
and I'm going to enter my second number so let's say
eight point zero
or else a point
three
it's now should print out the answer,
but
if you look over here.
that's not quite the answer we were looking for at five plus
eight point
three is definitely not fifty eight points three
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 their when we're entering the numbers
python's is always 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 number one and number
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 I auntie.
I can make an
open parentheses and I can make a close parentheses
and basically what this is gonna do is it's going to convert whatever is inside of these
open or close parentheses
into an
integer
number,
and an
integer number is basically a whole number so it's like one two three four or five so it's a,
it's a whole number another word that can't have a decimal point,
so what I could do is I could say
I nt,
and I could put these
inside of
parentheses.
and now what this is gonna 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 will print out the result so why don't we do that
we'll just see if this works I'm gonna
click the play button
and down here we'll enter our first number sums can enter into four
and then I'll answer 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 salmon 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 four point three and five point five.
you'll see that we're getting this error.
and that's because I'm using this
ain't
function of 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 programs.
there's another function we can use
instead of an
n 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 number so I can click play.
and I can just enter into number so I could say like four
and I could also say like five point five.
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 into an integer
to a whole number.
but in a lot of cases like in this calculator.
we want them to be able to answer any number that they want so we're going to use this flow
function.
so
I'm just going to recap what we did one more time we created two variables number one and number two
and we basically stored the two numbers that the user's implanted into those variables,
then we converted them into numbers
and
weed
out of 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 wanna show you guys how to build a
mad libs game in
python,
so a mad libs 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 I had
my web browser I just have
an example of a mad libs so you know
down here were basically just taking random words like an adjective or a noun or a pronoun
and entering them in
sort of a story so like you read through the story than like some random word pops up in
mad libs can be pretty fun and I'm sure <? ..>
yes he's heard of mad libs before,
but if not, now it is a little introduction.
so I want to create a mad libs in python
and this is going to be really cool so
down here I have this, like,
a little poem which says:
roses are red \ violets are blue \ I love you.
and so y'know, this is a compelling classic poem
but
I think we should mix it up a little bit and create a mad lib for this poem.
and so
instead of having this red color over here,
I want to have the user answer in
their own color
instead of having violets,
why don't we have them answer 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; it wants you basically have the user answering a color
enter in a plural noun.
an answer in a celebrity,
and 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 as we should create three variables
called color
plural noun in celebrity
and will store
the user's
inputs.
inside of those
variables.
so up here let's create those variables
the first very well I'm going to create is going to be called
color
so it's a color.
and I want to set this
equal to
on
inputs are going to say input
and inside of here I was going to give them a prompts all say answer 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
very also I'll say peoria
pluralism now
and want to say answer a
plural
now
and actually over here I want a meager
coal as well.
so then finally we'll create our
celebrity variables they enter a
celebrity and
so now basically we're creating three variables called
color plural noun in celebrity
and inside of them were storing the color that the user enters
the plural noun the user enters and the celebrity these adventures.
so
the 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 what is adding that celebrity.
so
now we have a prequel mallet
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 council.
we can play are a little mad libs games so
they enter a color I'm just going to say.
magenta
enter a plural now when we say,
microwaves
and enter a celebrity you at say Tom Hanks.
so
when I click enter what should happen is we should get our mad libs
printed out some
good enter
and now we have our methods was as roses are magenta
microwaves are blue
I love Tom Hanks.
so it's a pretty good madlib
and obviously you know,
the more,
the more print statements we put over here you know
the better the madlib could be and we could store as many, y'know, variables up here as we wanted.
this was just a very simple madlib we only took in
three inputs but you know you could 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 madlib 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 on its artsy 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 the 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 less than you would put a bunch of
related values
inside of that list and then you can use it throughout your program so
I'm actually eyes are 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 might we create a python
it's like a normal python variable so
the first thing we want to do and 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 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
opening closed square brackets.
so whenever you use these open and closed
square brackets python basically nose
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
tavern,
karen
and say jim.
so here I have three values
inside of my friend's array.
so when we're creating a normal variable in python generally we just give it one value
right now to create a variable
I would give it
one value may be a string or a number or a boolean.
but when we make lists
were 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 were 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 wouldn't matter
python's going to be fine with that.
let's change it 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 going to 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
where 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 started 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 at index position
one.
and so that is kind of how this 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 referred to it by its
index.
so over here after which I bought friends I'm just going to tape an
open and closed square bracket,
and inside
this open and close square bracket
I want to put the
index
of the element
inside the list
that I want to access
so fallen 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 going to print out that value it's just going to pronounce
cabin,
if only to access this
gem 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 going to give me access to this
value right here for jim.
and you can see that's what it does so when we use negatives it starts indexing from the back of the list so if I start at negative two,
this is going to give us
karen
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 an
interesting
we said 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 ones are
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 us like the first one
will 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 describe
jim and karen
or carrying gym in that order.
I could also specify a range.
so actually let me out in a couple other elements,
just so that we can illustrate this better.
so are in two more names.
so if I wanted to I could select like
the element and exposition one
up to the element at index position three so
it's going to grab
this one says one,
two
and it's actually not going to grab the third one so it's going to grab all the elements
up to but not including three so it'll grab
karen and
jim,
let's go ahead and run this and you can see it grabs canon japan so
you can either just put like
one with a colon after it and it'll grab that
in exposition in everything after it or even specify like
arrange 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
we'll all you have to do is just access that elements I could say
friends
and
karen is that at position of one
and I could change this to whatever I want so I could say friends
in that position one is equal to
mike?
and now
when we print out friends
and at 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 monastery guys
even more ways
that we can use these less we're going to learn how to link
add elements to a less delete elements from a less
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 wanna 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 my program you can see I have
two less 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 this awesome is
not only can 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 and python bees 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
when a warm
and when I 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 in 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 wanted to add onto 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 our friends,
and we also have
the numbers overhears 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 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 not a pen 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 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 volume kelly
and all of these other values ​​are going to get pushed
up
they are going
get pushed to the right
one index position so
now I run the program,
you can see we have kavin
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
jim value.
all I have to do is just a friend's dot remove.
and
I can just typing
whatever element I want to remove so
I want to remove
this jim element
is a 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 less than get rid of
everything I could just say friends thought clear.
and this is going to
give us an empty list so get rid of
every single element inside the list.
there's also another really cool
nothing which is called
pop
and you can just take out pop like that
and basically what pops is gonna 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 lists of easily 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.
why could say friends
dot
index
and in here
I can just pass
a value so I can say like
kevin.
and this is going to tell me the index
of
caverns are the tell me if
cabins in the list,
so I could play
you can see down here it says
zero
if I was to say like
oscar and here for example
now would 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
doth not in the list.
and so now we're going to get back
an error
because it's as
mike is not in the list
so that's an easy ways you figure out if a specific element inside of the list.
and if it is,
then you know what index its app,
you can also count the number of similar elements in the list so
I'm actually going to come down here copy this
gem element
and will add another
jim element into here
so I could come down here and I could say friends
dot
count
and
will pass in
jim
and this will tell me how many times the value
jim
shows up
inside of this
list.
so it should say to which it does down there because
we have two
values ​​with
on
text
jim
and so we have all these values ​​we could also sort this list so
I could come over here and I could say
friends.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 gonna put it in alphabetical order are so
you see we have jk
o and t so
the list went from being just random order like whatever we have up here to now it's in alphabetical order.
and that's also going to work for lucky numbers so I could say late
lucky numbers
dot
sort and then we'll turn out lucky numbers
and they should now be in ascending order,
which I'm realizing they already are up here but if I
was to take this forty two 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 not
lucky numbers list I could say
lucky numbers dot
reverse.
and now when we printed out instead of being forty to eight fifteen sixteen twenty three.
it's going to be twenty three sixteen fifteen eight forty two,
so it's basically just reverse the order of
the list.
and finally there's one more
or less functional monetary eyes which is copy.
so
I can actually create
another list.
and I could
make it as a copy so I could say like
friends 2.
and we could set this equal to
friends
dot
copy
and now friends
too is going to have all the same
attributes as friends,
because it's basically just like
copy 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 situation so
those are just some basic list functions and just to kind of like
you know get your feet wet in 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 a sartorial
talk to you guys bought tuples in
python.
now a tupel 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 tupel is actually very similar to a list.
it's basically a structure where we can store
multiple pieces of
information,
but
a tupel 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 though how to
create a tupel.
so I can come down here and I can create a tupel
and one of the most
Common
examples of tupels his coordinates.
so let's say I had a series of like xy coordinates I could create a variable
called coordinates
and I'm gonna set this
equal to
an open and close parentheses,
and whenever we want to create a tupel inside of
python we're going to use these
open closed parentheses
and
inside of here inside these proceeds we're gonna 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 cannot be changed or modified.
once we create our tuple up here.
you cannot modify it
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 its
it does it as it can't change
so I can 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 and closed square bracket.
and in here I can pass in
a
index
so troubles are indexed
starting at zero so technically we would say that four
is at index position one
and for -
sorry
it is in position zero
and five of them
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 is at
each entry.
but
if I was to come over here and try to change
one of the elements so if I said coordinates
and
coordinate one and I tried to set it equal to something else like
ten
python's actually going to give us an
error when we run this programs often run the program.
you see we get this error it says
type
air
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 couple like I said it's very simple you just created and then you have,
um.
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 one is to create a less 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 steal stored dna 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 couple 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 tunnels.
so I can say like
four five
six seven,
maybe we have some other coordinates like
eighty
thirty four.
so here I have in uh
I have a list
and inside of it we have these
tunnels.
but remember these topples 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.
topples or more of a niche
you know
they're using more special situations.
but
there are plenty of 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
to <...> some basics about tuples some basics about the difference between
lists and tuples
and so a tuple is a great thing to add in c or
python
repertoire
in this tutorial I wanna talk to you guys are by using
functions in python.
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 could 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'll show 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 as 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 "def" of
def
and
basically when
python sees this keyword it's going to say
good
this person wants to use a function.
so after we type out "def"
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 gonna
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 high with no
underscore or I could type out say high with an underscore.
both are considered like
good practices and python and 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 can put an underscore there
if we wanted.
after we type out the name of the function
I'm just going to type into open and closed parentheses.
and
then I'm going to type
a colon
and basically what this is doing is it saying to python lake
or eye
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 sold
over here.
you'll notice that when I clicked enter my
text editor automatically
indented attacks so it's automatically like
using this indent here
and that
having one of the rules and 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
invented
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 is a little thing any code inside this function needs to be indented
right so our function is just going to say hi to the user so I'm 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 gonna 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 of 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 a function,
you basically just type out the functions name and those open a close parentheses.
so I'm just gonna type out
say hi
and opening close parentheses.
and
now when we are,
when we run this program again,
you'll see that
it prints out
hello user so were 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 actively work so
up here I'm going to print top
and then down here
I'm going to print
bottom.
so I wanna show you guys the flow of
these functions inside the program so when I
run this program
you will see
we print out top
hello user
and then bada.
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
ok we want to execute the say high function
so python actually jumps
up
and it goes over this say high 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 and the function,
it's going to jump back
down here and it's going to move onto the next line which is bottom so that's how like the flow of functions,
again with functions generally when we're
naming these functions
and
you want them to be named
in all lower case,
and
usually when we're naming stuff in python
if there's two or more words we're going to use an
under space
or an underscore in between them.
so I can write this out as say under score high.
but
in a lot of situations though if I have a function like this where the name is really short
and might just be easier to leave it
without an underscore but
why we've spent on scoring now 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 gonna 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 has 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 high 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 gonna do is it's just going to say hello to whatever name got passed in here
so I'm actually gonna copy this
and
we will
do this twice also hello my kilo
steve
and now when I run this program
you'll see that
instead of just saying
hello user
it saying hello to whichever name
I passed in to 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 performance task
a little bit differently.
I could also
include more than one parameter so you can I mean
technically you could have as many parameters as you want
so I can put another one in here would 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
passing 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.
now when we run this program is going to call the function,
and it's going to use both of those pieces of information so it's a healthy
like you are thirty five hello steve you are seventy.
so
essentially you
were writing out this one line of code which just prints out like hello to someone
and were allowing this function to receive
two parameters are the name in the age
and depending on the name in the age,
the function is going to print out
hello a little bit
differently.
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 a number.
the only differences over here we're going to have to
convert this into a string,
but it's going to work.
just the same,
so you can pass in alm
strings
numbers bullion's
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 on
with python you're gonna you know
being 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 wanna talk to you guys about using the return statement in python,
functions
and other 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 gonna 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 to basically
executes task I wanted to execute all the code
and then I wanted to like
give me some information back
so that can be like a particular value.
it could be something like telling me how the task went
basically
on 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 two return information from
a function.
so I'm gonna 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 cuba number we take it to the power of three so if I was going to cuba like to
be like
two raised to the power of three
right it's just
pretty basic math.
so we're gonna make a function that will cuban 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
q,
we'll call the country
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 numb.
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 keep this number so we could just
take out numb
times numb
right
that's going to give us
numb 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 as a 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
facts
when we call this cube function
it didn't really do anything,
so
let me try to print this out so immediately prentice
function out it'll give us back the information that we want so
what about print
and will print
out the value of the function I'm terrific play
we 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 q being the number.
and
here's how we can use the return statement and 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's 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 will see
down here,
says
twenty seven so we got the correct answer.
so I can do this with anything wantonly cube like for something
as an hour 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 wanna illustrate this point a little bit further I'm going to go up here I'm actually going to create a variable
so I'm gonna create a variable called result
and I'm just going to set it
equal to
cube
for
this variable result is going to store
the value
that gets returned
from the cube function since
it's not in a store like
cube for its 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 turn out sixty four 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.
I guess for as a parameter.
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 I just want to point out one more thing
I'm actually not able to put any code.
after this return statements so if I came down here and try to put like a prince 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
does because when I used 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 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 wanna 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 could execute
other code when
other conditions are true.
so basically if statements allow our programs to respond
to
input that they are given so
depending on the data that we're using in the program
are 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
you 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 now we'll go ahead and do a really awesome example of the kind of like
sure yeah
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
under skin to skip whatever was in here and move on,
writes that
the basics of an if statement.
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 says
if it's cloudy.
if that conditions true
we're going to bring the umbrella.
but
in this case if the conditions are false
there is something else that we want to do
so in the case of here if this condition was false we just moved on.
but now if these conditions false
then we're gonna 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 me.
I order a steak.
so if this conditions 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 gonna say
otherwise
if I want pasta.
I order spaghetti and meatballs.
this is another condition.
if this condition is true we get the spaghetti,
if it's not true
then finally we're going to move on down here
and which is 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
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 those conditions are false we can do
other things.
or
we can check
other conditions
aren't 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 victoria we're going to look at some more advanced
if statements.
so what I want to do is create
a bully invariable.
so I'm gonna create a boolean variable that's going to store whether or not the user is a male.
so it's just going to be called
is underscored mail
and I'm gonna set this equal to true
because I'm a male.
so we have this boolean variable right and right now we haven't 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 up
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
such as
for example in
the other page we are specifying like
if I'm hungry or if it's cloudy.
here we're just going to check to see
if
is male.
so in other words
if the person is male.
so remember this has to be a
lake
it has to be able to be reduced to a true or false value.
so in our case
where it is using a boolean because that's like
the easiest thing we can do.
so
I said
if
I specify my
true or false condition.
now I'm 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 can 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 his mail to false
so instead of this being true I'd 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
more
and alice 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.
more
and now
anything that I've put below else
is going to get printed out when
that condition
isn't sure
so long we just put
you are not a male.
so now
we're able to cover both of these 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 an if statement so I could have like
dozens of lines of code if I wanted.
I'm just giving you guys are very simple example
so
why don't we make this more complex and I'm gonna 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 gonna 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 their mail and their tall or
if they're male and they're not taller
if they're not mail in their tall like
I can 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 key words
so
the first keyword I want to show you guys is the
or keyword.
so I can come over here and I could 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 their 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 can 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
high
and that's because
if we execute the code inside of this else
that means that
both of these guys are false
rates I'm saying
if they're male or their tall,
we're going to execute whatever isn't here
otherwise
This means
they're neither male
nor tall we're just going to execute this down here so
misery as how this works all
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 his male eagle
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 how the basics
of using the or operator.
and there's another operator we can use which is called
and
and basically a
similar to
or accept
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's in here so we could actually change this text
to say
you are
a
tall now
because we know for a fact that
if this is getting executed that are both male and tall.
we can also change
this print statement down here,
this could say you are
either
not mail 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 could the play button
and you'll see because they're both false
it's going to say you are either not mail 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 faster 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 mail are not tolerable.
so
that's the basics of
and and or
and you know
you'll be using those a lot of them 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 their mail and their tall,
but what if I wanted to check if their mail and they're not tall
so they're like a short male
I can use another
keyword in python
called
else f
or really it's just typed out
yes
I am,
and it stands for lcds.
and after this we can put another condition so
over here I'm going to check to see if their
male and they're not tall,
so I'm gonna say else if
the mayor
ismail,
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 their tall
and that I'm going to use a colon and now
down here
inside of here were basically just going to type out any of the code that we want to happen
when this condition is 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 mail and their tall,
so we can make one more
lcs.
and I'm actually just gonna copy this guy
and we'll paste it down here.
so I want to say,
if
not,
is male
and
his tall,
so now we're checking to see if their mail
and
are either not now and their 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
were going to be able to print something out for it so
if I mean 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 butter tall
if I made is mailed 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 taller 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 basically see how we can use
ifs
osoph's
and
else in order to
how I programmed to make decisions
and really to respond
to the different data in our programs.
so unable 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'll 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 are 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 on a 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 statement 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 diaries of those bullion's we're able to do certain things
in this tutorial we're going to look at another way to use if statements which is with comparison so,
instead of just using boolean values ​​we can actually compare
different value silly I could compare a couple of numbers like
compare couple strings
and depending on the results of those comparisons we can do
certain things are
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
on
biggest
number that we give it so
let's create this function I was going to say,
death.
and I'm just going to call it max
numb
and
inside of these parentheses I'm going to specify that I want three parameters as input.
someone to say
number one,
number two and number 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 that 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.
so I need to put like a true or 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
number
two,
and
number one
is greater than or equal to
numb three.
so basically what I'm doing here
is I'm comparing these different number 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 false value
write
num one is either greater than or equal to numb to or it's not
it's actually a boolean
value
rights 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 of the results of these two comparisons end up being true,
then we know that none one is the biggest
so I can just return
num one
I can say return
no
one
but we also want to check some other condition so I can say
alef
and
now I want to do some other comparison so I'm going to say,
else if
none
too
is greater than or equal to
number one
and
number
two
is greater than or equal to
number
three.
so I'm doing essentially what I did up here except for none too so again I'm comparing
these different numbers and this is gonna end up being a true or false value,
depending on the result of the comparison
so
down here again
we can just
returned home to
because if all this is true that means we're going to end up returning
none too.
and then finally we can just say else
so
if num one isn't the biggest and numb to isn't the biggest then
we can pretty much assume that
number 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 results I'll say max
numb
and will pass in like three four or 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 printed out five because that was the biggest.
if I was to change this middle onto the biggest so we make the middle on forty
now it will be able to
take on that situation and finally I can make this like three hundred
and
it'll be able to handle that so
no matter which number numb one number two or number 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 gonna want to be comparing different values
inside of python,
so over here we're comparing
numbers.
but we could also compare strings
so I can
easily make these both like
you know I can
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 can basically say like
if this string is equal to that string.
you can basically like compare
all different data types see nike compare in numbers strings.
you can also compare
bullion's
so
one more time
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 not 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 exclamation point will basically mean
not equal so it's.
if no one is not equal to
none too.
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 are going to be using in python so
using comparisons is really awesome and there's going to be tons and tons of situations
inside a python where you're going to want to compare different values
so
these can be extremely useful
in that
tutorial on how to talk to you guys about building a calculator in python.
now having been fallen along with this course you'll know that at the beginning of the course we actually created a very basic calculator.
basically the user could just enter into numbers and our calculator would add the numbers together and spit out the answer
in this is horrible honorary 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 were 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
mocked by,
and then I'll enter in the second number and will perform the operation
for them says it 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 will basically store the values ​​that the user inputs
into those variables so the first one will just be number one
and I'm gonna set this equal to
input
and we're going to give this a prompts also
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 and 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 gonna 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 of this should work out for us so I'm actually just going to
copy their sky
and we'll go ahead and
paste it over here,
and we'll paste it one more time so we
were basically just try to get three pieces of input from
the user.
so the second thing we want to get is the operator so I'm just gonna say
oh p
for operator and actually get rid of this flow
because
we want to keep this as a string.
and I'll just say
enter
operator so it's going to be like.
plus minus
multiplication whatever.
and then finally down here we'll get numb
to
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
o p.
in other words we need to figure out what
operator.
the user was
trying to do so they are trying to do additional they're trying to do a subtraction like
we want to figure that out,
so
we can use an
if statement
in order to
figure that out so I'm gonna come down here
and
I'm basically just going to create an if statement song and 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 a plus sign.
I'm just going to print
out
number one
plus
none too.
but let's say that instead of a plus sign they entered in something else so we can say
lf
op
is equal to.
and now we'll check if it's a minus sign.
and so here or just print
num one
minus
nothing too.
and I actually forgot to put a colon here.
so we can just do these for everything so now we can say.
lf
opie
is equal to
division
and here we'll just divide the two numbers so I can print out
number one divided by numb to it,
and finally we'll do the same thing for multiplication so I'll say
l
if
opie
is equal to
astronauts
and was print
out
number one times
none too.
so basically over here we have every
you know condition.
but I also want to put in an
more
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 sums usa
opie
are actually we just say
more
and here with this print out lake
invalid
operator.
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 the division sign or a multiplication sign!
depending on which one is equal to we'll do a different operation so
let's go ahead and run this 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 numbers to twenty three.
so now
it prints out the answer twenty eight so
our calculator actually works
let's try another
operator just to make sure.
so it would be six
and y we multiply it
by I don't know five point
to three.
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 right so we could check to see if they want an edition we could check to see
if they wanted subtraction
division and 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 enter it in like five
and now what does enter unlike
an open or close parentheses and
four,
so you can see here it's
able to
tell like the program 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 inputs to create a little calculator.
in this tutorial on hitachi guys by 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
what 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 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 situation
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 can convert
for example like
jay
one
two
january
or I could convert
one
ar
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 sure you guys how we can do that so,
the first thing we need to do
in creating a dictionary is just given 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 wanna
created 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 that I can give it
a
corresponding value.
and the way that we can do that is 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
jn
and
then I can type a colon.
and
I want to type out
the value I want to associate with it so we can say in january.
I could basically do this for every combination so I could say like
you'd be
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 soon say and they are.
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
in 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 jn,
this is actually going to give us a little warnings you can see down here it says
dictionary contains
duplicate
keys
dots they know no one we're creating dictionary so
you'll want to make sure that
the keys are unique
aren't so I'm gonna 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 twelve 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 you have to do is referred 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 close square bracket
inside here I can just type in one of the keys.
so for example inside here I could type in
and
oh thee.
and what this should do is it should give us back.
on
full
name for november so when I print this out,
you'll see that we get back down here.
nov.
so I can do the same thing for
any of these we can say like an a r.
and now we should get marched 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 qe
is actually another way we could do this too so I could
I say
month conversions dot
I receive
an inside of here I could also pass in the name of the key so I could pass in like
dc
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 keys 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 you so he put in an invalid
key.
so let's just
try to do that so honest put like aliyu 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 gonna want to
create some sort of a default
value.
so if I'm in this situation where I have a key
that's not mappable 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 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 and 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
strings so you could also use numbers
so for example I can see like zero
one
tan like I can put any numbers I want over here as keys
as long as their unique and that I could access those keys
just like I would normally down here
with this
get function or with those open and closed square brackets
so that's sort of the basics
of using dictionaries like me 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 wanna talk to you guys about wild loops in python,
and 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 could specify like a few different lines of code and then I could
put that code
inside of a while loop
and it would basically you loop through that code executing it repeatedly until a certain condition was false.
so our lives 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 healing
bare basics of how wildebeests work
and then in future tutorial is we're going to use while leaps to create little games and stuff like that so
let's talk about wild sum
when I create my wild 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
but just for our demonstration I'm going to.
so I'm going to call this,
I
am going to set it equal to one.
now what I wanna 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 wild is I'm just going to say while
I
is less than
or equal to
ten
and now I'm gonna put a
colon
and I'm gonna 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 gonna put a very simple line of code here where it's going to print
out the value of
I,
and then down here I'm going to actually
increment
eyes I'm going to add one
to eyes I must say
I
is
equal to
I
plus
one,
so basically I'm taking this I value and I'm adding one to it
is actually a shorthand that we can use in
python to do something like this though I could just say
I
plus
equals one
and not automatically going to add one
to
I
so this is like a little shorthand and
you'll see people in pipe on using that a lot.
so
over here we basically have
our wild lives I have
the outline for a while lives 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 company more than just going to move on.
so down here I'm actually just going to print
done with loop.
so this was kind of illustrate this for us.
so let's go ahead and run this program I'm just gonna 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 were 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's 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'm 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 gonna check the condition
again.
so remember I checked the condition
first time
and then I looped through all the code,
then I'd go back up to the top
and I check it again so I'm gonna 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 tenant.
so now we're going to go through and will
execute all this code again so I'm going to
print out
I so it's going to be too
and I'm going to add one to eye
so now we have three
then
python's 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
right
so I'm the last
iteration it's going to end up being levin,
we're going to come back up here,
and we're going to say
hum
is eleven less than or equal to ten
nope
and so sense 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 wildebeests work
we're specifying a loop condition.
as long as that conditions true
will 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 volumes to do
all sorts of things like wild fruits are very very powerful
in this tutorial on a show he eyes 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 wild loops and variables and
all these cool things
in order to build this game so,
the basic idea is we will specify lake 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 where it 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 can say
secret
word.
and we're just going to set this equal to a secret word so why don't we make it a draft.
that's a pretty good secret word.
and now what we wanna do is 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 gonna
call this
I guess
and
I'm just going to set it
equal to an empty string right now.
so
now we have a 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 were.
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 continuously 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 gonna say while
and
after a 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 gas
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 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
gas 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 guests.
I'm storing whenever they enter
inside of this guess
variable
and then what's going to happen is that we're going to come back up here
we're going to
check to see if the gas
is equal to the secret word
if the guests
isn't equal to the secret word,
then we're going to do it again.
but if the guests
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 were
since they have 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 he did so I'm gonna
click play.
and down here you can see it's prompting us to enter a guest's so I'm just going to type in some
you know random texts
we can type in whatever you 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 programs can 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 will 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 and 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 guests
hi
and we'll just set this equal to zero because initially
the user won't have
guest
down in this while loop.
every time we go through the loop,
I want to increment that gas can
write 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.
I guess
count
plus
equals one
and this is just going to add
one to the gasket actually whips.
so
after each iteration of this loop we're going to go ahead and add one
to the gas count
right 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 words on it it's going to be guests
limit
and
will 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
gases.
and
I'm just going to set this equal to false initially.
so this
out of gases variable is going to be a boolean.
and it's gonna tell us whether or not the user is out of gases.
so if out of gases 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.
so let's use these different variables in order to
make our program or 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 gasp.
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
I guess
count
is less then
I guess
limit
if gas count is less than guess limit.
that means that they haven't
guessed
on
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 guest count.
if this isn't true in other words,
if they
have reached their guests limit,
then I'm going to want to set
out
of
guesses
equal to
true
because there are guesses right they have no more guesses because
the gas count was less than the guest limit.
and so that means that there are two gases so they have no more gases.
so there's actually
one more thing we need to do inside of this while loop
we need to
add another condition
onto this loop guard.
so right now we're going to keep looping as long as the guest
is not equal to the secret word
but remember
if the users out of gases in other words if they've reached their gas limit
we don't want them to guess anymore,
right so if they ran out of gas is then
we want to basically break out of this loop and not give them more guesses so
I'm going to go ahead and add another condition onto here
and I'm gonna say
Come on
they haven't guessed the secret word
and
they're not
out
of gases so it's going to be not
out
of
guesses,
then
we're going to keep looping so as long as they haven't guessed the word and as long as they still have some guesses laugh 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 are two possible ways that this loop could andrei
either the gas is equal to the secret word.
so either the user guests of 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 I'm going to say
if.
and we're just going to say,
out
of
gases,
and,
if the user is out of guesses
that I want to print,
you lose
so we're basically going to be like
you lost the game.
otherwise though
if they're not
out of gases.
that means that they guessed the word correctly
so we're just going to pronounce
you win.
all right so now we have all the logic for this little guessing game set up
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 going to enter one gas
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 were able to guess it
in the number of trials 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 gas and unlike
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 guests variable.
then
we also created
some more variable so we created this guests
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 gas
were incremental
the guest count.
we also have guests
limit
and gas 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 guest
count
is less than the guests 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 gases
and out of gases is going to tell us whether or not they have some guesses left.
so 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 is 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
will 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 on a talk show guys by using
for loops in python.
now a for loop
is a special type of loop in python which allows us
to loop over a different collections
of
items.
so a lot of times will 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 lives provide a very specific purpose.
and the easiest way to kind of wrap your head around
why four lives are useful as it is 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 four lives are awesome so down here in my
text file and my a
python file.
I'm going to write out a couple different
for lives.
so the way that we create a for loop
is just by saying four.
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
every 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 save four letter
inn.
and now what I wanna 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 can put a string in here I can just put like draft
academy.
and now I can just put a colon
and so basically what this is gonna say it so 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
letters I can print out this letter variable.
and it's actually going to print out a different letter
inside of this dropoff academy string
on every single iteration
of this loop.
so
I'm gonna run this program.
and
down here inside of my counsel you'll see that I'm basically printing out
draff 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
third was our fourth with
a.
so
I'm essentially just looping through
all of the letters
inside of
draff academy.
so I'm saying
for
each letter
in dropoff academy.
I want to print
out
that
letter.
and so this is kind of like
how for 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 stores
the value
I
excited
right so we went through
this entire string 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 in array up here,
let's call it
friends.
I'm gonna set this
equal to a bunch of different values ​​so we can
put like a list of our friends in hearings zelig
jim
karen
and
kevin
instead of saying the letter why don't we call this
friend,
and I could say for friend
in
friends
and now will 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
everyone
element inside of that array so not here you'll see
we're printing out gym
and then on the second iteration were putting out karen and on the third would bring up kevin tso,
and he was a 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 asking me like an array we can also just loop through
a series of numbers
so I could say
index over here
and again this can be anything I'm just gonna call index
so we could say for index
in
range
and then in here I can pass in a number so I can pass in like
ten 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 ten,
not including ten
so starting with zero it's going to put out zero one two three four five six seven eight
nine.
but notice that it didn't print
out
ten
so it's essentially just printing out all the numbers between
zero and
ten
not including ton.
you can also specify
a
a range of numbers so I can say for example like
three and ten.
and now this will print out all the numbers between three and ten
not including ten
so run this program,
and you can see we print
out three four five six seven
eight nine
ten not ten
so whatever value that you put
here in this second position
is not gonna get included in the range
and ranges can be
really is also 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 for
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
alli end
and then inside parentheses.
the name of the array and so
this is going to spit out three because there are three elements
inside of here
right
kind of make sense.
so what I can do is I can say down here inside of this range function
I can just type in
eliane
and then
friends.
and so what this is gonna 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
everyone
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 care and kevin so
in fact
for each
iteration through this loop
it's basically going to be printing out
friends
zero
friends one
and then friends
too because
in heater were 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 can print out all the elements in the array
and
looping through something like an array is actually a very very common use case
for four loops.
but like I said we can you spoil hopes to loop through essentially any collection that we have so
we can loop through like a string that we could also loop through
you know something like an array so
this can be really useful and bats are 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
brace this would just be a simple program
you can use all sorts of logic
inside of these four lives.
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 to zero than 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 lake
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 don't be afraid to
put some complex logic
inside of these four lives
because
it can really make your programs more powerful
in this tutorial I'll show you guys how to build an
exponent function
are 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 could print.
we could say like
too.
and we can just do
two multiplication signs
and I could 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 a
cassettes
to cube
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 sun will 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.
something as a death
and now we need to give this
function a name so why don't we call it
raise
two
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 when you start writing some code.
here's the thing
inside the function we don't necessarily know
the value of this poem,
right
for them why have we knew
that we were just trying to like square this number or cube this number.
I could just return
like bass
num
times
basal cell
right
now it's square the number or I could keep the number
bass 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 sure you guys how it can use a for loop
to basically figured 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 the result
equal to one so we're gonna start this off at one.
now I want to create
a for loop,
so I'm basically just gonna say
four
and over here,
I don't want to specify like
a
index on aug eight index
in
and now want to specify a range
and arrange will basically
range us
through
a
collection of numbers.
so basically I want to
multiply the bass num
by itself,
as many times as the pound numb
specifics
right
over here I can just make this
a range
of
power
numb
right and so we'll basically a loop through
this for loop
as many times as pound them so eponymous two
will loop through it twice.
economists for will luther at four times
and every time through the loop will multiply bass num by itself
or actually you'll see we're going to multiply it by this result,
and so down here inside
this for loop
I can basically say
result
is equal to
result
times,
pacing up
this should actually give us everything we need to take this number
to the specific power so
the actual like
result
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 basin on by one
right.
and so now result
is going to become
the same value is based on
the second time through the loop so this would be if we were squaring the number.
we're basically multiplying resort by basin um again so it's essentially just
based on long times based on
the third time through the loop
we're multiplying result times bass num
so we're essentially just like
keeping 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
so let's come down
after this function and we're just going to call it so I'm just going to say,
will actually print out the answer so
save raise to power
and the inside of these pregnancies member 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
from the program
and you can see down here in the console were getting nine.
let's try something a little bit
crazier
drive three
to the fourth power
and will run this
aggregate
eighty one so that's
three to the fourth power.
let's try two to the third power
and were getting eight
yeah so that makes us
so
our raised the power function is working
just as expected and that is what he has to do it one more time.
so
inside of this function were taking in two pieces of input were taken in a base number.
we're taking it a pow 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
pounha um here is basically going to range from zero
all the way up to but not including the power number silk
we'll basically loop through power number of times
that makes sense.
and then every time through the loop we're just multiplying result by
base numb.
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 in 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 wanna 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 are 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 going to set this equal to
a normal let strangers like this
now normally if we were creating a list I could just put in a bunch attributes and bunch elements right,
and now all of these elements are inside of this number grid lists 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 numbered list is going to be a list.
the second item and the number grew less is going to be a list
etc.
so I'm going to show you guys have to do that
I can basically just come down here and
why would normally put the first
element in the list
I can just create another array
someone's going to make an open closed square bracket,
and now
I'm just going to say one two three
and I can create the next item in my list
I can say
four five six,
and then let's make another item in this list.
I can say
seven
eight nine
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 numbered red lips write this
high level overall list,
we have
four elements
in all of those elements
are
myself
lists.
so essentially what we're doing is we're creating a grid right
this grid has one two three four rows
and one
two three carbons
right
for rose
three columns and that's kind of like
why these can be useful as we can create a grid like
structure inside of python
using
two dimensional lists.
so this is a pretty cool list structure.
and I wanna show you guys,
how it can access
individual elements
inside of this list structure so
for example let's say I wanted to print out one of these values ​​if they 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 closed 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 it's the zero of element in the array.
this would be row on its it and exposition one
this would be row to row three etc so I could put
roses grow
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 to column three.
so
I'm going to put it another zero
because this is at the
zero
index
of this first
array so
I've been 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 can also do this for like for this eight for example so this is going to be
zero one two
can be row two and
column one
and now we should be able to print out that
e.
so that's basically how we can access elements inside of this
two d
list.
the next thing I want to show you guys
is a
nested for loop
nested poorly with the situation in mega said well 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’ve basically shown you guys on a parse through
a
two dimensional
list or a two dimensional array.
so basically we're 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 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 am I put that for loop inside of here,
so now I'm going to say four
column
in a
row.
and basically what this is going to give us is
everyone
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
over that makes sense
so
down here.
now we 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 for loops together in 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 strings we can take him like a phrase or a word
and will 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
to be done
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
and any instances where there was a vowel
so
in our case let's just say
ae I o u.
I would convert that vowel
into ag
so dog would map to
ddg
cat would get mapped
into
цг т.
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 gonna 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
jeez.
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 gonna
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 avowal 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 make sure you guys how we can do this.
I'm going to make a loop somewhere safe for
and I'm just going to say a letter
in
phrase.
and so now
when I looped through here I can access
everyone
individual letter
inside of the phrase that they passed in.
so what I wanna do is I want to check to see if
the letter is a vowel or not
right.
if it's a vowel than we can
add
ag onto translation.
if it's not available
then we can just add on whatever
letter it was onto translation
it is so anyway
many users
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
inn.
and over here I'm just going to take out all the vowels that was ae I o u.
and
ae
And about you.
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 wanted 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 ag onto
translation
and the case where we don’t have a vowel.
I'm just adding on
whatever letter it was on to 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
we only 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 gonna
I pass
whatever the user
inputs
so
I can just say
input
and
inside of these parentheses I can say the problem 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 gonna run the program
it says
enter a phrase actually let me
format this a little bit better.
good
that's it says enter a phrase.
so let's go ahead and do that.
so
let's just type in
the example we use before him and 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 only.
to be or not to be,
this is like a little bit longer than a string,
and you can see
it translates it into our
draft language.
so that's basically how we can
create our little a translator out and that's pretty cool.
as you can see we're using a for loop
in combination with an
f 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 upper case.
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 lower case 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 could check to see
if
letter dot
is
upper
and if the letter is upper case
then
we can just set the translation equal to
the translation
plus
a capital g,
otherwise,
we can just set it equal to the lower case g.
it's a now we're able to control
both of those situation so
let's just test this and see if it works
and to rephrase
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 wanna talk to you guys about comments in python.
this is going to be a pretty short tutorial I just want to give you 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 council it's going to print out 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 in python sees it as just going to ignore it it's not going to execute its not going to print anything out.
it's just gonna kind of ignore it and comments are
basically 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 tax dino comment inside of a file
nor retreat of
a comment
I could use this
hashtag symbol.
so in click hashtag and now I'm
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 run the program
that doesn't show up anywhere.
it doesn't matter what
I put off at halftime
this is just like ignore by python,
it's just for me or another developer to come in here and use.
so a lot of times 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 lay explain a line of code
to for example
I can write a comment that would explain
this print statement sighted sailing
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 ok
cool
so comments are useful for
leaving little comments in a python file
you want to make
comments on multiple lines.
you can just create a new line and use another hashtag
and so that I could type out whatever my next comment was
I mean generally if you're gonna put comments on multiple lines you want to just use multiple hash tags
plot
there's actually another way that you can write out comments
so you can use a triple
quotation marks so I'm typing out these single quote
and I'm typing not 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 can
put you know
as much taxed 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 link official python like style guides or whatever
just recommends that if you're going to put comments on multiple lines you used 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 making stuff up.
and so what you can do is you can actually comment
out that line of code so
I can 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 commented out.
and that way you're not actually deleting the code
from your file
you're just commenting it out so pythons gonna ignore it,
and those are really the two uses for comments as to write little notes
right little comments about different things
or to comment
out code and
as you go forward and python
I'm sure you'll find uses for
comments
in this tutorial on a talk to you guys about
catching errors in
python,
and a lot of times when we're writing python programs
you'll encounter of an
error 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 though completely stop your program from running.
and what we can actually do is we can watch out for
you know a 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
its
input enter a number,
basically what's happening is I'm prompting the user to enter into 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 around 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 answer 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 appears it's a little easier to see.
but what if we run the program again.
so it says okay enter your number.
let's say that I break the rules might don't enter a number so it is ignore what the prompts says and I put
you know
some random text in here,
now when I enter this
the program's going to throw an error,
you see down here it says value error
invalid
literal for ants
base
with basten
basically
paved and answering
an integer like
you said you would
write.
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 them in situations where something can just go wrong in our program.
and up 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
prav 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
in a try accept blog will basically allow your programs 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 into correct number great,
but if they don't enter in the correct number then we can basically like
account for that so they entered it in something like a string when they're supposed to render in a number.
we could tell them like hey you entered in the wrong thing
so I'm sure you guys how we can use these try accept blocks are going to come up here
and I'm just going to say try
to make a 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 tape 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 stream.
so let's go ahead and run our program.
and it's just gonna say enter a number.
so we're going to ignore that will enter this end
and now instead of yelling at us in breaking the program
just says hey
invalid input.
so was able to handle the error they got called it was able to handle the invalid
input error
basically just told us hey
nowhere 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 a 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 except
blocks.
but here's the thing you 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
two 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 in my program for the most part
this is gonna 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'm
gonna create a variable called
value and I set it equal to ten divided by the 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 I run the program.
it basically says
zero division enter division by zero,
we're not allowed to do that right.
if I took this and I put this down and out of this try 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 accepts and 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 air I we we
put something in that was wrong you
can convert the value.
so I can create two different except blocks to catch two different types of error 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 air.
so if I just put zero division error in here.
now it's going to catch that zero division later so I can say
divided by zero.
I can also make another accepts I could say
accepts
and
we can catch the other exception that we got so I think it was
a value air right now is
we put in the wrong type of value
inside of that
inputs so now we can print out
invalid input.
so
with these two except blocks were basically able to handle both of these
situations are
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 answered in
a string set 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 it
when we get a value error we can say invalid
input.
when we get a zero division error we can say divided by zero and 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 except
I can say as
and I can type in a name here so a lot of times people will just say like.
ye are our
and down here
I can print out the actual error so I can just print
er r.
and so if we end up dividing by zero.
so if I say answer is equal to ten 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 print
out the specific error that got thrown.
now
a best practice in python is to use these
specific error so you always want to accept
and except for a specific
error
when you don't want to do is just say
except
and
then just accept anything under the sun.
it's this too broad
and
if not in good practice and pythons you wanna be catching specific errors like this
in this tutorial on a talk to you guys about reading from
external files in
python,
you know 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 gonna 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 can 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
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
in the same directories on my case
app dot python and employees dot text
are in the same folder
within the same directory so I can just type out the name of the file liking to say
employees dot text.
and 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
which you could actually open files in
a couple different modes
and the first mode is called read
so I just put an
r here and that's going to stand for a 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 without information.
another
mode is called right
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 a pen.
and it's 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 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 are just going to be working with
regular are so we're going to be reading
from the file.
now this open function will essentially just
open
on
file so it's going to like
golden map file inside of our file system
opened 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
in
close
function so I can come down here and say.
employee
file
dot
clothes.
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 and you want to also make sure that your closing the file at some point so generally
once you're done reading it and you can just close it.
so that's kind of like how we can open and close the file.
now let's talk about how we can get information from the far right
there's no point 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 filed to figure out what's inside of it.
and I'm gonna 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 as readable.
so before we do anything else generally is 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
in poorly 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
aw here so if I put like right
now
redouble 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 is not that was in that file.
I can also come down here and we could read an individual line inside this file so I can say employee filed dot
read line.
and what this is gonna do is it's gonna 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 red 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 line of code,
and then
printed again down here
I'm saying employee filed out 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.
when I run this program you'll see we print out jim sales men
and dwight salesman.
so if I was to do this multiple times 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.
we can say instead of employee father
read line we can say dot read
lines.
and what this is gonna 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 sales men that's the first item in the array
to white salesman
the second
item in the array.
it's basically taking each line and putting it inside of an array.
so if I want 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
to white salesmen line
because that is at index position one inside of the array.
you can also use this reliance function
with a for loop
so I could come up here and create a for loop
is going to save four
and we'll say
employee
and
employee file
and then for each employee we just want to print them out so
it actually started
we have to put employee file dot
Read
lines up here
and so now there's a loop through all the employees
in this employee filed out 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 files is 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 in 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 r.
then you can do all sorts of stuff with it and you always want to make sure that
you close it when you've 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 on 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 the information in it I could pass through it
use that information to do certain things,
but it isn't a reading a file I can also write a file and that's what I want to talk to you guys about today's writing new files and appending onto existing files
over here I basically just have some code written out
and this essentially just
reads information from this employee's dot text file so you can see over here,
I am specifying the mode which is
r and that stands for read
and then down here under his reading
all of the contents of the file and spitting it out on the screen.
so I'm gonna click the play button over here,
and
you'll see that
if this executes so it's printing out
all of the
lines of code in our text file so over here I'm in this employee's dot 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 is that we wanted to add them onto this list.
good
I can come over here to my app dot python file
and
instead of
reading from the file
I want to
append it to the files I want to say
a
and appending to the file basically
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 filed out right I'm going to be able to write something to
the end of the file so
I can basically does right 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 somewhere on my program.
and
you'll see that nothing shows up down here in the council.
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 resource is 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 employee's dot text file
it when I hadn't
added toby again
right so it added this employee here
again
and also you'll notice that in this case,
these employee didn't go onto the next line
I accidentally ran my file again and all the sudden it messed up
this file over here
and so appending you really need to be careful because if you actually go under file again or have you
you know,
append something on something wrong to the file it's permanently it's getting saved inside of the file.
so I want to talk to you guys are a little bit more about you know
appending another thing we can do let's say we wanted to add another
employee
and you'll notice over here in this employees that 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 kelley 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 upend
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 are aware of like these special characters that you can use they call them
escape characters
and you're in time you're audi 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 can also just like
overwrite a file or I can
write
an entirely new file.
this is as 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 filed out 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 gonna put this inside the file so
when I run this
and we go over to this employee's dot text file
you'll see we only have one line inside of this file now it's just
kelley customer service.
that's because I was using double you
not
a when you use w it's just
overwriting 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 as he could 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 song in a 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 to so if I wanted to create like a web page
I could say
index start
each tier mel
and I could also add in some
each team malcolm here so
if you don't understand html don't worry about it but
if you do I can put like
a paragraph in year
another paragraph like
this is
html
basically ht mouth like a webpage.
and the point I'm trying to make is that you can write out a webpage
inside of python by doing something like this so
now when I have played this
we get this
index style
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 is
tons of applications for writing to files and pythons and great language for
working with reading writing
doing all itself with files
in this tutorial on its altea 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 are useful variables are 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 those variables.
all the stuff from that
external file
inside the file that I'm currently
working on so
I'm sure you guys how we can use module and then we'll talk about like
you know where you can find awesome modules and and really why modules make python and
awesome
language,
so over here in my text editor 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 progress,
she'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 or a kilometer.
and then we have this list here which lists out all of the
members of the beatles,
am I also have a couple different functions down here so I have this get file extension function.
and this
basically just was
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 the dice
so you pass it a number
bypass it like a six it would roll a six sided dice
a passive
nine at roland nine sided dice etc
so
this is like a pipe on 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,
ha,
I think I could use one of those functions
that was inside of that useful tools file
actually yeah
must
simulate rolling the dice in my program
well instead of having to go over here
copy this
function and then
paste it over here in 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 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 it so I was going to say,
useful underscore
tools
and pythons 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 important statement
I can actually use all of the functions
inside of that useful tools file.
so
for example I could simulate rolling the 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 and my little suggestions menu it's telling me like beetles feet miles
get file extension meters and kilometers roll dice.
so it's giving me access to all of the stuff that was in that file so over here I could just say like
roll dice and we could pass this like a ten will roll a ten sided dice.
and
now this should actually
be able to run that function so
will simulate rolling a ten cited diet as 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 this 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 mean modules are very simple module as 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 my web browser.
and
among this website
it's actually like the
official python docks
and basically all I did to get here was just typing
list of python modules and google,
and depending on the version of python you're using you're gonna want to make sure that you click the right one.
I flicked on the version three one
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 if there is 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 functionalities of
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 are 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
to sort of look through a lot of these different modules is no 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 they'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 on the official
python docs
but
he's using
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 actual
python module for doing why
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 in finding them
you can actually save yourself a bunch of time because
chances are that somebody is already written a module to do
you know
what you're trying to do are like
part of what you're trying to do.
so now that we've taken a look at all these different python modules
I wanted to 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 in python but the question is
where all these files store
right
when I was over here in my program
for example I was using this useful tools
dot pie fire 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 healy
where all of these stored
and there's basically
two types of models here there's built in modules which means they're just built into the python language so
we can't just automatically have access to them.
and there's no
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 on linking
base sixty four
bd be
been ascii like
if I come over here,
and you'll see I'm over here in milo 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
amusing.
there's a folder here called lab
and this is a very important folder and 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 sixty four hears bd be right.
a lot of these external modules are stored
inside of this lib folder and
like is that there's no external modules there's also a few modules that are just like a built in
modules
that are not can be stored inside of here
and you can actually tell so
for example if I was to come over here and click on
base sixty four.
it tells me where the source code
so the source codes in lab forward slash
base sixty four.
we are able to see that right how is I saw that inside of my lib folder
this been ascii though for example if I click on this,
you'll notice that this
is not
giving me a folder cause this is basically just like built in to python so we don't have to worry about
you
lucky enough I'll just kind of like
it just how it works.
so some of these are external some of them are built in.
and
I want to show you guys
are
in addition to using these modules races.
there's a lot of good stuff here and honestly you could spend.
you know for 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 either 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 module is as third party modules
that don't just come
preinstalled with python,
and I'm sure you guys haven't do that really quick so
the first thing you need to do is obviously find
a python module that you wanna installed you can download
and I have actually used one in the past.
python docs it's an
external module that you can use to
basically use python to create where documents which is pretty cool as you can like
format where documents inside of it so I'm just going to look that up here
in google.
I'm just gonna type in python
docks
and
here's a thing there's tons of these
extra modules you don't have to use python dogs.
I'm just giving you guys an example
but really it he just look up like
useful third party python modules there's going to be lists of hundreds of these things online
or in my case though
python docs has a website
and it basically just
has like some installation instructions are gonna come over here
and it tells me that I can install python docs using the command pip
install python docks.
so this brings us to
something I want to talk to you guys about which is a
pip
and pip
is essentially a program
and actually if you have a newer version of python three it comes preinstalled with python three
and you can use pipe 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
somno sure you guys are how we can do that,
but what I want to do is open up the
command prompt or the terminal on my computer.
if you're on a mac you wanna open up your terminal.
if you're on a windows computer you want to open up your command prompt
I'm using a windows
right now,
so I'm just going to come down to the search bar and type a and cmd
and this command prompts you to come up so I'm gonna click this
inside of the command prompt we can actually use
pip
firstly we want to do is check to make sure that pips install
and like I said if you have a newer version of python three pictured calm just
pretty installed
and it should do some work in here if you don't have a newer version of python three though,
you might have to install
pip
separately
and there's tons of instructions online and how to install pip.
so I'm just going to check to make sure that I have
to type in
pip
haven hyphen
version,
and they should spit out the version of pip
that I currently have.
so as long as we have to
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 docks.
it was just
python dogs like that.
now again
you don't have to install python dogs
I'm just doing this for
this tutorial just going to 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 ninety to ninety five percent 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 they'll be like some detailed installation instructions but out say
for the most part any late
legitimate
python
module is going to be able to install using pip.
so
over here to understand a pip
install and add a module name so
python hyphen docks
and when I click enter this is going to go off and install python dogs for ourselves,
we enter
and it's going to go off and install everything we need for python docs.
so you can see we have
successfully installed python docks
zero point eight point six,
so
I'm sure you guys
where exactly this god
placed so
normally when we install
a
external
third party module.
it's going to get put inside
this limb folder.
but it's gonna get put inside of a special folder in here called
site packages
so slight 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 dark zero point eight point six
pi three points sakes thought eg 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 docks.
so I used python docks
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
deal c x.
and
now I can actually use it so I can just say like docs dot 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 can 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
pick to do it so I could just say tip
and install and we could just
again saying
that a module names of
python dogs
and people now
and install this
on our computer.
so if I was 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 be dozens and dozens of python
courses covering each one of these modules,
you know
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 know you can make sure that you have owned by checking the site packages folder or the lips folder
but really now it's on you to just come and go out and
use these modules and don't shy away from this because modules are a huge
part of python
you're definitely going to want to include them in your python stack.
in this tutorial on a talk to you guys about classes and objects in
python
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 deal with usually things like
string cilic plain tax
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 not all things
can be
represented using
strings numbers or bullion's right,
there's a lot of
things in the real world that we can't represent in automating a string or an array,
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 it
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
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 in
and python we could create a class for it and
essentially what a classes is 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 its
super awesome and classes are extremely useful
causes 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 a model a real world objects and I wanted to be a student
right where 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 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 sure you guys how we can do that and create our student class
it's over here.
I'm just gonna make a new file.
wouldn't we get a new pipeline vase I'm just going to call it student dot pie
and
inside of this student dot pi file I want to create
a student class.
so the way that I can do that is just by typing
class.
and now I'm gonna type
the name of the class
that I want to create.
so in our case we are going to create a class called student so I'm just gonna 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 invented 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 unlike modeling
a student I'm creating our student data type.
and I can use things like strings,
integers and bullion's
in order to
map
out what a student should be in what a student should have.
so I want to create something called an initialize
function.
and the way I do that as just by
typing out adef
and I was going to take
two underscores,
and then the word
I am I t
and then two more underscores,
and
you also want to type out
open close parentheses
and inside of those parentheses we want to type out
self,
and I'm on a colon after that.
what I can do inside of this initialize function
as 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 gonna say self
a comma.
and now we can start thinking about
you know
what values ​​will
represent a student inside of our
python programs so
if I'm thinking lake
unthinking something like name
right so every student
in our programs should have a name.
they also might want to have a major
because they're in college,
they probably also will have a gps,
so their grade point average like
how they're doing in school
and lets to find one more thing that let me think,
why don't we make
a bullion
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
innate 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
as a gps,
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 meme
is equal to name.
and then I'm going to say self dot major
because you get the major
self dot gps
is used
the gps.
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 gonna 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 gonna 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 a gps and
it says whether or not they're on probation
dots 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 gps.
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 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 gonna call this
student one
and I'm going to set it equal to
student
and I'm gonna make an open and close parentheses
and now
inside of this parentheses.
I want to give this student
a name,
a major
a gps
and ann
is on probation
value.
so I'm going to say it will just create a fake student saw to say his name's
jim
and he's studying
business
and
we want to give them a gps and maybe has like a three point
one.
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 rubber an
object
is just
one instance
of a class.
so the class is like an overall template it defines what a student is
put an object
is an
actual student with
actual information so
we could call student one now
this is a student
objects.
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
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 students
gps
and it's going to print out the students
gps
three point one.
so now that I created this
student
objects.
I can actually access the information about the student
so essentially I'm 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 too.
and
essentially we do the same thing we just give it different information so
we could say lake
pam
and her major is
art
and she has like a
two point five.
and let's say that she is on probation.
so now I have another student student too so if I wanted
I could access information about that student we could say like student to dodgy pa
and now this has given me
the dpj of
student too.
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.
so remember I passed in a name
I passed in a major at passing a gps.
when we create that student
we're actually calling
this function.
and when I pass in the name
the major and the gps
those values ​​are actually getting stored over here in this name.
this major this gps
et cetera,
right
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
as he put the name
and basically what this means is
the actual objects name
is going to be equal to the name that they passed in
so self dot name as an attribute
of student,
raises the student is storing a name the student is storing a major the student is storing and gps,
but that's different from
this name
this major in this gps
remember and these are all just values ​​that I passed in
there just parameters.
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
ok.
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
objects
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 gps
is going to be equal to the gps that we passed in.
that's basically what's happening here so when I say self
is referring to the actual object so
over here what 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 a super powerful.
and what's cool about classes and objects as 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
or I can model anything that I wanted.
inside of my program
I can just give it certain attributes right
so that's what's cool about classes we can model real world objects.
and we can create our own
data types
in this tutorial I'm gonna show you guys
how to build a multiple choice quiz in python.
so basically we're gonna
set up a little multiple choice quizzes and we'll have the user take the quiz and as they take the quiz
will keep track of their score and then at the end will 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 these programs are we're going to use a lot of cool
stuff in python.
the first thing I want to show you guys
actually did this before I started the video it's
this little a ray is called question prompts.
and I basically just wrote out all the questions that are going to be.
or I should 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.
and the next one is
what color of banana is 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
right
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 like
on
actual question itself.
and then there's the answer to the question in both of those attributes needs to be
kept track of
prav I need to keep track of what we want to ask and I need a cap to keep track of what the answer is.
so what I'm actually going to do is create
a
question class
so I'm gonna create a little data type for questions
and inside of that question class will be able to store
the question prompts,
and will also be able to store the question's
answer,
so I'm just gonna 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
dot
py so we'll call a question up high
and inside of this question dot pi file I'm going to make a class so I'm just gonna say class
question.
and I'm going to put a
colon here.
so inside of this
question class we want to define it and initialize
function so
I must say deaf
to underscores
in him
too underscores,
and
then we're going to type out an
opening close parentheses
self
and that a colon.
so after this self we wanted to find the different attributes
that will describe or that will
be included in a question so
we're going to include a
prompt
everyone to include an answer so every question we'll have a question prompt
and a question answer
now down here I'm going to take those values ​​and assign them
to
the actual
class objects only to say self
dot
prompt
is either the prompt.
and I must say self dot
answer is equal to answer.
so 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 going to set it equal to
to open or closed square brackets
and we're gonna start creating question
objects so I can just say
the first element in this questions ray will be a
question,
and we want to pass
questioned prompts
zero.
and the answer to the first question was
a
red green and that's the color of apple so I'm gonna pass
that first question.
and I'm also going to pass
the answer which is a,
and you see down here I'm getting an error so
seeing unresolved
references
question
so actually what we need to do before we can
create these questions is we need to import this question class on mckillop been here
come up here and say
from
question
import
question
so now
down here in this questions array we're
not getting that hair anymore so we created one question objects.
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 pace this down here,
or it's an hour creating
two other questions in 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 is that the
new york
Yes
so it's
cmb.
so essentially what we're doing is we're creating
three questions.
each one is getting a different question prompts,
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 these are the questions and it has to check to see
if they got the answer right so
down here
lower in the file
let's create a function.
so I'm just going to say
death.
we'll call this run
test
and
inside of his prentices
were 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've been here
all I want to do is a 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'm gonna create a variable called score
and I'm going to set it equal to zero,
so every time the user answers a question right will increment this
score
variable
right 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 these 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 wanna make a variable down here called
answer.
and this is going to represent the
user's answer to the question.
and I'm gonna
set it
equal to
input
and I need to give this a prompt
so
I'm actually going to give this prompt for the questions I can just say
questioned 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 so I can 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
as 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 results 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 up are not like hey you got like
two out of three questions are you got five out of six questions right
or you got zero out of ten questions right well
what does per not how they did so
to say to you got.
and now I want to print out how many questions they got right
so this is just going to be this 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 say
str score
so basically say you got
however many they got right
out of so we use this little
forward slash.
and
now we'll just say
how many questions there were so we can say.
and then I'm going to have to convert this to a string because it's going to be in numbers we can say string
and then want to figure out how many questions were in that questions array so I can say.
ellie ann
and inside of here we can just say
questions
so this is kind of like
this
long state and just to turn out how many questions that were
so that over here to say
correct.
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 tests.
and I'm just going to pass it that
questions are ready to be created before so
we created this questions array with all of these questions
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 didn't hopefully
we don't getting errors.
there's a lot of code there so
it's possible that we might.
okay so this is what color are apples has a tough one I once 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
be bananas aren't magenta
and we'll get the third question right so it says what color are strawberries
output be cassettes the right answer,
so now
you'll see that our program prints out
you've got two out of three correct.
so not only did to 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 tasks one more time.
and
we'll just like
get two wrong this time so we'll get this one wrong I'll get this one wrong and I get this one right.
so now it says
you got one out of three correct so
say would actually great 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
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 needs were ill bizarrely 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 ron 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 to cobble times on your own and see if you can create your own multiple choice test
in this tutorial on talks 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 where it can
give a specific information about those objects.
over here I have this student dot pi file
and
inside of here I created a student class I was headed class student
and I gave this student
a couple of different attributes I gave it a name,
a major and a gps
so I can give this student.
all of this information so when I create a student objects.
it'll have
all of that information
in it.
now over here on this
app dot pi file it actually created to students
so
student one is
oscar who was studying accounting and has a gps unit three point one
student to as phyllis who's studying business
and she has a three point
e.
so
what we can actually do is we can
use functions inside of these class files so I can actually define a function
instead 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 can define a function
call
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 honorable
and
let's basically just say the rules are being on honor roll is you have to have a
gps iif
three point five or above.
so here gps three point five or above that means you're on the honor roll
good
over here I could actually
write the code for that so I could say
if
self dot gps.
and when I say self over here
this is referring to like
the actual students gps
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 dot jepa
is greater than or
equal to
the read point five.
then we can return
true
because this student
is on the honor roll
otherwise
an else
we can just return
false because
if their gps
isn't above a three point five that
they're not on
the honor roll.
so
that's essentially our little on honorable function and you can see it's a very small function,
but it provides
a service to the objects of this class right
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 dot pi file.
I could just print
out that values ​​so I could say
print
and I'm gonna see if
student one
is on the honor roll.
so
now when I run this you'll see it should print out false
and is putting out false
down here
because student one only has a gps
of
three point one.
if I was to do the same thing on student to
students who has a gdp of three point
eight
so we should return true.
and that's exactly what happens 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
should be like giving us information about the class or it could also be like modifying information about the class.
and so that's the basics of using functions and classes I mean this is a pretty simple tutorial but
I really just wanted to introduce you guys to the concept
of
pudding
with functions
inside of classes this is going to be really useful and
if you're making a class you always want to think about like what functions can I put inside of here
that will
you know essentially
healthy user to either figure out information about the object
or
modify different values ​​in the objects
in this tutorial on antarctic guys about inheritance
in python.
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 could 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 it that's confusing don't worry I'm
sure you guys exactly what this is
in this video are
let me show you about some stop 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 the shaft can do three things.
the chef can
make
chicken.
the chef can
make solid
and the chef can make
a special dish.
and whatever the chef makes a dish
he basically just says
the chef makes a chick in order the chef makes
a salad
or the chef makes barbecue ribs for this special dish.
so basically just
saying whatever the chef is doing so
if I was to come over here to my app dot 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 shafts I'm creating a new chef.
so now if I came down here and I send 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 are on the program.
now it's going to say the chef makes barbecue ribs so it 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
rise to this
chef class is just modeling like some generic schafroff
we're representing some generic chef
in our program,
but let's say that I wanted to create
a
class that modeled the 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 can actually use this
chinese shaft out python file to create our chinese
chef
class
tom's going as a 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 are generic chef can do.
so the chinese chef is like a special
schafroff it's 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 can paste them in here
and so now the chinese chef can do everything
that the other chefs 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 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
lunge
chicken.
all right so now we have our chinese chef class set up
and you'll notice that it can do everything
that
the normal chef could do.
so let's go back over to our app dot 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 employ for
chinese chef.
so let's go ahead and make
a chinese chef
object
sit down here I'm going to make one arrow to say
we.
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 da
make special dish.
when I run this program you'll see
on
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.
I went I wanted to use all of the functionality that was inside this
chuffed 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.
and they especially like imagine if this chef had like
twenty or thirty of these different functions in it.
we have to copy and I have the pace.
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 the parentheses I can say the name of the class that I want to inherit from so I can just say a chef.
and I'm also going to have to import this song to come over here and we'll say
from
chef
in poor
shelf.
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 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 anecdote pi 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 stuff.
the chinese chef will still be able to make
chicken.
so when I run this program
you'll see it out 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 my stuff
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 makes a special dish
function
from this
chef class
is what I can actually do is I could come in here and I could
override
that
make special dishcloth
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 chefs are 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 solid 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 wanna 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 are 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.
the way we use the python interpreter is by opening up
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.
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 window as 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 in 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 you know windows and you know what we can move stuff around we can
you know basically just like
interact with the computer.
with graphics
with the terminal or the command prompt
is a place where you can interact with the computer using text commands.
so you know am I going to get too much into
what the terminal the command prompt dog 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 and 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
three
and since we're using python three on monotype python three.
but you can also just like normal python and I'm pretty sure that gives you python two.
now here's a quick disclaimer.
I'm
on windows,
you may run into a problem
where
you can't use this python three command,
and it's possible that
if you're running into that problem
python
python three hasn't been added to your windows
path variable.
and basically what that means is your computer doesn't recognize the command
python three
and that's the case
all you have to do is just go and google
how to add python three 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 not on your windows
path variables such as google around for that and you should be able to find an answer,
but onsite python three and then click enter
and it's going to open up this cool thing down here so
you can see it basically just says like here's the version of python.
now
I'm going to enter so we can have some like
new lines here
and
put down here this is the python interpreter so we can write
python code in here and it's going to work so I could
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 can say like number one
is equal to ten.
I can say numb two is equal to ninety.
and then I could print
out number one
plus
none too
and it's going to be able to do that for me?
I could also use awesome like a function so I can define a function in here,
we can make a function that is called say hi and it'll take a name parameter.
and
you'll see here when it saw I wanted to create a functional
put these three little dots
and I can index in and we can write the code for a function so I can say like
print
hello
last name.
and now I have a function called
say hi so I can break out of this by this entering again
and I would call the function so I could
I say
hi
and we'll say out of me.
I'm like
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 can easily afford the wicked use while loose.
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
we need to test somethin 'out great this is a perfect place for it.
you'll have to set up a file
you'll have to use like some
id you don't have to execute a filing is going here it's quick it's dirty,
you get it done.
but if you're writing an actual program.
you definitely definitely want to use
a
text editor.
the text editor is 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
havent really been using it at all,
just because I think it's a lot easier to teach when we're inside of a file,
then inside of here but
you know
don't count this guy out
the python interpreter is awesome a lot of people who use it and it's great if you just need a task
you know little bits of code out without having to set up some
huge environment so
python interpreter
definitely awesome out player out there you know have some fun
and take advantage of it.
hey things are washing if you enjoy the video please leave alike answered
garbage off academy to be the first snow and release new content
also were always going to improve so if you have any
constructive criticism or questions or anything leave a common below
finally if you're enjoying job academy and you want to help us grow
head over to draft academy dotcom ford sauce contribute and invest in our future.