[Music]
hey what's going on guys welcome to my
Python crash course for beginners now I
did a crash course about a year ago and
we use the Eclipse IDE and what I've
always hated about that video is the
unnecessary amount of time that I took
just to set up eclipse it was like 10 10
12 minutes or something like that
and I wouldn't even suggest using
Eclipse anymore we're gonna be using vs
code which is what I use for virtually
everything I do I also miss some things
and it just could have been a better
video so we're actually gonna just jump
right into it and what I'm doing is
using the Python sandbox which is
actually from my django course on udemy
I'll put a link in the description if
you're interested on in that and if you
look in here and that the link to this
is in the description to this repository
so we just have a bunch of Stata files
for certain things we'll be looking at
like strings functions classes and they
basically have nothing in them but maybe
some comments and we're gonna add to
these as we move along so you might want
to download the sandbox and go ahead and
open it up in vs code or whatever our
text editor or IDE that you're using so
as you can see I already have all the
files open here and we're going to be
starting with variables dot pi so you
can go ahead and open that now obviously
you need to have Python installed so
that your system can interpret the
Python code so you need to have python 3
which you can get from python or just go
to downloads I'm on a Mac so I would
just click here if you're on Windows you
can download the Installer so you just
want to get that installed and then open
up a terminal vs code has an integrated
terminal that I'll be using and if you
say python - - version on a mac you're
probably gonna see python 2.7
which is not what we want to use once
you install python 3 if you say python 3
- - version you'll see either 3.7 or
whether it's a later version or whatever
as long as it's version 3 if you're on
Windows you'll probably see version 3
with just Python ok so whatever whatever
is version 3 that's what you want to run
your file
so let's clear this up and we'll get
started so this variables dot pie
we're gonna go over a couple things
we're gonna go over how to define
variables some of the rules and also
data types okay because just like with
any language there's a bunch of
different data types we're only going to
look at the more common ones there's
there's others that aren't used very
much I'm not going to get into this is a
crash course but basically a variable is
a container for a value which can be
various types and if you've if you know
any other language I'm sure a lot of you
guys know JavaScript if you've been
following my channel you know what a
variable is okay so before we talk about
variables so I just want to quickly look
at how to comment in a Python file and
by the way your Python file should end
with a dot pi extension so a number sign
or hash tag whatever you want to call
this this is how you can do it's just a
regular comment just a single line
comment these triple quotes whether
single or double these are called doc
strings and they're often used to define
functions so you'll see these a lot in
function declarations but they can also
be used for multi-line comments as you
can see here so let's go over some of
the variable rules that are the naming
conventions so there case-sensitive if
you create a variable called name all
lowercase and name all uppercase these
are going to be different variables okay
they must start with a letter or an
underscore they can have numbers but
they cannot start with a number and
that's a very common rule for variables
in in many different languages
alright so let's start off by creating
some variables so I'm going to create
one called X and I'm gonna set it to one
and now in Python there's no semicolons
so if you're coming from JavaScript even
though you don't need semicolons in
JavaScript most people do use them
coming from PHP or Java or any language
like that you're probably used to
semicolons so you might find yourself
screwing up a little hair in there and
putting in a semicolon I know I still do
it but yeah no semicolons also when we
declare a variable we don't have to do
like var or anything like that there's
an
special symbol like PHP it's just simply
the whatever you want has the variable
we also don't have to do like int or
anything like that now this is going to
be casted as an int by default so I'm
just going to put the the variable type
here of int and I'm gonna set another
variable of Y and I'm gonna set that to
2.5 so this is actually a float okay or
a floating-point number next one is
going to be name and I'm gonna put some
quotes here and we'll say John and this
is actually a string or an STR is the
official type I just put these I want to
make this nice and neat just so you guys
can have this as a reference later on
and I will have the finished version in
the github as well
and by the way strings can have single
quotes or double quotes but I prefer a
single and let's do is cool and we'll
set that equal to true and this is a
boolean or a bool okay now it's
important to know that with ilion's you
want to have a capital t for true or a
capital F for false if I do that it's
it's gonna be looked at as a variable
called true which doesn't exist okay so
those are the main types that you'll be
dealing with so there's an easier way if
we want to declare all these variables
we can actually do a multiple assignment
so let's go ahead and say multiple
assignment and again I'm gonna put a lot
of comments in this code in in the
sandbox just so you guys have it as a
reference so let's say we want to do
exactly what we just did above we could
say X Y name and is cool and we could
set that to a set of parentheses and
then we can put the values so one and
two point five string of John and a
boolean of true so this line here does
exactly what we did up here only it's
it's more compact okay so let's go ahead
and output something so in Python we
have
a function called print that we can use
now if if you've ever used Python 2 you
might see something like print hello in
Python 3 you want to make sure that you
use parentheses okay so we want to go
like that
so if I run this file by saying python 3
and the name of the file which is
variables dot pi we get hello okay if i
want to output one of these variables
like x we'll go ahead and run that we
get 1 we can also output multiple things
so we'll do XY name is cool and we'll
run that and you can see it will output
all that stuff all right and of course
we can do most of the stuff that you can
do in any other language like for
instance basic math I could create a
variable called a and set it to X plus y
and if we wanted to add a on the end of
this print right here and run that
you'll see we get 3.5 now we can also
check the type so let's say we wanted to
check the type of X we would do a let's
do a print and in here we're going to
use the type function so we're going to
say type and X and let's see what that
gives us so it gives us this class int
ok so the type is going to be in here
now there may be cases where you want to
cast a different type to a value like
let's say we want to make X which is an
int of 1 let's say we want to make that
a string of 1 so we can do casting so
I'm going to set X equal to and then I'm
gonna use STR okay so we can use the
type and then pass in whatever we want
to turn into a string which is X itself
and then I'll go ahead and run this and
now you can see that we get class STR if
we want to change let's say Y which is a
float into an int we could say y equals
int and we'll pass in Y and let's print
out y and let's also the type of while
it's also print out y
itself okay so what we have now is int y
is now an int and then we just get two
okay so it went from two point five to
two because an int is a whole number
whether or negative or positive let's
say we wanted to change Y back to a
float okay so Y is now an int of two so
I'll create a new variable called Z and
let's say float and pass in Y and let's
see what we get for the value of Z as
well as the type of Z and we get a class
of float and the value is 2.0 okay it's
not going to go back to 2.5 because when
we change it to an int it was then
turned to 2 and then we turned it back
to a float so it's gonna be 2.0 okay so
hopefully that makes sense so we're
gonna move on I don't want this to be
too too long I want to keep it under 90
minutes so we're gonna move on to
strings okay so let's open up strings
dot pi and what I really want to go over
is is like inserting variables into
strings formatting them and I want to
look at some of the string methods and
how how we call those okay all strings
have methods attached to them and we're
going to look at some of those so let's
set a variable up here a couple
variables one of name will say Brad and
let's do age which will be an int of 37
okay now to concatenate which means
basically to insert a variable into a
string we do that pretty much the same
way that we wouldn't JavaScript so we
could say like hello my name is space
and then we'll put a plus sign just like
we would in JavaScript and then if we
run this with Python 3 strings dot PI we
get hello my name is Brad now let's say
we want to put the age in there as well
so we would concatenate back to a string
and I am space and they'll put a plus
sign and the age variable and let's
see what happens when we run that so we
get an error because we can only
concatenate string or STR and age is an
int so what we would do in this
situation if we really wanted to do this
is we would cast this to a string just
like I showed you in the last file so we
just wrapped that in STR and run it and
now it works now there's better ways to
to do this to insert variables into into
strings so we can do string formatting
and one way is to do positional
arguments so let's say arguments by
position and we're gonna say print and
I'm gonna put these curly braces in here
okay actually you know what we'll do
let's yeah let's just do my name is and
then I'm gonna put curly braces and then
name and I am age okay and then what we
want to do is call the format method so
we do dot format and then we can set
name equal to name which is this
variable up here okay and then we're
gonna set age equal to the age variable
so these are just basically placeholders
so whatever whatever is in this whoever
is in this right here should show up
here when we run this so I'm gonna
comment this one out and then run the
file and we get the same thing my name
is brad and i am 37 and notice we didn't
have to do any string format casting or
anything like that
now this is one way to do with this
there's another way and that's using f
strings
okay now f strings were they're only
available in python 3.6 and after okay
so if you're using an earlier version
you shouldn't be able to use f strings
but they're pretty convenient so
basically what we can do is just put an
F right here and then put our quotes and
say hello
my name is name and I am age now that's
all we need because what's going on is
this is an actual variable that's up
here so we're directly putting the
variable into the F string name and age
so if we go ahead comment that out and
run this we get hello my name is Brad
and I am 37
all right so pretty pretty easy and it's
this is very similar to JavaScript
template literals only instead of using
backticks we just put an F right here in
front and we don't use the money sign
like that okay I'm going to be making a
lot of references to JavaScript cuz I
know you know that's that's mostly what
I do on my channel so I know a lot of
you guys are JavaScript developers
alright so let's move on here I'm going
to comment that out and I just want to
look at some string methods and how we
how we do that so let's set a variable
of s and I'm just going to set it to
hello world all lowercase and let's say
we want to capitalize string so we can
cut we can actually use a method called
capitalize so I'm going to do a print
and then all you have to do is take that
string and then dot the met whatever the
method is in this case capitalized okay
make sure we put parentheses okay
because it is a it's a method which is
basically a function so let's go ahead
and save that and we'll run it and we
get hello world and notice that the
first letter is capitalized that's what
it's going to do now I have some other
methods here that I want to show you
what I'm not going to take the time to
type them all out because they're
basically used in the same way it's just
dot and then the method so I'm just
gonna paste them in and go over them
real quick so we have upper which is
going to make all uppercase make all
your characters uppercase you also have
lower we have swapped case which will
swap the cases this is this Len is
actually a function that can be you
dawn strings list spec basically any
data type and it's gonna get the length
so in this case it would get the the
amount of characters in the string we
also have a replace method so if we
wanted to take the word world
and replace it with everyone if we were
to print this out it would say hello
everyone count so if we want to have a
substring like just H we could count the
number of H's inside the string starts
with is going to return a true or false
value based on how it starts so this
would return true because our string
does start with Hello ends with is the
same thing only it's going the other way
its ends with this would return true
because our string does end with D split
is going to take our string and turn it
into a list which is basically an array
and it'll have all the words in the list
okay find find is going to find the
position of a character so in this case
it's going to look for the first R it's
going to give you the number basically
the position number is alphanumeric
alphabetic and numeric these are all
going to return either true or false
based on if all the values are either
alphanumeric alphabetic poor numeric
okay so let's go ahead and run this and
it's just gonna log all of the prints
that we have here so basically this this
list right here is from the split so it
splits hello world into a list find its
gonna look for the position of the first
R which as you can see is 8 all these
the alphanumeric
alphabetic in numeric these will all be
false and the reason for that is because
of the space if I were to get rid of
that space it's now alpha and alpha
numerics so we should get true for those
two so if we run that again you'll see
we have true for both alphanumeric and
alpha of course it's gonna be false for
numeric all right so yeah I mean there's
other methods as well I just wanted to
give you some examples of some of the
string methods so let's go ahead and
clear this up and let's move on so we're
now going to talk about lists
which are very very important in Python
they're basically like arrays so if
you've dealt with JavaScript or PHP and
many other languages it's similar to an
array okay so let's first just create a
list and in the the definition is a
collection which is ordered and
changeable they also allow duplicate
members
okay there's other data types that for
instance are unordered and don't allow
duplicates and we'll get into some of
those in a little bit but let's go ahead
and create a list so I'm gonna say
numbers variable called numbers and
we'll set it to some brackets and we'll
just put a bunch of numbers in here okay
so that's one way to do it we can also
use a constructor so I could say let's
do numbers too and we'll set that to the
list constructor and this is actually
going to take in another set of
parentheses and then we can put our
values in like that okay so this is
gonna both of these are gonna create the
exact same thing a list with 1 through 5
so if I go down here and I go ahead and
print numbers and numbers 2 and we run
this file with Python 3 list dot PI
you'll see that they're both the same
really but this I don't really like
doing it this way I don't really like
using constructors it's just like in
JavaScript when you can say like new
array and then put your array and which
I don't use either so this is a the more
common way to create lists all right now
I'm actually going to create another
list called fruits and this is the one
we're going to work with so this is just
gonna be an array of strings like apples
oranges will do grapes and pears all
right so we have a list of strings now I
want to go over some of the stuff that
we can do here such as
a single value from a list so to get a
value let's do a print and let's pass in
the name of the list which is fruits and
then we just want the index of whatever
it is we want let's say we want oranges
we would use one for that and the reason
we use one is because lists are zero
base ok just like pretty much any array
in any language it's going to be 0 1 2
and so on so if I go ahead and run this
we get oranges now let's say we wanted
to get the length of a list so for that
we use the same Len function that we
used in for a string we just do Len and
then pass in fruits ok so if I go ahead
and run that we get 4 because it's 4 I
for items or values in this list let's
say we want to add to it or I should say
a pen do we want to add to the end of
the of the list so we'll say a pen to
list and we could do that by just saying
fruits dot append and then whatever we
want let's say mangoes and down here
we'll just go ahead and print fruits and
let's run this oops and it's just gonna
go ahead and add mangoes to our list and
then print it out all right at the same
time we can remove stuff so let's say
remove and we'll say fruits dot remove
and then we can simply put the value in
let's say we want to remove grapes ok so
if I save this and we go ahead and run
it
notice that grapes is now gone if we
want to insert it into a specific a
specific position not just append it we
can use insert ok we'll say insert into
position so say fruits dot insert and we
want to pass in where we want to insert
it which is going to be positioned too
and then the item we want to insert
which is strawberries all right so let's
save that
and let's run it now you can see
strawberries is now in position to zero
one two now we can also remove from
certain positions so a Pierre we use
from the remove method and we pass in
the value we can also remove it by the
position by using the pop method say
remove with pop so say fruits dot pop
and let's take strawberries out which is
in position two okay so if we run that
now you can see strawberries is gone we
can also reverse the list by saying
fruits dot reverse you run that now you
can see that mangos is first and apples
is last we can also sort okay if we want
to sort alphabetically we can say fruits
dot sort and if we run that you'll see
it's now in alphabetical order you can
also reverse sort by same thing just
calling sort but passing in reverse
equals the boolean of true and now if we
do that you can see it's sorted in the
opposite direction all right so there's
other stuff there's other methods and
stuff but these are some of the the
really popular ones oh if you wanted to
change a value you could do that as well
so let's actually let's say fruits and
we'll say the first value we want to
change that - I don't know blueberries
okay so if I run that you'll see well I
mean we're doing a reverse sort so it's
at the end but it replaced apples which
was in the zero position with
blueberries okay so that's list let's
move on now to tuples and sets
okay so we want to go to tuples
underscore sets dot pi so a tuple is a
collection which is ordered and
unchangeable okay and also it does allow
duplicate members the important thing
here is unchangeable okay so let me give
you some examples I'm gonna clear this
up down here and let's create a tuple so
we'll call this fruits just like we did
with the array and to create a tuple you
want to use parentheses okay I'll say
apples oranges and grapes now you can
also use a constructor just like you can
with lists and just about any other data
type so I could say equals tuple passing
another set of curly braces apples
oranges and grapes alright and if I want
to print fruits and fruits too and we
want to run Python 3 tuples underscore
set stock pi and there we go so they're
both the same thing
and I don't again I don't really use the
constructor so one thing to remember
with tuples is if you only have one
value you want to leave a trailing comma
okay so if I say fruits 2 equals and I
go just apples like that and what I'm
gonna do is I'm going to print out
fruits 2 and I'm also gonna print the
type okay so I'm going to say type
fruits 2 and let's see what that gives
us so it gives us apples no parentheses
around it and it says class string okay
so if you don't have the trailing comma
it's gonna look at it as a string if I
put a trailing comma like that even
though it looks a little weird if I run
it now it's 4 you can see it's a it's
clear
a tuple and it has a class of two bull
ok so just remember that let's say
single value needs a trailing comma all
right so to get a value from a tuple
it's just like he would with a list so
we could say print let's say fruits one
get rid of this and we'll run it and we
get oranges because oranges in the one
position now I showed you with a list
actually let me just put a comment here
I showed you that with a list we could
say like fruits grab one of the indexes
and change it to let's say pears but
with tuples you can't you can't change
the value okay just right up here it
says unchangeable so let's see what
happens when we when we run this we get
this type error and it says tuple object
does not support item assignment okay
tuples cannot be changed let's see what
else if we want to delete a tuple or
anything really we can use the del
method so we can say del let's say let's
do fruits 2 and then if we were to print
fruits to see what happens
you know see it says right here wait
actually it's still giving us the error
from this so I'm going to comment that
out and run it and it says fruits 2 is
not defined okay we did define it up
here but we deleted it completely so now
it's it's no longer defined all right we
can also use len with with tuples so
let's get the length of fruits
and it'll go ahead and run that and we
get three
okay so fruits has three items in it all
right so let's move on to sets so we
clear this out now a set is a collection
which is unordered okay so it's
unordered and it's unindexed there's no
duplicate members
okay so I'll show you exactly what that
means but to create a set let's use
fruits we already use fruits up here
let's just we'll do fruits set and a set
is created with curly braces and I mean
you kind of have to decide when you're
when you're programming you have to
decide what the best data structure is
to use should you use a list you should
use a set tuple I mean every situation
is different in some work in in certain
situations and some don't
I mean if you if you don't want to have
duplicate members then a set might be
what you're looking for but I'm not
going to get into all of that stuff this
is just about syntax and learning the
basics so a lot of times you'll want to
check if something is in a set and
there's a lot of different ways to do
this but we can just say apples in
fruits set and that's going to return
either a true or a false so if we run
that you'll see it returns true cuz
apples is in the fruit set we can also
add to set and we can do that by saying
fruit set and then we can use the add
method and let's say we want to add
grape okay
and actually we need to print this out
so it's a print
fruit set and we'll run it and down here
you can see that grape has been added to
the set if we want to remove something
we can use the remove method so we could
say fruit set dot remove and let's
remove great okay so if we go ahead and
run this now you'll see that there's no
grape we removed it if we want to clear
the set entirely we can do that as well
so we can say fruit set and we want to
do dot clear now if I try to print fruit
set even though I cleared it it just
gives us basically an empty set okay now
you can delete it altogether which is
different from clearing it out clearing
it oh you still have the fruit set
there's just nothing in it if you want
to delete it with with del and then you
try to print it out it's going to give
you an error and say it's not defined so
deleting it is the same as just never
defining it alright so I think that's
gonna be it for tuples and sets so let's
move right along to dictionaries and
dictionaries are there collections which
are which are unordered they're
changeable and indexed no oh that's one
thing I forgot to show you I meant to
show you this in sets
no not sets yes sets because there's no
duplicate members so I want I wanted to
show you what happens if we were to add
something that's already there
sorry about that guys so I'm gonna go
down here let's see how should it did
this before all this
let's say add duplicate so there's
already apples right so we'll add apples
and if we run that you'll see we don't
get any kind of error or anything like
that it just doesn't add apples twice
okay it's already there it's not gonna
add it twice okay so I just wanted to
show you that before we moved on so a
dictionary is very very similar to an
object literal in JavaScript it's
similar to a hash in Ruby you know every
language they're all very similar than
they have similar data structures but
sometimes they're they're they're named
differently or there's there's subtle
differences but if you know like
JavaScript objects and Jason it's
they're very similar to that so let's
create a dictionary so we'll say person
I'm gonna open up some curly braces and
then we have key value pairs now the
keys should have quotes like that okay
and then we're gonna put this as a
string of John so first name we'll put a
comma and then last name which we need
quotes so last name doe and let's do age
and age I'm gonna make an int so no
quotes okay and if we go ahead and print
and we can print person let's print the
type as well okay and we'll go ahead and
run this with Python three dictionaries
dot pi so it gives us the dictionary and
it gives us the class of dict all right
so we can also use a constructor
so to use a constructor we can say it's
do person 2 equals dict and in here we
can do first name
this is a little different will do first
name equals sarah class name equals
Williams and if we go down here and we
print the type and the value of person 2
you'll see that it prints out the
dictionary and the class of dict I'm
going to comment that one oh now if we
want to access a single value or get a
value like let's say we want to get the
first name we can go ahead and print
person now in JavaScript we would do dot
name with an object literal in python
with a dictionary we're gonna use
brackets and say person name like that
I'm sorry first name okay so let's go
ahead and run that and we get John now
there's another way to do that do this
and that's using the get method so if we
say print person dot get and then we can
just pass in as a parameter the what we
want to get which let's do the last name
okay so if we go ahead and run that we
get Doe so you can use either way this
is the the more common way we can also
add something so let's say add key value
will take person and let's say we want
to add a phone number so we'll say phone
and we'll set that to a string look at
there I go with a semicolon
okay and then down here we'll go ahead
and print person run that and notice
that now there's a phone number added to
this this dictionary another thing we
can do is we can get all the keys if we
want so let's say get dictionary keys
and we could do that by saying person
dot keys okay so let's go ahead and run
that and what it does is it gives us
this dict keys and then first name last
name age and phone at the same time we
can get the items so let's change this
to items and we'll save that and it
gives us this right here dict items and
first name John last name Doe so that's
how we can get items now I'm going to
show you how we can copy a dictionary so
I'm gonna creates a person - I don't
have that up here right yeah I commented
that out so I'm gonna say person -
equals for the person dictionary with
the copy method okay and this is similar
to yes I know I keep bringing up
JavaScript but again I know a lot of you
guys are very familiar with JavaScript
so this is kind of similar to how the
spread operator works where you would
use it to get all the values of an
object and then if you wanted to add to
it you could so what we're gonna do is
take person to now which is a copy of
person and let's add a city value to it
so we'll say city equals Boston alright
and then we'll go ahead and print person
two and run this and now right here is
where we want to look this is person two
which is a copy of person 1 our person
but also has the city of Boston
okay so let's move on I'm going to show
you how to remove an item so to do that
we can use Dell I'll say Dell person and
let's let's remove the age so we'll pass
in age and then down here we'll go ahead
and print person and let's run it and
this last one here you can see there's
no age another way we can do this is
with pop so person dot pop and we can
just pass in whatever the field we want
to remove so let's say phone and now if
we run this you'll notice that the age
and the phone is gone let's see we can
also clear okay just like we can with
some of the other data structures we can
do person dot clear and if I try to
print person what we're gonna get is
just an empty dictionary so just this
this set of empty curly braces we can
also get the length let's see we still
have person too so let's do print and
just like with the other structures we
can use the Len function and then just
pass in person to and let's see what we
get so that gives us five all right so
it gives us 5 because person 2 has 5 key
value pairs now just like with
JavaScript a lot of times you'll have an
array of objects so we can also have a
list of dictionaries so let's create
people and set that to a list so we're
going to create some square brackets
here and then inside here we'll have a
dictionary and let's just do name I'll
say name Martha
age 30 and then we'll put a comma and
we'll put another one say name Kevin and
age 25 okay so now we have a list of
dictionaries I'm gonna show you later on
how to how to loop through these and
stuff but for now let's just go ahead
and print this
so we'll print people and there it is so
it's just an array of dictionaries now
if I wanted to get let's say the name of
this first person here in this list I
could say people 1 which is gonna choose
this dictionary okay this item and then
the specific field which I want which is
name so that should give me Martha so we
run that I'm sorry it's you me Kevin
because Kevin is in the one position
Martha is 0 all right so I think that's
gonna do it for dictionaries let's clear
this up and let's move on to functions
so we're gonna open up functions pi now
a function in Python it basically does
the same thing as it does in most
languages it's a block of code which
runs when it's called now there's a big
difference in the way we format
functions then most C syntax languages
ok we don't use curly brackets we use a
colon and then we use indentation okay
and that can be with tabs or spaces so
let's go ahead and create a function so
I'm just going to create a simple
function called say hello that just
takes in a name parameter and then just
prints that out so to define a function
we actually use the DEF keyword okay so
we want to define say hello and instead
of doing this like we do in many
languages we put a colon and then we
just want to indent one over from here
okay
and envious code and in any good text
editor or
it usually does this for you so let's go
ahead and just print and I'm actually
gonna take in a parameter here so let's
say name and let's just do a print let's
use an and we'll use an F string here so
we'll say print hello name
okay now this by itself isn't gonna do
anything we just define the function we
actually have to call it so we'll say
say hello and then we have to pass in a
name we'll just say John Doe
alright so go ahead and save now I do
have the prettier extension installed in
vs code so it does kind of format it on
save a little bit so that's if you see
me save and things change a little bit
that's why so let's run this we're gonna
say Python 3 functions dot PI and we get
hello John Doe alright now we can also
do default arguments or parameters
because if I were to do just say hello
without anything in it we get an error
it says missing one required positional
argument but what we can do if we wanted
to set a default we could say like let's
say equal say let's same Sam so name
equals Sam and if we run say hello now
with no argument you'll see we'll get
hello Sam okay so you can do that
alright so usually you're gonna deal
with return values okay when you run a
function usually it's it's you get
something from it you're not you almost
never are gonna print something out like
this unless it's point is to to be a
logger or something like that so let's
create a function called get some and
get some will take in num1 num2 i'm sure
you can guess what this is going to do
it's going to let's see we could just
return let's actually create a variable
within the function scope called total
we'll set that equal to num 1 plus
num2 and then we're going to return
total okay now when we set variables
inside functions it's in this scope okay
so outside of it is going to be the
global scope this is the the function
scope so this will return the total so
we can go down here if I just do get
some and pass in let's say three and
four and they try running this we're not
going to get any output because we
didn't print print is what causes the
output to the console so I could wrap
this in print and run it and then we get
seven a lot of times though you'll use
the return value in some way and you'll
assign that to a variable so let's say
num equals get some of three and four
and then we could print num run that we
get seven so num is equal to seven
because it's equal to the return value
of that function alright so I want to
move on here to what's called a lambda
function so a lambda function is a small
anonymous function it can take any
number of arguments but can only have
one expression and again I'm comparing
this to JavaScript very similar to arrow
functions okay so if you're familiar
with es6 if you use react or anything
like that
I'm sure you've used arrow functions
before this is pretty similar all right
so let's take the to get some example up
here and let's let's turn this into a
lambda so we could just simply do get
some equals so we're setting a variable
just like in JavaScript we would do like
Const get some I could even so get some
equals and then we use the lambda
keyword okay and then we want to put in
our parameters so we have num1 and we
have num2 all right and then we're going
to put a colon here which is kind of
like an arrow and then
with a narrow function and then our
expression which is going to are the
body of the function which is going to
be num1 plus num2 okay and it's gonna
it's going to implicitly return whatever
this expression equals and it's gonna
put it into this variable so if I go
ahead and I say print get some and let's
pass in 10 and 3 so if I run this we
should get 13 wait a minute oh you know
what I think my prettier extension just
turned this into it I just
it added def and added the return I
shouldn't need those though I shouldn't
even need these parentheses yeah it
turned it into a regular function let me
try that again it got rid of the lambda
and everything see sometimes this
prettier extension doesn't do what I
want yeah see I save it and it turns
into that huh you know what let me
disable that real quick oh that's not
right so I'm gonna go to settings format
on save let's set that to false all
right so all right so now it's not I'm
saving it and it's not redoing it for me
so let me try this again
and we get 13 all right so that's a
lambda so we're gonna move on now to
conditionals so 1 clear this up and
let's open up I will save it
let's open up conditionals dot Pi which
is right here and we're gonna look at if
statements if else statements and these
these work pretty much like any other
language you test to see if something's
true or false and you you do something
based on that condition so I'm just
going to create a couple variables up
here let's say X equals 10 y equals 5
and we when we do an
statement we can use comparison
operators like equals to not equals to
greater than less than greater than or
equals less than or equals so let's do
an if statement here now we don't have
to use parentheses like that like in a
lot of languages you'll use this syntax
we're just going to say if X is greater
than Y and then we want to put a colon
and then just indent the next line and
I'm just going to do a a print will use
an F string and I'm just gonna say X is
greater than Y and obviously you can do
whatever you want in here I'm just gonna
print this out though okay so let's save
this and let's run it so Python 3
conditionals dot PI and we get 10 is
greater than 5 now if I were to change Y
to 50 and we run it we're not going to
get anything because this isn't running
because this is not true now you can
also have else okay so if we wanted
something else to happen if this isn't
true then we could do that let me just
put a comment here so this is a simple
if and then we'll just do let's say if
else so I'll go ahead and just copy this
and then let's put an else so we do else
: and then just indent the next line and
I'm gonna copy that print and just
reverse it we'll say Y is greater than X
okay so we'll go ahead and run this and
we get 50 is greater than 10 okay it's
running the else because this is not
true so let's see let's come let's let's
actually copy this if-else because now I
want to show you how to use an LF or an
else if it's actually a LIF you know IIF
so let's say not only do we want to test
to see if it's greater than or less than
but also if it's equal to so in the
middle here I'm going to put a LIF :
and then we'll go ahead and print
something so we'll print X is equal to Y
okay so if I run this right now we'll
wait a minute what'd I do wrong here
no it's syntax oh I didn't put a
condition so this Elif here obviously
this needs a condition we're gonna say
if X is equal to Y all right so if we
run this now we're gonna get 50 is
greater than 10 but if we change this to
be equal like let's change Y to 10 as
well and we run it now we get 10 is
equal to 10 okay this right here this
condition is true so it's running this
now we can also do nested if statements
which is an if within an F so for
instance if I wanted to do if X is
greater than 2 and then do another if
statement here and say if X is let's say
less than or equal to 10 then let's do a
print we'll say X is greater than 2 and
less than or equal to 10 so we can do
that and right now it's equal to 10 so
that should run wait a minute oh I
forgot my indent
okay so now it runs now this isn't a
great way to do this if you wanted to do
this exact condition what I would
suggest doing is a logical operator and
using and okay because what you're
saying here is both of these need to be
true right so what we could do is we
could instead say if X is greater than 2
and X is less than or equal to 10 okay
so it's a cleaner way of doing it and
then we would run that
okay so let's comment this out and run
that and we get the same thing and you
can also do or and not so if I were to
copy this and do or the difference here
is with and both of these have to be
true for this to run with or just one of
these has to be true one or the other so
this would be or
all right and then we also have not so
for not let's just do something really
easy I'll say if not and we can pass in
here a condition like x equals y then
oops actually we'll just type this out
so print X
all right and that shoe actually they
are equal right now so I'll change that
to let's say 20 and now that should run
so you'll see right here 10 is not equal
to 20 so in fact all three of these are
running because they're all they all
evaluate to true all right actually no
they don't hold on oh yeah they do
because we didn't even test why all
right so I'm gonna comment this stuff
out and let's move on to membership
operator so basically what what this is
used for a lot of times is to test to
see if something is in like a list so if
we do numbers and we set this to a list
and let's say we want to test to see
look we don't need that if we want to
test to see if 3 is in the list I'll
actually change the x value up here to 3
so let's say if X in the name of the
list which is numbers then we could do
something and we could even do this we
could say print and we could do X in
numbers just like we did in the if and
what this will do is give us a true or
false okay so if this is true then this
this is going to give us true so if we
were to run this you can see we get true
at the same time we have not in okay so
this is this is an in
and we also have not in so we can say if
X not in numbers then let's print X not
in numbers right now that's not going to
run because three is in the numbers but
if I were to change it to let's say
thirteen and run it we get true which is
corresponding to this right here okay we
also have identity operators of is and
is not so for instance and I don't use
this too much but we could just to show
you it's possible we could do like if X
is y and then we could do the same thing
we did above by printing the conditional
or that the we'll get a boolean from
this condition print X sorry is why so
let's actually comment this out and if I
run this we get nothing because X is not
Y but if we change it to 20 and we run
it then it should give us true okay it
gives us true because X is y and then at
the same time you could do is not so we
could say X is not y and then we'll
print true if X is not Y so we'll change
it to something else and we should get
true again from that statement and
that's that alright so that was a little
longer than I wanted to spend on
conditionals but yeah so it's it's
pretty simple at least basic if if-else
and stuff like that so now we're going
to move on to loops
okay loops are an important part of any
language a loop is used for iterating
over a sequence that is either a list a
tuple a dictionary a set or a string or
a custom a custom condition so let's do
a simple for loop that loops
over a dictionary so we're gonna say
people equals I'm sorry not a dictionary
a list so let's say John Paul Sarah and
Susan alright so I just want to do a
simple for loop so I could say for
person it's actually a 4 in if you're
familiar with es6 we have 4 ins in
JavaScript es6 so we can say for person
in people and let's go ahead and print
and let's just put an F string in here
we'll say current person and then we'll
put in the variable person which
represents whatever person in that in
that current iteration so let's go ahead
and run Python 3 loops pi and we get
current person John it goes through
again Paul Sarah Susan now we can also
break out of a loop if we want so let's
let's look at break so I'm actually
going to copy this but I want to say
here I'm going to use an if statement
I'm gonna say if person is equal to
let's say Sarah then I want to break
otherwise I want to print this current
person so let's see what happens here if
I comment this out
notice that we get just get John and
Paul so what happened is we broke out of
the loop we basically stopped it if the
person was Sarah now we also have a
continue so let's see what happens if we
do that so I'm going to copy that and
instead of saying break we're still
going to test to see if it's Sarah but
instead of break let's do a continue and
let's comment this out so now we get
John Paul and Susan so it didn't just
stop the loop when it got to Sarah like
it did with the break it basically just
skipped Sarah and kept going and then
called Susan okay so that's the
difference between break and continue so
we also have something called range okay
so let me give you an example of that
alright so I'm gonna say for I in range
now we could put anything we want in
here basically any range any numbers
first I'm going to show you how to use
it with the list we just created the
people list so we can take the length of
people okay so for I in the range of the
length of the people list and then let's
just print say people I okay so this is
simple similar to just a standard for
loop we're gonna go ahead and run this
and it's just gonna print out all the
all the people's name I could've did I
was an F string current person or
whatever and instead of just person I'd
have to use people and then I and we
could do custom ranges as well so if I
wanted to do for don't want that for I
in range and then we could put let's say
0 to 11
and then let's just print we'll print an
F string we'll say number : and then
we'll put in I okay we'll run that and
it's gonna go as you can see whips it's
gonna go from zero to ten
all right so starts at zero goes to ten
because we said zero it's not actually
going to go to 11 it's not going to show
the 11 it's going to go to the the
number before that so we can also do
while loops so while loops execute a set
of statements as long as the condition
is true so we could set a variable
outside of the loop like count set it to
zero we could say while count is let's
say less than or equal to ten then let's
go ahead and print and I'll say count
count and then we just need to increment
count by one so we can actually just say
count we could do equals count plus 1 or
the shorthand would be just plus equals
1 like that so let's try it out let's
clear this and run it and we get count
up to 10 okay because we said less than
or equals to 10 if we just did less than
it would bring us to 9 all right so
that's loops next thing we're going to
look at is modules okay so a module is
basically a file containing a set of
functions to include in your application
you have core Python modules
you also have modules that you can
install with the PIP package manager and
Jango is one of those as well as custom
modules okay so you can create your own
modules and I'm going to give you an
example of all three of these so first
thing I'm gonna do is I'm gonna show you
how to import a core Python
jool where you don't have to install it
you don't have to do anything except
import it an example of that would be
the date/time module so to import a
module we use the import keyword and
let's say import date/time and I want to
use this so I'm going to go down here
and let's say we'll create a variable
called today and let's set that to
date/time dot date dot today okay and if
you look at the documentation you'll see
all the different methods and properties
and stuff that are available so let's
print today and let's see what we get so
we're gonna say python three modules dot
pi and that's what we get okay so today
is eleven twenty eight twenty eighteen
now notice that I did date time dot date
dot today so this date object is
actually in date time and if I wanted to
I could actually import just date from
day time okay instead of importing the
whole thing the way I would do that is
by saying import I'm sorry I'd say from
date time I want to import just date
okay now if I do that I can then do
today equals date dot today okay so I
don't have to use day time because I
brought I imported in date from date
time so let's save that and see we
should get the same thing excuse me
let's look at another one let's bring in
time so we'll say import time and let's
see let's go down here and let's create
a variable called timestamp and set that
to
time and then we'd actually have to do
dot time taught time stamp for that to
work wait
is that right no I'm sorry it's just
time dot time like that and then let's
go down here time stamp and what it does
what that time stand our time method
does is prints out the current time
stamp so let's run this again and it's
gonna change each time okay because the
time stamp is obviously keeps changing
now this looks kind of funny so what we
could do is just import time just the
time method from the time module so just
like we did with the date time we could
say from time import time and then we
could simply go like that and that
should do the same thing alright so
those are the those are examples of just
core modules that are available by
default with Python
now python has a package manager called
pip and again comparing it to JavaScript
where you have no js' you have your NPM
package manager you know you have ruby
gems a lot of different languages have
different package managers for
installing external modules or programs
excuse me
so to install something with pip you
want to do pip install but since my
Python 3 is available in my Python 3
command then I want to use pip 3 ok
unless you set up what's called the
virtual environment which we're not
going to get into in this in this crash
course we do do that in the the Django
udemy course but let's go ahead and
install something using pip or pip 3 in
my case so this there's a very very
small module called camelcase which just
takes a piece of text and makes a camel
case I mean
capitalizes each word or whatever so
let's do pip3
install camelcase now what this did
actually it's already installed so
that's why mine went so fast but when
you when you run it like this it's
installing it globally on your system
now like I said you can install
something you can create something
called an in virtue of virtual
environment where when you install
something with PIP it gets just
installed in that environment you can
also use something called pip and for
pip env which does the same thing you
have these encapsulated environments in
my case it just got installed globally
kind of like doing npm install with a -
- g or - - global now if i want to see
what's installed I can say pip 3 freeze
ok so this is going to show me all the
modules that I have installed and since
I'm in a global scope this is what I
have installed globally and you can see
camel cases right here alright but if I
were in a virtual environment it would
just show me what is in that environment
so now that we have that installed we
can import it just like we did with
these other ones so I'm actually going
to put a comment here so these are core
modules and this is we'll say pip module
so we can import camelcase it'll
actually pop up here so in Port Campbell
case and let's go down here and what we
need to do is basically instantiate a
camel case object so we'll say C equals
actually no it will bring in camel case
bring in the methods so I'll say from
camel case we're gonna import a method
which is called camel case and we want
to make these uppercase alright so doing
that we can just say C equals
camelcase now this this object now has
methods on it one of them is hump okay
so we can say print C dot hump I know
that sounds a little weird and let's
pass in like hello there world all
lowercase and let's run that and you can
see that what it is it made every first
character of each word uppercase okay so
it's a very very simple module but it's
just an example of how to install
something with PIP and how to import it
and use it so the last thing I want to
do here is create a custom module and
use that so we have this validator dot
Pi file which is already created already
filled out basically what this does is
it validates an email okay and we
imported the re the regular expression
module which is a core module we created
a function called validate email takes
in an email and then checks the length
of it and then it uses that regular
expression module which has a match
method and passes in a regular
expression to validate an email okay and
it returns a boolean or returns a true
or false based on if that is an actual
email or not so let's say we want to use
this as a module so I'm going to go back
to modules dot pi and we're going to
bring this in so let's say imports
custom module so say imports validator
and actually you know what I mean we
could do this and we could use validate
or dot validate email but we could also
do from validator import validate email
it'll actually show us show us that
okay now we can use this validate email
function and we could have as many
functions as we want in the validator if
you wanted to have like I don't know
validate username or something and even
have it connect to a database and see if
their username is already taken or
something like that so down here let's
have an email we'll set it to test at
test comm actually it should be a string
so it's a valid email and we're gonna
just do if validate email pass in that
email and then we'll do a print email is
valid okay and then we'll do else print
email is bad alright so let's get rid of
this print here get rid of this one okay
let's run it so we get email as valid if
I change this to be like let's say test
and we'll put in like a number sign here
we get email as bad so it's using this
validate email function it's bringing
that in from our validator module which
is a custom module that we created okay
so that's how you can use custom modules
alright so now we're going to move on to
classes okay with Python you can do all
types of different programming types you
know you can do procedural you can also
do object-oriented programming and a
class is used to create objects okay an
object has properties and methods
methods are basically just functions
that are associated with it
almost everything in Python is an object
and you can see when we have a string we
can call we can call methods on it a
string as an object and almost
everything is an object so let's go
ahead and create a class
I'm gonna try to do this quick because
we're really running out of time here so
let's let's create a class called user
and if you've created classes in PHP or
Java C sharp and it's it's pretty
similar so we can have a constructor so
a constructor is basically a function
that runs when you instantiate an object
from a class and we want to define this
as double underscore and knit double
underscore and this is gonna take in
self and it can take in other other
properties as well like let's say this
user we want to have a name and then
inside here we can see we can assign
self dot name equal to name now in a lot
of languages and when you use a class
you'll use this okay so in a pie in a
standard Python class you're gonna use
this self ok so just think of that as
this if you're coming from like es6 or
PHP or something so to create an object
say we'll initialize user object we can
do like let's say Brad losing my voice
Brad equals user okay so we saying Brad
is going to be a user object and we can
pass in a name like that so just doing
that will create the object so I mean if
I save that and I run Python 3 classes
hi we're not gonna see anything but it
didn't stand she ate the object in fact
they can do a print
let's do print type Brad
and you'll see that it gives us
basically telling us that this is a user
object now I'm gonna add a couple other
properties here so let's do name email
and age and then we just need to go in
here and say self dot email equals the
email that's passed in self dot age
equals the age that's passed in and then
we'll go down here we'll add that in so
brad at gmail not my real email address
by the way and then it's due 37 okay so
we're just adding extra properties now
let's create a method that is associated
with this actually first before I do
that I want to show you that we can
actually access the properties so let's
actually print let's print Brad dot name
okay if we run that we get the name we
can access any property we want Brad dot
age all right now I want to create a
method so to create a method it's just
like you know any other function we're
gonna use def let's call this greeting
now these methods that we create within
the class are going to take in self and
we want to just return
let's return an F string and we'll say
my name is now when we want to access
any of these properties from any method
within the class we need to use that
self keyword okay so what say self dot
name and I am self dot age and then down
here if we want to run that method we
can simply take the Brad object and call
greeting make sure you put parentheses
because this is a it's a method it's not
a property and if we run this what do
they do
I forgot my colon
so if we run that we get my name is Brad
traversée and I'm 37 okay if we wanted
to do another method like let's say has
birthday and what I want this to do is
just add an extra year to my age so I'll
say self dot age and we'll just do plus
equals one so we'll add a number now if
I go before the print before we run the
greeting and actually I don't need to
print we just need to take the Brad
object and call has birthday once we do
that it's going to add one to the age
and then we'll per do the greeting so we
start off with 37 have a birthday and
then when we print the greeting it
should be 38 and there we go excuse me I
am 38 okay so I mean that's the the
basics of creating a class with with
methods and instantiating it and calling
those methods you can also extend
classes so let's say I mean we have a
user class let's say we wanted to have a
customer class and extend that user
class okay so we'll go down here and we
want to just create a class called
customer and a lot of times in other
languages like I'm sure a lot of you
guys work with es6 and JavaScript you
would do like extends user in Python we
want to actually pass it in as a
parameter like that okay and then let's
basically copy this constructor so we'll
put that here
and I want I want a customer to be able
to have all of these fields but also a
balance and I want that balance to
initially be set to zero so I'm gonna
say self dot balance and let's set that
to zero by default
okay and then this customer class is
going to have a method called set
balance so let's say def set underscore
balance and that's going to take in self
and it's also going to take in a balance
all right and then what we want to do is
take self dot balance and we want to set
that to the balance that's passed in
okay so now let's go down here and let's
in it let's initialize a customer so
let's say janet and we'll set janet to
customer and customer takes in the same
things as a user because it extended it
so we'll say janet johnson janet at
yahoo.com we'll say 25 all right so we
have janet who is a customer so we
should be able to set a balance so let's
say janet dot set balance and we'll set
it to let's say 500 all right now I also
want to have a greeting for the customer
but I want the greeting to also have a
balance in it so I want to overwrite
this greeting so what I'm gonna do is
copy this and paste that in and we just
want to add right here to the end of
this string we'll say my name is name I
am age and my balance is
self-taught balanced alright actually
first of all I want to show you that we
can still access parent class methods so
if we don't even do if we don't even do
that yet let's do Janet dot balance
I'm sorry not balance greeting sorry
guys this is a long-ass video so if I
run that hold on wait a minute
Janet oh I didn't print it you know I'm
not even gonna bother editing that how
guns I don't care okay so you'll see
that Janet even though as a customer can
still call greeting even if it's not
defined in the customer class which is
right here because customer extends user
okay but if we want to overwrite that
and have it do something else like have
the balance in it we can do that so now
if I run it we get my name is Janet
Johnson I'm 25 and my balance is 500
alright so yeah that's that's pretty
much it guys so you create your class
you can add properties methods and you
can call those properties and methods
after you instantiate whatever that
object is okay so let's move on to
dealing with files
okay so files dot PI so python has
functions for creating reading updating
and deleting files so I just want mean
there's a lot you can do but I just want
to basically show you how to how to
create a file open it right to it stuff
like that so to open a file we can
create a variable let's call it my file
we want to set it to open and then give
it a name and this open will actually
create it so I'm
I call it my file dot Tia txt it's gonna
be a text file and we want to write to
it so we want the mode which is gonna be
W okay for writing so let's save this
we'll go down here and we'll run this so
python three files dot pi and over here
you'll notice that it created my file
dot txt just this one line created a
file on my system so we can get info on
this file so we can print so let's do
print name yeah we'll just do this so
print name my file dot name okay so my
file is basically an object that has
properties on it that we can access so
we can do name we can see if it's closed
or not so let's say is closed and this
has a property called
closed and we also have a property
called mode change that to opening mode
all right so let's run this so gives us
the name is closed as false opening mode
is W and what it mean what I mean by
closed is not the actual file closed on
our system but if we closed it within
within our script here so let's write to
the file okay so I'll say right to file
so we can take my file and we can call
dot write and let's put in the string I
love Python alright and then let's also
do my file dot write because we can keep
writing to it we can append to it let's
say and
javascript okay and then we want to
close it so we're gonna say my file dot
close so if we ran this is closed
undoubted be true so let's save that and
run it and let's go to the text file and
there we go I love Python and JavaScript
okay so let's say we want to F this has
already been closed let's say we still
want to append to it we still want to
add to it so what we can do is we can
then open it again so just like we did
up here I'm gonna say equals open my
file text only this time we're going to
use a we want to append to it otherwise
it's we're gonna overwrite it we don't
want to do that so go ahead and paste
this in let's put I also like PHP and
then we'll go ahead and close it okay so
we'll run that again let's take a look
I love Python in JavaScript I also like
PHP so it's as easy as that and if we
want to read from a file we want to open
it again this time we want to use the we
want to use our plus as our mode and
let's create a variable called text set
it to my file dot read let's read one
hundred characters and let's print it
okay so we'll go ahead and run that and
there we go
I love Python and JavaScript I also like
PHP so that's how that's starting to
work with files obviously there's a lot
more you can do but we're gonna move on
now to working with Jason
okay so jason is commonly used with data
api's here's how we can parse a jason
into a python dictionary so a lot of
times you might be dealing with
third-party api's where you're dealing
with data that
comes in is Jason and you're gonna want
to be able to parse Jason into a Python
dictionary so you can work with it
you'll also probably want to parse a
dictionary as Jason so there is a module
called Jason that we can use so we want
to import Jason and I'm gonna just
create some sample jason like we can
pretend we're getting this from some
outside api or something so let's say
user jason equals and it's gonna be a
string of jason which has double double
quotes around the key in value so let's
say first name john last name doe and
age that's a number so we can do 30 all
right now we want to be able to pass
this to a dictionary okay just like you
know we have jason dot stringify and
jason dot parse and javascript we have
methods like that in python so let's say
user equals jason and we want to do
jason dot loads and then user jason okay
so then we'll go ahead and print user
and then down here we want to run Python
and the name of this file is PI
underscore Jason dot pi and the reason I
unexpected indent oh ok so it gives us
the dictionary of the user now the
reason I called this pi underscore Jason
and not Jason PI is if I use Jason PI we
get a conflict because that's the name
of the module in case you're wondering
that all right so let's say we wanted to
just get just the first name
we could do print
let's do print actually we'll move this
down here
let's do print user first name and such
it's a dictionary we should be able to
do that so let's run that and you can
see we get John alright now I'm going to
show you how to do the opposite and take
a dictionary and turn it into JSON
format so let's say we have car which is
gonna be a dictionary so make we'll say
Ford and let's say model Mustang and
let's say year 1970 so that's a Python
dictionary right now we can say car
Jason will create a variable and then we
can use that JSON module and we can say
dot dumps and pass in car okay so this
will do the opposite so if I go ahead
and print car Jason let's comment out
these prints and let's run that and now
you'll see that it's in JSON format we
have the double quotes alright guys so
that's gonna be it for this crash course
I hope you enjoyed it I know it was very
very long but I wanted to cover a wide
range of just core concepts that you're
going to need to know to do anything in
Python and and I know that it's kind of
difficult to see how a lot of this stuff
is useful like how can you actually
create something from it but it's all
needed if you look at any Python script
you're gonna see dictionaries functions
conditionals lists you're gonna see all
this stuff so what I would suggest next
is to move on to tutorials or books or
courses that are going to actually show
you how to put it all together and build
a project with it but again these crash
courses these are always gonna be the
very first step to take so that's it
guys please leave a like if you liked
the video
also if you want to check out the django
course that this is based off of i'll
leave a link in the description with a
promo code for just $9.99 alright that's
it thanks guys and I'll see you next
time