Learn Python – Full Course for Beginners [Tutorial]

Learn Python – Full Course for Beginners [Tutorial]


in this course I’m gonna teach you everything you need to know to get started programming in
python.
Python is one of the most popular programming languages out there,
and it’s by far one of the most sought after for jobs
and so if you’re trying to get a job
or you’re trying to you know,
automate your life
or you try to write awesome scripts to do a bunch of different things than
python’s for you
honestly
more and more developers every day are moving their projects over to python because it’s such a powerful
and the surgeon
easy to use language,
a lot of programming languages out there just aren’t very beginner friendly there’s
a lot of syntax there’s a lot of like little things that if you get wrong the program will yell at you.
python is the complete
opposite of that.
you basically just type out what you want to do in python does it it’s that simple there’s not
a whole lot of syntax to learn the learning curve is literally zero you jump in,
you can start writing your first program in seconds.
in this course
I’m gonna teach you guys.
everything you need to know to get started in python.
I designed this course
especially for python,
and each lesson has been specially designed with examples
that will help you along the way.
with so many people starting to learn python the question isn’t,
why should you learn pipe on the question is why shouldn’t you ?
and I think for a lot of people the reason they might not want to learn python is because they’re intimidated or they’re afraid that it’s going to be too hard.
trust me I am going to hold your hand through this entire course we’re going to talk about all the core concepts in python we’re going to look at
everything you need to know
to start programming in python and start being confidence are writing scripts and start writing programs that are awesome in doing cool things in your life.
anyway
I’m super pumped to be teaching you guys python.
I can’t wait to get started in this course
and I hope you guys stick around and fall along with the course and learn this amazing programming language
in this tutorial dominates three guys how to install python onto your computer.
and we’re also going to install a text editor that we can use to write
our python programs
in.
so the first order of business is to actually install
python on your computer.
so we want to do is head over to your web browser
and
you want to go over here to this page it’s just
www dot python dot org
forward slash
downloads
and on this
page there’s
going to be
two buttons down here
and this is going to say download python three point six point three.
and at least
the version that I have right now or download python two point seven point one four.
so here’s the thing about python there’s actually two major versions of python
that are in use
currently
there is
python two
and there’s python theories you can see over here,
this is like two point seven point one four
basically any python two
version we would just refer to as like python two
and here’s like the latest
python three version.
and so there’s these two versions that you can download and the first thing we have to decide on which version do we want to download.
and here’s the basic difference
python
two
is a legacy version of python which basically means that
it’s like an
old version and it’s not being like actively maintained or supported
like officially by python anymore.
python
three is like the future of python it’s like the newest version it’s the one that’s getting actively maintained and supported.
so there’s pros and cons to picking either won
a lot of people say like python too
has been around longer and so there’s more like
libraries there’s basically just more python code written in python two
so therefore like there’s a few more things that you can do in pipe onto then you can do in python three
but then again
python two is not supported and you know,
five or ten years from now
probably no one’s going to be using it.
python three it like a site is like the future of python it’s the newest version it’s the one that
you know it’s sort of being maintained going forward.
so for the purposes of this tutorial I’m going to be teaching you guys
python
three
so we’re going to want to download python
three
now here’s vaguely
if you learn python three or you learn python to the differences aren’t very big.
there are a few differences in like some syntax and just so
you know little things here and there,
but if you learn python three you’ll be able to jump right in and
code in python two as well don’t let anyone
tell you that
you have to learn one of the other.
we’re going to learn python three just because it’s I think probably the best for a beginner to learn.
and so that’s the one that we want to download so I’m just going to click download python three
and we’re gonna go ahead and download
this version.
and when that’s done downloading I’m going to head down to my downloads folder
and you’ll see here we just have this file
doubleclick,
and we get this like python installer so I’m going to click through here,
and we’ll be able to install python three on our computer
right when that’s done and stalling we can just close out of this and we should begin to get us now we have python three
installed on our computer.
the next step
is we need to choose a text
editor so
we’re going to be writing a bunch of code so we’re going to need some you know environment of some program that we can write all that code in.
and really you can write python in any text editor you could write it in like a note pad or tax at it,
it doesn’t really matter
but there are special
text editors that are designed
just for writing python code.
and these are called
id ease and not stands for
integrated development environment.
it’s basically just a special environment where we can you know ron and execute our python code.
and it’ll you know basically like tell us how we’re doing so were
the only if we write something that’s wrong or we
have errors it’ll kind of
point us in the right direction of what we need to do to fix that stuff,
so in this course we’re going to be using an idea
and
one of my favorites
and one of the most popular ity ease for python is called pi charm.
so I’m over here on this website.
it’s called
jet brains
dotcom
forward slash
pi charm.
and this is the idea that we’re gonna be using in this course
so I’m just gonna come over here and click this big download
now button
and we’ll go ahead and
download this
pie charm program
right so I’m getting this page are here you can see there’s two versions of pi charm
one as a professional version and that one’s like paid they have to pay for it.
but then there’s this community version down here and that’s free and open source so I’m going to download this one
and this should have everything we need to get started using python
right when that finishes downloading I’m just going to
pull that up in my downloads folder
and
again we can just sort of
run this guy
and I’m on a mac so I’m going to have to drag it over to my applications folder.
all right so now have pi charm
and we have python
three installed on your computer.
so we’re ready to start programming in
python.
in this tutorial we’re going to create our first
python program.
and we’re going to be able to run it and
see how everything works.
the first thing I want to do is open up
pi charm so pi charm was the
idea he the
integrated development environment
that we downloaded in the last tutorial so
I’m just going to go out and search for it on my computer
and I can just open it up
and we first opened up high charm we should basically just get a window
prompting us to create a project.
so you can see that down here.
actually the first thing I want to do though is come over here to configure
and I’m just going to
click on preferences.
and I actually want to
change the appearance
so
I’m gonna go ahead and
give this a theme so I just went over here to appearance and behavior appearance
and I’m gonna change the theme to
darker color
because I think it’s a little bit easier to
use and look at,
but you don’t have to do that if you don’t want you
so
down here
I’m just going to click
create new projects
and it’s going to
give us this little window right here so I can just name my project.
and I’m just going to name this
graph.
and then there’s other option down here where it says interpreter.
now
what we wanna select
is
python version three
so if I click down here you’ll notice that there’s a couple different options.
so I’m on a mac computer
and
by default mac has
python
version two installed on it.
if you’re on a windows machine I’m not sure if it does but
in this tutorial we’re going to be using python three so you wanna make sure that you have python
three selected as the interpreter,
otherwise
you
might not be able to fully follow along with what we’re doing in this video.
so now it’s going to come down here and click
create.
and this is going to go ahead and create our projects
for us
right so now we have
our
python project
up and running,
and I’m sure you guys how we can create our first
python program,
so over here in this draft folder and this is just that project that we created.
I’m just going to right click
and I’m saying new
and I’m going to come down and click
python file so we want to create a new python file this will be our first
python program.
and
I can just give this a name,
so why don’t we just call this
app
and it’s going to click ok
all right so now we have our first
python file
open
and we can just start typing in some python so I want to show you guys.
does a very basic python program.
and this is just going to be a hello world programs so we’re just gonna print something out onto the screen.
so when I say
what I want to do is I want to type out p r iyer anti prince
and wanted to have an
open and closed
parentheses
now
inside of those parentheses we can make some
quotation marks
and inside of the quotation marks I’m just going to tape out
hello
world.
so this is like a very basic python program.
and what this is gonna do is
it’s just going to print out hello world
onto the screen.
and so now what I can do
is I can actually run this file and we’re actually getting a little arrow here it wants us to create a new line
at the end of the file.
and so now in order to run our python program I’m just going to come up here to the top
and I’m cinematic run
and we’ll click this run button right here
and I will see what happens
so it’s
asking me what I want to run I can just click add up
and
down here at the bottom you’ll notice that we have this text that just got printed out
hello world,
so
whatever I
print
or whatever I put inside of this print statement is going to get printed out
down below in the council so
as long as you’re getting that print statements work then you
actually written your first python program.
and as we go through it
forward in the course were can be writing
all sorts of awesome python programs.
and we’re going to do an
all sorts of cool stuff
in this tutorial I wanna talk to you about writing a basic python programs so we’re going to write it as a very simple python program.
we’re going to look at how we can go ahead and write up programs we’re going to talk about
how our programs are getting executed by python
and we’re also going to draw out a little shape onto the screen so
this is going to be pretty cool I’m excited to
talk you guys about just the
bare basics of python,
and really just sort of give you an introduction so
over here
we have our
basic text editor and so
any of the
python code that I write inside of this file
is actually going to get executed by a python so
this python file
has a bunch of
python code inside of it.
and basically what we’re doing when we’re
programming in python is we’re just giving the computer a set of instructions.
right that’s really what programming is it’s just like we’re specifying a set of instructions
and the computer is going to go off and execute those instructions that we give it.
and so
the more complex the instructions we give to the computer the more complex
the tasks
the computer can
carry up.
so I’ll show you guys how we can use some python instructions or some python code
in order to draw out a shape onto the screen so
I’m actually going to draw out
a little triangle.
so over here,
we can actually print something out onto the
screen and python using something called a print statement so I can just type out
print.
and inside of these parentheses.
I can just type
some quotation marks
and inside the quotation marks
we can
put whatever we want to print out onto the screen.
so I’m gonna
copy this and I’m actually going to make
a few of these little prince statements
and we’re going actually draw a sheep.
so
we’re gonna draw like
a triangle type shape
so down here it’s gonna draw a forward slash
and then up here I’ll make a space and we’ll do another foreign sash
here will make
two spaces
with united forward slash.
and over here
we can make three spaces and afford sash.
and now I’m going to draw like a vertical bar is going all the way down.
and actually we can do some underscores here as well.
I think high school
right so now we have our triangle looking shave and you can see
it looks kind of like a right triangle
so I can save my file.
and I can either come up here to run and
click run app
or of using pie charm you can also just come over here and click this play button
and when I click the play button I want you to notice what happens down here
so
down here we have this little window and
this is called the consul
and it basically the council is a place where python is going to
output some information so
when we use this print statement were basically telling python late
hey can you print something out onto the consul.
so
there’s a lot of situations where we’re gonna wanna see what’s going on in our code.
and we can use that print statement and
print things out to the council.
and we can basically like have a little window into what our programs doing
in our case we’re just going to be printing out this triangle onto the consul.
so I’m gonna come over here and click this play button
and you’ll see down here
we’re actually printing out that
triangle
so mission accomplished we were able to print out our shape.
so I can really draw any shape I want it as long as I was
able to
specify
inside of these print statements.
so
essentially what python’s doing when I click that play button
is it’s going into this file and it’s looking at all of these instructions in order.
so the first thing it does is it goes to this instruction and it says
ok,
they want me to draw out these like little forward slash in this vertical bar onto the strain,
once it does that it goes down to this instruction it says
ok
they want me to print something on again
when it prints dot out it goes down to this instruction
etc
so
python is actually going to execute the lines of code that we write
in order.
so
the order in which I write the instructions
matters a lot.
so for example if I was to take this last line here
and put it up here at the top.
now when I save the file.
instead of just drawing up that triangle.
it’s going to draw out
this little upside down
bottom thing at the top and then
the rest of the triangle down here so
the order of the instructions actually matters a lot.
so this is basically how your python programs are going to go.
as we go through this course we’re going to learn more and more of these little lines so here we’re just using this little print function basically and just print something out on to the screen,
there’s a bunch of little things like this that we can learn as we go through
will learn more and more instructions that we can give to the computer.
but in this lesson I just wanted to kind of show you guys the basics right.
oh
we can essentially just define these little instructions for the computer
and the computer will execute them
in order.
and this tutorial I wanna talk to you guys about using variables in
python.
now in python and you’re going to be dealing with
a lot of data
and so a lot of times in our program we’re going to be working with
all types of information and data and values,
and sometimes that data
can be difficult to manage.
so in python we have this thing called a variable.
and it’s basically just a container
where we can store certain
data values.
and when we use a variable when we put those
data values inside containers.
it makes it a lot
easier for us to work with and manage
all of the different
data inside of our programs.
so variables are extremely helpful and it’s sort of like a
core topic in
python that you’re really going to want to know and master.
so in this tutorial omelets each you guys.
everything you need to know to get started with variables we’re going to look at what they are
why they’re useful and we’ll look at the different types of variables
and the different types of
data that we can store
inside of variables,
who over here in my
python file I just have a very simple program.
and it’s basically just printing some text out onto the screen.
and so this is actually a little story that I wrote it says.
there once was a man named george
he was seventy years old.
he really liked the name george
but didn’t like being seventy.
so this is a valid python program I can go ahead and
run this program
and you’ll see down here in the council
everything prints out and
it looks great so
we have an awesome little python program here.
well let’s say that inside of our story I wanted to change
the character’s name
so instead of naming the character george let’s say I wanted to name the character
jon
well
I’m going to have to go through and I’m going to have to manually change the name george to the name john at every place inside of this story where it’s mentioned.
so I’m not to go over here and I’ll say
ok
john I’m going to have to manually change that
and then
ok it’s out here we’re going up to again manually type in john and change it to john.
so in order to change the character’s name I had to manually go in and change it in both places.
and now let’s say
ok
maybe we want to change the characters ne
age also so in addition to changing the name.
we also want to change the age let’s make him a little bit younger so why don’t we say john is going to be thirty five.
so you see here I had to come here and manually change the age and then come down here
and manually change it again
right.
and now that we changed it like
it’s going to work it’s going to be updated in our program.
but you’ll notice that in order to make that change like I had to look through the entire program find it where that
value was and change it,
and
this was only with
four lines
of the story imagine if I had a story that was like
thousands of lines long.
and we mentioned that the character’s name and
age like
hundreds of times.
we don’t have to look through each one of those lines and
manually change the character’s name
and that is not a very good way for us to do this
and
it’s really not a good way for us to manage the data in our program right the
character’s name in the age.
and so we can actually use a variable
in order to store
the character’s name and the characters.
age.
and when we use that variable it’ll make it a lot
easier for us to put the character’s name and age.
inside of our little program here.
so I’m gonna show you guys how we can create a variable for the character’s name and age.
now it’s going to go up here above this print statement.
and
up here I want to create a variable.
so when we create a variable in
python we actually need to give
python
a couple pieces of information.
the first piece of information we need to give python is the name of the variable so I need to actually assign a name
to this container where we’re going to be storing information.
so all I have to do is just type out
the name of the variable
that I want to create so I’m going to create a
variable called character name.
so I’m just going to say
character
adam’s they underscore
name
so generally when you’re creating a name
for a variable in python.
you want to separate different words
with an underscore so I have two words here and I’m separating them with an underscore.
and now what I need to do is I need to
put a value
inside of this very more so
basically what I can do is I can just say
equals
and over here I can type a value
so I’m just gonna type out the character’s name inside of
quotation marks.
so we’re just gonna type out john.
so now we have a variable for the character’s name
and below this character named variable I want to create another variable called
character
age certain just going to tape out
character
underscore
age.
and I’m going to set this
equal to the characters age which is thirty five.
so we now have two variables,
one representing the character’s name and one representing the characters
age.
so what I can do now as I can replace
the character’s name inside of this story
with
this variable.
and I’m gonna show you guys how to do that
so if we want to put a variable inside of this print statement.
I’m actually going to have to do a couple of things.
the first thing I’m going to have to do is
end off this
text in here so I’m going to have to put
a
quotation marks here the end
and you can see I’m basically
wrapping this whole thing into a
single like quoted line.
and now I also have this tax overhear the end so I’m going to have to put a quotation mark here.
so now I have
a
bunch of tax year in quotation marks.
and then I also have tax over here in quotation marks
now inside of here
instead of
saying the character’s name
I want to refer to that character name
variable
so I’m just going to say plus
character
underscore name
and I want to say another plus sign so I basically I’m saying I want to
print out all of this text,
plus
the value that stored inside of character name
plus
all of this text.
and what this will do is they will actually print out the character’s name we print this
so I’m gonna go ahead and run this program
and you’ll see down here
and actually let me put a space over here,
and we’ll run it again.
so you can see down here,
we still are printing out there once was a man named
john.
and so what python is doing is when it executes this line of code
it’s coming over here and it’s seeing
ok they want to turn out.
the value inside of character name
so it
python’s going to go up here it’s going to get the value,
and it’s going to
insert it inside of there
so I can basically do this for every instance of the character name in my file so I can
you know
r is copy this guy right here
and
I can just paste it down here where the characters name is
so apace this
and you can see now were doing exactly the same thing that we did up there.
I can also do this for the characters
age someone to come down here
and I’ll paste in what we just paste it in.
and I’m going to change this instead of character name was changed to character
age.
and I’m going to do this
in one more spots are going to do this over here we’re mentioning the age
and again I’ll just change this to
carry
your age.
and so now when I run this program it’s going to print out the same story they were printing out before,
but
now I don’t actually have to type in the character’s name and the characters age.
I can just refer to this
variable.
and the cool thing about variables as if I wanted to change the character’s name.
now all I have to do is change it up here so I could change the characters names you like
tom or something.
and we could also change their age so I could change
the age to be like fifty
and now that’s going to automatically update throughout our entire story
when I click this play button you’ll see
now we’re using the name tom
and he’s fifty years old.
so that’s where variables can come in handy and
this can be a really awesome way to control and manage
the data that’s inside of our programs
with variables you can also
modify their values
so
for example let’s say that halfway through the story I wanted to change the character’s name
why can just make some new lines over here,
and down here
I can actually assign a new
value to one of these variables.
so I can say like
character
underscore name,
and in order to give it a different value all I have to do is just say equals
and will give it a different value and so
let’s say halfway through the story we want to change the character’s name to mike
one now when I run my program
you’ll see here in the first part of the story it says
there once was a man named
tom.
and in the second part of the story,
it’s referring to the name as
mike.
so I actually
updated the variable
inside of my program
over here
and that’s a really awesome way
you know a lot of times in these programs you’re going to want to be modifying the values of variables
so you can do it
just like that.
and so finally I want to talk to you guys about the different
types of data that we can store inside of these variables.
so over here
I have the character name and character age
and
I’m actually storing what are called strings,
so a string is basically just
plain tax straight
so the name tom that’s just plain text
down here these guys are strings as well.
so there’s a data type in python,
called a string and it’s just
basically going to store
plain text
so
you know any text that you would have like in a story early know someone’s name
you can store inside of a string.
there’s also another type of data
that we can store inside of python
which is numbers
so an additional storing text we can also store
numbers.
so if I wanted to start a number for example
let’s say we wanted to store the character age instead of inside of a string.
we can store it inside of a number so
I could just type out fifty.
and when we’re storing a number we don’t need these quotation marks
you only need that quotation mark
when you’re storing a string so if I wanted to a number I can just
write it out like this,
and in addition to using whole numbers I can also use decimal numbers.
so I could say like fifty point five six seven eight
to one three
and pythons are going to be able to store
that number as well.
you can store all different types of numbers
and we can also store
what’s called a boolean value
and a boolean value is essentially a true or false value.
and there’s a lot of instances in programming where we’re going to want to represent
true or false
data
for example I can have a variable called like
his mail
and this would tell me whether or not someone was a male,
and in my case it will be true because I’m a guy right so I can
actually this needs to be
capital
so
I could sort of value of either true
or
false.
and actually if we were naming this in python
we want to use an underscore or so I would say
is underscore mail
and this can be either true or false
so
true or false values is maybe not something that you’re used to dealing with like in the real world
but
in programming we’re going to be using a true or false values
all the time they’re super important so
like I said there’s three basic types of data that we can work with in python there’s
strings which is just like a plain text,
numbers which would be like either decimal numbers or whole numbers like this,
and there’s true or false
values.
and these are the basic types of data I mean there’s a bunch of other types of data that we can use but I would say ninety nine percent of the time.
as a new user
in a new programmer to python,
you’re just going to be dealing with these
three types of data
so
those are the basics
of the data
and
also with variables
and you’re going to be using variables
all the time in python so you definitely want to practice up and get comfortable
using them
in this tutorial are talks you guys about working with strings in python.
now what the most common types of
data that we’re going to be working with in python is going to be strings
and strings are basically just
plain text so any text
that we want to have inside of our program
we can store inside of a string.
so I wanna talk to you guys about all the cool things we can do with strings and we’re basically just going to get a
full introduction into why strings are awesome.
so over here,
I’m just going to actually print out
a strings I’m just going to say print
and
inside of these parentheses.
I can type out a string
in order to create a string I need to use quotation marks so I can make an
open and close quotation marks
just like that
now
inside
the quotation mark I can put whatever text
I want the string to have so we could say like
draff
academy.
and so now we have a string
with the text
draff academy inside of it
so if I run my program.
now down here we’re going to print out draff academy as you can see,
and when we have these strings there’s actually a bunch of cool things that we can do with them so
one thing I could do would be to create a new line inside of the string so if I wanted I could come over here,
and I could save backslash
n
and you can see it got highlighted in a different color.
and what this is gonna do is it’s actually going to insert a new line into the string so now it’s going to say draff on one line
and academy on another line.
so I’m gonna click play.
and you’ll see down here we just get draff academy,
an addition to the backslash
n
I could also use
a backslash
quotation marks so
if I wanted to put a quotation mark inside of my string.
I can’t just put a quotation mark like that
because pythons going to think that I’m trying to end
this strength.
so if I want to include a quotation mark I can just use this
special backslash character
and that’s called the escape character and it
basically just tells python that
whatever character comes after it we want to
render a literally so
when I say backslash
quotation mark.
it basically means like
hey python
I want to point out a quotation mark.
and so now we’ll be able to print
out
a quotation mark
right there.
I can also use this to print out of backslash so if I needed to put out a backslash.
I could see backslash,
and
it’ll just print out a normal backslash now
so you can see just like that so
if you want and you can use that backslash to make
new lines or pronounce quotation marks,
or you can just use it as a normal
backslash
in addition to just typing out a string here I could also create a string
variable.
so I could come down here
up here and we can create a variable and let’s call it
phrase.
and I’m going to set it
equal to
drop off a colony,
so I can
store this string value inside of a variable called phrase
and then when I want to print
out that variable,
or I want to access that string variable.
I can just
type the name of
the variable.
and you’ll see that it’s going to print out the value that was stored inside of it.
I can also use something called concatenation
and concatenation is basically the process of taking a string and
appending another string onto it.
so I could come over here and I could say phrase,
and I can say
plus.
and now I can add in another string so I could say like
is cool.
and now this is going to say
druff academy
is
cool,
so I’m basically appending another string
onto another one they call that concatenation.
and so in addition to doing all that stuff we can also use a special thing is called
functions
and a function is basically just a little block of code that we can run
and it will perform a specific
operation for us,
so we can use functions to modify our strings.
we can also use functions to get information about our strings.
so
I’m gonna show you guys a couple common functions we can use with the 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 lower case.
I could also do the same thing for 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 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
l e n
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’s 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
ei,
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
a c a d.
and this is going to tell me where this starts
inside of my string so when I click play,
it’s going to give me an
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 a z in here,
that’s not in here.
it’s actually going to throw an error so when I play this you’ll see that we get this error down here
because
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
ok
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
my 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 s t r
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
my
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 p o w
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 it’s 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
f l o r
and what this will do is it’ll basically just grab
the
lowest number so it’s essentially just going to chop off this decimal point.
and you can see over here now
we get a three because we’re using that floor function.
there’s also another one called
seal.
and that’s just going to do the exact opposite so that’s just going 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 that is right now,
just know that when we put this line of code into our program
it gives us access to a lot more
math
function so 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 wanna 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 always is going to be like.
ok
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 n t,
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..>
yeah
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 colour 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
the
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 colour 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 one so
just keep that in mind.
so there’s actually other ways that we can access like just
portions of this list so for example,
let’s say I just wanted to select the last two elements in this list and I didn’t want 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 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 a 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’re going to
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
the
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 j k
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 x y 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 can’t 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
d e f
and
basically when
python sees this keyword it’s going to say
okay
this person wants to use a function.
so after we type out “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’ll 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
ok
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 parameter so 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
the
function can just communicate back to us like hey,
here’s some information.
and that’s what the return keyword can allow us to do
the return keyword can basically allow
python 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
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’ll 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 the
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 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 this 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
like
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 reducible 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
else
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.
else
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
tall
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
that 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
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 so
this is going to be pretty cool and this is really going to give you guys a full understanding of what you can do with if statements so
for this tutorial I actually want to create
a python
function
and this function is going to
give us the maximum number that we pass into it so
this function is going to take three parameters as input,
and it’s going to print
out
the
biggest
number that we give it so
let’s create this function I 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
once again
these are
comparison
operators and
this one is greater than or equal to but there’s a bunch of other comparison operators we can use so
the most basic is just going to be a double equal sign
and 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 this
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 in 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 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 are they 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
o p
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
l f
o p
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
else
statement so
in the case that the user enters in an operation.
that’s
not
like one of the four that we specified up here I just want to throw an error sums usa
opie
are actually we just say
else
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 do 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
a n
two
january
or I could convert
an
a r
into
march.
like I want to be able to have a program that can do that for us and that’s actually a pretty good use case for a dictionary so
we’re actually going to create a dictionary.
and I’m 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
j n
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 j n,
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.
the
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
say
month conversions dot
get
an inside of here I could also pass in the name of the key so I could pass in like
d c
and now we’ll be printing out
december.
and what’s cool about using this get function
is I can actually specify a default value that I want to use.
if this 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 normally would 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
ok
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
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 continually ask the person
to guess the word
until they guess it correctly.
so let’s go ahead and create our while loop so I’m 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 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
cao
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.
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
guess
count
is less then
guess
limit
if gas count is less than guess limit.
that means that they haven’t
guessed
the
total number of guesses that they have so they have some guesses left.
and
if that’s the case and if that’s true,
then I’m going to go ahead and
give them a guess and then we’ll increment the 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 wasn’t 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 anymore guesses so
I’m going to go ahead and add another condition onto here
and I’m gonna say
wow
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’s 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
each time
it will most likely have a different
value so
in our case I’m just going to call this letter.
and I’m going to 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
each
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’s 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
each
individual friend inside of this list.
just like we did before but now I’m doing it with
a range,
so you can see I’m
typing out jim care and kevin so
actually
for each
iteration through this loop
it’s basically going to be printing out
friends
zero
friends one
and then friends
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
specifies
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,
actually will 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 be a pretty cool lesson.
first thing I want to show you guys though are
two dimensional lists so down here in our
python file.
I just want to create
a
basic list
so I’m just going to call this
number grid.
and I’m 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
themselves
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
each
individual
column or each individual
element inside of these
arrays
so for example inside of the arrays that are
the elements of the number grid
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
become
g
so if I was going to translate an english word into
a draft word
for example the word dog,
I would look through the word dog
and
any instances where there was a vowel
so
in our case let’s just say
a e I o u.
I would convert that vowel
into a g
so dog would map to
ddg
cat would get mapped
into
c g t.
so those are the basic translation rules.
obviously this is a pretty simple language but
you get the point any vowel is going to become a
g and then we’ll get
the draft language.
so we’re going to build
a draft
translator
over here in our
python file.
let’s
start making this translator the first thing I’m going to do is just define a translate function.
so I’m going to make a function here and I’m just going to call it
translate
and
inside of these parentheses
we want this to take
one
piece of information which is going to be
the phrase
that we want to translate so I’m gonna say
phrase
and now inside of this function we need to figure out like
ok
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 for 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
each
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
a g onto translation.
if it’s not available
then we can just add on whatever
letter it was onto translation
anyway so
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 a e I o u.
and
a e
I
o to 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 a g 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
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.
okay
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 of 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
right
you want to be able to handle all these different situations,
and
in order to handle them we can use
something called a try except block
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 long period the 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 so
for example if
this ends up breaking the program this division by zero,
then it’ll be able to catch that so down here it’ll tell us.
hey
you divided by zero,
but if I was to get rid of
this line of code,
and
I just
entered in the number wrong
so I came down here and I 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
e r 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
the
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
a w 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 isn’t 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
do 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.
well
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’re 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’s
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
I need to
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’s some sort of functionality that you want 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 module 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
isn’t
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 obviously is 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 c m d
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 modelling
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 a d e f
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
an
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 p a
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
the
actual question itself.
and then there’s the answer to the question in both of those attributes needs to be
kept track of
right
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
p y 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 it
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
yeah
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 this questions array.
I want to do something.
and
basically the first thing I want to do is ask the user the question
and I want to store their response inside of a variable.
so I’m going to say
I 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 the 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 s t r,
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 question
objects inside of it.
I’m basically just going to pass that into this run test
function.
so now let’s run our program and
see how we 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 honours
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 honourable
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
well
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 j e p a
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
met functions
inside of classes this is going to be really useful and
if you’re making a class you always wanna 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 so
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 modelling 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
my.
and then
my chinese chef.
we can print out this special dish so
remember the chinese chef can do
everything that the normal chef could do
so if I say my chinese chef da
make special dish.
when I run this program you’ll see
the
regular chef is making barbecue ribs and the chinese chef is making orange
chicken
so everything works out.
and this chinese chef also has an extra method called make
fried rice.
and
you can also
make
fried rice.
but here’s the problem right when I go over here to this
chinese chef dot pie.
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 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 it’s 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
say
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 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.

