Hello everybody and welcome to Python as fast as possible.
In this video I'm going to be trying to teach you Python as quickly as I possibly can.
That means I am going to go over things quite quickly, I'm going to talk very fast and
I will note that this is not for absolute beginners.
If you are looking for a beginner level tutorial series, I have many on my channel, I will
link a ton of resources down below and if you feel that I missed anything or made any
mistakes please do leave a comment.
That being said, let's go ahead and get started and get into Python as fast as possible.
To get started we need to download and install Python.
Please download the most recent version of Python from the Python website which will
be linked in the description.
If you are using any version of Python which is below 3.6 that will not be compatible
with this tutorial.
When downloading please check the box that says add Python to path, this will make
your life easier in the future.
Now Python automatically comes with its own environment to write code in, this is idly.
You can use this to write your code if you'd like but I prefer to use Visual Studio
code which is a text editor.
To download that you can go to the link in the description and just download the stable
build for whatever operating system you are on.
Now if you are using VS code there is one little bit of setup we need to do to get started
here.
So before we get started I thought it would be a good idea to talk to you about some
of the things that Python is used for.
Here I am on my channel in the playlist tab and you can see a bunch of different playlists
just giving you an idea of some of the things that I've created with Python and taught
on this channel.
Python is a general purpose programming language.
It is very easy to learn, versatile and really easy to write it.
The core advantage of Python is typically how simple it is, how easy the syntax is to
read and just how fast development is with it.
That being said Python is mostly used on the web and machine learning, artificial intelligence
and data science related tasks.
Now the sponsor of our video knows a ton about that and that is simply one.
I need to thank Simply Learn for sponsoring this video and giving you all a discount on
their data scientist master program that was co-developed with IBM.
This program is comprised of six unique courses that implement a unique blended learning
experience and a high engagement online classroom environment.
During this program you will master 30 plus in-demand skills and work with tools and languages
like R, SaaS, Python, Tableau, Hadoop and Spark.
You will master these skills through 15 real-life projects and one capstone project during
this 12-month comprehensive course.
You will also be given 1200 USD worth of IBM cloud credits to use during your 24-7 access
to the IBM Watson platform.
After completion of this course you will be giving certificates from IBM and Simply
Learn to testify to your skills as an expert in data science.
Get started by hitting the link in the description.
I want to start by covering the core data types in Python.
These are int, float, string and rule, and otherwise known as integer.
Now an integer or int is simply any whole number that does not have a decimal point on
it.
So 2, 3, 7, 3, 7, that was going to be an int, negative 9, that's an int, negative some random
large number is an int.
So long as it does not have a decimal point on it, we consider it an int.
A float is almost the opposite of that, it is simply any number that has a decimal point
on it.
So something like 272.0, this is a float even though you can consider this a whole number.
The point 0 adds a level of precision to this which makes it a float.
Of course something like 9.7 would be a float whenever you see a decimal point, that means
the number is a float.
Next we have string.
A string is simply anything surrounded by single or double quotation marks.
Typically we consider this a sequence of characters or numbers.
So if I type something like hello and I type something like hello, these two strings are
actually completely equivalent.
It does not matter whether you single or double quotation marks, there is no difference.
I will note that if you want to embed a double quotation mark or single quotation mark
in the actual string itself, then you would usually wrap the string with the opposite
type of quotation mark.
So in this case I want to have this double quotation mark inside the string.
So I wrap the string with single quotation marks.
Now just to note here, if I do something like 4.6, even though you might consider this
a float, this is a string, again because it is surrounded by single or double quotation
marks, remember the definition anything defined or surrounded by single or double quotation
marks.
Next we have Boolean.
This is one of two values.
We have true or we have false.
Some people may consider this one and consider this zero.
We're going to look at these later on and you'll see why that's important, but that
is a Boolean value and these are the four core data types in Python.
The next topic I'd like to cover is output in printing.
This is very important and we will be doing this a lot in this video.
In Python there's a simple function called print.
This is how you output something to the console when you run your program.
Typically when you print something you'll put it in strings so I want to print say hello
world.
So I will print the string hello world like that and if I run my program by pressing
this run button you can see down here in the console we get hello world.
Now notice that if I just type hello world like this and I don't put it inside of a string
and I run my program I will get an error.
This is because I cannot simply print something like this.
The program does not know how to interpret hello world if it is not wrapped in strings.
That is because this is not one of the core data types and we have no variable or anything
that denotes what hello world is.
That being said if I want to print a number say like 4.5 Python knows what 4.5 is because
it is a float you can see when I highlight it it says float.
If I run this it will print 4.5 out and there will be no problems.
Now if you want to print multiple things what you can do is separate them by commas.
So say I would like to print 4.5 and then I would like to print hello.
When I do this it will automatically add a space between the two things.
Let's just put another string here just to show you what I mean.
So 4.5 hello notice it adds that space in between.
Now we will talk about how you can print things without the space in between later on but
that is the basics of a print.
If I want to print many things on the same line I can go hello I can say end I can
say 87 I can say false and I can run this and this wall where hello and 87 false.
Now there's a few arguments that you can pass into the print function.
If you do a comma and then type end equals you can denote what you want the end of this
print statement to be.
By default the end here is a character turn which is backslash n.
What that means is go to the next line after this line is printed.
Now notice if I don't add this backslash n in fact let me just show you what happens
by default.
So let's say have hello and 87 I'm going to get rid of this and I print both these things
out we get these on two separate lines.
That is the default behavior of the print statement in prints on a new line.
Now notice if I add end equals and instead of a backslash n let's say this pipe like that
and I run this now at the end of the print statement it simply prints a pipe.
It does not add the carriage return which means we do not move on to the next line.
That's a little bit more advanced but essentially the carriage return is that backslash
n and again that means just move down to the next line.
That is the basics of printing and output and now let's move on.
Next let's move on to variables.
Variables are an important part of any program in Python they are quite easy to create.
When you create a variable in Python you simply type a variable name a space and equal sign
and then assign it to some value this value is going to be some data type some function
something else that we'll talk about later on.
To give you a basic example I'm going to set the variable hello equal to the value 10.
Think of a variable like a bucket that stores some data or stores some type.
Now if I want to access the value that is stored in hello what I can do is I can simply
say print hello what this will do is it will say okay this is a variable I know it's
a variable because I saw it earlier on the program let me see what its value is and I will
print that value.
So now I'm going to run and you can see that we get Tim.
Let's make another variable let's call this world let's actually set this equal to the
value world.
Now let's print hello comma world and let's see what we get we get Tim and then space
world.
Variables are somewhat complex you can make them equal to other variables.
If I decide to change the value of world to actually be equal to the variable hello what's
going to happen is to say okay hello is a variable what is a low equal to hello is equal
to Tim so I will print Tim.
Now watch what we're going to get we get Tim Tim.
Now what happens if I come here and I change hello to be equal to no.
If I do this what's going to happen is world will not change because world was set equal
to the value of hello at this line at this point in timeline three because our program
reads from top to bottom the value of hello was Tim.
So world gets set to Tim then hello gets set to no that does not change the value of world
so what you're going to see is we're going to have Tim and then we're going to have no.
So let's have a look here and sorry other way around we get no Tim my bad that we printed
a hello first and then world I have that mixed up.
As the basics of variables there's many other things that we can do with them but let
me just tell you about the naming conventions for them.
So in a variable you are not allowed to have any special characters and you cannot start
with a number so you are allowed to have an underscore that is the only exception in terms
of special characters usually you will use that when you're noting a space and in Python
the convention is typically to separate things using an underscore in other languages you
may see something like hello world if you wanted to make a variable that has multiple words
in it but in Python stick with snake case which is what this is known as the other one
is camel case and use underscores to separate your variables just to give me an example
of what I mean you cannot start a variable with a number so nine hello notice I'm getting
the red line popping up here and I could not do something like hello nine that does not
work but if I want to hello world 32 that is totally fine that is a valid variable name.
The next topic I want to talk about is getting user input just like printing something
is very simple getting input is simple as well what we do is we simply use the input function
we have our open and close parentheses and then we type what's known as a prompt inside
of these brackets like this now this does need to be in a string unlike the print statement
we cannot do something like 4.5 in here that will not work we do need a string so what
I'm going to do is I'm going to add a prompt say I want the user to type their name
then I will say name I'll do a colon and then I will add a space between the colon
and the end of the string the reason for this is that the user will be able to start
typing out on this line right after the prompt so if I don't have a space then what's
going to happen is the user will start typing right where the colon is which of course
is not going to be good so let's go ahead and run this and have a look at what happens
we can say name colon space and now I'm going to type 10 that's great but how do we
actually get the value how do we store what the user typed in so we can use it later
well we need to assign this input to a variable so what I'm going to do is I'm going
to say name is equal to input I just want to note because I forgot to say this previously
we could have capitals in our variable name if we want that would be fine but I'm not
going to do that and now what will happen is whatever we type in will be returned to
us in the statement as a string and it will be assigned to this variable name so if I go
ahead and print the value of name what's going to happen is whatever we type in here
will be printed out so let's have a look at this name let's type Tim and we see that
Tim is printed out below there now we of course can get multiple user input so I can
say something like age equals input age colon and now let's see what happens when I run
this so we go name Tim age 20 and we could put print those out if we want but I'm not
going to actually you know what let me show you one example where I print them out let's
say hello and then let's go comma name and then you are comma age years old so this is
a way that we can combine variables and strings so that we print out a meaningful responses
hello whatever the name is you are whatever the age is years old so in this case I'll
say name Tim age let's go 12 and says hello Tim you are 12 years old all right so that
is it for input now we're gonna move on and talk about how we can actually convert say
numeric input into a integer or float because a default comes back as a string all right
so the next topic I want to cover is arithmetic operators these essentially like plus minus
multiplication exponent how you perform mathematical operations in Python now
they're just something to keep in mind when we're performing and using arithmetic operators
we have to make sure that the data types on the left and right hand side of these operators
otherwise known as the operands are the same data type or are both numbers what I mean
by this is if I have something like hello and I try to add nine to it well that just does
not make any sense and we're gonna get a error and something's gonna be wrong with our
program so just keep that in mind I cannot do hello minus nine cannot do hello divided
by nine you actually can use the multiplication you can take a guess of whether that's
gonna do but I will show you that later all right so let's go through the basic operators
the first one is the plus sign very straightforward we can add nine plus three or in this
case I can add x plus y so x is nine y is three we can add those two values together ignore
the squiggly when I save that goes away now so I'm gonna keep in mind if I make this
3.5 this works totally fine even though these are different data types since they're
both numbers I can add them together of course I can do the subtraction let's run that
I can do the multiplication which is an asterisk and I can do the division we do the division
we get some crazy number just keep in mind that the maximum precision and Python I believe
is 13 decimal points there is ways to get beyond that but that is kind of too advanced
I'm not gonna cover that right here and when you ever do division or you're dealing
with rounding and all of that that's always a problem in computers so just keep that in
mind you're not gonna get the most precise answers with division like this if this is
a non terminating decimal point anyways hopefully that makes sense now I just want to point
something out if I actually have say nine over three of course you know that that answers
three and that is a whole number right three is a whole number but this is actually returning
to us a float not an end even though neither of these data types are afloat the reason
for that is because whenever we use the division operator it returns a float because
it does not know if it's gonna need to represent a floating point or it's gonna have to
represent some level of precision so just keep that in mind if for some reason you don't
want this to be a float of course you can convert the result to an end by just doing
int like that okay so those are some of the basic arithmetic operators next we have
exponent so that's two asterisks like that this will raise x to the power y so we can
see that gives us 729 and then next we have floor division so what I'm gonna do here
is actually make this a 10 have a guess at what you think this is going to give but this
gives us three the reason it gives us three is because this will give us the integer result
of whatever the division is imagine this was like nine point three three three or whatever
it may be it just simply removes all of the decimal points and just give or sorry it
be three point three three three removes all the decimal points and just gives us whatever
that integer result is next is mod this is the percent sign this returns to us the remainder
after a division in this case 10 mod three is gonna be well the answer is nine and then
remainder one right so that is why we get one from this here I think that's it for those
operators now I will simply show you how we can do say order of operations so if I want
to order of operations we can use brackets or if we want to evaluate things in a specific
order this does follow the simple order of operations which the acronym I know for it
is bed mass which is B E D and I hate this auto complete it's making it very hard
to do this M a S okay so what this stands for is brackets exponents division multiplication
addition subtraction Python does follow those standard order of operations if you just
write an expression but of course it's always easier to use brackets and spread things
out so I can say something like x mod y and then I can multiply that by two since I didn't
say it the integer division and mod operators are in the lowest order of order of operations
so if you use mod that is gonna be below addition and subtraction pretty sure that's correct
but someone can correct me in the comments if I'm wrong about that but anyways you get
the point you can use brackets and that is how you can perform and do arithmetic in a certain
way alright next thing I need to cover here is simply just an example using input so what
I'm gonna say is num equals input and we'll just say number calling like that but what
I want to do is simply print the result of a num minus five so I want to take whatever
number they type in and I want to subtract five from an imprint that to the screen so
let's try it num equals five notice that it says unsupported operating types for minus
are for sorry minus string and int so what that's saying is that num is a string even
though it looks like an int to us I'll explain why in a second and we cannot subtract
the number five from it that just doesn't make any sense the program doesn't know
what to do with that so think about this why is num a string we typed in five this should
be an int value from what we understand about our data types well this input automatically
returns to us whatever we type in in a string so just imagine that whatever we do here
it's gonna give us a string and that means that if we want this to actually be a numeric
value and be able to perform arithmetic on it we need to convert it to an int so there
is this fancy function called int in Python this will take some string assuming that there
is an int inside of that string and it will convert it into its integer representation let's
have a look now when I type a number let's say 10 of course we get the value five it
took num which was 10 so the string 10 you know I'll type it out like here we have
the string 10 and it simply converted that to the actual integer value 10 now you can
imagine we have other functions in Python that do this as well we have say float that's
gonna take whatever we have here and convert it into a float now notice what happens when
I do this if I do float 10 we get five point oh whenever you have at least one float
in your arithmetic it's going to automatically return to you a float it needs to keep
the level of precision that was in the original you know operation or any original value
so just that's something to keep in mind whenever you're doing you know plus minus multiplication
if there is one float as the operand so on the left or right hand side of the operator
then you're going to get a float result back alright that's all I want to show for this
now let's move on so the next thing I want to cover is something called string methods
before I do that though I need to define what a method is so let's create a variable let's
make this equal to hello and we can just say hello equals the string hello I want to show
you this function called type which will tell us the type of variable so if I print
the type of hello well you can imagine what type this is going to be it simply it's data
type let's have a look here we get class string you can ignore the angle brackets in the class
if you're more advanced you probably already know what this means but essentially what this is
saying is that this string or this variable here hello is an instance of the class string again
big words you don't have to know what those mean but for anyone more advanced hopefully that
makes sense to you alright so how do we use a method on a string and what is a method well
method simply is something with a dot operator so we have dot we have some name whatever the
method name is in this case it's gonna be upper and then we have an open and close bracket or
parentheses whatever you want to call them now I just want to show you what these methods do so
if I say hello dot upper this is a method that we can call only on strings so since this is a
string I can use this dot upper method on it and imagine what this is gonna do what this actually
does is it uppercases or puts the entire string in completely uppercase so if I have a look now at
hello and we printed out we can see we get hello in all capitals now I don't need to call it like
this I also can do hello dot upper now the reason this is gonna give me the same thing is because
well this is storing the value of the string which is hello and then it was just making an
uppercase right here now what happens it says okay well what is hello equal to oh well hello is
equal to the string hello so you can think of it as a substitution this string just goes where
that is makes it upper and then prints it out there we go upper now let's have a look at what dot
lower does you can probably imagine already but it simply makes the entire string lower case
these are useful when you're getting user input and you want to validate and check if it's
equal to something which we will do later on so if you're other methods we have dot capitalize
you can imagine what this does it simply capitalize the first letter in the string and then
makes the rest of it so it looks good right so I've had a sentence I said hello world like that let's
have a look at this it's gonna give me hello world properly and if I added a capital W like that
it removes that and it just kind of formats it as if a sentence would next we have this method
called count what count does is it will count a specific string within the string so it looks
very substring in the string and how many times that occurs let's see I wanted to see how many
double L's were inside of the string well I can say hello dot count double L and then that will count
and tell me how many are in here now have a look and guess how many you think are in here well there's
actually zero the reason there's zero is because these are capital L's and this was looking for
lower case L's so that's why it's very important to make sure you're differentiating between
capitals and lower cases because two lower cases L's are not the same as two upper case L's
but if I want to fix this problem I can make the entire string lower case so I could say hello
dot lower and then I could count all of the double L's in the lower case string so now let's
look at this in this case we get the value one let's make this a little bit better and let's look
for all of the is there any other ones that we can look for I guess we can look for how many
owes there all right in that case we get two owes so those are a few basic string methods there
are some more but I just want to show you how you use these and the fact that you can chain them
on each other because what happens is this will return a string you can imagine that the substitution
of this just looks like you know hello world and then it's going to count all the owes inside
of hello world and return to us that value which in this case was two all right so now we're going
to move on to conditions and conditional operators and before I mention that I just want to go
over something that I forgot in one of the previous sections this is simply string multiplication
and string addition this is important so I'm just going to cover it quickly but essentially
let's have a variable and that's equal to hello so x equals to a low have another variable
and this is equal to actually we can say three well there's this cool thing in python
but what I can actually do is multiply a string by an integer and that will simply repeat
the string the integer number times so if I say x multiplied by y even though these are the same
data type which I said before might cause problems since it's a string and it's an integer and the
string is on the left hand side we can do this so I can say hello at this gives us hello hello
because we multiplied the string by the integer so just something to keep in mind that we also
can add two strings together because that actually makes sense so if I say hello and then I say yes
well I can add these two together this is known as a concatenation so let's have a look at this
and I get hello and then yes so you can do that you can add two strings together and you can
multiply a string by an integer just need to throw that in there because that obviously is something
that's important to understand all right next we're going to talk about conditions and conditional
operators so a condition is simply something in python that evaluates or compares to variables or
data types and returns to us either a true or false value based on the comparison so to you know
giving example of a comparison is x equal to y is x less than y those are comparisons and the
answer to that is either yes or no or in our case true or false so we have that true and we have
these false this is Boolean so the result of a comparison is simply a Boolean value all right so let's
look at what these operators are we have two equal signs which checks for equality is the left
hand side equal to the right hand side we have not equal to which checks for inequality so is
the left hand side not equal to the right hand side we have less than or equal to greater than or
equal to less than and greater than now as a few other random ones we're not going to go through
those but these are the core conditional operators and let me show you an example of a condition
let's say I have x equals hello and y equals hello I want to check for the comparison so I might
do something like print x equals equals y are these two things the same let's see yes they are we get
the value true now let's do they're not equal to sign are these things not the same no they're
not so we get false that is the basics of checking for equality now what if I add a capital L
inside of this string well we know that lowercase and capital L are different so if I do this
we get true which means no these two strings are not the same they are different if I do double
equal sign then we get false now interesting thing with strings we can actually compare them using
the greater than sign and less than sign now it's not super intuitive so I have to kind of show
some more primitive examples here to give you an illustration of how this works but if I have
something like a and I say is a greater than let's actually go with capitals that what do you
think the answer to that is well that's actually true now the reason for that is every single
character inside a python is represented by what's known as an ASCII code we actually have a look
at what this ASCII code is by looking at what's called the ordinal value of a character so if I print
out the ordinal of z we can actually see that that's 90 if I print out the ordinal of a we can
actually see that that's 97 so what that's telling us essentially is that this uppercase said is
actually represented by the number 90 and the uppercase or sorry the lowercase a is represented by
so what we do is we would compare the two ordinal values of these characters and see that well actually
a is greater than z now if we do this so I say a greater than b then that's going to be false
because b is actually represented by 98 there is some logical ordering for these characters
but I just want you to understand that we can compare strings using greater than less than
equal to all of those but just understand that we're comparing the ordinal value of the strings now
let's say we have something like ab what we're going to do is simply just go from left to right
and compare all of the characters so we would say okay is a greater than b yes it is imagine
these two are the same so we had a and then we had d like that if these were the same then we
would go on and compare the next character which would be b in this case we get false because
obviously the d is greater all right so that is how that works now let's just show a few examples
let's just say 7.5 equals equals seven is that true obviously that's false let's say 7.5 greater
than seven obviously that's going to be true and what about 7.0 equals equals seven have a look at
that we get true all right that is the basics on the conditional operators and conditions of course
you can store these things in variables I can say result equals six equals equals six and print
result like that and we get to the value true okay let's move on now to change conditionals
so a chained conditional is essentially combining multiple conditions together to create one larger
condition what I have right here is a basic example I have three conditions right I have result one
result two and result three result one says is x equal to y result two says is y greater than x and
this one said is z less than x plus two you can imagine that these have brackets around it it
works the same so all of our conditional operators have a lower precedence than all of the arithmetic
operators so if I do something like z minus two less than x plus two well that's not going to
say z and then negative two less than that it will evaluate on the left hand side and it will
evaluate on the right hand side and then it will combine and use sorry this conditional operator
so hopefully that makes sense I'll just keep that in mind that you can actually write expressions
on each side of a conditional operator okay so now that we have that essentially what I want to
do is say okay how can I make a condition that uses result one result two and result three so
obviously all of these are equal to true false true false that are equal to something like that
so what I can actually do is use three separate keywords so and or
oops and not so these three words are what I can use to chain
conditionals together and create larger larger conditions from specific conditions
themselves so what I can do is do something like this I can say result four equals result one
or result two now what is the or operator do well the left hand side and the right hand side
of or have to be a true or false value which they are because these evaluate to true or false
what or will do is it will look at the left hand side and right hand side it will say okay
if the left hand side is true or the right hand side is true this whole condition is true of
course if they're both false that's the only situation where this whole condition here is going to
be false so in three scenarios because there's four possible scenarios here it's going to be true
and the other scenario it will be false so if both of these are true obviously at least one of
the most true so that evaluates to true so let's actually just print result four and have a look at it
great we get true now what we can do is we can add another or if we want and we can say result three
now what this says is okay if result one or result two or result three is true this whole thing is
true which of course it is now let's see how we can combine other stuff into this we can actually
use this not keyword what this does is it takes whatever is on the right hand side of it so in
this case it's gonna be a true or false value and just flips it so in this case if if result two
is true which it is it's just automatically gonna become false so if I actually I can show you on a
more basic level if I just print a set of result four I print not true like that this will print
the value false which you can see down there and if I print not false then this will print true
now what I can do is do something like this I can say not false or true so let's have a look at
that and there we go we get false so it takes whatever this is which is true and then it just
reverses it and makes it with the not now those are two that we've talked about so or and not now
what about and so and essentially says okay is what's on the left hand side and the right hand side
true if both of it is true they are true the whole conditions are used true otherwise it is false
so only in one scenario out of the four possible scenarios here with two variables or two values
on the left and right hand side is this gonna be true so let's have a look at this and we get true
because obviously this is false then we not it and that turns to true now of course we can do
a combination of and and or now when we use these combination of different things right so not and
or we have to know which is gonna come before what so kind of in the order of operations now the way
it actually goes is not is first and is second and or is last so just keep that in mind because
that will be important based on the way that you write your expression so essentially if I say
false and true or true then what we're gonna actually do is evaluate the false and true first
and then we will evaluate the or of whatever this is and then that so in this case this will be false
so then we get false or true and then this would turn into true then we would have the not
and that would turn that into the false now we're moving on to the fun stuff which in my opinion is
the if else if and else statement if this essentially allows us to actually check if something
occurs or if a condition is true and if that is true we can do something specific otherwise we might
not do anything or we might do something differently so for the example I'm gonna do here I want
someone to type in their name and I want to say if their name is equal to in this case Tim I will
tell them they are good at Python I don't know something random so in this case to do that what I
will start doing is writing an if statement so if syntax and if statement is if condition
like that I know that's spelled wrong but that's fine condition colon indented block whatever I want
to happen in the indented block and then that is the basic syntax of the if statement so for
here if I want to check if x is equal to Tim I will say if x equal to and then the string Tim with
the capital T I will simply print you are great so now what will happen is if and only if x is
equal to Tim will I print you are great now in any situation so regardless of if the name is equal
to Tim or not I will always do this so I will print that statement hopefully that self explanatory
so let's go ahead and do Tim we can see you are great always do this now let's do another name let's
go with Joe and it says always do this it did not print this statement now the addition to the if
statement is of course I can add more stuff underneath this indented block so you do another print
statement and I would add a space so let me actually just show you how this works if I do Tim now
you see we get that space by just printing an empty print statement essentially okay now what we
can do those we can say okay how about I only want to print you are great or I want to print some
other message I don't want to print two things or what I can do is I can print I can add this
else this else must come after an if statement and essentially what allows me to do is say okay
if this isn't true do this so it's an if else right if true do this otherwise do this never
will I do both so in this case I'll just print no you know not very intuitive or it doesn't
really make my sense but that's fine so now I'll go ahead and I'll say name Tim says you are
great and let's actually just go with no and then it prints out no all right so that's the basics
on the if else now let's say we actually want to check multiple things so maybe we want to check
a bunch of different names and if the name is Joe maybe we want to say you know thanks Joe I don't
something random right well what we can do is use this elif statement the elif statement can be used
as many times as we want but it must come after an if and it must be before any elses so I can't
just go ahead and start writing these random elifs everywhere only is gonna come after an if statement
so there's only ever one if only ever one else but as many elifs as we want and you'll see
how this works in a second so the elif syntax is the exact same except it starts with the el so I'm
gonna say elif x equals equals Joe and I'm gonna print by Joe why not so now let's run this
let me just show you what happens when I go with Joe it says by Joe and let's just print some
random it gives us no now of course I don't need this el statement I can use an elif without the
el statement that's totally valid in this situation if I do something random just nothing will happen
but if I put Tim or Joe then it will give me the valid response now let's add another elif let's say
if elif x equals equals I don't know let's go with Sarah we can print random okay and now let's
try this out and that works now of course we could add the elis at the end but that is the basic
for the if elif and else now of course you can chain and add stuff inside of another if statement
I could put another if statement inside of this but just make sure you keep the indentation correct
because that how it that is how it determines what we're going to be printing and what we are doing
if this statement is true so in these next few sections I'm gonna be talking about collections how
we deal with collections looping over collections and all of that fun stuff but what is a collection
well collection is simply an unordered or ordered group of elements now the two collections I'm
gonna discuss here in this section is a list and tuples so if we have a list a list looks something
like this it is simply square brackets now inside of the square brackets we can have a series of
elements elements are simply some data type so I could have the integer four I could have the value
true I could have some string high and these elements do not need to be the same type like in
many other languages so this is a list and the way that the list works is that it can store
a bunch of different elements and a list is an ordered collection which means the order in which we
enter things into the collection matters and is maintained in fact it's very important in the list so
how do I actually access elements in the list how do I deal with the list what are some things
we can do with it well of course I can define a list by simply opening up some square brackets
and then defining some elements inside I also can just leave square brackets like this empty that
would give me an empty list the first thing to look at on the list is this function called
Len Len will tell us the length of the list this also works on strings and a few other things as
well but if I look at the Len of x that tells me three now just to kind of show you here if I say
y equals high and I were to look at the Len of y after I look at the Len of x we obviously
get two so Len is a function in Python that we can use to look at the length of stuff
all right so that is one of the things we can do on a list what we can also do is we can
append things to it what append does is add to the end of the list so if I want to add to the
element hello I can say x dot append now I can print out x and if we have a look we get four true
high hello of course we added this element to the end of the list I can also extend the list by
another list so let's say I have a list here and inside of this list I have four five and just a
bunch of other stuff well when I extend it what that will do is take all of the elements from
this list and simply append each of them to the end of the list so you can imagine extend
its internal implementation just loops are all this and appends all those elements to this list
now what about removing something or popping something off of the list well I kind of already
gave away what one of these things is but what pop does is remove and return the last element
in the list so if I actually go ahead and print x dot pop what this will do is it will print
and it will remove high from the list and you can see that when we print x again so we get high
and now our list has high removed because we popped it off of the end now another argument for pop
is actually the index of what we want to remove when we look at a list every single element in the
list is identified by a number that number is known as its index otherwise known as its position
in the list when we start looking at index is in the list we start counting at zero so the first
position in the list is zero the next is one and the last is two if we want to know the last index
in a list we look at the length of the list and subtract one in this case we have three elements
the last index would be to the first is zero so if I go ahead and say x dot pop zero what that's
going to do is remove the first element from the list which is four so now we remove that and
all of a sudden if we look at this new list the first element at position zero index zero is
going to be true now if we simply want to access elements of the list maybe don't want to remove
them we just want to look at them we can say x put the square brackets which means we're going to
access something and we can put the index of the element that we want to access in this case will
access true there we go we print true and that is the basics now what I can also do is say
let's do something like x zero equals and change its value to say hello now when I print x we'll get
hello true hi let's have a look and there we go the reason I can do this is because lists are
what's known as mutable this means this x right here does not actually store a copy to the list
it stores what's known as a reference to the list and the actual items itself are stored somewhere
else so for people that are more advanced and understand that I'm hoping if you guys have come from
another programming language that makes sense but lists are mutable which means they can be changed
I can change all of these elements and if I do something like y equals x I go ahead and I make
that change to x but I don't make it to why you're going to notice that x and y will be the exact
same so I have hello true hi hello true hi making a modification to x made a modification to y
because x and y are storing a reference to this list not a copy of this list if I wanted to copy
the list I would have to do this I would have to do two square brackets a colon inside we'll talk
about how this works later but now notice if I made this copy that change does not apply to
the other list there's a lot of other stuff that you can do with lists but those are the basics
and hopefully that makes sense next I'm gonna talk about tuples so tuples are similar to lists except
they are immutable so a tuple uses round brackets instead of square brackets and it works pretty
much the exact same except we cannot append we cannot remove and we cannot change elements so a
tuple is really an immutable list you can think of it like that it means it cannot be changed once
it has been defined we want to change it we actually have to redefine it so what I can do is I can
print out say x zero and that's fine that will give me the correct answer so that'll give me zero
but if I go ahead and do something like x zero equals five and I try to do that you're gonna notice
we get an error because the tuple object is not support item assignment reason for that is because
again it is immutable I also cannot do something like x dot append I can try but I'm gonna notice that
again we'll get another error tuple object has no attribute append so this is a tuple they're not
used a ton although they are used sometimes and just to note here you can have lists inside of
lists so I can say something like you know I could have a tuple inside of a list I could have a
list inside of a list inside of a list and I could have elements inside of this they don't need to
be uniform size sometimes in other programming languages when you're looking at arrays if you have
embedded lists every element has to be another list stuff like that just want to point out that
you can do something like that and I think that's all I'm gonna cover right now for list and
tons all right the next topic I have for you is for loops now for loop if you've learned another
programming language just allow us allow us to iterate a set number of times so we'll be talking
about a while loop the main difference between a while loop and a for loop is a while loop is running
and in defined amount of times we don't know we're going based off of a condition which we'll
see shortly a for loop we know how many times we're gonna loop at least relatively so what I'm gonna
gonna do is I'm actually gonna show you how we can just create a basic for loop to print the numbers
at from one to ten say for I in range and then inside of here we simply put ten I'll talk about
this range function doesn't a second but let me just show you how the for loop actually operates
so now I can show you that this prints zero one two three four five six seven eight nine does not
include ten so the way we set up a basic for loop if we're just gonna be iterating through
account is we say for I or for some variable this is gonna be our iterator or a counter variable
whatever you'd like to call it in this is a keyword we always have for I in and then some word here
in this case we're gonna put range range is a function that actually creates a collection of numbers
based on the input that we give it the input to range is the following start stop step so we're
allowed to put up to three and we can have less than three arguments inside of this range function
essentially start stop step is saying okay what number do we want to start at what number do we
stop at one number do we step at now if we only have one argument so we only pass one number
by default that is the stop argument we usually start at zero so by default our range will start at
zero and we will go to whatever number we put here most of the times you're just gonna put one
number and this defines the ending range so in this case it says okay we're gonna start at zero
and we're gonna go up to ten but not include ten notice we didn't print ten now if we had
two arguments that's gonna be default start stop so I will say okay we will start here at one
and we will stop at ten but not include ten if we had three we'll include step and that tells us
how much we should increment every single time by default we increment by one but we can't increment
by two and we can increment by negative number as well let's actually say that we want to start
at ten and we wanted to go to negative one and we want to step by negative one well let's have a
look at what this looks like and just test it out okay so running you see we start at ten right there
and then we went all the way down to zero now of course we could start at negative ten and once
we'll happen here we do nothing the reason for that is we start at negative ten we were going to
negative one and we're stepping by negative one but since we were already passed negative one we
aren't gonna go there obviously and we just immediately stopped so that is kind of how the range
function works so for i in range again start stop step by default if you just put one argument
that's gonna be the stop if you put two that's start stop you can mess around with that and see
how it works but I think that's enough of an explanation the next thing we can do is actually loop
through a list so let's say we have four i in and now instead of range we'll simply just put a
list we'll say four i in range and we can do three four and just a bunch of numbers here now if
we print i it will simply print all of the numbers in that collection or in that list it will step
through one at a time and print all of them now let's say that maybe we didn't want to do that
maybe we actually wanted to keep track of what index we were at we could say x equals the list we
could say four i in x and then we'll sorry that actually would not be a great way to do it we can
say four i in range the length of x and then instead of just printing i we could print x square
brackets i let's have a look at this here you see how that works the way this works is we're
gonna go up to but not including the last index of x or the length of x sorry so in this case
one two three four five six so this right here we'll say six so four i in range six that means
the maximum value of i is going to be five so we say x zero x one x two three four five and print
all of the values out now there is another fancy thing that we can do if we want to avoid
using that it's called enumerate what enumerate will do is create indexes and values for all of
the elements in our list we can say four i comma element in enumerate x and then what I can do is
comma element and you'll see how this works what we do is you get zero one two three four five and
then all of the elements so this works kind of like a combination of the two four loop styles I
just showed you what we actually have a range so we go from zero up to five those are all the indexes
in the list and then we print all of the elements for each of those indexes in the list and we just
have two variables here that denote that so the next topic to discuss is while loops now while
loops are pretty straightforward essentially what they are is while condition equals equals true
go ahead and do something so we're gonna have some condition here before this colon and then after
the colon there's gonna be an indented block and whatever's in that indented block will run inside
of the loop while that condition is true to illustrate this I will say i equals zero so while i
is less than ten we'll say print run and then we'll say i plus equals one this is another way
that you can add one two variables so you could say i equals i plus one or you can do i plus equals
one you can also do something like i multiplied equal by one or divided equal by one so
multiplied by equal two what that's gonna do is say okay multiply i by two we can do something like
that i if we want it as well just showing you some more syntax for some interesting things that
we can do in python but as the basics of a while loop if I go ahead and run this we can see that
this is gonna run ten times and there isn't really much more to talk about for the while
if other than the fact that we can implement a break statement so we could also write this same code
like this while true i plus equals one and then if i equals equals ten break and i will simply
break out of whatever the closest loop is to us imagine we had another while loop like this and we
said while true inside of here then this would break this while loop not the other while loop
hopefully that's making sense anyways let's move on to the slice operator so the slice operator
is personally my favorite part of python there is implementations in other languages but none of them
are as nice as python in my opinion and what this allows us to do essentially is to take a slice
of a collection like a string or a list or a tuple i and do something with it so let me just
show you what i mean the slice operator is simply the square brackets like this with a sequence of
coins and numbers within it way that it works is we have a start a stop and a step just like
the range function that we looked at before so if i put something like x here and then i go start
stop step what i can do is say okay i want to start at zero i want to stop at index these are all
indexes that we're putting in here three and i want to step by two now actually let me just stop at
index four just so we get some more values in here but let me show you what this looks like we
get the value zero two when i have zero four two into my slice what this is saying again is start
at the first index which is zero i go to the fourth index but do not include it which means stop
as soon as we hit this and step by two so we're gonna start at zero then we're gonna go to two
and then we're gonna go to four but since four is the stop here we're not gonna include that in our
slice now when we do the slice operator we don't have to include all of these different things as
arguments in fact what we can do is just include a stop just include a start and all of that so
just like the range function if we don't include anything so we just sorry we just include a
colon like that and then we put something on the right hand side that is saying stop at whatever
index we put here so if we do this it says start at the beginning and stop at the end now if we
do something like two colon that says start at two and stop at the end so whenever we leave a blank
we're assuming it's either the end or the beginning or one in the case of a step so if i did
two colon colon maybe i did two colon four colon this is saying okay start at two stop at four and
then step by one but i don't even need to include that i could just do two colon four but now
let's say i wanted to go four and then colon two colon negative one what this is saying is okay start
at four go to two and step by negative one so now what this is gonna do is start at four which is
actually this and it's gonna give us four and then three and then it's not gonna include two
or any of the other one so that's what we'll get in fact let's look at this we see we get four three
now one of my favorite things with a slice operators to reverse a list all we have to do is
colon colon negative one this means start at the beginning stop at the end and then step by negative
one so this will actually start stepping backwards which is kind of interesting so when we look at
that we get eight seven six five four three two one zero that is an easy way to reverse a list in
Python now of course all this works on a string as well let me show you that with s you see we can
reverse the string like that and we can also just do the standard arguments so we would typically
do let maybe we'll just step by two so start at the beginning stop at the end and step by two
we get hlo so as the basics of the slice operator there's a bit more stuff that we can do with
this but that is kind of how it works and i believe this works on tuples as well we can just try it
out to make sure that i'm not lying to you guys but if i do that you can see that that does indeed
work and we can use the slice on kind of any collection of elements all right now we are moving on
to sets which are an underrated but extremely useful data type in python now set is simply an
unordered unique collection of elements what that means is there is no duplicate elements we do not
keep track of order or frequency of elements all we care about is if something is there or if
something is not there the reason we do this is because a set is extremely fast to do what's
called lookups removals or additions when we're talking about a list let's say we have something
that's you know has a bunch of elements in it if we want to say remove the first element from the
list that actually involve shifting the position of all of the other elements because we're
keeping track of the positions of these elements with the set since there is no notion of position
adding and removing something is as simple as just adding or removing that element there's no
other operations that need to be done which means it's very fast to use so think about a set to be
used only in a situation where you care if something exists or doesn't exist not about the
frequency or order in which that item exists so to create a set is pretty straightforward we can
say x equals set and we use oh the open bracket and close bracket now the reason we don't actually
write something like say s equals and then this which is really the representation for a set
is because what this will do is create a dictionary now technically if we want to create a set we
can do something like this and this will create a set for us that initially has the elements
for 32 and two inside of it this is what's known as a set literal if you're not going to be
creating an empty set it's totally fine to do it like this so long as you add an element inside
but if you are creating an empty set then you need to use this constructor otherwise you're
going to end up creating a dictionary which is not what you want to prove the simple point you
have print out the type of just an empty you know set representation and you'll see that it
gives me dict like that of course that's not what we want anyways let's have a look at this set
and I'll just show you what I mean by unique unordered collection of elements so 432 to 2
if we print that out we get 32 to 4 so obviously a different order than how we added in there
and notice that the duplicate twos were removed now the set operations are pretty straightforward we
can add something to a set with s dot add our set dot add I'll put five and inside of here and
let's just keep printing out s there we go we can see we added in there we can also do s dot remove
and that will well since there's nothing in there give us a key error now if there is the element in
there so we can do it like this it will remove that element for us and then there's a few other
things that we can do as well for a set one of the good ones is actually checking if something is
in a set what I can do is say for in s what this will do is simply check if the element for
is in the set and give me a true or false in this case it is true and remember this happens and
what's known as constant time which means it's just extremely fast to do this as opposed to a list
so we can say thirty three in s that's gonna give me false now the uh parallel I want to make
here is if I had s equals to four thirty two two two like that sorry actually let's go s two
and I said print and let's just go two in s two this operation the one that's actually in the set
is going to happen much much much much much much faster than this one of course this isn't a big
deal right now because these collections are quite small but as these get uh extremely large
just understand that using a set is very beneficial when it comes to looking things up checking
if something's in there removing and deleting there's a few other operations we can do on sets
let me just define s two and I'll just quickly show you a few of them uh we can do for example
the intersection or the union of these sets so I can say s dot union s two and we'll go ahead
and have a look here and we can see we get the union of these two sets essentially adding them
together we can do the difference of the two sets if we want we can do the intersection and we
can do the symmetric difference and all that other fun stuff that you can do with sets now I'm not
gonna get into all that stuff just know that these are sets and that's some of the stuff that
you can do with them so now I'm gonna talk about dictionaries now if you're from another
language what may be familiar to you here is a hash table or a map something like that they're
very similar to dictionaries but a dictionary is essentially a key value pair you have some key
you have colon and then you have some value that it corresponds to now of course that value has
to actually be a valid data type but it could be something like a list it could be a single value
like that and the way that you access and create new key value pairs is something like this I can print
out say x and then the key inside of here in this case it's actually called key and what that will
give me if you look down here is simply the value for because well that's what corresponds uh
to this key so you have keys and you have values now to add a new key is pretty straightforward
what you can do is you can say x inside of square brackets you can define whatever key you like
it can already exist if you want we can say something like key two and we can make that equal to five
now note that these keys do not need to be the same uh sorry same data type so I don't have to
string keys I can have integer keys and whatever keys I want new something like x to equals eight
and there we go you see that works perfectly fine in fact I can make this a list
just to prove to you that the values don't have to be the same either and there you go now just
like a set this actually works using a hash which means this is very fast to retrieve add and modify
values within the dictionary in fact it's so fast you can kind of assume that it's in constant time
although there is a chance there will be something known as hash collisions although I'm not
gonna talk about that because that's probably a little bit more advanced than what most of you know
anyways what we can do here in terms of operations on the dictionaries we can check if something
is in the dictionary so I can say okay is some key in the dictionary key in x there we go print that out
and that says true a few other useful things that we can do is we can get all of the values from
the dictionary so we can say x dot values print that out and it says dict values and it gives us a
list of four usually when you do this you're gonna want to actually create a list out of this
because that is a different data type that's returning to us and there you go we get the value
for now similarly we can do dick dot keys so x dot keys and there we go we get a list that has
all of the keys in terms of iterating over or deleting values I'll first show you how you delete
something to delete something we can say x and then the key that we want to remove in this case
it will remove this key so let's go ahead and do that and well I guess it didn't really show you
that it was deleted let's just print it out to prove that there you go and we get an empty dictionary
of course like I showed you before we can use that in operator and to loop over it there's actually
a few interesting ways that we can do this so I can say for a key value in x dot items
and then what this will do is simply return to me all the keys and values so I can print key value
and you'll see we'll get a key for so if I want to loop through all the keys and all the values
I can use dot items if I don't want to do that I can just say for key and then I can say in x
what this will do is simply just give me all of the keys in x and if I want to access the value
of course I can say x key like that and that will give me the value and we get the same thing
popping up there that's kind of the basics for dictionaries there's a few other methods as well
but I'm not going to talk about those that is a dictionary and this next one is a fun one this is
comprehension now as far as I know Python is one of the only programming languages that actually
has this a lot of people absolutely hate it but I personally love it and if you learn to love it
you can use it quite well now to do a comprehension essentially what that is is a one the line
initialization of a list of a tuple of a dictionary of many other things so let me just show you what
I mean I can do something like x equals a list and then I can say x for x in range 5 now if I go ahead
and I print the value of x you're going to see what we get here let's run it like that you
get zero one two three four the way this works is essentially we can actually define a for loop
inside of this list and what it will do is say okay we're going to loop through as if we normally
would and whatever we have on the left hand side here is actually the element that we're going to
add into the list so I could do something like x plus five for x in range five and there we go we
get five six seven eight nine ten I can also do something like you know x mod two and or x mod five
there we go I could also you know grab something else I could say zero for x in range five that
would give me a list of all zeros you can see how this might actually be useful now if we want to
go into a more complex example I could do a list and then inside this list I could say zero for x
in range 100 for x in range five now what that's going to give us is five of these lists that
each have 100 zeros inside of them you can see that's quite large but that is kind of how
that works now we can also go ahead and add some more complex stuff to this too we can say zero four
or I can say I for I in range 100 if I mod five equals zero so essentially if I is divisible
by five then what we can do is actually add it into the list let's have a look at that and there
we go we get all the increments of five so you can play around with that and see how that works
but this also works for dictionaries in fact let's just do the same thing here let's make it
it a dictionary and now we'll just go I colon zero so we have all these keys that are initialized
at zero we can go there and have a look we can see that dictionary works and of course we could
do the same thing for a set we can just say I for I in range blah blah blah and there we go now
we get a set so we can actually do this for tuples as well except we're not going to do it in the
way that you might assume if you just go ahead and do this this actually returns to us what's
known as a generator object it's a bit more advanced for this kind of video so I'm not going to talk
about that but what we can do if we want to make this work for tuple is we actually type tuple we can
use the tuple constructor which is simply this and then do the comprehension inside of there
now that will return to us the tuple you can see I tried to do it the other way before and that
returned to me the generator object anyways that is comprehension these can be used in a
whack of different things they're definitely interesting and good to know and one of the best
features of Python that is not in many other languages so now I'm going to start talking about
functions so to define a function and Python you simply use the def keyword this means define
then you pick your function name in this case I'm going to pick funk the function names of the
same as variable names in terms of how you name them you can put some positional parameters inside
of here if you want or you can leave it empty you add a colon indented block then anything inside
the indent will run when you run the function so let's go ahead and actually just run this
and show you the basics of this so that is pretty much how you run and define a function of
course you can define multiple functions you can actually define functions inside of functions if
you wanted to this is totally valid if I wanted to do something like this and then I could call
funk like that now of course this is a more advanced use case but just showing that you can do
that because of the way that python works in fact functions are actually objects which means you
can technically return them I'll show that in a second but let me talk about arguments first so
let's say I want to add the argument x y inside of here of course that means now when I call the
function I need to add some value for x and for y so let's go five six and now if I after run
want to print those I can print x and I can print y let's have a quick look we get run five six
of course we can return something from function as well maybe I'll return x multiplied by y now if we
actually go ahead and print the value of this function what will happen is it will evaluate what is
here return that to us and print that out we get the value 30 now note that when you return multiple
things from a function which you can do I can return x multiply by y and maybe x divided by y as
well this will return the value to us in a tuple so if you look here we get actually two values
we have I guess the x y value here and that's inside of a tuple now if we want to unpack this
tuple and say separate this out into different variables we could of course index at zero and index
at one but a much cleaner and faster way to do this is to do something like r1 comma r2 equals
funk five six what this is gonna do is take these two values here and simply separate them into
the r1 and r2 variable now if I go ahead and I print r1 r2 and I have a look here you can see that
we get 30 and then zero point eight three three three four that is the basics for a function if you
want to do a optional parameter you can do something like z equals you can set a default value I
usually set it as none and what this means is that you do not have to pass that in but if you
want to you may and it will override the optional value that you have here so let's just start by
actually printing out z I'm not gonna call it with z to start you can see that oops let's run that
for a second we get run five six none and now if I add a z so let's just add seven there we see
then seven none we are going to get seven printing out right there that is the basics of a function
and now we're gonna move on to some other more advanced aspects of it all right so I'm gonna talk
about something that's really cool called the unpack operator and then I'm gonna talk about star
arcs and star star quarks but I first want to show you an advanced example of a function
so if I say define funk like this and then maybe I'll say take x and I define funk two inside of
here this is totally valid by the way you can put a function inside of another function and then
inside of here what I'm actually gonna do say this function is going to print x and I'm going to
return funk two but notice I don't call it now this is just to prove to you that functions are
objects hence they can be passed around just like variables so what I can actually do here is I can
go ahead and I can say print or sorry not print but I can call funk four or funk three and then I
can actually call it like this what this is doing essentially saying okay I'm gonna call this function
here with the value three what this function does is it returns to me another function so it gives
me funk two notice it's not calling the function it's simply returning it now I'll actually just
show you what I mean first before confusing anyone let's print this out before I do the call
statement on it but you can see that this actually tells me this is a function object it's a funk
dot locals funk two so it's pretty much saying that funk two is defined within funk now if I
actually call this because that's just the function reference or object is not actually you know
being evaluated if I call it like that we can go ahead and we get three and then we get none of
course the print statement's gonna print none because there is no there's nothing to print from the
return of funk two but just have a look here we get three now this is equivalent if I did something
like this I said you know x equals funk and then on the next line I just called x since x is equal
to a function funk two I can call it just like I showed you before and there we go we get three
anyways that's the advanced aspect I want to show now what I want to do is show you about star
arcs and star star quarks so I'm gonna put in like this all right so before I define what this does
what I'm gonna do is just show you what the unpack operators and Python so let's say have a list
maybe we'll call it x and we just have a bunch of random numbers in this list so what the unpack
operator does is actually separate all of the elements from a list or from a collection into individual
elements the best example of this is the print statement if I print asterisk x which means unpack
x what this is gonna do is take all of these elements out so separate them by individual elements
and pass them into the print statement as arguments so instead of just printing you know say the
entire list what it's actually gonna look like is print one comma two three comma two three six
both while you get the point that is what this is gonna do so just have a look at what happens when
I print that I get all of these things separated by spaces like what happened if I just pass them
an individually not being in the list now look what happens when I just print x let's have a look
see it actually prints the list out so what this does is unpack whatever we have in a
tuple or a list or some kind of collection and sends it through as arguments to a function so that's
where star arcs and star star quarks come in although you'll see that in one second so let's say I
have you know x y that we need inside of function we got to pass x y what this will do is simply print
x and then print y now let's say I actually have maybe a bunch of pairs that I want to pass
this function so maybe I say pairs equals and then I have like one two three four what I'm gonna
do is do a four looping say four pair in pairs and what I want to do is call this function with
those pairs now then I avoid to do this would be to go funk okay pair zero and then pair one great
that would work but that is not to be Pythonic way to do this what we'd actually do is do
asterisk pair and what this will do is take one two and take three four unpack them separate them
and pass them as arguments to funk so if you have a look at that that actually works and that is
why the unpack operators really useful you can also use this actually on dictionaries as well
now I'll show you how it works with dictionaries a little bit more complicated involves two asterisks
what this does is if I have keys I can say x representing obviously the argument x and then
make that equal to two and then I can say why and this could represent the argument y and I'll
make that equal to five now if I run this this works fine I needed two asterisks because this was
dictionary and that is kind of how this works yeah I don't I guess really need the four loop anymore
to illustrate this but the double asterisks is used for dictionaries the single asterisks is used
for a tuple or for a list now I just want to make the point here that let's say these are not
in the correct order so it's why and then x this will still work and that's actually why this is
really useful because you don't have to have it in the correct order so long as you name the
arguments as the keys so now hopefully that will explain to us how star arcs and star quarks work
so star arcs and star star quarks essentially imagine you have a function and you don't know how
many arguments positional or keyword arguments you want to accept this what quarks stands for keyword
arguments well what you can do is you can use star arcs and star star quarks and what that will do is
allow you to pass in an unlimited amount of regular arguments and keyword arguments so if I
actually go ahead and go funk and then I pass like one two three four five and maybe I'll pass
some keyword arguments like you know one equal zero two equals one I know this doesn't really make
any sense but you get the idea now if I actually run this imprinted out you can see that when I
print arcs and print quarks we're getting a tuple that has all of the positional arguments which
are these and all of the keyword arguments which are one and two now if I wanted to actually use
any of these what I can do is I can unpack them so first of all I can unpack arcs and what this does
if we have a look here is it will print out one two three four five I could try to unpack quarks but
you're gonna see what happens when I do this and hopefully if you remember the last example you'll
realize why this is not gonna work it says one is an invalid keyword argument for print so what
this is gonna do when I unpack quarks is it's gonna take all my keyword arguments and it's gonna
say one equals zero and it's gonna say two equals one and pass that to the print statement now
obviously one and two well those are not valid arguments for the print statement so we're gonna get
an error anyways that is arcs and that is quarks you're gonna have to look into this on your own
and kind of learn about how it works but that is the basics and I just wanted to show that to you
so that you're aware that you can do these cool fancy things in Python next one is gonna be very
fast but this is just defining scope and global so when we look at a function like this there's a
notion of a scope so we have x equals Tim we have defined funk and then we have name as a parameter
and what we're doing is we're saying okay we're gonna change x to be equal to name so essentially
if I was naive and I didn't know any better I would assume that that what's gonna happen as well
this variable up here would change when I pass this name to this function any of you that program
before probably know that's not gonna work but if you have a look here you can see that x does
not change before or after when I call funk with the string changed the reason for that is that
this x variable is local it is within the scope of this function which means it cannot be used
access change from with from outside the same thing here with this x this one is actually global
this is on you know not not defined within any scope other than this file which means well I can't
just directly change it from this function I could however access it from this function I could
print it here but I can't actually change its value because if I do that what's gonna happen is
I'm gonna create a new x inside of here which is equal to name and it will be local to this
function it will not modify this however there is one interesting thing we can do in Python which
is the global keyword if I global x now you're gonna notice that the expected behavior will occur
now we get him and we get change what that is saying is okay I want to use x as a global variable so
I want to reference x in the global scope never use this this is never good to use and if I ever see
a Python program that has it I always get upset but this is something that you can use and I feel
obligated to show you because there is some very very rare situations where you may need to use that
all right so now I'm gonna spend about 10 seconds showing you how to actually raise an exception in
Python there's a keyword called raise maybe in Java you've seen something like throw and raise can
raise an exception if you want to do that so I could raise exception and then inside of here I could
just say bad you know whatever I want now if I run the program as soon as we hit that line immediately
I'm gonna get an exception and it says raised exception bad exception bad of course that is the
basics there is more arguments and things you can do with this this is a base class which means you
can extend it when you get to object or into programming make your own exceptions and make all that
fancy stuff for our cases that's all we really need to know I could raise like a file exists error
any other kind of error that I want and then again add some description for it by just putting a
string inside of the brackets that's enough for that now let's show how we can actually handle
exceptions all right so now that we know how to raise exceptions it makes sense to learn how to
handle them so in Python rather than having like a tried catch we actually have a try except
finally block so what I can do is do something like try that means I'm going to try to perform
whatever code is inside of here maybe I'm gonna get an integer division by zero error so I can use
you know seven over zero that's gonna raise an error then I can create an except lock I can accept
an exception as e what this is gonna do is mean whatever the exception is will be stored in the
variable e and then second print out that exception and we can continue running the program this is the
basic try except block you don't actually need to define something here I can just say try except
like that if I want and also accept a specific exception so I don't have to just put exception
this is general this means any exception that occurs I will actually catch you can only catch
specific exceptions if you want but again that's up to you so in this case you see we get division
by zero it just printing out but it didn't actually raise or you know show us that error so did
raise it but we didn't actually see it now if I go ahead and just do this what we can do is we can
say oh well that is the actual problem and now our program is gonna crash and not work but if we
we have this except block like this of course we're good to go now there also is a notion of a
finally block the finally block will run it no matter what usually you put cleanup type operation
inside of here maybe you're trying to write to a file some exception occurs and the no matter if
this was successful or not you want to close the file after you would put that inside of finally so I
could say print finally like that have a look here and you can see we get finally I'm not gonna
go through this too much I assume if you're gonna be using this you understand how try except
and finally work but that is the implementation in python and that is the basics we're raising and
catching an end and now we're gonna discuss the lambda so lambda is actually a really cool aspect of
python I'm pretty sure they have them in many other programming languages as well but it looks
something like this essentially a lambda is a one line anonymous function what that means is not
really a named function you don't define a lambda using the def keyword what you can do is something
like x equals lambda x and then x plus five what this means is this lambda is gonna take one
argument which is x and then it's going to return x plus five not multiplied by x didn't mean to do
that that means if I call x like this and I don't know x2 and we print this let's just go ahead
and have a look at this here oops again did not mean to do that we see that we get the value seven
now I will note that this is not the advised way to use a lambda we're gonna see how to use it in
just a second in the next section but this is the basics of how it works it's just a one line
anonymous function so I can do something like xy as well and then I can do something like x plus
y not capital y and if I go ahead and call x with I don't know two three or two 32 sure that's
fine several look here we get to the value 34 so that makes sense on how the lambda works it looks
intimidating but it's really not that complicated and in the next one you'll see why these are
and now I'm going to talk to you about map and filter which are two useful functions and Python
which make use of the lambda functions now you don't necessarily need to use lambda function but
a lot of times it comes in handy so let me show you what I mean let's make a list let's add
oops I wanted to do a few more commas than that let's add a bunch of elements inside of here to
make it kind of nice and long so we get some interesting output and now what I'm going to do
is I'm simply going to show you how we use map so what map we'll do is we'll take all of the elements
of a list and use a function to map them into a new list so if I say something like MP standing
for you know map I'd say map which is the actual function now what I do is I put a function inside
of here that I want to use for the map so this will make sense in a second let me say lambda x
x plus two so this essentially saying add two to every single one of these elements or add two
to x in this case and then what I do is simply pass x now I know x is kind of confusing so maybe
we will just change this to i so that's easier to differentiate but what this is going to do is say
okay we're going to map all of the elements inside of x so in this list x to this lambda function
which means take this lambda function and apply it on every single element of x and then put that
into a new list now what I'm going to do is I'm going to print the list representation of MP
map actually returns to us a map object we can use that if we're going to iterate over it but it's
usually more useful just to convert it right into a list so let's have a look at what this does
and there we go now we get three four six and you can see all these elements have been added by two
now we knew more complicated one maybe we can multiply by two it'll seem more of a difference
and there we go now the next function I want to show you is filter so hopefully you have the idea
of how map works but filter I don't want to say the opposite but it does something a little bit
different so this function here which is a lambda what it's going to do is instead of actually
returning some value it's going to return true or false and it's going to tell us whether or not
we should include the item in our final filter list or filtered object so essentially this lambda
function or whatever function we put inside of here has to return true or false based on the value
of an item so what I could do is say something like I it I mod 2 equals equals zero so this means
you know only return it if it's even so this is the function take some value I tells us if it's
equal to or sorry divisible by zero and if it is we'll include it so let's have a look now
and you can see that we only get all of the even elements in our list now of course what we can do
is also define a more complicated function so we can say define funk take some value let's go with i
and we can do the same thing here we can say return i mod 2 equals equals zero but we can also say
you know i equals i multiplied by three so we can do that and then we can say i mod 2 equals
equals zero then instead of using a lambda here we could simply just write funk that's totally
fine so we just put the name of the function that we want to use and then we run this and it
works fine but this is why lambda's useful because a lot of times you don't want to define your
own function up here you just want to define it right inside of the map statement or the filter
statement so that's kind of where you would use this there's another few use cases for it but
this is kind of the most common and where you'll often see it so anyways that is map and and for
our last and final topic I'm gonna be covering something called f strings now f strings are new
in python 3.6 if you have any version lower than that you are not going to be able to use f strings
but they're a really cool way to actually just manipulate and create strings so what i can do is
do something like x equals i can do an lowercase f or a capital f it doesn't matter and then a string
notice that this will highlight or maybe it'll be italicized depending on what editor you're using
and well what you can do is type a string as you normally would except now if you want to embed
an expression what you can do is do that inside of curly brackets so i can do hello and then i could
say like you know six plus eight and this will actually be evaluated and now when i print this
out it will give me the answer 14 so this is great because if you have some variables say like i have
you know i don't know tim equals 89 or something and i want to embed that in the string without
having to concatenate it and convert it into a string with str i can simply just use the f string
and just embed it with all the formatting by putting it inside of these curly braces so that is
kind of how that works you can go ahead and mess around with this if you want to really see how
it works but essentially you just put an expression inside of these curly braces it will be
evaluated and then it will turn into a string really great easy way to work with strings of course
you can print an f string as well i could print hello and then we can do that and in fact let's
run this and just see what we get and there we go we see we get hello 89 so that is f strings
so that being said i'm going to conclude the video here i am officially out of breath i went
very fast do this tutorial with the purpose of making it as quick as possible and not wasting
any of your time if you guys appreciate this effort please do leave a like subscribe to the channel
and of course let me know what you want to see next i will quickly mention that i did miss a few
things in this video uh specifically object oriented programming in advanced language features
if you would like to know about those two things i have a ton of resources related to them i will
leave them in the description the first is a about an hour long video on object oriented programming
and python that covers kind of all the fundamentals and everything you need to know then the next one
is a whole series on expert level features in python i believe that's about six videos and cover
things like meta classes context managers generators all of that anyways as i said i hope you enjoy
if you like subscribe i will see you in the next one
Introduction to Python
Downloading Python
Core Data Types Overview
Understanding Print Function
Working with Variables
Creating a Virtual Environment
Getting User Input
Using IDLE for Python Development
Using Arithmetic Operators
Introduction to Visual Studio Code
Understanding Order of Operations
Extending Python Functionality with Libraries
Basic Code Execution in Python
Navigating the IDLE Environment
Writing Basic Python Scripts
Executing Python Scripts from IDLE
Understanding Errors and Debugging
Exploring Visual Studio Code Extensions
Creating a Python Project
Managing Python Packages
Introduction to Lists
Using Git for Version Control
Debugging with Visual Studio Code
Best Practices in Python Development
What is a general purpose programming language?
How do you add Python to the system path?
What are the four core data types in Python?
How to use the print function effectively?
What are the benefits of using a virtual environment in Python?
How does IDLE simplify your Python coding experience?
Why is Visual Studio Code popular among developers?
How can libraries enhance your Python projects?
How can you enhance your coding experience with IDLE's navigation features?
What steps do you need to follow to write a basic Python script?
How do you execute a Python script within IDLE and what should you watch out for?
What are the essential steps to create a Python project?
How do you manage Python packages effectively in your projects?
What role does Git play in managing your Python codebase?
How can user input be stored in a Python variable?
What are the basic arithmetic operators in Python?
How does Python handle order of operations?
In this tutorial, we will learn how to download and install Python, create an environment to write code, and use a text editor to write and run programs. We will start by explaining how to download the most recent version of Python from the official website and how to check the box that adds Python to the system path for easy future use. Then, we will move on to explain the different environments provided by Python, including idly, which is an interactive environment where you can write and run code, and Visual Studio Code, a popular text editor for writing and running programs. Finally, we will provide tips on how to make your life easier when working with Python.