A2 Basic US 12 Folder Collection
After playing the video, you can click or select the word to look it up in the dictionary.
Loading...
Report Subtitle Errors
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
w