100 Comments on “Learn Python – Full Course for Beginners [Tutorial]”

  1. Super good tutorial! coding just came to my mind some hours ago and i just really wanted to learn it and i found this tutorial and loved it thank you.

  2. This is the best coach ave ever come across, also followed his 4 hours long SQL tutorial and felt like i just covered 4 years worth of CS in 4 hours

  3. I douwnloaded Python a few years ago (probably version 2.0) and I wrote a fairly simple Craps (the dice game in a casino) simulator. Fairly simple program with a couple nested loops and if/then/else statements. My laptop got fried and I lost it all, so I'm trying to rebuild it.

    I feel like there was a built in IDE where I could write all my code, then execute it, and Python would produce a new file that would spit out all the data I was interested in. So am I missing something, or does the new version not have the IDE? I can't seem to get Pycharm on my computer.

    I'm only 20 mins into this, but I suspect that a lot of this video is too beginner for me, and I'd like to try to jump into what I was doing a few years ago.

  4. If you have windows make sure to click on "Add Python to PATH" when installing, otherwise you won't be able to run python commands from the command prompt (he goes into this later in the tutorial when running pip).

  5. This was perfect for me. I hadn't messed with programming since my Pascal days. Great instructor and the pace was just right.

  6. Anyone else run into the issue of their site-packages folder being orange and not being able to import docx?

    [Edit] I think I solved this. The issue was in how I was creating new projects. If you are creating a new environment (a virtual machine) to develop a project in, you must select "inherit global site-packages" for the packages to be visible to the virtual machine. Alternately, if you choose the existing interpreter option, and then select wherever your python.exe interpreter is located (usually C:Users…AppDataLocalProgramsPythonPython??python.exe), you should be fine as in this case I don't believe you are creating a separate virtual environment. Also, AppData is a hidden folder so you'll have to select "Show Hidden Files and Directories" to enter it. I know, confusing af (especially for a beginner), and I'm not sure if I have it correct conceptually, but it seems to be working for me.

    Note: I am running Windows 10, have python 3.8.0 and PyCharm 2019.2

  7. I tried to simplify the guessing game cause I thought that a couple of variables were redundant imo, nice tutorial btw. My changes were:

    secret_word = "girrafe"

    guess = ""

    guess_limit = 3

    while guess != secret_word and guess_limit != 0:

    guess = input("Enter guess: ")

    guess_limit -= 1

    if guess_limit == 0 and guess != secret_word:

    print("Out of guesses, SUCKA")

    else:

    print("Well done bub")

  8. Well, I am in the CJ field and looking to get into IT. Coding is the way and I don’t give dam about sitting at desk to code. If I would have gave this 4 hour long video a chance then by now I could be on a intermediate level for python. After I spent 10 mins and comparing what he taught me to the book. My leading Curve is about to go through the roof and great job.

  9. Does anyone know how to make the "run" button on the top right corner of pycharm automatically green and ready to run when i open a new project? Thanks!

  10. Can anyone also recommend Khan Academy for programming??

    I am hyped for this video!! Will watch later today it's late now…

  11. 0:00 introduction

    1:45 installing python and pycharm

    6:40 setup and hello world

    10:23 Drawing a shape

    15:06 Variables and Data types

    27:03 Working with strings

    38:18 Working with numbers

    48:26 Getting input from users

    52:37 building a basic calculator

    58:27 Mad libs Game

    1:03:10 Lists

    1:10:44 List Functions

    1:18:57 Tuples

    1:24:15 Functions

    1:34:11 Return statement

    1:40:06 If statement

    1:54:07 If statements and comparisons

    2:00:37 Building a better calculator

    2:07:17 Dictionaries

    2:14:13 While loop

    2:20:21 Building a Guessing game

    2:32:44 For loop

    2:41:20 Exponent Function

    2:47:13 2D lists and nested loop

    2:52:41 Building a Translator

    3:00:18 Comments

    3:04:17 Try/Except

    3:12:41 Reading Files

    3:21:26 Writing a file

    3:28:13 Modules and pip

    3:43:56 Classes and Objects

    3:57:37 Building a Multiple choice quiz

    4:08:28 Object Function

    4:12:37 Inheritance

    4:20:43 Python interpreter

  12. Anyone else noticed that all the examples are based on characters from the US office TV series
    Also thanks Mike…great course!

  13. Unlike college this guy doesn’t repeat himself over and over, taking forever to explain the simplest concept. He explains everything without over explaining. This guy is an OG.

  14. what if you want to sort a list with numbers as strings , ["7" , "5" , "24" , "19"]
    Will .sort() work on it ? (im currently away from my PC i will obviously check it at home)

  15. i am unable to run pycharm it is showing:

    "Fatal Python error: initfsencoding: unable to load the file system codec

    ModuleNotFoundError: No module named 'encodings'

    Current thread 0x000043c0 (most recent call first):

    Process finished with exit code -1073740791 (0xC0000409)"

    can you please help

  16. print(str(my_num) + ' my fav num'
    can be shortened to
    print(my_num, ' my fav num')
    isn't that much more simple? Or is there a difference?

  17. In the first variables example you put a space after the comma, but not after the full stop (period). But both printed ok – so I guess it doesn't matter? 23:05

  18. I watched the whole thing, but somehow I feel like I knew that already from the time I started to learn C, the problem is with all knowledge I still don't know how create a "real" program with Windows and options and buttons :/

  19. I tried madlibbing before you said anything and I was wondering if this was okay…
    I did this before you explained so…
    print('She is', input('Adjective: ') + '!')

    print('She is', input('Verb: ') + '!')

    print('She is really', input('Verb: ') + '!')

    print('She is', input('Emotion: ') + '!')

    print('She needs to', input('Noun: ') + '!')

  20. i just wanna say that i appreciate everything you have done for us and you have helped us in so many ways. thanks so much!

  21. Great job, man. I'm a 54 year old programmer with 39 years of programming experience, and enjoyed watching your clear and simple presentation. I haven't used Python in the past, but will very likely be using it in the future.

  22. Question: at ~26:02 he takes the quotation marks off of the number and says you don't need them. But when I try that, I get an error saying I cannot concatenate an int with a str. Am I doing something wrong?

  23. You are a GREAT teacher. I admire your ability to explain difficult concepts with simple but effective examples. Thank you for providing this video to the public.

  24. Why do so many people like this language? It's the ugliest thing I've ever seen – indents replacing brackets (i.e. no individualism) – completely unreadable

  25. yo vertical bars what? how is it htat you get vertical bars? not to tech savy so feel free to haze but if you could also let me know

  26. Hi! can someone explain why the return statement at 1:37:53 is necessary if I can do it without the statement? Thanks.
    def cube(num):

    print(num*num*num)

    cube(3)

Leave a Reply

Your email address will not be published. Required fields are marked *