Full transcript
Introduction
0:00hello everyone and welcome to the
0:01complete python course this course is
0:03designed to teach you everything you
0:05need to know about the Python
0:06programming language this is going to
0:08assume you're a complete beginner you
0:10have no knowledge of python and bring
0:11you through right from the beginning all
0:13the way up to expert and advanced level
0:15features so this course is actually a
0:17combination of a bunch of videos that
0:19I've created over the past few years it
0:21starts with beginner Python Programming
0:23goes to object-oriented programming then
0:25into intermediate and advanced level
0:27features so those are the four main
0:29section of this course it's kind of
0:31split up in those four sections and
0:33within those sections there's a bunch of
0:34topics now if you want to figure out
0:36what section to jump to or you want to
0:37see what's involved in the course look
0:39in the description there'll be a bunch
0:40of different timestamps that show all of
0:42the different topics that are covered
0:44and feel free to skip through and go to
0:46the ones that you find the most
0:47interesting so with that being said this
0:49video is taking a very long time to
0:51compile and create so I would appreciate
0:52if you guys leave a like you subscribe
0:54and you consider becoming a channel
0:56member if you want to support me on a
0:57monthly basis so with that being said
0:59let's go ahead and get into the full
1:01python
Variables & Data Types
1:14course hey guys so welcome back um today
1:17I'm going to be doing a Python tutorial
1:20Series so this is the first episode in
1:23the series it's kind of an introduction
1:25showing you how to download python how
1:27to install it and then going into data
1:29types and variables so the first
1:31important thing to understand about the
1:34Python programming language so don't
1:36worry if you've never programmed before
1:38um I'm going to be going over everything
1:40very slowly and in depth so that you can
1:43understand I recommend that you follow
1:45along with everything I'm doing in the
1:46tutorial uh pause the video at certain
1:48points rewind if you don't understand um
1:51it's important you understand these
1:52parts so that we can move forward into
1:53more complex
1:55ideas so obviously the first um thing
1:58that we're going to have to do if we're
1:59going to learn python is we have to
2:00install it on our computer so uh we're
2:03just going to go to the python website
2:05up here python.org
2:07downloads right here and all you're
2:09going to do is just click download
2:11python
2:133.6.1 now I'm not going to do that
2:15because I already have python installed
2:17and downloaded on my computer but pretty
2:19much once you uh download this it's
2:21going to pop up down here in your
2:23downloads folder just run the exe it's
2:25very simple um and you'll be running
2:27python in no time so now pause the video
2:29go ahead and do that and then we can go
2:31to the next
2:32steps okay so once you've done that
2:35you're going to notice that you have an
2:37application that looks like this it's
2:38called idle um Python and then it'll
2:41have the version so I'm using version
2:443.5 um don't worry if you have a version
2:47that is larger than that so 3.6 or even
2:49one that's before that most of the
2:51things that I do in this will work in
2:52all the versions of
2:54python okay so go ahead and run the idle
2:57you'll notice the first thing that pops
2:59up is the python shell as you can see at
3:02the top here the shell now this right
3:05here is not where we're going to write
3:07all our programs it's the console kind
3:10of so here I can type any lines of code
3:13for example I want to
3:15print a name like this and it'll run
3:18right away so that's not what we're
3:20going to do you can play around with
3:21this if you want but we're going to go
3:23to file and we're going to click new
3:26file and then here we go so now we have
3:29a new document that we're going to be
3:31working on and we're going to be
3:32programming in so I'm going to go ahead
3:34and I'm going to save this right away as
3:36tutorial one into my tutorial folder
3:41which I believe is on my
3:42desktop right here yep right there
3:45awesome okay so there we go we've saved
3:48it and now we're ready to learn a little
3:50bit about data types so pretty much
3:52python is a fairly basic language in
3:55comparison to a lot of other ones a lot
3:57of things that it does is uh more simple
3:59than other languages but it is important
4:01to understand the basic data type so
4:03there's four main data types in Python
4:05there is a few more but we're just going
4:07to talk about these ones right now um
4:09the first one is an integer so that can
4:12be uh a short form int that stands for
4:15integer now um an integer is any number
4:18so such as 1 4 76 any whole number it's
4:24important that is a whole number it
4:25could be a negative number as
4:27well5 um zero those are are all
4:30integers okay now we have strings now
4:34the short form for Strings is St now
4:37pretty much a string is anything
4:39embedded in quotations so like this uh
4:42this is my name Tim that is a string and
4:44then we also have something like this
4:47this would be a string and even like
4:51this this would be a string so although
4:53there is a number in here since it is
4:55between these two quotations here it is
5:00a string so even if I did another number
5:02like like that string um now it's also
5:06important to understand that it doesn't
5:07matter if you use the single quotations
5:09or the double quotations sometimes we
5:12use the single quotations because inside
5:14of our actual quotations we want to use
5:17double quotations or maybe um if we use
5:20the double quotations because we want to
5:22use the single quotations inside of our
5:24actual string like that okay so those
5:27are just a few examples um now we have
5:30Boolean variables uh or data types sorry
5:34so Boolean data types is something like
5:38true and false so true and false pretty
5:42basic um these are reserved words in
5:45Python um meaning that they're
5:47highlighted in yellow and that you
5:48cannot use them for variable names
5:50that's going to be important later on um
5:53just know that they are important words
5:55uh true and false and they do start with
5:57capitals so um for example if I typee
6:00true like this uh it's not highlighting
6:03Orange right so that's because true um
6:06in Python the keyword for that starts
6:07with a capital so true and false um then
6:10we have the um decimals which are the
6:14float uh data type like this so anything
6:17with a decimal floating point so
6:201.23 um anything like that any decimal
6:23number with a floating decimal point uh
6:25would be a
6:27float okay so those are the four basic
6:31um data types um there is a few more
6:34that I'll go on to later on but you just
6:36have to understand these ones right now
6:38so now that we know these data types
6:41I'll give a quick little quiz here so
6:43I'm just going to put a
6:45few things right
6:50here and then I want you to pause the
6:53video and go ahead and try to determine
6:55what type of data type these things are
7:00okay so go ahead pause the video and
7:02then I'll go over the answers in a
7:05second okay so we'll start with the
7:07first one here uh hello it's the same
7:09example that I have up here except in
7:11single quotations so we can see that it
7:13is a string data type okay so that is
7:16the short form Str Str now we have the
7:19number
7:20123 that would be the integer data type
7:24we have another number here that has a
7:25decimal point so
7:273.22 that would be a float
7:30we have the keyword true this data type
7:33is a Boolean data type then we have two
7:37in quotations this is very important we
7:39don't want to get this mixed up this is
7:40a string some people may think that it's
7:43an integer because it's two but no it
7:45actually is a string because it is
7:47inside of the quotations and then same
7:49thing here three inside of the double
7:51quotations again is a string not an
7:53integer okay so now that we have an idea
7:56of the basic data types um I'm going to
7:59show you how to create a variable so
8:02pretty much you may have heard of
8:03variables before in math or something
8:05like that um in math we tend to use X as
8:08a variable we say x may be five x may be
8:11four anything like that um it's the same
8:14thing in Python now declaring a variable
8:16in Python is very easy all you have to
8:18do is type the variable name so in this
8:20case I'm going to use name and then
8:23simply give it a value so this value as
8:25you may remember is a string this is my
8:27name and I'm just storing it in the
8:29variable
8:30name now if I want to access that
8:33variable I want to see what that
8:35variable is I can print it to the
8:38console by simply typing the variable
8:40name which is name so now we'll see what
8:43happens um if I run the program just
8:45going run run module I'm going to use F5
8:47from now on so that's the uh thing and
8:50you can see it prints out Tim just like
8:52that okay now um say we did that we have
8:56name equals Tim we printed it to the
8:58screen now maybe we want to change that
9:00variable well it's very easy again all
9:03we have to do is just type the value
9:05name and now maybe we're going to change
9:06it to Bob and now we want to see what
9:08the new VAR new variable is so we will
9:11print it to the screen we'll print name
9:14cck F5 to run that and we'll see name
9:17and or sorry Tim and then Bob so like we
9:20have here name equals Tim print name
9:23name equals Bob print name right so
9:26we've set it like that okay so that's
9:28pretty basic um
9:30now you can create variables of
9:31different data types so for example if
9:33we had a variable age I would use an
9:36integer data type so like 18 for that
9:39variable and then again if I wanted to
9:41print into the screen I would type print
9:43and then the variable name like
9:45that okay 18 perfect right so we printed
9:49that to the screen um again if the
9:52variable names um there's a few
9:54restrictions on variable names so here
9:58you just see I've used three lowercase
10:00letters um your variable names can
10:03contain underscores um they cannot
10:05contain dashes um they can only contain
10:08underscores text and they cannot start
10:11with a number I believe so if I try to
10:13do one one name equals 2 for
10:18example I run into an invalid syntax
10:21error because we can't name a variable
10:23starting with a number now I can end it
10:25with a number I
10:27believe let us try
10:31yep so that's fine it's let us do that
10:33name equals one I could do name
10:36underscore um person maybe yeah namore
10:40person you see if I run that program
10:42everything's fine there um but I can't
10:45put something like a star in my variable
10:50name you see how we've got an invalid
10:52syntax there right this is because these
10:54symbols represent something else in the
10:57Python language so just know for names
11:00um we can use capitals if we'd like I
11:01could use name like
11:04that run the program that works fine but
11:07I cannot start it with numbers so I
11:09cannot put a number there um and I
11:11cannot contain anything other than
11:13underscores texts and numbers in or at
11:16the end of the variable name now here's
11:20a quiz for you if I make a variable
11:22called name and do another variable
11:24called
11:26name um are they the same thing now
11:29that's a good question um are these two
11:32the same thing um no they are not the
11:34same thing so capitals do matter in
11:36Python the all capitals variable name is
11:40different than the variable name so
11:43again name like this is different than
11:45all of those variables there okay so I
11:48think I've covered um a little bit on
11:51variables and data types today um this
11:53is the complete Basics just wanting to
11:56get everyone caught up um in the next
11:58video we'll go into some more advanced
12:00things maybe uh operations of variables
12:03adding them together subtracting
12:05multiplying um buing conditions things
12:08like that okay so I hope you enjoyed the
12:10video today um stay tuned and we're
12:13going to have another one out tomorrow
12:15um and like the video if you if it was
12:17good and subscribe so yeah thank
Basic Operators & Input
12:24you hey guys so welcome to the second
12:27video in the series um if you remember
12:29in the last video we talked about data
12:31types and variables uh we went briefly
12:33into some of the things that python can
12:35do um today I'm going to be talking
12:37about operators and we're going to be
12:40doing some print statements some input
12:43um a little a few small console
12:45applications just applying some of the
12:47knowledge that we've learned so pretty
12:50much the first thing I want to do is I
12:51want to go back and I want to review
12:52what we did in the last lesson very
12:53quickly so we talked about variables and
12:55data types so for example the X variable
12:57we could set it to something equal to
12:58two like that uh a name we may equal to
13:02our name right so I make that equal to
13:03Tim um so there we go we have the name
13:07right there equal to 10 we'll start with
13:08that okay now if you remember in the
13:11last one I didn't talk about this but I
13:12used something called print now the
13:14print statement pretty much takes an
13:16argument so inside of these brackets the
13:20thing that is inside that brackets is
13:21called the argument so it takes a uh a
13:25string argument usually so I will give
13:27it the name so now you'll see again we
13:29did this in the last one just want to
13:30review I'm clicking F5 simply to run the
13:32program when I go to the console it
13:35simply puts Tim onto the console just
13:37like that
13:38right okay um very basic we already did
13:42that so now let's say well we want to do
13:45something else so let's let's print here
13:48and we're just going to put a string and
13:49we're just going to type this in
13:50ourselves so let's say hello comma what
13:54is your
13:57name that's going to print to the
13:59console now we want to get what the
14:01user's name is now there's a way to do
14:04this in Python it's very simple we're
14:06going to make a variable name it makes
14:08sense to put the variable name here and
14:10do an equal sign because it's a variable
14:12right we're going to type the word input
14:16just like this now what's going to
14:18happen if I run the program here I'll
14:20show you is it's going to say hello what
14:24is your name and it's going to actually
14:25allow me now to type into the console
14:28which I couldn't do before now nothing
14:30happens after when I click enter because
14:31we don't have anything else after that
14:33but it's allowing me to
14:35type okay so now well we want to print
14:38out what the name is that was said so
14:40let's just simply type print and then
14:44name all right and we have hello what is
14:47your name I say oh well my name's Tim it
14:49says Tim all
14:51right now well we just printed out the
14:54name but maybe we want to go a little
14:55more advanced than that we want to say
14:57well hello Tim how are you doing Tim
15:00something like that right so I'm going
15:01to just put a comma here going to
15:04separate these two things and I'm going
15:06to put in a string and I'm just going to
15:08type what I like to type so I'm going to
15:09say hello with a comma and then you see
15:13that I have this other comma here that
15:15is outside of the quotations meaning
15:17it's not a string it's actually just
15:18separating the two arguments in here and
15:20you'll notice what happens now when I
15:23print is it goes hello what is your name
15:25so I say oh my name's Tim it says hello
15:28Tim
15:29right so that's pretty straightforward I
15:31hope that's easy to understand we're
15:32just using the input to get an input and
15:34then we're printing it back out to the
15:36screen okay so let's do um let's go into
15:39the next part of this now we'll use that
15:40again later but let's talk about
15:42operators so operator that term may
15:45sound familiar to you um in math uh we
15:48use things called operators so these
15:50four operators hopefully should look
15:52familiar to you um this is a plus sign
15:55this is the addition operator we have
15:57the minus sign which is the subtraction
15:59operator the division sign which is the
16:02well division operator and then the
16:04multiplication sign which is again the
16:06multiplication uh operator so there's a
16:09few more operators that we'll talk about
16:10but these are the four basic ones in
16:13Python now you remember how I talked to
16:15you about data types this is where this
16:17comes in when we use these operators
16:19it's important that we use them on
16:21certain data types so for example um in
16:23math I could do something like 3 + 4
16:27right so what is 3 + 4 equal well that
16:29is equal to 7 so the computer can
16:31actually do that math operation by using
16:34that operator to return those values so
16:36let's let's give an example here quickly
16:38so I have uh let's do num one we're
16:41going to use variables here uh remember
16:43I can use a number in the variable as
16:45long as it's not at the beginning and
16:46I'm going to give it a value of 45 and
16:48then we'll do num two and we're going to
16:50give it a value of three now if I want
16:53to print to the console um let's say num
16:57one plus num 2
16:59well you can guess what that's going to
17:01give us it's going to give us 45 + 3 I
17:04hope if this is not an
17:07error yeah so that gave us 48 right so
17:11oops didn't mean to make that full
17:12screen num 1 plus num two is 48 all
17:16right so now what about minus let's try
17:19this 42 basic um we can do
17:22multiplication here with the
17:24multiplication
17:26sign there we go 135 and then we'll try
17:30again with the division sign just to
17:32show you all of them we get
17:3415.0 like that okay so that's pretty
17:37straightforward um those four basic
17:39operators now in Python there's a few
17:42more operators that we want to talk
17:44about now for example in math we have
17:46something called exponents right so how
17:48do we do exponents in Python well it's
17:51actually two stars is how you do an
17:53exponent all right and now maybe there's
17:55something called integer division um
17:58which I'll get into later but I'll just
17:59show you the operator for it right now
18:00it's two slashes that means uh I'll give
18:03you an example a math example here
18:0564 /
18:07by um let's do 10 um would give us
18:11usually in math a value of 6.4 right
18:15that's if you use one division sign
18:17sorry what am I doing times I mean
18:19divided by would give us 64 I have 6.4
18:22but now if we do two division signs here
18:24like this it actually gives us a value
18:26of six that's because it just um it
18:29doesn't worry about the remainder at all
18:31it just tells us um how many times 10
18:34can go into 64 um and that's all it can
18:37go in six times evenly so it gives us a
18:40whole number as our answer that's called
18:41integer division this double double
18:44slash like
18:45this okay and then we have another
18:49operator which is actually the modulus
18:51operator um so this is the percentage
18:54sign and this gives us the remainder so
18:57here if I do five 5 modulus
19:012 then the remainder of that is actually
19:051 because 5 / two is four then with a
19:09remainder of one right so it's not going
19:10to give us the decimal points it's again
19:11just going to give us the remainder if I
19:13did five / by four again the remainder
19:15is one 5id by 3 the remainder would be
19:17two like that okay so that's what the
19:19modulus sign um gives us so we'll put
19:21modulus here and then there's probably a
19:24few others that I'm forgetting about but
19:26we can go into those later
19:29so now um same thing let's keep using
19:31our number variables here and now let's
19:33introduce a third variable called
19:35num3 now um I want to show you how we
19:38can use the variables so num one and
19:40let's see if you remember what this
19:42operator is uh so that's the exponent
19:44operator so that means num one which is
19:45going to be our base uh which is 45
19:48raised to the power of four because
19:50we're going to use num two and we can
19:53print num
19:55three press okay
19:59and you can see we get a pretty large
20:00number um that's because of how
20:01exponents work and
20:03then same thing here if we want to do
20:06maybe integer division see what we're
20:08going to
20:09get we get 11 right a whole number and
20:13then say we want to do
20:18modulus we get one okay so now let's tie
20:21all these things together with the
20:22operators that we've used the variables
20:24the prints and the
20:26inputs so um let's start by just get
20:29doing a little print statement and
20:31saying pick a
20:35number okay pretty basic we're going to
20:37pick a number and then we're going to
20:39take the input so we're going to say num
20:40one again is equal to the input of
20:45that okay so we got the input in Num one
20:48um now let's say
20:52print pick another
20:57number all right so now we're going to
20:59pick another number so we're going to do
21:00num two is equal to input like that and
21:04now what we're going to do is we're
21:05going to introduce our third variable so
21:07we'll call it sum because we're going to
21:08add these is equal to num one plus num 2
21:12and now actually you can't use sum
21:14because it's a reserve word so let's
21:15just do in all capital sum because
21:18remember how we talked about variables
21:19capitals and lowercase are different and
21:21then we're going to print to the
21:24console the sum like that okay
21:29so we run the program says pick a number
21:31uh let's pick a number let's say four
21:33and let's do 32 now what do you think
21:36it's going to
21:37be oh oops okay
21:42so yeah someone equal num one plus num
21:46two okay so this is why um I talked
21:48about data types so I'm actually uh
21:50Happy this happened it's because what
21:51actually happens when we get the input
21:53of something is it gives us the type of
21:55a string um which in this case is not
21:59what we wanted to do so um you saw there
22:02I'll run it again um it's a good mistake
22:04that this happened actually we have four
22:06and we have a three and it gave us 43
22:09that's just because we added the string
22:10four to the string three um so that
22:13simply gave us 43 but now we know in
22:15actual math that 4 + 3 equal 7 so if we
22:17want to do um the integers we have to
22:20actually convert these variables into
22:22integers so in order to do that remember
22:24I showed you the keyword um int like
22:27this before we're just going to put
22:29brackets around our two variables here
22:32like this
22:33integers and now hopefully we should get
22:37the correct answer when we do four and
22:39we do three and you can see we've got
22:41seven so um I didn't mean for that to
22:43happen originally but I'm happy it did
22:45because it shows us why data types are
22:47important so when we take the input of
22:49something we're typing it from the
22:51keyboard and that is actually a string
22:53so um here we can print again um don't
22:57worry about what I'm doing right now but
22:58I just want to show you the type which
22:59is going to give us if it's a string if
23:01it's an integer of num two just to show
23:03you what the type actually is so we pick
23:06a number we pick two and we pick three
23:09it tells us that three is actually a
23:12string so that's why when we added
23:14originally three and two or two and
23:16three or whatever it was um it gave us
23:18just them added together so two and
23:21three rather than what it should be
23:22which is five okay now same thing here
23:26if we had a number and we wanted to
23:28convert it into a string all we would do
23:31is we would type Str Str around the
23:34number so for example three and then
23:36that would give us three just like that
23:39so A String so now we've kind of gone
23:42over a conversion of variables um
23:45conversion of numbers data types how to
23:47get input from the console using the
23:49input like this um and some basic
23:51operators of python so uh review this I
23:55hope you followed along and then in the
23:58next next lesson we'll move into some
23:59more advanced operators and we'll maybe
24:02start with conditions okay I hope you
24:04enjoyed uh please subscribe like the
24:06video and I'll see you in the next
Conditions
24:14video hey guys uh welcome back to the
24:17third video in my Python Programming
24:19tutorial series um today we're going to
24:21be talking about something called
24:23conditions so I hope you remember from
24:25the other videos we talked about input
24:27printing we did variables data types and
24:31then we talked about operators so
24:32operators like plus minus um division
24:36integer division modulus right uh
24:38multiplication all those operators today
24:41we're going to be talking about a few
24:42more operators and then getting into
24:45conditions so here in my file right here
24:48um I've put down four basic comparison
24:51operators so pretty much a comparison is
24:54something that's going to return a value
24:56of true or false so here we have um
24:59let's say if a real world example is Tim
25:02equal to Joe well no they're not because
25:07Tim obviously spelled differently than
25:08Joe so that's going to be giving us a
25:10value of false um same thing if we want
25:13to go into numbers we say 18 is greater
25:16than two well is that true yes it is so
25:20that condition gives us a value of true
25:23uh it's a very simple concept but it's
25:25something that we have to understand as
25:26we go into decision Visions later on
25:29which will be in the next
25:30video okay so these comparison operators
25:33I'll read them off and just kind of give
25:34a definition of each of them so the
25:36first one can e be a less than or a
25:38greater than sign uh same thing with the
25:40second one right so it's just going to
25:42compare if something's less than greater
25:44than um usually these are used for
25:46integer values um that data type uh we
25:50also can use them for Strings but it's a
25:52little more complex and I'm not going to
25:54get into that right now maybe in a more
25:56advanced tutorial later on now the
25:59equivalent operator so the equal sign
26:02here now a lot of people think well if
26:04I'm comparing something why wouldn't I
26:06just use one equal sign like this well
26:08that's a very good question and a lot of
26:10people get mixed up on this it is
26:11actually two equal signs that's because
26:14in Python um the one equal sign is a
26:17declaration um declaration operator so
26:21for example if I put a variable X and I
26:24want to set it equal to four right um
26:27this is is what we do with the one equal
26:30sign we're setting values right so when
26:32we did name is equal to Tim things like
26:35that we're using one equal sign now if
26:38we're going to check something we're
26:39going to compare them we have to use two
26:41because two equal signs and one equal
26:44sign have a very different meaning so
26:46it's important to keep that in mind that
26:47the comparison operator for equivalency
26:49is two equal signs okay the next one is
26:53not equal to um so we have an
26:56exclamation mark and then we have the
26:58equal sign like this okay so an example
27:01of not equal to because some people may
27:03get confused here is say if four not
27:06equal to 5 oops what did I type
27:10there not equal to 5 okay so what do you
27:14think this would give us just think
27:16about it for a
27:17second well what it's actually going to
27:20return to us is true because the
27:23condition is if four is not equal to 5
27:28and here we can see obviously four is
27:30not equal to 5 so now again if I give
27:33another example we put five here we say
27:34five not equal to 5 well that would
27:37return a value of false because five is
27:40indeed equal to five now we can do the
27:43same thing with strings so for example
27:45we have hello oops spelled incorrectly
27:48there we go
27:50and Tim uh now you can probably guess by
27:55yourself that this is going to return
27:56true because him is indeed not equal to
28:00hello okay uh it's pretty
28:03straightforward um but a lot of people
28:05may get mixed up with the not equal to
28:07if it's returning true or false okay so
28:10those are the four basic comparison
28:11operators that we're going to use so we
28:13have equal to U not equal to and then
28:15greater than and less than so greater
28:17than and less than again we use for
28:19numbers so one less than two again
28:22that's going to return true like that
28:25okay so now um we're going to start
28:28printing from some things to the console
28:30and maybe doing a little bit of a quiz
28:32here um just to see how you guys are
28:34going to do so a booing condition I'm
28:37going to store it in a variable so I'm
28:39going to say name uh oops actually we're
28:41not going to do that we're just going to
28:42print to the
28:43console 2 less than
28:46three okay now you guys guess and uh
28:51tell me what you think is going to be
28:52returned to the console it's either
28:54going to be true or false I'll give you
28:55a hint right now
28:59now it gives us a value of true that's
29:01because this condition 2 is less than
29:04three um is true it's equal to three
29:08right um so now if we switch the sign
29:10and we make it greater than like that
29:13well you can guess what's going to come
29:14up so we click enter and we get false
29:18that's because this condition returns a
29:20value of
29:22false okay now we'll do another
29:24condition here all right um hello
29:28equal
29:30to Hilo okay um and guess yourself what
29:35this one's going to return false okay
29:38that's because they are spelled
29:40differently um now we're going to do the
29:41not equal sign just to show that one as
29:43well hello not equal to hello and we'll
29:46see that gives us true because they are
29:49not the same um fairly straightforward
29:52but a lot of people do get these things
29:53mixed up now I'll show you what happens
29:55when I try to just do one equal sign
29:58like
29:59this see we get keyword can't be an
30:03expression that's because we're trying
30:05to declare a variable here but we cannot
30:09do that okay because we're just using
30:11one equal sign here we need to use two
30:12all
30:13right okay so now let's try another
30:16example maybe a more advanced one uh
30:19this is a little bit of a trick okay I
30:20want to see if you guys remember from my
30:22last tutorial what I talked
30:26about so take a guess right now what you
30:29think is going to be returned you think
30:30it's going to be true or do you think
30:31it's going to be
30:32false well it was false okay um that's
30:36because again what we talked about in
30:37Python um capitals matter right so any
30:42capital letter is different than a
30:43lowercase letter so something like this
30:46um yeah it's going to return false all
30:48right it's important to understand that
30:50okay so we' very briefly uh gone into
30:53conditions now um I'm going to write a
30:55few on the side here you guys can go
30:56ahead um I'll put them in print
30:58statements and test yourself and see if
31:00you get them right or not
31:03okay I'm going to add a few tricks from
31:06the other lesson um try to kind of apply
31:08some of our
31:09knowledge and
31:12see if we can get them correct
31:16okay so just give me a minute here I'm
31:18just going to write a few
31:22out and then we'll go through the
31:24answers afterwards okay so you know what
31:26we'll just we'll just do these ones
31:28for right now and we'll do a few other
31:29examples okay so these are a little more
31:31advanced than what I was talking about
31:32before but they are conditions we have a
31:35left side of the condition and we have a
31:37right side of the condition so I hope
31:38you've taken a guess now of what you
31:40think these are going to be cuz I'm
31:41about to print out the answer right now
31:43Okay so we've gotten true false and
31:46false all right so for the first one two
31:49less than three that's an example I
31:51already gave that gives us a value of
31:53true because that condition is true uh
31:56now we have two - 3 + 4 greater than 5
32:01um so you can do the math there and see
32:02that that is going to be false and then
32:06this one some people may have gotten a
32:07little bit of a little bit confused on
32:09cuz I actually use the words false and
32:12true in my condition that's going to
32:14return false or true so I have print
32:17false equals equals true well is false
32:19the same thing as true no it's not so
32:21obviously we going to be returned with
32:23the value
32:24false Okay so we've talked a little bit
32:27about conditions now um in the next
32:29tutorial we're going to be doing
32:30something called decision so I'll give
32:32you a sneak peek on what that
32:34is something like this
32:38okay so stay tuned for that um the next
32:41episode should be up tomorrow um and
32:44yeah I hope you guys enjoyed the video
32:45go ahead like And subscribe and yeah
32:49share with your friends thanks
if-elif-else
32:56bye hey guys guys uh welcome back to the
32:59fourth video in my Python Programming
33:02tutorial Series today we're going to be
33:03talking about decisions so we're going
33:05to be using the if else and L if
33:08statements in Python so pretty much I
33:11gave you a little example last time on
33:13what these are but if
33:16condition equals
33:19true then do this sorry not his do this
33:25okay so pretty much the syntax for the
33:28basic if statement which is what we are
33:30going to cover first is like this so if
33:33you remember my last tutorial I talked
33:35about conditions if you haven't seen
33:37that go back and look at that first
33:39because that's going to be an important
33:40part of today's lesson and you have to
33:42understand that so if condition is true
33:46then we are going to do this now let's
33:49just dig into the syntax a little bit
33:51here um so you're going to start with
33:52the keyword if then we're going to put a
33:55condition so the condition could be
33:57something like this if 1 is less than
34:00two if
34:03tree is equal
34:06to plant something like that okay
34:09anything that can return a true or false
34:11value you can use variable names you can
34:13say if x is equal to Y anything like
34:16that is condition and it uses a
34:19conditional operator like I talked about
34:21in the last video and then you are going
34:24to end your condition with a semicolon
34:28or sorry not a semicolon like this with
34:29a regular colon and then you going to
34:31Simply click enter and it should tab you
34:34in one line now it's very important that
34:36you have this
34:38indentation um if you have your code
34:40like this it's not going to work python
34:43reads the lines because of indentation
34:45so it's very important that you have
34:47whatever statements you want to run
34:49after the condition returns true
34:51indented properly and this will also be
34:54very important as we move further on in
34:56different tutorials and we have have
34:58lots of different indentation levels
35:00within the program okay so let's do a
35:02real example now instead of uh this kind
35:05of pseudo code here so we'll start off
35:07by just getting some input from the user
35:09so let's say let's get their age okay so
35:13age is equal to input like this and then
35:16just if you put something inside of the
35:18input like this uh it actually gives a
35:20prompt to the user so we'll say
35:23input your
35:26AG like that
35:28and then we will say if the age is equal
35:35to 16 then we will print to the
35:40screen hey
35:43your oops 16 like that
35:47okay um all right so let's go ahead and
35:50just test it out right away I know I
35:51haven't really explained it but I just
35:53want to show you how this
35:55works okay so we have input put your age
35:58so I'll say two as my age and we can see
36:02that nothing happens okay um now I want
36:06to show you one more I hope you've
36:07caught this trick already from what I
36:09talked about in the last videos but
36:10we'll see now you notice here I'm going
36:12to put in
36:1416 and nothing's going to happen now the
36:17reason for that is because
36:1916 here is different than the 16 that we
36:23get from age if you remember whenever
36:26we're getting a number or anything from
36:27the console we actually get it as a
36:29string so this 16 that we're getting
36:32actually looks like this now in Python
36:34again we have different data types right
36:36so this is an integer data type well
36:38this is a string so when we're comparing
36:40integers and strings um they're
36:42different so we have to
36:43convert our age variable into an integer
36:46before we can compare it like that okay
36:49now we'll try
36:50again say if int age equals 16 so now we
36:54put 16 in and says hey you're 16 like
36:57that okay
36:58um perfect so that's pretty
37:00straightforward um now we can also do
37:03some other conditions so let's do a
37:05greater than 16 now okay so this is
37:08another conditional operator that we
37:09talked about before um and yeah we'll go
37:12ahead we'll try it now so we'll say
37:1415 now nothing happened because
37:16obviously 15 is not greater than
37:1916 now if we put in
37:2217 it works okay great
37:27um all right and also uh I just want to
37:29show you there's another operator that
37:31we didn't talk about last time a
37:32conditional operator and it's the
37:34greater than or equal to and the less
37:36than or equal to so to convert your um
37:40greater than sign into a greater than or
37:42equal to sign all you have to do is add
37:43an equal sign like this and now anything
37:47greater than or equal to 16 will work
37:50whereas before it had to be strictly
37:52greater than 16 and same thing with the
37:54less than sign like that okay uh I just
37:57forgot to talk about in the other video
37:58so I figured I'd put it in here all
38:01right um great so let's go into another
38:04example then so let's say um it's great
38:08it's telling us we're
38:0916 um or let's in this case we're going
38:12to
38:13be older than 16 right so hey you're
38:17older than 16 but what if we want to
38:20message when we are younger than 16 well
38:24the way to do this is using something
38:26called an lse statement so the Syntax
38:28for this uh it simply has to come after
38:30an if statement you can't just leave
38:32your own else statement like this that
38:34will not work it has to be after an if
38:37statement is placed and then all you
38:38have to do is put a call in and click
38:41enter and make sure the indentation is
38:43the same as the indentation for the
38:45first
38:46if okay so now we're just going to
38:50print you are younger than
38:5516 okay and we can go ahead we can try
38:57this to see if the else is going to work
39:00and we say well we're 15 and you are
39:03younger than 16
39:05great okay um so just to explain this a
39:09little more how this really works when
39:11we're reading through the code well
39:12python reads code line by line like this
39:16now when it reaches this if statement it
39:18checks this condition to see if it is
39:20true or false so since this condition um
39:24was false in the last example we say if
39:26false it says Okay so we're not going to
39:28run this line of code we're going to
39:30skip to the else statement and we're
39:32going to do this okay so if we had typed
39:35anything in that does not return true so
39:38makes this condition not return true
39:40then it would go to this else statement
39:43like this okay um yeah and then same
39:47thing say we type in a number 17 um and
39:50that is greater than 16 it's not going
39:52to do the L statement it's just going to
39:54do the if statement okay um I hope that
39:56makes sense
39:57now we're going to go into another layer
40:00okay so let's get into a new example
40:02here um let's talk
40:06about height for a roller coaster okay
40:08so for some roller coasters uh you have
40:10to be taller than a certain height and
40:12you actually can't be too tall right so
40:15again we'll get the input from the
40:17console so we'll just say height is
40:20equal to input and we're just going to
40:23do this in meters like 1 meter 2 meter 3
40:25meter just to make it really easy okay I
40:27know that's not a realistic height but
40:28just for the purpose of this example so
40:31we say height is equal to input um again
40:34we have to remember that when we're
40:36going to check the condition we have to
40:38make sure we put it in the int because
40:40as we get it from the console it's going
40:41to be a string okay so we'll say if
40:43height is less than 1
40:47M we're going to say you cannot ride so
40:50we're going to print to the
40:52screen you cannot ride okay now we only
40:57want to allow the user to ride if their
41:00height is in between 1 M but less than 2
41:05m okay so as you can see I've introduced
41:08a new word here called L if all right so
41:11this means if this condition up here is
41:15false then we're going to go to this one
41:17we're going to check if this condition
41:19is true or false if it's true we're
41:20going to run whatever's in here um
41:23otherwise we're going to keep going so
41:25you can have as many l ifs as you
41:28possibly want um in a decision statement
41:32like this okay um you can only have one
41:35else though because else is just the
41:36default so anything you type in um that
41:39doesn't equal any of these statements so
41:41like if isn't true Al if isn't true the
41:43other L if isn't true then it'll go to
41:45the lse statement okay so L if the
41:51height is so if it's less than one you
41:54can't ride and if it is greater greater
41:57than two you also can't ride so we're
42:01going to
42:03say you cannot ride I'm going to say and
42:08we'll say over 2 m and then we'll put
42:12here just so we can distinct them under
42:16one meter okay and then now we're going
42:19to add in the lse statement like this
42:21and we're going to say
42:23print you can write okay so I know I
42:27just did a lot there so we'll go through
42:29it quickly we pretty much have a
42:32condition up here we already talked
42:33about the if statement so if this
42:35condition is true we're going to print
42:37this and we're going to skip everything
42:39else we're not even going to bother
42:40reading it because we know that it's not
42:42going to be any of those conditions okay
42:44so now we do the L if here right so we
42:47say well if this condition is false
42:49we're going to go ahead and we're going
42:51to check this condition so we say oh
42:53well this one's true so then we're going
42:54to execute this line of code which is
42:56the print and we're not not going to do
42:57this else statement now say we go
43:00through these two and they're both false
43:02okay so this returned false and this
43:04return false then we're going to go to
43:06the else and we're automatically just
43:08going to print whatever is in the else
43:10statement like that okay so we'll go
43:12ahead uh we'll run the program and we'll
43:15try it out make sure I haven't made any
43:17errors here okay so the input um I
43:19didn't give a prompt so I'm just going
43:20to type in the height uh a number all
43:22right so we'll say
43:25one you can ride great okay um that is
43:29because again we have the strictly less
43:31than sign now if we wanted it to be you
43:34have to be over one foot we just put an
43:36equal sign here and then same thing here
43:39okay so we'll do that and now I'll show
43:40you again if I put in one you cannot
43:46ride under 1
43:47meter okay so less than or equal to one
43:50right okay um let's try again now we'll
43:53do it maybe a number greater than two so
43:56let's say put four it says you cannot
43:58ride because you are over 2 m and then
44:01again if we do like a decimal number
44:03maybe uh
44:051.1 Okay so we've run into an error
44:08there that's fine just because we can't
44:09convert a floating decimal point into an
44:11integer but yeah I hope you get the
44:13point from that example here um these
44:15are just the basic if L if and L
44:18statements I'll do one more just to show
44:19you here we could do another L if here
44:21saying for example um if in
44:25height is equal to remember we do two
44:27equal signs right not one then we're
44:30going to
44:31say
44:34print while you are
44:38tall okay so as you can see we can do
44:41infinite elf State L if statements like
44:43this and we can only have one lse
44:45statement and one if statement at the
44:47beginning
44:48okay uh so we'll run this one just to
44:50show you one more time how this works
44:52I'll put in five so
44:54five uh you cannot write over two 2 m
44:57okay so that's because what actually
44:59happened here is we went if we checked
45:01this condition now it turns out that um
45:05five is greater than two so we did this
45:07one and we didn't bother reading the
45:08rest now if I wanted to change this uh
45:11so that it would check the five first
45:14all I would have to do is put this LF
45:16statement I'll do it right
45:17now above the other L if
45:21statement like this and
45:25now if I type in 5 it'll work wow you
45:29are
45:29tall okay so uh that's it for the if L
45:34if and L statements um in the next
45:36tutorial we're going to go into
45:37something called chain conditionals and
45:39do some more advanced examples of this
45:41we're going to do some addition
45:42subtraction and some more things with
45:43other operators okay so I hope you
45:46enjoyed um please don't forget to like
45:49And subscribe the video and I'll see you
45:51again tomorrow in another video
Chained Conditionals & Nested Statements
45:59hey guys uh welcome back to another
46:01video this is the fifth video in my
46:03Python Programming series and today
46:05we're going to be talking about chained
46:07conditionals and nested if statements um
46:11so pretty much chained conditionals are
46:12just adding multiple conditions in one
46:16uh one line with using words like and
46:19and or okay uh we also have keyword not
46:23that we're going to talk about as well
46:24so if you haven't seen the last videos
46:27in my series go ahead and get watch
46:29those now because they are prerequisite
46:30for this video all right so let's get
46:33right ahead and start um so if you
46:35remember before we have something called
46:36conditions so for example if we have the
46:38variable X I'll set that to two and the
46:40variable Y which is equal to three we
46:42could have a condition like xal equal y
46:45now obviously we know from the last
46:47video that this is going to give us a
46:48value of
46:50false oops and I cannot type today sorry
46:54um and that is because two is not equal
46:57to three pretty straightforward I hope
46:59you have all understood that from the
47:00last video all right so what we want to
47:03do in this now is we're going to want to
47:04check multiple conditions um so we'll
47:07put a condition here so if x is equal to
47:10Y and then we can put a word in like
47:13this the and word um now we'll add
47:16another condition so if x +
47:19y is equal to 5 Okay so this first
47:25condition is going to give us a value of
47:26false so if x is equal to Y because they
47:28are not the same but now x + y does
47:32indeed equal 5 so that is going to give
47:34us a value of true so where I'm
47:36highlighting we have true and then where
47:38I'm highlighting now again we have false
47:41so because we have the keyword and this
47:43if statement is not going to run so I'll
47:46put a print here just to show you we're
47:49going to print out true or let's just
47:52say
47:53ran and you'll see when I run the
47:55program nothing is going to happen
47:56happen that is because when we use the
47:59and keyword it means both conditions on
48:01either side must be true so if we change
48:04this to something like if Y is equal to
48:083 now that is going to be
48:11true and we'll print out
48:14ran okay so that's pretty basic now
48:16let's do the or keyword so we'll put or
48:19right here and we'll change this
48:22condition back to if Y is equal to X and
48:24now take a guess on what you think is
48:26going to happen well pretty much with
48:29the or keyword it means either of the
48:32conditions have to be true so if Y is
48:35equal to X or x + y is equal to 5 then
48:39we are going to run this line now since
48:42X+ y does equal 5 we have a true here
48:45and again Y is not equal to X so we have
48:47a false and you'll see that the program
48:49is going to run that's because only one
48:51of the condition conditions has to be
48:54true for it to run
48:56okay so that is the and in the OR
48:58keyword they are pretty straightforward
49:00and pretty basic now I'd like to point
49:02out that you can actually add as many
49:05conditions as You' like so now I can
49:06have an or I can have an and um I could
49:09have another condition I could do as
49:11many as I'd possibly can imagine as many
49:13as I'd like to do um so now we'll put an
49:16lse statement in here and we'll change
49:19this just a little bit to use another
49:23keyword we'll just put a side face here
49:25to show that we didn't run it that we
49:28ran the else okay um so now there's
49:30something called the not word okay so
49:32we're going to put not right here around
49:34it and then I will show you what it does
49:36so pretty much not reverses anything
49:39that you have inside of the brackets of
49:42the knot so in here we have if Y is
49:44equal to X or x + y is equal to 5 so we
49:48know again that this gives us a value of
49:50true um so now since true is inside of
49:53the KN true becomes false now we have if
49:58false um so obviously that's going to
50:00bring us to the else statement um which
50:02is going to print the sad face so I'll
50:04show you that that does indeed work so
50:08we have the sad face right now if we
50:10change these so that both of these
50:11conditions are false you'll see that we
50:14have false false um which is going to
50:16give us an overall value of false um and
50:19then we're going to have the not which
50:20is going to change that into a true and
50:22will allow us to run it just like that
50:25ran um um so yeah so that's how the not
50:29the and and the ore work uh and now
50:31let's get into nested Loops or sorry not
50:34nested Loops Nest nested if statements
50:37so we'll do a basic if statement here if
50:39x is equal to
50:422 um if Y is equal to
50:463 um and then we'll add our
50:49El's like this and we'll put some print
50:53statements in here
50:58okay uh just give me a second while I
51:00type out the print statements and then
51:02we will go into what this is going to
51:06do x = 2 and Y = 3 um so you may have
51:12guessed already but pretty much we can
51:15actually add as many if statements
51:17embedded or nested as we'd like so I
51:19could add another if statement here and
51:21just constantly keep checking um now the
51:24reason I showed you chain conditionals
51:26is because if I didn't want to go
51:28through the hassle of adding all of
51:29these uh nested statements I could just
51:33do an and right here and check for
51:35another condition but sometimes we do
51:37want to do uh nested statements so we'll
51:39say here x = 2 y does not
51:44equal uh what is it
51:46three okay so uh I know I just did that
51:49pretty quickly but let's walk through it
51:50now so we have if x is equal to 2 then
51:54we're going to run whatever is in here
51:56because it is indented uh so now we come
51:59in here and we say well is y equal to 3
52:02let's say it is now we're going to print
52:03out this statement and we're going to be
52:05done with this loop with this uh bit of
52:08codes we're going to skip down to the
52:09end of the program where we have nothing
52:11else um then so let's say we do another
52:13example X is equal to 2 but y equal 4 so
52:17that means we're actually going to run
52:18the else which means now we've got print
52:21X is equal to 2 but y does not equal 3
52:25and then the last case is X is not equal
52:27to 2 so we don't even bother checking if
52:30Y is equal to 3 we just go down and we
52:32print X does not equal 2 so we'll change
52:35around the variables and we'll show you
52:36how this works just very
52:38quickly so we can see X is equal to 2
52:41and Y is equal to 3 that is because
52:43obviously up here two and three now if I
52:44want to change Y and we change it to
52:47four we get X is equal to two but Y is
52:51not equal to three so that means we ran
52:54this one in here now let's change it so
52:57that X is equal to 4 as
53:00well and we can see we get X does not
53:03equal
53:042 all right so that's uh the basics on
53:08nested statements and chained
53:10conditionals I hope you found this video
53:12helpful um stay tuned for more videos
53:15we're going to be getting into some more
53:16advanced topics and in the next video
53:18we're going to be covering
53:20Loops um yeah so if you like the video
53:23uh please subscribe and like and I'll
53:25see you in the next one
For Loops
53:33hey guys welcome back to another video
53:36this is the sixth video in my Python
53:38Programming series and today we're going
53:40to be talking about for Loops again if
53:42you missed the other videos in the
53:44series please go back and watch them
53:45first as it's going to be hard to
53:47understand if you haven't seen them
53:48already Okay so let's get right into it
53:51today we're going to be talking about
53:52four Loops um and a loop is a new thing
53:54now if you're a beginner in Python and
53:56I'll talk about how they work we're
53:57going to start off by just writing one
53:59out um getting right into it with the
54:01syntax so 4X in
54:05range and then we're going to put a
54:07number which is going to be our starting
54:08position so in this case I'm going to
54:10put zero um and then a stop point so 10
54:14okay and then we're simply going to
54:16print out X now I know we did this
54:18pretty quickly but let's talk about what
54:22this is and the syntax so a for Loop is
54:25going to run
54:26a certain amount of times so in this
54:29case we're going to start at zero and
54:32we're going to go to 10 this means the
54:34for Loop is actually going to run 10
54:37times so what's going to happen is we
54:40are going to have our variable X which
54:42is right here and every time the for
54:45Loop runs X is going to be increased by
54:48one now we can increase it by more if
54:51we'd like to by adding another comma
54:53here but by default it is set to one
54:56like this I've just put it in so that
54:57you can visualize it so really we have
55:00something start a
55:03stop Oops stop and an end start stop
55:07step sorry my bad and I'll just put
55:09commas here to show you how it works so
55:12you can see we have our zero that's our
55:13starting number our stop which is 10 and
55:16our step which is going to be one so I
55:20know you don't quite understand yet but
55:22you will after I show you a few examples
55:24so we have X is our variable which is
55:26going to be holding the number um of
55:29what iteration we are through now
55:31iteration uh just means how many times
55:34we've already looped through this Loop
55:37okay so let's uh do an example here all
55:40right so we have X and we've just
55:42started running this for Loop now right
55:44now X is zero we're going to print X
55:47we're going to print zero to the screen
55:49and then we're going to add one to X now
55:52this time we come through the loop we
55:54say well is X less than 10 yes it is so
55:57now we're going to print x x is one now
55:59we're adding um two okay so now X is 2
56:03is 2 less than 10 yes 2 is less than 10
56:06so let's print two okay and we keep
56:09going until eventually we print nine
56:12then X has one added to it so it becomes
56:1510 well 10 is not less than 10 so we're
56:18not going to print out 10 all right I
56:20know I just talked a lot there but let's
56:22see it in
56:24action all right so just like I said we
56:28print out the numbers 0 through 9 now
56:31this may seem weird um because I have
56:33the value 10 here so you'd say well why
56:35doesn't it print out 10 it's because of
56:37the way the loop works so like I said
56:40really what's Happening Here is this um
56:43plus equals 1 so pretty much this means
56:45we're just adding one to the variable X
56:48um you can also write it like this x =
56:51x + 1 like that all right so we print
56:55out X and then we add one to X and we
56:58have to be less than 10 for this Loop to
57:00run so on the ninth iteration we have
57:02well 9 is equal to 9 + 1 so X is equal
57:06to 9 + 1 uh we get the value 10 now
57:08again 10 is not less than 10 so we are
57:11not going to print out 10 um yeah so
57:15that's pretty much how a for Loop Works
57:19um it's pretty basic this is the syntax
57:21we have four and then a variable name so
57:24this doesn't have to be X we could use
57:25anything we'd like to use could use
57:27hello I could use um any other word it
57:31could be as long as I want as short as I
57:32want doesn't matter um then we have the
57:35word in which is highlighted in Orange
57:38and then range now the range takes a
57:41different amount of arguments pretty
57:43much an argument is whatever is inside
57:44this brackets so like at the beginning
57:46how I showed you we just did zero and 10
57:48this means we have a start and we have a
57:51stop so we start at zero and we go to 10
57:56now I can also I can actually put one
57:58argument in here like this 10 and that
58:01just means that we just have a stop so
58:03by default python will say well we're
58:05just going to start at zero and we're
58:07going to step by one now I haven't
58:09showed you the step yet so we'll do that
58:12um so again we're going to start at zero
58:13we're going to stop at 10 and we're
58:15going to step This Time by two now what
58:17step is is how much we add to X every
58:20time so before I showed you we had x = x
58:23+ 1 um in this case since the step is is
58:26two it's going to be by two like that
58:28okay um so let's go ahead let's run
58:32this we have 0 2 4 6 8 and you notice
58:37how we again do not print out 10 because
58:3910 is not less than 10 okay again uh
58:43we'll do another example with the step
58:44let's put five in this time now you can
58:47guess what's going to
58:49happen 05 it actually only prints out
58:52two things because we start at zero so
58:55we print 0 and then we add five we get
58:58five now 5 + 5 again is not less than 10
59:00so we are not going to print out 10 okay
59:03so that is the tutorial on the for loop
59:06we're going to do another type of for
59:08Loop where we're going to use something
59:09called a list a little bit later on but
59:11right now we're just using this one this
59:13is the basic one I encourage you to play
59:15around with it yourself um and yeah so
59:19if you like the video please go ahead
59:21and like it and subscribe and I will see
59:23you again for another video thanks
While Loops
59:32goodbye hey guys welcome back to the
59:34seventh video in my Python Programming
59:37Series today we're going to be talking
59:39about wow Loops so in the last video we
59:41talked about for loops and before that
59:42we talked a lot about conditions um so
59:45it's going to be important to understand
59:46those things before we can move on today
59:48to this video so if you haven't seen
59:49those videos go back in my channel and
59:52check those out first okay so let's get
59:55right into it today we're going to be
59:56talking about while Loops so let's first
59:58of all just put an example down and get
1:00:00the syntax okay so we have while
1:00:04condition then we are going to do what
1:00:08is ever is inside the loop so it seems
1:00:10simple
1:00:11enough um but they can get fairly
1:00:14complex um so well condition is equal to
1:00:19uh
1:00:19true so for example this condition here
1:00:22this could be a variable um well that is
1:00:25equal to true then we're going to do
1:00:27whatever is inside of this Loop so again
1:00:30we keep talking about conditions um
1:00:32they're very important in Python we
1:00:35really need to understand them before we
1:00:36can move into more complex topics okay
1:00:40so uh well condition equal to true we're
1:00:42going to do whatever is in this Loop so
1:00:44how does this really
1:00:45work well uh pretty much what happens is
1:00:50we have the W Loop here we have the
1:00:51condition so this is true this means
1:00:53we're going to do this now what happens
1:00:55is we actually come back up to the top
1:00:56after we've done this and we say well is
1:00:58this condition still true um well yes it
1:01:00is so that means now we're going to do
1:01:02this again and we just continuously do
1:01:05this until eventually the condition is
1:01:07equal to false or we have a keyword
1:01:10appear called
1:01:12Break like this now I'll do an example
1:01:15so we can understand this but this is
1:01:16just the basic syntax we have while we
1:01:18have a condition and then we have a
1:01:20colon followed by an indented block
1:01:22which will be whatever is going to run
1:01:24okay so let's start off by using a
1:01:26variable we're going to just call it
1:01:27loop we're going to set it equal to true
1:01:30now we're going to make our while loop
1:01:32we're going to say while loop um Now by
1:01:36just putting a loop here it defaults to
1:01:38say while loop is equal to false um
1:01:41putting Loop there is the exact same
1:01:42thing as typing this um it's just
1:01:44shorter to put Loop instead of doing the
1:01:46equals equals true okay we're going to
1:01:50get some input from the console so we're
1:01:52going to say name is equal to input and
1:01:56then I'm just going to put in here uh
1:01:57insert
1:01:59something
1:02:01okay and then we're going to put an if
1:02:03statement which we talked about in
1:02:04another video so if you haven't seen
1:02:05that go back and check that out we're
1:02:07going to say if name is equal to
1:02:11stop um then we are going to break out
1:02:15of the loop by typing break or we could
1:02:18also do this we could set the condition
1:02:21equal to false so we set the variable up
1:02:23here equal to false now I know I just
1:02:24typed a lot there so let's um talk about
1:02:27what really is going to happen so we
1:02:29have our condition we say while loop is
1:02:31equal to True um right now we set it
1:02:32equal to True up at the top so it's
1:02:34going to automatically run at least once
1:02:37now we get input from the console um and
1:02:40store it in the variable name now we
1:02:42have a basic if statement here that
1:02:44pretty much says if name is equal to
1:02:46stop well then we're going to stop the
1:02:48loop um and we're going to break out of
1:02:50it otherwise we're going to keep asking
1:02:53the user for to insert something until
1:02:56eventually they type stop so let's see
1:02:58how this all
1:03:00works okay so insert something I say one
1:03:04123 it didn't like that once me insert
1:03:06something again I say we okay what about
1:03:09hello keeps going okay now I'm going to
1:03:11type
1:03:13stop and you see that the program stops
1:03:16running okay so again I'll show you that
1:03:19we can do this by just setting the
1:03:21variable equal to
1:03:23false same thing again if I type hi
1:03:26keeps going I type stop it's going to
1:03:29stop and then maybe if we get rid of the
1:03:32variable here we can also just type in
1:03:34the word
1:03:35break like that and same thing again if
1:03:38I type in some random things and then I
1:03:40type stop it stops okay so pretty much
1:03:44the way that the break keyword works is
1:03:47by simply saying okay are we inside any
1:03:50Loops right now um it checks whatever
1:03:52Loop you're inside so right now we're
1:03:54inside the while loop and it says okay
1:03:56well I've this line has now run so what
1:03:58we're going to do is we're going to get
1:03:59out of the loop so it pretty much will
1:04:01now start reading from the next blank
1:04:04line in the file um meaning that it's
1:04:07going to get out of this Loop um and
1:04:10just continue going down the
1:04:12program okay and that's pretty much it
1:04:15for Wow Loops we can talk a little bit
1:04:17more about why we would use a w Loop
1:04:19instead of using a for Loop um the
1:04:21reason we might use a w Loop is for
1:04:23something like this so for example if
1:04:24you want to ask someone to insert a
1:04:27password so we have password like this
1:04:31um and we'll just change this variable
1:04:32with password so pass for short um and
1:04:36now maybe we wanted to check we wanted
1:04:38to say um well oops well let's just do
1:04:42password because pass is a keyword in
1:04:43Python I forgot about that um password
1:04:47and we want to make sure that the
1:04:48password has for example at least one
1:04:51letter in it well we're going to keep
1:04:53asking the user to re-input their
1:04:55password until it meets these certain
1:04:58criteria so for example it might have to
1:05:00have one Capital it might have to have
1:05:03one letter in it uh it may have to have
1:05:05a few numbers something like that right
1:05:07so we would keep asking the user for a
1:05:09password um until they gave it to us
1:05:11correctly now the reason we wouldn't use
1:05:13a for Loop for this is because a for
1:05:15Loop um usually we know how long we want
1:05:18to run the for Loop for um that's why we
1:05:21put a number in the range so for example
1:05:22we put 10 or we put 12 while as a w Loop
1:05:25we don't know how long we're going to
1:05:27run it for so for example someone could
1:05:29get the password first try like they
1:05:31could put it incorrectly um with the
1:05:33right criteria or it could take them 30
1:05:34tries right we don't know how long it's
1:05:36going to take them okay so that's it for
1:05:40today's video um if you like the video
1:05:42please like it and subscribe and I will
1:05:45see you again tomor tomorrow with a uh
1:05:48new
List's and Tuples
1:05:54video hey guys guys welcome back to the
1:05:56eth video in my Python Programming
1:05:59Series today we're going to be talking
1:06:00about lists now lists can be fairly
1:06:03complex uh but they are a very important
1:06:05part of python and they are very
1:06:08important to understand so a list is
1:06:10another data type so before we talked
1:06:13about strings integers booleans and
1:06:15floating decimal points so floats um and
1:06:18today we're going to talk about list so
1:06:19lists again are their own data type so
1:06:21let's just start off by doing an example
1:06:23right now so pretty much uh a list which
1:06:26is denoted by the square brackets here
1:06:29uh is a collection of different data
1:06:31types it could be the same data types or
1:06:33it can be different data types so for an
1:06:35example I'm going to put a apple in here
1:06:38in my list I'm going to put a comma and
1:06:39I'm going to add another item so maybe
1:06:41we'll put a pair and then maybe we'll
1:06:43put a number as well okay so we have the
1:06:46list fruits and in the list we have
1:06:49three items um now everything here
1:06:52separated by a comma is known as an item
1:06:54so for an example our apple is an item
1:06:58our pair is an item and the integer
1:07:00three is an item now you can see that
1:07:02they're all different data types or
1:07:04sorry there is different data types we
1:07:06have two strings and we have one integer
1:07:08value um now this is fine you can store
1:07:11um different data types in one
1:07:14list um okay so now let's just print the
1:07:17list to the console so we can see what
1:07:20it looks
1:07:21like print it out and you'll see we
1:07:24simply get what exactly what we we've
1:07:25typed uh into the idle so we get apple
1:07:29pair and 3 now if we want to access an
1:07:33individual item in the list we need to
1:07:35do something um a little different so
1:07:38right now we just printed out the whole
1:07:40list but say we want to just print out
1:07:42pair um now what we're going to do is
1:07:45we're going to use the square brackets
1:07:47so we're going to do the list name so
1:07:48fruits and then we're going to put
1:07:50square brackets right beside it no
1:07:52spaces and then inside of the square
1:07:54brackets we're going to type in what's
1:07:55known as the indic of that item um so
1:07:58the way indices work in Python is when
1:08:01you create a list everything is
1:08:02automatically assigned in indic um so in
1:08:05this list we have indices 0 through two
1:08:08now it may seem weird that we start at
1:08:10zero instead of one but that's just the
1:08:11way that computers count um and the way
1:08:13that they work so we have to understand
1:08:15that apple is going to be assigned the
1:08:17integer value of zero so the indic zero
1:08:20meaning I would put zero in here if I
1:08:22wanted to access Apple because that's
1:08:24the first element
1:08:26in our list and then we have pair so
1:08:29that's the second element or item um now
1:08:32that's actually going to be represented
1:08:33by one here so fruits one will give us
1:08:36the value of pair and then fruits two
1:08:38will give us the value of three um now
1:08:42this is uh sometimes difficult for some
1:08:44people to understand but just really try
1:08:45to remember that the first item always
1:08:48is zero so if you would like to access
1:08:50the second item in your list you're
1:08:53going to type in one um into to the
1:08:56indic thing here okay so I'm just going
1:08:57to run this and show you how this
1:08:59works and you see we get pair okay
1:09:02that's because um we have the first
1:09:04indic which represents the second item
1:09:08which is
1:09:09pair okay um now there's some other
1:09:12things we can do with list as well so if
1:09:14we want to add something to our list
1:09:17there's a few ways that we can do it um
1:09:20the first way uh which is the only one
1:09:22I'm going to show you right now is
1:09:24probably the easiest and simply what
1:09:25we're going to type is fruits followed
1:09:28by the dot uh so a DOT a period and then
1:09:31we're going to type a pend okay now this
1:09:34means to add to the end of the list so
1:09:38inside of a pen I'm going to put the
1:09:40element or the item that I'd like to add
1:09:42so say we want to add uh another fruit
1:09:45let's think of another fruit that we
1:09:46could add maybe a
1:09:48strawberry okay we want to add
1:09:50strawberry into the list I'm just going
1:09:51to get rid of three so it makes more
1:09:53sense okay so right now in our list up
1:09:55here we have fruits which is a variable
1:09:57name um containing the list that has
1:10:00Apple and pear now we want to add
1:10:02strawberry to the list so let's start
1:10:04off by printing the list before we add
1:10:06it so we'll print off fruits and then
1:10:09we're going to print fruits one more
1:10:11time after we add this item to the
1:10:16list and you see we have the first list
1:10:19which is what we defined originally and
1:10:21then when we added or appended
1:10:23strawberry to the end of it it added it
1:10:25in just like this so that is a way that
1:10:28we are able to add things into a list um
1:10:31by using dot append now there is another
1:10:35way um it's known as the slice operator
1:10:37using something called insert um but
1:10:40we're not going to do that right now
1:10:41we're going to talk about the slice
1:10:42operator in a later tutorial so stay
1:10:44tuned for that okay so fruits. append
1:10:48we've added strawberry um now if we
1:10:50wanted to add another item we could do
1:10:51the same thing and we could do append
1:10:53again and we could put in another fruit
1:10:56maybe we want to put in a blueberry okay
1:10:59just like that and again we'll see that
1:11:01it does the same thing we have apple
1:11:04pear and then apple pear strawberry
1:11:06blueberry okay um all right so that's it
1:11:10for adding to the list um now what about
1:11:14changing something in our list uh so
1:11:17let's just add right here at the
1:11:19beginning we'll put in Strawberry so we
1:11:22have three items and now let's say we
1:11:24want to change the middle item in our
1:11:26list to a different fruit say we don't
1:11:28like pairs we want to remove it we want
1:11:30to change that so what we're going to do
1:11:31is we're simply going to type the um the
1:11:34name again so fruits followed by the
1:11:36square brackets and then the indic of
1:11:38the item you want to change um so again
1:11:40remember if we want to change pair we're
1:11:42going to use the Indy one um not two
1:11:45because this is the way that it works
1:11:46we're going to use one and then we're
1:11:48going to change it to whatever we'd like
1:11:49to change it to so say I want to change
1:11:51it to Blueberry I can do that like so
1:11:55and then again we can print out fruits
1:11:59and we'll see if it
1:12:01works okay so we get apple blueberry and
1:12:04strawberry that's because we''ve changed
1:12:07uh the item at indic one into blueberry
1:12:11and we've replaced that we replaced Paar
1:12:14with blueberry pretty
1:12:16much okay um so that's how that works
1:12:19for list for changing items again um we
1:12:23you lists are very useful for storing
1:12:25large amounts of information so this is
1:12:28a very small list but in many programs
1:12:30we use something called
1:12:32multi-dimensional list uh which I'm
1:12:34going to talk about again in another
1:12:36tutorial um and yeah so one more thing
1:12:39before we end it here uh there's another
1:12:41data type called a tuple so a tupal um
1:12:45it's kind of hard word to pronounce but
1:12:46it's used for coordinates and it's also
1:12:50used um for things like colors and
1:12:53rectangles um it can also be used just
1:12:55to hold information as well but it's a
1:12:58different type of data type um than a
1:12:59list so we're going to talk about it
1:13:01quickly uh okay so a tuple pretty much
1:13:03looks like this so if we set a position
1:13:07and we set it equal to something like
1:13:10this not in square brackets in the
1:13:12regular round brackets and then we
1:13:14simply put um the items in like this now
1:13:17we see we get um it looks very similar
1:13:20to a list the way that it works again we
1:13:22can use different data types so I could
1:13:23put something like hello in here as well
1:13:25well um but it does function a little
1:13:27differently and again things like colors
1:13:29we would also store in a tuple so we
1:13:31would use like 255 255 255 that's simply
1:13:34white in uh red green blue colors and
1:13:37again if we print out the
1:13:39type of the
1:13:42color you can see that we get a class
1:13:46tupal okay so I'm not going to go too in
1:13:48depth with the tuples here I just wanted
1:13:50you to understand what they are because
1:13:52we may be using them in some later
1:13:53tutorials okay so I hope you learned how
1:13:55to use lists and a little bit about tups
1:13:58today they're going to be important in
1:13:59the next tutorial um so yeah I hope you
1:14:03enjoyed if you did please like And
1:14:05subscribe and I will see you in the next
Iteration by Item
1:14:12video hey guys welcome back uh today
1:14:15we're doing the ninth video in my Python
1:14:17Programming series and today we're going
1:14:19to be talking more uh more about for
1:14:22Loops so a more advanced type of for
1:14:24Loop in terms terms of iterating through
1:14:26a list so you may remember a few videos
1:14:28ago we talked about for loops and I
1:14:30mentioned that there are two main type
1:14:32of for Loops that we can use um and the
1:14:34one that we talked about there was
1:14:36looked like this so we had for X in
1:14:39range and then we had a range like this
1:14:43and we simply printed out the numbers
1:14:47like this so we see we got 1 to 10 just
1:14:49like that okay uh so that was the for
1:14:52Loop we talked about there um but the
1:14:53one we're going to be doing today is is
1:14:55a little bit different so I'll leave
1:14:56this here for an example uh so the first
1:14:58thing we need to do is we need to make a
1:15:00list because we're actually going to be
1:15:01iterating through every item or every
1:15:04element in the list so again I'm just
1:15:06going to use the same example I used in
1:15:08the other video fruits and I'm going to
1:15:09make it equal to we're going to have
1:15:13apples
1:15:15pears and strawberries strawberries just
1:15:19like that okay so we have our fruits
1:15:21here and now um we're going to get right
1:15:24into the syntax for this new for Loop so
1:15:26we're going to do four and then any
1:15:29variable name we'd like so in this case
1:15:30I'm going to use fruit and then we're
1:15:32going to use the keyword in once again
1:15:35and then instead of typing range this
1:15:37time we're going to type the name of the
1:15:39list we want to iterate through so in
1:15:40this case it's going to be called fruits
1:15:43just like that and now um we can print
1:15:48out the fruit sorry the fruit just like
1:15:52that so pretty much what is going to
1:15:55happen is we have a list with three
1:15:57items in it we have apples pears and
1:16:01strawberries now we want to print every
1:16:04fruit in that list so we do that by
1:16:07saying for fruit in fruits meaning for
1:16:10every item in fruits so one two and
1:16:14three items we simply want to print out
1:16:17that fruit so this Loop is going to run
1:16:18three times and we're just going to
1:16:20print out every fruit inside of the list
1:16:24just like this
1:16:26and there you go we have apples pears
1:16:28and strawberries just like
1:16:30that um so yeah that's pretty much how
1:16:33you can iterate through a list and same
1:16:36thing if I want to add more elements to
1:16:37the list so 8 um 90 just like that again
1:16:42it'll just simply print out all of the
1:16:44items in the
1:16:46list now uh why might we do this you say
1:16:49well there's a few reasons we may want
1:16:52to iterate by item rather than by indic
1:16:56so the other one uh the in range is
1:16:58known as iterating by indic or by index
1:17:01well this one is known as iterating by
1:17:03item so there's a few reasons we might
1:17:06want to do this but the first one is if
1:17:07we only want to print something out uh
1:17:09if it's a certain value so we just want
1:17:11to check through all of the items in the
1:17:13list and see if there are certain value
1:17:15and if they are then we're going to
1:17:16print it out or we're going to do
1:17:17something specific to that item uh so in
1:17:19this case I'm going to say if fruit is
1:17:22equal to for example
1:17:25pairs then I want to print
1:17:29out pairs so we'll just print out the
1:17:31fruit but if it's not equal to pairs so
1:17:34anything else then I will print
1:17:38out not pairs like that so this is an
1:17:42example of how we can look through every
1:17:45item in the list um and simply check if
1:17:48it is a certain value and then if it is
1:17:51we're going to print it to the screen so
1:17:52we'll try this and we see we have not
1:17:54pairs pairs and Then followed by a few
1:17:56more not pairs um so that's a good way
1:17:59to do it by item um otherwise we can
1:18:02still do this using Indy uh it's just a
1:18:04little more complicated or not as
1:18:06efficient so we could do
1:18:084X in range and then in this case we'll
1:18:13do zero and six because we have to do it
1:18:17six times because there's six items in
1:18:19the list and then we'll put a colon and
1:18:22then we'll say the same thing so except
1:18:25this time we're going to say if fruits
1:18:28followed by the square brackets and then
1:18:30X because this is going to be our indic
1:18:32equals equals pairs then we want to
1:18:37print fruit or fruit X
1:18:42oops otherwise we'll
1:18:45print not a pair just like we did in the
1:18:50other loop not a pair okay so we'll run
1:18:53that and we see we get it twice just
1:18:55because I had two for Loops there so it
1:18:57does work uh the exact same way except
1:19:00you can see that this one takes longer
1:19:02to write because we actually have to put
1:19:05the list name followed by the square
1:19:06brackets whereas in this Loop all we
1:19:09have to do is simply type in Fruit um
1:19:12because it's automatically going through
1:19:14the list um another reason too we may
1:19:16want to do that is because in this case
1:19:18we had to actually see how many items
1:19:20were in the list by counting um and then
1:19:24we had to put that number in whereas
1:19:25here it'll just stop at the end of the
1:19:27list automatically um now we could do
1:19:30this so Len this is a new word so I'm
1:19:33just showing this now uh this means
1:19:36length and then inside we can put
1:19:38something like a string or a list so in
1:19:41this case I'm going to put the length of
1:19:43fruits and that would give us a value of
1:19:46six and you'll see if I just get rid of
1:19:51this that it'll work the same way again
1:19:54um um by using the length of
1:19:57fruits okay so that is how to iterate
1:20:00through a list by item uh I hope you
1:20:04enjoyed if you did please leave a like
1:20:06And subscribe and stay tuned for more
1:20:09videos in this
String Methods
1:20:16series hey guys welcome back today we're
1:20:19going to be doing the 10th video in my
1:20:21Python Programming series and today
1:20:23we're going to be talking about string
1:20:25Methods um so a method is simply
1:20:27something you can call on a string by
1:20:29using the delimiter of a period followed
1:20:32by whatever the method is and then
1:20:34brackets like this now sometimes we have
1:20:37to put some things inside of the
1:20:39brackets um but most of the time we can
1:20:42just leave them with empty brackets okay
1:20:45so let's get right into an example we're
1:20:46going to start by talking about the dot.
1:20:49strip method um now what this one does
1:20:52is it pretty much removes all of the
1:20:54leading and trailing white spaces from a
1:20:57word or from a string sorry so we'll
1:21:01start by just making a variable and
1:21:03we'll make it equal to the input from
1:21:05the console and we'll just say
1:21:08input something like
1:21:11that and then we're just going to print
1:21:13out our input so oops print out text do
1:21:19strip okay so again what strip does is
1:21:22just going to remove all the white
1:21:23spaces before and after the word and
1:21:25it's best to show you with an example um
1:21:27so you can see if we type a few spaces
1:21:30so 1 2 3 4 and then I type hello and
1:21:33then a few more spaces and I click enter
1:21:35it simply gives us hello that's because
1:21:37it had removed all of the spaces from
1:21:40the string while as if I don't have the
1:21:43dot strip here so if I get rid of that
1:21:46and we run it and I do some spaces again
1:21:48and I type hellow and some more spaces
1:21:50it's going to give us those spaces as a
1:21:52part of the string and most the time so
1:21:55when we have a string we want to remove
1:21:57these spaces so that's why we use do
1:22:00strip did I spell that right strip yeah
1:22:03just like that okay um all right so that
1:22:06one's pretty straightforward just
1:22:07removes these spaces now you may want to
1:22:09do this if you're checking um if a
1:22:11string is equal to a certain thing and
1:22:12you're getting a user input and they may
1:22:15have accidentally typed the space before
1:22:16or after the word right okay so now
1:22:19we'll move on to the Len um so this one
1:22:22is actually not a method because you
1:22:24don't call it using a DOT um using a
1:22:26period followed by the word it's
1:22:28actually known as a function which I'll
1:22:30get into later so this just means that
1:22:31it takes something inside of the
1:22:33brackets um so this stands for length
1:22:36Len and pretty much what it does is
1:22:38Returns the length of a string or of a
1:22:41list so right now we're going to use a
1:22:43string and we're going to put text in
1:22:45here and I'll show you again what it
1:22:47does um 1 2 3 4 5 remember although this
1:22:51is a number since I'm typing it in the
1:22:53console it actually gives me a string
1:22:55value so this will work and it says that
1:22:57the length of this is five again uh that
1:23:00one's pretty useful I used it in the
1:23:02last video um to determine how many
1:23:04times we had to iterate through a list
1:23:06because we got the length of the
1:23:08list all right now for lower so pretty
1:23:11much what lower does um is it turns
1:23:15everything into lower cases in our word
1:23:18um so again it's best to just show using
1:23:20an example if I type something like this
1:23:24uh uh with a few lower cases a few
1:23:26capitals and I click enter you see we
1:23:28get everything in lowercase
1:23:32letters all right and now for Dot Upper
1:23:34you can probably guess what this one's
1:23:36going to do it's the exact same thing
1:23:37except in this case everything is going
1:23:40to uppercase
1:23:42so like this and you see we get all
1:23:45uppercase
1:23:47letters all right um and now for the
1:23:51last string method that I'm going to
1:23:52talk about although there is T tons of
1:23:54different string Methods um there's
1:23:56probably over 40 or 50 I'm going to
1:23:58leave the link down below to all of the
1:24:00string Methods on the python website so
1:24:02you can go check them out if you'd like
1:24:04to see some more um so this one is
1:24:06called do spit now this one I'd say is
1:24:08probably one of the most useful methods
1:24:10that I um that I use all the time and
1:24:13what it does is it actually creates a
1:24:15list out of the string that you give it
1:24:19um so inside of these brackets you're
1:24:21going to put what's called a delimiter
1:24:23and what a delimiter is is pretty much
1:24:26what you want to split by so for example
1:24:29I'm going to put a period in here just
1:24:30so we can get an example so we have text
1:24:34dosit and inside of the brackets for dos
1:24:37split we have a period so it's best to
1:24:39just show an example oh invalid syntax I
1:24:42think I have one too many brackets there
1:24:44yes I do okay so we'll run it so I input
1:24:47something I'm going to type
1:24:49hello.
1:24:51tim. by. High just a few random words
1:24:55and you can see they're all separated by
1:24:57a period or by a DOT now when I click
1:25:00enter you see we actually get a list
1:25:04with all of the words and you notice
1:25:06that we don't actually have a period in
1:25:09this list that's because the way that do
1:25:12split works is it gets rid of the
1:25:14delimiter so since the delimiter was a
1:25:16period it's not going to include the
1:25:17period in this list now this may be kind
1:25:20of confusing um we'll do a few more
1:25:22examples so hope you hopefully you can
1:25:24understand
1:25:25so usually what you do um is you would
1:25:28you you would leave these uh brackets
1:25:30blank so you wouldn't put anything in
1:25:31them and by default Python's going to
1:25:33say okay well I guess you want to
1:25:35eliminate by a space um so the way that
1:25:38we can do this again um what'll happen
1:25:41is if I type a sentence like my name is
1:25:44Tim um and I click enter you see it
1:25:47separates by spaces and we have no
1:25:50spaces in our list because it removes
1:25:53those spaces now you could also
1:25:55diiminate by anything you like so I
1:25:57could do an a for example um now if
1:26:01click F5 and we just type a bunch of
1:26:03letters and I put an A and then another
1:26:05a you see we get um the letters and
1:26:08again there's a space at the end because
1:26:10I typed a space right um so yeah okay so
1:26:14that's how dot split works now splits
1:26:17very useful um if you want to do certain
1:26:20things with a list we're going to talk a
1:26:21bit more about it later on um but those
1:26:24are some basic string Methods that we
1:26:26talked about today again there are tons
1:26:28more on the python website which I will
1:26:30link down below um if you like the video
1:26:33please like And subscribe and I will see
1:26:35you again
Slice Operator
1:26:42later hey guys welcome back to the 11th
1:26:45video in my Python Programming Series in
1:26:47today's video we're going to be talking
1:26:49about the sliced operator um which is a
1:26:52cool thing that we can use on lists and
1:26:54and strings in Python so if you remember
1:26:56in the previous video if we wanted to
1:26:58access an element in our list we did
1:27:01fruits so the name of our list here
1:27:02which I've already got defined as apples
1:27:04pear and strawberry um and then we would
1:27:06do the items so for example fruits one
1:27:09and we can print that to the screen um
1:27:11just to remind us what this
1:27:14did and we got pair just like that
1:27:17because that is the second item but to
1:27:19reference that we have to use one
1:27:21because in computers we start at zero
1:27:22right um okay yeah and I just wanted to
1:27:26mention I didn't don't think I did this
1:27:27in the list video that you can actually
1:27:29do this on strings as well so say I
1:27:31wanted to access the E there I would
1:27:33just do text one and we get the E
1:27:36because that is the second uh item in
1:27:38this string or character maybe I wanted
1:27:41to do the 11th
1:27:43one then that would give us another E
1:27:45because 11 is right here at the end of
1:27:47like so yeah now what does the slice
1:27:50operator look like so pretty much the
1:27:52slice operator is these two square
1:27:54brackets but inside of them you're going
1:27:56to put two colons just like this now
1:28:00these colons in between them we type
1:28:02some words uh or we're going to put some
1:28:04numbers but in this case I'm just going
1:28:05to show you with words so we have start
1:28:08stop and step um that's what goes where
1:28:12these colons are now if I remove this
1:28:14colon that means I just have a start and
1:28:16a stop and then if I remove this colon I
1:28:18just have a start and that's how we
1:28:20reference what element we want to uh we
1:28:23want to print out right right if we had
1:28:24just put a number there so we'll add the
1:28:26back in and this now we're going to
1:28:27use numbers so the start stop and step
1:28:29works exactly the same way as the range
1:28:32function so you may remember when I did
1:28:33a for Loop we did 4X in range and then
1:28:37inside of the range we did some numbers
1:28:39like this so we would start at one stop
1:28:41at two and step by three like that right
1:28:44so it works the exact same way except in
1:28:46this case we're going to be using um it
1:28:48on lists and on strings right without
1:28:51these numbers so the range like this
1:28:54okay so let's just do an example um so
1:28:57let's start at zero and let's end at
1:29:00this o so the O is 1 2 3 4 five um but
1:29:05if I want to actually include the O I
1:29:07have to put five because remember five
1:29:09would be the space here um and if we
1:29:12stop on let's say four here you'll see
1:29:14what's going to happen um it's not going
1:29:16to print out the O uh because when we
1:29:19stop we do not include that number
1:29:21remember from the range function so if
1:29:22we want to have the O we'll stop at five
1:29:25just like that hello um okay yeah uh now
1:29:29if we wanted to start just from the
1:29:31beginning say we didn't know um all we
1:29:34have to do is just put nothing there and
1:29:35it's going to default start from the
1:29:37beginning so it'll give us the same
1:29:38thing and then if we wanted to start
1:29:41from somewhere so say we want to start
1:29:42at two and we just want to go all the
1:29:44way to the end now we say we don't know
1:29:47how long our string is we don't want to
1:29:48count all of these things and say oh
1:29:50well let's stop at 17 or whatever number
1:29:53it may be by just leaving this blank
1:29:56it'll actually default to stop at the
1:29:58end of the string and you'll see that so
1:30:00we get l l o I like python right it
1:30:02stopped at the end of the string
1:30:05automatically okay so now let's talk
1:30:07about the uh the step feature so if we
1:30:10want to start at the beginning and we
1:30:12want to stop at the end we'll just put
1:30:14nothing in between these two colons
1:30:16because it's going to default from the
1:30:18beginning and default to the end but now
1:30:20we want to skip every other letter that
1:30:22means we're going to put a two here for
1:30:24the step so that way we're going to get
1:30:26h l o then we're going to skip the space
1:30:30here we're going to get I skip the space
1:30:31get L and so on so I'll show
1:30:34you and you see we get that um a bunch
1:30:37of gibberish like this maybe you want to
1:30:39go every third letter that means we
1:30:41would step by three like
1:30:43this and there we go we get a few less
1:30:46letters and same thing we could do four
1:30:47five um and continually going now say we
1:30:50want to start at the third letter or the
1:30:54fourth because we're typing three here
1:30:55so L um we want to stop at the end and
1:30:57we want to step by
1:30:59three then you can see what's going to
1:31:01happen there so we get l i i TN right so
1:31:05a bunch of gibberish okay now this works
1:31:07again same thing for fruits uh it just I
1:31:09don't have as many items in the fruits
1:31:11list so that's why I was showing you
1:31:12with text but if I put the colon in here
1:31:15and we put another colon uh let's say we
1:31:17start at let's start at one so that
1:31:19means we're going to start at pair and
1:31:21let's just go to the end and we'll print
1:31:23that out and again we get Paar and
1:31:25strawberry so we didn't include the
1:31:27apples because we started at pear right
1:31:31so yeah that's pretty much how the slice
1:31:33operator works now there is one other
1:31:34cool thing we can do with the slice
1:31:36operator um and it's called the insert
1:31:39function or the insert kind of uh thing
1:31:41with the slice operator so if you
1:31:42remember uh we H when we wanted to add
1:31:44something to a list we use dot append
1:31:48right so I showed you that in my list
1:31:50video we did fruits. append and then
1:31:53maybe we want to add blueberries like
1:31:55this I know that's spelled incorrectly
1:31:57but there we go we'll fix that quickly
1:32:00blueberries okay and then we can print
1:32:03out fruits once again just to see what
1:32:05it looks like so if you remember this
1:32:07just adds it to the end of the list but
1:32:09say we don't want to add blueberries to
1:32:11the end of the list we want to add it in
1:32:13between Pairs we want to add it in
1:32:14between Apples and pear um we want to
1:32:17add it at the beginning we want to add
1:32:18it somewhere else right how would we do
1:32:20that well we can use the slice operator
1:32:23to do that so instead of using the dot
1:32:25of pend we'll use a slice operator so
1:32:27we'll go like this and this time we're
1:32:29going to just put one callon and then
1:32:31type the indic see where we want to
1:32:33insert it so if we want to insert it at
1:32:35the beginning we're just going to type
1:32:36zero and then zero like this we do need
1:32:38to do them on both sides and then an
1:32:41equal sign and we'll set it equal to
1:32:42whatever you want in this case we'll
1:32:43just put B for
1:32:44Simplicity and you'll see we get B at
1:32:47the beginning of our list now say we
1:32:49want it in between Apples and pear well
1:32:52in that case we would put one and we put
1:32:54one again like
1:32:56this and we get apples comma B comma
1:32:59pear comma strawberries just like that
1:33:01now if we want to add it to the end um
1:33:04we would have to find out what number
1:33:05that is so we do 3 three I believe that
1:33:07should work we'll
1:33:10see yeah so that adds it to the end for
1:33:13us um and then if we want to insert it
1:33:16in between pear and strawberry we would
1:33:17do 2 two like that and there we go we
1:33:21get it in between there so that's how
1:33:22the insert works for the slice operator
1:33:25um I hope you learned how to use a slice
1:33:27operator today we will be using it a
1:33:29little later in some other videos and
1:33:31doing some more complex things with it
1:33:33um if you liked the video please like
1:33:35And subscribe and I will see you again
1:33:38in the next
Functions
1:33:44one hey guys welcome back to another
1:33:47video this is the 12th video in my
1:33:49Python Programming series and today
1:33:52we're going to be talking about
1:33:53functions so this is a function up here
1:33:57um we're going to talk more about the
1:33:58syntax and what a function does but it's
1:34:01first important to understand what is a
1:34:04function well if you're in math you know
1:34:06that a function pretty much you pass it
1:34:08an x value and then it does something to
1:34:10that x value and you get a yalue back
1:34:13well it's a similar idea here in Python
1:34:15except there's different types of
1:34:17functions that can do different things
1:34:19so the first function I've just written
1:34:21right here um pretty much I've named it
1:34:23add two and what happens is we give it
1:34:25an x value and then it simply returns x
1:34:29+ 2 now this doesn't have to make sense
1:34:31to you right now I just want to show an
1:34:33example of what a function looks like so
1:34:35if we wanted to make our own function
1:34:37what we would type is we'd type DF this
1:34:40stands for definition um and then the
1:34:43function name so maybe in this case I
1:34:44want to do another math related function
1:34:46so I'll do
1:34:48subtract two and oops two as a word and
1:34:53then I'm going to put two brackets and
1:34:54inside of the brackets we're going to
1:34:56put something that's called a parameter
1:34:58so in this case I can name it anything I
1:35:00want I'm going to put number in here and
1:35:02then I'm going to do a colon click enter
1:35:06it should tab me in one space and I'm
1:35:08simply going to return X or sorry number
1:35:13minus
1:35:142 now I'm I'll talk about what all this
1:35:17really does um a little bit later but I
1:35:19just want to give some examples so now
1:35:21if we want to call our function or want
1:35:23to actually use these functions we have
1:35:26to do something we can't just leave it
1:35:27like this I'll show you if I run this
1:35:29right now nothing's going to happen what
1:35:31we actually have to do is we have to do
1:35:33a um call statement so what I can do is
1:35:36I can do add
1:35:38to and then in here we have a parameter
1:35:42X but what's actually going to happen is
1:35:45I have to give add to a number because
1:35:48we're going to add two to that number
1:35:49and it's going to be returned so let's
1:35:52say seven
1:35:54now you'll see what's going to happen
1:35:56here uh I'll run it quickly and nothing
1:35:59prints out to the screen that's simply
1:36:01because we haven't printed anything what
1:36:04actually happens is in this add to call
1:36:06statement we have it sets x equal to 7
1:36:09so it comes up here and it says okay
1:36:10we're going to add two to S so we have X
1:36:13here we add two and then we're going to
1:36:16return that back down here so pretty
1:36:19much we call this function and it
1:36:21returns it right here now I have to set
1:36:23this equal to a variable or simply print
1:36:26it out to the screen so I'll set it
1:36:27equal to a variable right now so I'll
1:36:28say new number is equal to add to 7 and
1:36:33now if I print
1:36:35out new number like this we get nine
1:36:41okay so that that's how that works all
1:36:43right now what about subtract two so we
1:36:45can do the same thing here with seven so
1:36:47we do subtract two we'll leave seven in
1:36:51here for number
1:36:54and you'll see we get five so that works
1:36:57correctly now I can do it again I could
1:36:59do
1:37:0012 and we get 10 and maybe I could even
1:37:03change what I want to do in these
1:37:05functions so this one's called add two
1:37:07but maybe I wanted to add two to X and
1:37:11then I wanted to square it so then I
1:37:14would do that and then if we go back to
1:37:17the add two so I'm going to put 12 + 2^
1:37:202 which should be 14 squar which will be
1:37:22a pretty big number which will give us
1:37:24196 just like that so you can kind of
1:37:27see um and understand how these
1:37:29functions are working they have a
1:37:31parameter this is what that's called up
1:37:33here so a variable name I could do X I
1:37:35could do y I could do Zed doesn't matter
1:37:38I can do any letter I want any name I
1:37:40want um it makes sense to call it
1:37:42something that makes sense in terms of
1:37:44the function so here x works fine but if
1:37:46I was doing a string so maybe we want to
1:37:48write another function let's let's do
1:37:50another function here and we'll do
1:37:52Define and we'll call we'll say
1:37:55print
1:37:56string and now inside of here I'm just
1:37:59going to put string like that as our
1:38:02parameter name and then we're simply
1:38:04just going to print the string to the
1:38:07screen so now what actually happens is
1:38:10instead of setting something equal to a
1:38:12variable all I have to do is I have to
1:38:14do print string this is my call
1:38:16statement and then in here I'm going to
1:38:18give it a string so I'm just going to
1:38:19type
1:38:21hello and you'll see if we run the
1:38:23function
1:38:24we get hello and we didn't print it down
1:38:26here we just printed it from the
1:38:28function so I know I've gone kind of
1:38:31fast but the easiest way to understand
1:38:33the functions are to show examples of
1:38:35them and how they're working pretty much
1:38:37there's a
1:38:38parameter I'm going to give the
1:38:40parameter I'm in the argument of the
1:38:42call statement so this in the definition
1:38:45of the function is called a parameter
1:38:48and in the call statement in between the
1:38:51brackets it's called an argument
1:38:53um so those are just some key words now
1:38:56I can actually use these functions as
1:38:57many times as I want so I can print
1:38:59string again and I could
1:39:01print my name is Tim just like that and
1:39:04you'll see it should do it twice yeah
1:39:05there we go so that's why these
1:39:08functions are extremely useful because
1:39:10say for example I'm writing a big
1:39:12program and I know that I would need to
1:39:14do a certain thing multiple times well I
1:39:17don't want to have to constantly write
1:39:19it out um in the main line of my program
1:39:21here when I can just have a function
1:39:23that's going to do it for me and all I
1:39:25have to do is call that function using a
1:39:27simple call statement like this so we
1:39:29haven't really done many big programs
1:39:31yet where functions make sense but
1:39:33you'll see when you start programming
1:39:34that you're going to be using a lot of
1:39:36functions um to do things because you're
1:39:38going to be repeating code um now these
1:39:40are very basic functions we have
1:39:42subtract two add two but you can have
1:39:45like physics functions for example if I
1:39:47know I'm need to find the uh
1:39:49acceleration of an object then maybe I
1:39:51would make another function here and i'
1:39:53do
1:39:54Define excel I'll just do like that uh
1:39:57short form and then I'll put in mass and
1:40:01the force like that so you see I can
1:40:03actually have more than one um parameter
1:40:07I can have two parameters here and I'm
1:40:08going to say a is equal
1:40:11to mass Time Force I know that's not
1:40:15correct but we could do something like
1:40:16that mass time
1:40:18force and then I could return the
1:40:21variable a and now if I I called my
1:40:24Excel function and I printed that to the
1:40:26screen well that would give me what the
1:40:28acceleration would be right um so that
1:40:30those are just some examples of why
1:40:32functions are extremely useful well
1:40:34we're going to talk more about functions
1:40:36I'll do a more advanced tutorial um a
1:40:38little later on but this is just an
1:40:40introduction so that you can hopefully
1:40:42understand how they work pretty much you
1:40:43have if you're writing a function you
1:40:45have the deaf keyword um you have the
1:40:49function name so this case it's
1:40:51highlighted in blue so I did add two
1:40:53then you have brackets and say for
1:40:56example when we do a function we can
1:40:58actually do one without a parameter so I
1:40:59could do Define do something and in
1:41:04here I could simply print high right I
1:41:09don't need to have any parameters that
1:41:11just means when I call this function
1:41:13so do something I don't put anything
1:41:16inside of the brackets right we just do
1:41:18this and we see hi hello my name is Tim
1:41:21right um so yeah doesn't have to have
1:41:24parameters it can have multiple
1:41:25parameters um if for example in this
1:41:27acceleration function you have multiple
1:41:29parameters that means that when you call
1:41:31the function so I'll do an example here
1:41:34then you're going to have to put two
1:41:35numbers in or two strings so I do 2
1:41:38comma 5 like that and we'll print this
1:41:42to the to the screen because we don't
1:41:43print it in that function so we can see
1:41:45what we're
1:41:47getting so yeah we get hi hello my name
1:41:50is Tim and then 10 right so acceleration
1:41:532 okay um so yeah that's just the basics
1:41:56of functions we're going to talk more
1:41:58about them uh in more in depth later but
1:42:00just try to understand how they work and
1:42:02yeah so if you like the video please
1:42:04like And subscribe and I will see you
1:42:06again
How to Read a Text File
1:42:12later hey guys welcome back this is the
1:42:1513th video in my Python Programming
1:42:18series and today we're going to be
1:42:20talking about reading from a file so
1:42:22specifically a text file and we're going
1:42:25to be using something called
1:42:27file.io in Python so the first thing we
1:42:30need to do before we can read from a
1:42:31file is we need to create a file so I'm
1:42:34just simply going to go and find the
1:42:36directory where my python tutorials are
1:42:39so oops not here they are right here in
1:42:42the tutorials folder um and now you see
1:42:45I've got all my python uh scripts right
1:42:47here and all I'm going to do is I'm
1:42:48going to right click and I'm going to
1:42:49click
1:42:50new and then I'm going to find text
1:42:53document like that now I can name it
1:42:55whatever I like I'm just going to name
1:42:56it file for right now file. text now you
1:43:01may want to populate the uh text
1:43:03document so put some words in here so
1:43:05I'm just going to put a bunch of
1:43:06different words hello Tim
1:43:09python Learning Easy just some random
1:43:13random words in my text file I'm going
1:43:14to save that um and now we can go to
1:43:17reading in the text file so it is
1:43:21important that when you make the text
1:43:23text file you do have to save it in the
1:43:25same directory as your python script so
1:43:29if I were to put the text file here on
1:43:30my desktop and I have my python script
1:43:33in the tutorials folder here it's not
1:43:34going to be able to find the file
1:43:36properly um you would have to do
1:43:38something which is a path joining which
1:43:40I'm not going to talk about in this
1:43:41video but maybe in a later one so make
1:43:43sure that you save your file in the same
1:43:45directory uh and same folder as where
1:43:48your script is okay so now that we've
1:43:51done that we're going to read from the
1:43:53file so the first thing we need to do is
1:43:55we need to create a variable so we're
1:43:56going to name this file you can name it
1:43:58whatever you'd like and you're going to
1:43:59set it equal to the keyword open and
1:44:02then inside of open all you're going to
1:44:04do is you're going to type the name of
1:44:06your file so file. text a comma and then
1:44:10the mode you want to open your file in
1:44:12so in this case we want to read it so
1:44:14we're simply going to put a lowercase R
1:44:17inside of the quotations just like that
1:44:20and now that's pretty simple that's all
1:44:22you have to do in python to open up the
1:44:24file and to prepare to read it now make
1:44:28sure you do put R in here if you put
1:44:30nothing or you put a W for example which
1:44:32would be write mode it's actually going
1:44:34to clear the whole file which you don't
1:44:36want to happen you want to leave the
1:44:38contents of the file in there right so
1:44:39we need to open it in read mode with
1:44:41this R that's very important um now what
1:44:44we're going to do is we're simply going
1:44:46to read all of the things from the file
1:44:47so we've opened the file now we need to
1:44:49read it so in order to read the file uh
1:44:53we have to type a few things so we can
1:44:55do it in multiple different ways the
1:44:56first way I'm going to show you is this
1:44:57so I'm going to make another variable
1:44:59I'm going to call it f and this one
1:45:00we're going to make it equal to file.
1:45:03read
1:45:05lines like this okay and then we can
1:45:08simply print out F just to show you
1:45:11what's going to happen you see we get
1:45:14all of the words that are in the file
1:45:17now there's an interesting thing here
1:45:18you may notice that there's a back sln
1:45:22attached to all of the words except for
1:45:24the last one that's because I'm going to
1:45:26open up the text file again and show you
1:45:28um you notice here I didn't actually
1:45:30type back sln but every time we click
1:45:32enter like this it actually creates
1:45:35something called an escape character
1:45:37which is that back sln so if we want to
1:45:40read in the file we're going to have to
1:45:42remove that um character afterwards
1:45:44right so it's just something to keep in
1:45:46mind that if you have things on
1:45:47different lines each of the end of the
1:45:49lines is going to have a back sln on
1:45:52that line that you don't actually see
1:45:53here in the text document but python
1:45:55will read that
1:45:57in okay so we don't need to save that um
1:46:00yeah so let's move into the second way
1:46:03to read it properly now so we have f
1:46:05equals file. read lines now we want to
1:46:08remove all of these back slashes uh get
1:46:10rid of these ends from our lines so
1:46:12there's a way we have to do that and
1:46:13it's by using a for Loop so we're going
1:46:15to do a for Loop just going to say for
1:46:19line in F because now F has read all of
1:46:23our lines so we're going to do what's
1:46:25called iterating by item if you remember
1:46:27from my other videos so each line so
1:46:29every item in this F list here that just
1:46:32created by file. readlines we're going
1:46:34to remove that character and we're going
1:46:36to create a new list so let's make a new
1:46:39list we'll call it new list just make it
1:46:41a blank list like this new list and then
1:46:46here we're going to say um what is it
1:46:50new list. a p and if you remember what a
1:46:54pen does that simply means add to the
1:46:55end um of this new list and then we're
1:46:58going to add the line and we're going to
1:47:01use the slice operator to remove that
1:47:03back sln so we're going to Simply do the
1:47:06colon and
1:47:07then1 now what this does is we it will H
1:47:11take all of line except for the last
1:47:14character so negative one um I didn't
1:47:17talk about this in the other video but
1:47:19negative 1 pretty much just goes to the
1:47:21last character but does not include the
1:47:23last character when you do negative 1
1:47:24like this so just just follow along for
1:47:26now you don't have to completely
1:47:27understand it but just put that negative
1:47:28one and now if I print out new list
1:47:31we'll see what we get new
1:47:34list F5 and you see we get rid of all of
1:47:39the back slash ends now we have one
1:47:41issue here um down here on ease uh it
1:47:44should say easy but we've actually
1:47:46removed the last back sln from that
1:47:49string so if we don't want to do that
1:47:51now we need to actually add an if state
1:47:52statement into our for Loop so inside of
1:47:55the for loop we're going to type if and
1:47:57then we'll say line and we'll put a
1:48:00negative 1 again to represent the last
1:48:02character in that line equals equals
1:48:06back
1:48:07sln and then we'll simply tab this in
1:48:09because remember everything in Python
1:48:11needs to be indented properly for it to
1:48:13be read um and now we'll try this and
1:48:15we'll see if this
1:48:17works and there we go so we got hello
1:48:19Tim Python and learning but now we
1:48:21forgotten one thing we haven't actually
1:48:23included easy so now we need to do an
1:48:26else statement so we have else and then
1:48:28we'll do new list do append and we'll
1:48:32simply append the line just like that
1:48:36and now we can print it out and we will
1:48:37see what happens and we get hello Tim
1:48:41python learning and easy now there is an
1:48:44easier way to do this I just want to
1:48:45show this example because you may want
1:48:47to do something where you're checking
1:48:49through all of the lines and you want to
1:48:51see if the last character maybe the
1:48:53first character is equal to a certain
1:48:55item and depending on what that string
1:48:57is or depending on what it is you may
1:48:59want to add it to a new list you may
1:49:01want to do something to it so I just
1:49:02want to show you a way that we can do
1:49:03that pretty much this is iterating
1:49:06through every line in the file so file
1:49:09here so F creates a list of every line
1:49:11and then we go through every line of it
1:49:13and we see we say if the end of it has
1:49:15this back sln then we're going to remove
1:49:18the back sln um otherwise we'll just add
1:49:21that line into our list right
1:49:23okay so now the easier way that we can
1:49:25do this is actually with something
1:49:26that's called The Strip string method so
1:49:29in my other video I talked about this
1:49:30strip method that removes the spaces but
1:49:33what we can actually do with this uh is
1:49:36we can remove what do you call it sorry
1:49:38we can remove the back sln with the
1:49:40strip method as well so all we have to
1:49:42do here now it's a lot simpler is do new
1:49:45list do append and then inside of here
1:49:48we're simply going to type line. strip
1:49:51and now this will automatically remove
1:49:53those back SL ends and we'll see if this
1:49:56works there we go so we get everything
1:49:58and you can see it's a lot simpler and
1:49:59we don't even have to do the if
1:50:01statement um for the easy like that so
1:50:05that pretty much is how you read a file
1:50:08in using python there are some other
1:50:10ways to do this but this is the most
1:50:12basic and easiest way to understand
1:50:14we've also talked about how to remove
1:50:16that it's called an escape character at
1:50:18the end of the string that back sln and
1:50:20in the next video we're going to talk
1:50:22about writing to a file I hope you
1:50:24learned something if you did please like
1:50:26And subscribe and I will see you again
1:50:28in another
Writing to a Text File
1:50:34video hey guys and welcome back today
1:50:37this is the 14th video in my Python
1:50:40Programming series and we are going to
1:50:42be talking about writing to a file so in
1:50:44the last video we talked about reading
1:50:46from a file and now we're going to be
1:50:48writing to it so the first thing we need
1:50:49to do is create a file so we already
1:50:52have a file created here called file.
1:50:54text I did that in the last video and
1:50:56inside we have nothing so I'm just going
1:50:58to put the word hello in here so that we
1:51:00can test something out later okay now it
1:51:02is again important that you have this
1:51:04file in the same directory as your
1:51:06script so tutorial 14 which is the
1:51:08script I'm using right now uh is in the
1:51:10same directory that's important
1:51:12otherwise python will not be able to
1:51:14read that file properly okay so now
1:51:17we're going to do the same thing we did
1:51:18in the last video we're going to do file
1:51:20is equal to open the file name so in
1:51:22this case file. text oops that has to be
1:51:25the lowercase my bad file. text and the
1:51:29mode so in the last one we used R but in
1:51:31this case we're going to use W and that
1:51:33stands for right now there's one thing
1:51:36we need to do which I forgot to do in
1:51:37the last video and that is close the
1:51:39file so we do need to actually close the
1:51:42file so that it saves our changes which
1:51:44I forgot to do in the last video and
1:51:46it's very important that we do that so
1:51:47my apologies but here we go file. close
1:51:51and again if we had changed this name to
1:51:52anything else then we would do that name
1:51:54do close because the file is contained
1:51:56in the variable file like that okay so
1:52:00now we want to write to the file so all
1:52:02we have to do is type the variable name
1:52:04so file and then do write which is very
1:52:06simple and inside here I'll put a new
1:52:08word I'm going to put python like that
1:52:10now before we run it let's notice what
1:52:12is inside of our file. text so in here
1:52:16we have the word hello now I'm going to
1:52:18run the
1:52:20script and nothing happen here obviously
1:52:23because we just wrote to the file but
1:52:24when we open the file again you see now
1:52:27it says python now what actually
1:52:29happened was we removed the word that
1:52:31was in there before so hello and we over
1:52:34wrote it with python so what happens in
1:52:37this mode here W is it actually clears
1:52:40the entire entire file and then it
1:52:43writes what you want to write to it so
1:52:45if you have this W here it is going to
1:52:47clear the entire file now there's a way
1:52:49to get around this but I'm not going to
1:52:50be showing that in this video today
1:52:53okay okay so let's write something else
1:52:55to our file so file.
1:52:57write and then I am learning how
1:53:02to write to a file just like that and
1:53:07again we'll run the
1:53:08script and we'll open the text
1:53:12file and you see we get everything here
1:53:14on the same line so that's okay um but
1:53:17you may want this to be on different
1:53:19lines so how would we do that well if
1:53:22you're remember from the last video
1:53:24there was something called an escape
1:53:25character that we got when we read in
1:53:28the file we had that little back sln
1:53:30attached now that's the same thing we
1:53:32need to do when we're re writing to the
1:53:34file if we want things to go on a
1:53:35different line so on this line here I'm
1:53:37going to type the backs slash and then
1:53:39the
1:53:40N uh and now what's going to happen is
1:53:42it's going to have this Escape character
1:53:44which is going to tell python we need to
1:53:45move to the next line and then it is
1:53:48going to type this right here so we can
1:53:51show what's going to happen here we go
1:53:54F5 and then if we open up the file once
1:53:56again you see Python and then I am
1:53:58learning how to write to a file so that
1:54:00happened again because we use that back
1:54:02sln the Escape character now same thing
1:54:05we could use this in between uh a line
1:54:07so if we wanted to write a large thing
1:54:09and we want it on different lines then
1:54:10we can put the Escape character in there
1:54:12like
1:54:13that same thing open the file again and
1:54:16we'll see what happens and we see we get
1:54:18it on a different line so that's how you
1:54:20can write things to a different line uh
1:54:23now this has been a pretty short video
1:54:25today but that's really all it is uh to
1:54:28writing something to a to a file in
1:54:32Python now in the next video I'm going
1:54:34to show how we can write and read to a
1:54:36file at the same time so that would
1:54:38involve not wiping the entire file when
1:54:40we open it but if you learned something
1:54:42today please like And subscribe and I
1:54:45will see you again in another video
Using .count() and .find()
1:54:53hey guys and welcome back today this is
1:54:56the 15th video in my Python Programming
1:54:59series and we are going to be talking
1:55:01about some more methods that we can use
1:55:03on strings and on lists these methods
1:55:06are I'm going to put them in a comment
1:55:07so that I remember is
1:55:10theind and the do count method and now
1:55:14these can be used on strings and on
1:55:17lists so let's go right ahead and give
1:55:19an example so the do find method this is
1:55:22this is a uh very useful method in
1:55:25Python it's not in a lot of other
1:55:26languages so it is specific to python so
1:55:28what we can do is we're going to make a
1:55:29variable we're going to call it string
1:55:32now we can set this equal to whatever we
1:55:34want so let's just say hello for this
1:55:36case for
1:55:37Simplicity and then we're simply going
1:55:39to
1:55:40print
1:55:42string oops if I spelled it correctly
1:55:45string
1:55:47doind and then inside of theind brackets
1:55:50here so the argument for this meth meod
1:55:53we are going to put in what we want to
1:55:54find so we want to see if we can find
1:55:58the indic of any of these letters here
1:56:01so if I put in
1:56:03L you'll see what happens is I get two
1:56:06and that's because there's an L the
1:56:08first l in the string is found at the
1:56:11indic of two just like that um now you
1:56:14may be confused right now I haven't
1:56:16completely explained it but that's fine
1:56:18so as you can see we have two L's in the
1:56:20string right now when I put in this l in
1:56:24the
1:56:25brackets it gives me a value of two now
1:56:28that is where the first L is found in
1:56:31the string if I put in O for
1:56:34example you'll see it gives me four and
1:56:36there's only one o in the string so it
1:56:38gives me the value of where that o is
1:56:41which is the fourth indic of the string
1:56:43the fourth index uh if I put in
1:56:46h you see we get zero and now notice
1:56:50what happens if I put in a number or a
1:56:52letter that's not actually in the string
1:56:54so I'll put in seven for example the
1:56:56string seven and you see we actually
1:57:00get1 that's because if python does not
1:57:02find that inside of the string it's
1:57:04going to tell you that it's negative one
1:57:07so rather than zero it gives you
1:57:08negative 1 just like that so this is
1:57:11useful because maybe we want to check in
1:57:13a password for example if it contains a
1:57:16certain elements so maybe if there's a
1:57:18dash in there we don't want them to be
1:57:20able to use that password or maybe we're
1:57:22not allowing a certain character or we
1:57:24need a certain character to be in a
1:57:26password or a string this is where find
1:57:28is useful um and it'll tell us if that's
1:57:31in the string or not now there's
1:57:33actually another useful uh string method
1:57:36here that is do count now it does a
1:57:38similar thing except instead of finding
1:57:40the index of the letter it counts how
1:57:43many of them there are in the string so
1:57:45now we're going to go to do count so
1:57:47we'll go do count like this and again
1:57:49inside of the brackets we're just going
1:57:51to put a string uh so let's do L and you
1:57:56see if we F5 here we get the value of
1:57:59two that's because obviously there are
1:58:01two L's in our string now I'm just going
1:58:03to type a bunch of random letters and
1:58:05show you that this does work for all
1:58:07kinds of things so if I type in a for
1:58:10example see how many A's I typed we have
1:58:13six A's in that string now let's try a
1:58:15zed I know I didn't use Zed here I'm
1:58:17pretty sure at least oops that's my bad
1:58:23uh we forgotten a uh parenthesis there
1:58:26sorry okay and we get zero right so
1:58:29instead of -1 it counts and it says well
1:58:32there are zero of them in the string so
1:58:34we're going to return zero again we can
1:58:37use this to find if there's a certain
1:58:40character in the string so if we say you
1:58:41can only have a maximum of three numbers
1:58:44in your string well we could count and
1:58:46we could say if there whatever how many
1:58:48numbers right um the dot find is
1:58:51different than this because it actually
1:58:53tells us where that is so if I do find
1:58:57again and now maybe I put in an a well
1:59:00we know there's six A's in the string
1:59:02but which a is this one going to give us
1:59:04so it says five because that is the
1:59:06first place that an a occurred in the
1:59:08string so we see here at the fifth indic
1:59:11or index that's where the first a is
1:59:13it's not going to give us the A's from
1:59:14back here if we wanted to find the A's
1:59:17back here we' have to use a for Loop and
1:59:19we'd have to Loop through the string in
1:59:20different parts of the string to find
1:59:22where all the A's were um again yeah so
1:59:25that's how you use doind and do count
1:59:28it's very useful if you want to check
1:59:29certain things so I'll show you an
1:59:31example here if we wanted to check a
1:59:33string how we would use the count method
1:59:35or the do find method so I'm going to
1:59:37say if
1:59:40string. count and I don't want to allow
1:59:43any let's say underscores in my string
1:59:47is greater than zero so that just means
1:59:50uh if it's I guess we could also say not
1:59:52equal to zero so if it has if it
1:59:54contains at least one of these
1:59:56underscores then we're not going to
1:59:57allow it so we're going to print to the
2:00:01screen not good just like that okay just
2:00:04to show how this works now here instead
2:00:07of actually having a preset string we're
2:00:09just going to get input from the console
2:00:10so we can do this a few times and see so
2:00:12input and we'll say please type
2:00:16something as the prompt like
2:00:19that and otherwise so in L state in here
2:00:22if it doesn't contain any underscores we
2:00:24will print
2:00:26good like that okay so now I'm going to
2:00:30type hello that was our first
2:00:33string and oh what happened
2:00:38there good so we get good sorry I don't
2:00:41something glitched uh in the program
2:00:43there we'll rerun it I'm not sure what
2:00:44happened there
2:00:46F5 please type something I type hello
2:00:50there we go now we get good I'm not sure
2:00:51what happened the other one okay so now
2:00:53let's do it again and this time we're
2:00:54going to include an underscore so we'll
2:00:56do underscore uncore hello it's going to
2:01:00say not good because it counted these
2:01:02underscores and it said well there is
2:01:04more than zero underscores so it's not
2:01:06good and then same thing if we put an
2:01:08underscore in the middle of the word or
2:01:09we put a ton of underscores so h e
2:01:12l o whatever just like that uh not good
2:01:17because there's underscores in the word
2:01:18just like that and yeah that's pretty
2:01:21much how the doind and the do count
2:01:23methods work there's a ton of uses for
2:01:25these and it's really useful um that
2:01:27they have this method because in a lot
2:01:29of other programming languages they
2:01:30don't have this count and find so it
2:01:33makes it a lot harder if you're trying
2:01:34to find how many things are in a string
2:01:37or if you're trying to find the index of
2:01:38a letter so I hope you learned something
2:01:40I hope you enjoyed if you did please
2:01:42like And subscribe and I will see you
2:01:44again tomorrow for another video
Introduction to Modular Programming
2:01:51hey guys welcome back this is the 16th
2:01:54video in my Python Programming series
2:01:57and today we're going to be talking
2:01:58about python modules and import
2:02:00statements so this is something new that
2:02:02I haven't talked about yet but python is
2:02:05a modular programming language meaning
2:02:07that we can use multiple files together
2:02:09to create one program and you'll
2:02:11understand what I mean later in the
2:02:12video when I show some examples um but
2:02:15yeah so pretty much in Python at the
2:02:17beginning of all your Python scripts
2:02:19what you typically do is you write
2:02:21something like this it says import and
2:02:23you can see this highlights an orangey
2:02:24yellow meaning that it is a keyword in
2:02:27Python now there are many built-in
2:02:30modules or what they called um in Python
2:02:32that contain a list of functions and
2:02:35classes that we can use in our python
2:02:37script so for example the math module
2:02:40import math this allows me to do things
2:02:43like dot square root or it's the square
2:02:46root function which I believe is like
2:02:49this and then you can put something
2:02:52inside of it um so yeah there's plenty
2:02:54of cool things like that there's another
2:02:56module it's called pame Uh we're going
2:02:58to be doing this later I'm going to be
2:03:00talking about game development with
2:03:02python if you want to see that leave a
2:03:03comment down below and I'll get it
2:03:05started soon and then we have import we
2:03:07can also import something called OS and
2:03:10this gives us uh opportunity to do
2:03:12things like file paths and lots of
2:03:15different things like that we can import
2:03:16images we can import tons of different
2:03:18things into our files um in Python so
2:03:22we're just going to use the math module
2:03:24today for demonstration purposes but
2:03:27pretty much what happens is there's a
2:03:28bunch of built-in modules in Python so
2:03:30this one is known as a built-in module
2:03:32math uh when you download and install
2:03:35python it pretty much you download that
2:03:38module the pame module that I was
2:03:40talking about is not a built-in module
2:03:42that means you actually have to go to
2:03:43the internet you have to find it and
2:03:45install it on your computer before you
2:03:47can import it into your Python program
2:03:50and now what's really cool about python
2:03:52is that it's an open- Source language
2:03:54meaning that you can actually create
2:03:55your own modules um and they contain
2:03:58functions classes and you can use them
2:04:00in many of your different programs or
2:04:03you can also send them to a friend maybe
2:04:05or post them online for other people to
2:04:06be able to use so let's get right into
2:04:09it and uh let's do some things here so
2:04:13if we want to use this math module we
2:04:15have to first start by typing math so
2:04:18the name of our module here okay so math
2:04:21and then afterwards we're going to do
2:04:23the name of a function or a class in
2:04:26that um that math module so you see a
2:04:29bunch of things pop up there uh in that
2:04:31little box so if I do the dot again in
2:04:32wait a second it should pop up yeah so
2:04:34there we go and you see we have cosine s
2:04:37tangent all that stuff uh
2:04:40degrees factorial all these things
2:04:44inside of our math module so these are
2:04:46new things that we wouldn't be able to
2:04:48use before so I'll show you if we do
2:04:50math.pi and I print that to the screen
2:04:53well we should get an endless number of
2:04:55digits like this math.
2:04:58Pi uh is it running there we go so it
2:05:01just had to print a bunch of numbers so
2:05:03math.pi it didn't print all of them I
2:05:04think it only went to 10 decimal points
2:05:07um so yeah and then I'll show you if I
2:05:09comment out this uh this import math
2:05:13what'll happen if I try to do this we
2:05:15get an error because well math is not
2:05:17defined we didn't import it at the top
2:05:20of our program so we'll uncomment this
2:05:22now now I can do things like math.
2:05:25degrees uh degrees simply turn something
2:05:28that is in radians into degrees so if I
2:05:31did math. Pi in here for example and we
2:05:34print that to the screen we get 180
2:05:37degrees like that uh they also have the
2:05:40math. radians I believe which does the
2:05:43opposite so it turns a degree into a
2:05:46radian so if we did 60 degrees
2:05:49here like that
2:05:52and we get
2:05:531.47 which is about pi over 3 uh radians
2:05:57like that so yeah there's lots of cool
2:05:59things that we can do with these modules
2:06:01now I'll actually show you an example of
2:06:04how we can use our own modules and how
2:06:06we can import our own modules into our
2:06:08function so I'm going to just start by
2:06:10typing import I'm going to type my
2:06:13module just like this okay um now I
2:06:16haven't actually created this module yet
2:06:17we're going to do that in just a second
2:06:19now I'm going to go and I'm going to
2:06:20find the directory that my main script
2:06:22is in so it's in tutorials up here I've
2:06:25got this open and remember uh how we had
2:06:27the file in here as well because it has
2:06:29to be in the same directory so I'm going
2:06:30to create a new file in that directory
2:06:32so file new file I'm going to save this
2:06:36one as my module now spelling is very
2:06:40important again capitals do matter uh
2:06:42and it has to be in the same directory
2:06:44as our main script which it is like that
2:06:49now inside of this module I'm going to
2:06:50write a very basic function if you
2:06:52haven't seen that video yet go check
2:06:54that out first um we just talked about
2:06:56some basic functions and what they do in
2:06:58the use of them so I'm going to Define
2:07:00I'm just going to call it my funk like
2:07:03this it's going to take a parameter
2:07:04called X and all it's going to do is
2:07:07return x + 5 so very basic function like
2:07:12that but just for demonstration purposes
2:07:14here okay and now I have my module
2:07:17imported like this so I can actually use
2:07:19my module so I'm going to do print my
2:07:23module dot my funk because that's the
2:07:27name of my function and then inside I
2:07:29need to give it a number so what number
2:07:31do we want to use maybe we'll use
2:07:33six and we print this out and you see we
2:07:36get 11 so by simply all I did was create
2:07:39a new file put it inside of my um same
2:07:43directory as this script here and easy
2:07:47enough we were able to use it now I can
2:07:49actually create multiple functions in
2:07:50here so I can create
2:07:52Define another Funk like this that'll be
2:07:55the name this one will take X again and
2:07:57inside here I'm going to return X
2:08:00integer division five and then again we
2:08:04could use that one so instead of my funk
2:08:06we'll use another Funk like this and
2:08:08let's just type in a big number like
2:08:10that and see what we
2:08:13get there we go so again it's working
2:08:16perfectly fine so that's how uh modular
2:08:19programming is what it's called works in
2:08:21Python now obviously if you're going to
2:08:23be doing modular programming it's
2:08:25usually for larger uh programs and
2:08:28bigger programs where you want multiple
2:08:30files to keep things organized say maybe
2:08:32if you're doing a flight simulator you
2:08:34may have one module that includes all of
2:08:37your physics functions so you know that
2:08:40when you need to do physics you're going
2:08:41to import your physics function and then
2:08:43you can or module sorry and you can use
2:08:46the functions from there and the great
2:08:48thing about this is that these uh these
2:08:50modules are reusable in other python
2:08:53files I can import them and I can reuse
2:08:55these functions I don't have to
2:08:57constantly rewrite them inside of my
2:08:59script like this so this has been a very
2:09:02basic tutorial uh just an introduction
2:09:04to modular programming I showed the math
2:09:06module a little bit if you want to learn
2:09:08about some of the built-in uh sorry
2:09:11modules in Python then just go ahead and
2:09:14go to their website and you can see a
2:09:15bunch of the different ones that they
2:09:17have another one that we may be using
2:09:18later is called OS so the statement for
2:09:21that is just import OS and if you want
2:09:23to get started with game development you
2:09:25can look into pame which is this cool
Optional Parameters
2:09:28thing that I'm going to be doing a
2:09:29tutorial series on later so yeah that's
2:09:32been the video for today we talked a
2:09:34little bit about modular programming if
2:09:36you enjoyed please like And subscribe
2:09:38and I will see you again tomorrow with
2:09:40more
2:09:45content hey guys and welcome back to
2:09:47another YouTube video this is the 17th
2:09:50video in my P Python Programming series
2:09:53and today we're going to be talking
2:09:54about functions more advanced functions
2:09:56and something called optional parameters
2:09:59so let's just give a quick refresher of
2:10:01what a function is I'm just going to
2:10:02write one very quickly I did do another
2:10:04video on basic function so if you
2:10:06haven't seen that yet go check that one
2:10:07out first now here we've defined what
2:10:10we're going to call the function Funk
2:10:13and then inside of these brackets we put
2:10:16something called parameters if you
2:10:17remember that from the other video so in
2:10:19this case I'm just going to put X as my
2:10:21own only parameter and then I'm simply
2:10:23going to print x to the screen just like
2:10:26that so A very very basic function but
2:10:29it's just to give you a quick refresher
2:10:30of what it does now when we wanted to
2:10:32call our function all we had to do was
2:10:33write our function name and then put a
2:10:37brackets or the brackets like this and
2:10:39then inside of our brackets if we had a
2:10:40parameter set we had to do we had to
2:10:43give it that parameter so we could do
2:10:44three we could do hello let's just do
2:10:46hello in this case and we'll click F5
2:10:49and we get hello on the screen screen
2:10:51just like that now we can change this
2:10:53again to maybe Tim which is my name just
2:10:56like that and we get Tim printed this
2:10:58screen very basic very easy just like
2:11:00that okay so that's pretty much what a
2:11:03function is now what we're going to be
2:11:05talking about today is more uh in- depth
2:11:07on these parameters so you remember we
2:11:09can give it multiple parameters so we
2:11:10could do x uh and we could do maybe text
2:11:14like this so we had X and text and then
2:11:16we could print x to the screen and maybe
2:11:18we'll say if text is equal to
2:11:22um let's say one so the string one then
2:11:25maybe we'll
2:11:27print text is one otherwise so else we
2:11:32will
2:11:35print text is not one just like that
2:11:38very easy okay so now what we have to do
2:11:40when we call the function is we have to
2:11:42give it another parameter in this case
2:11:44I'm going to give it two and we'll see
2:11:46what
2:11:47happens we get Tim and then texter I
2:11:50misspelled that my bad is not one now if
2:11:53I change this parameter to one again
2:11:55we'll
2:11:55see that we get text is one now again
2:11:59that's just another example but in some
2:12:01cases when we call a function and we
2:12:03have many parameters or maybe we don't
2:12:05have many but we don't want to
2:12:07constantly be rewriting these parameters
2:12:10we want to have something called the
2:12:11default value for our parameters so X
2:12:15maybe we always want to give our
2:12:17function the value of x but in text here
2:12:20we always want it to be set to something
2:12:22unless we otherwise specify so in this
2:12:25case I'm going to say text and then the
2:12:28equal sign and I'm going to put two just
2:12:31like that now here I'm going to remove
2:12:35the one and you'll see what's will
2:12:37happen here we'll just run it to show
2:12:39you and then we'll go in what's actually
2:12:40happening so we get Tim and then text is
2:12:43not one so what actually happened here
2:12:46is since I didn't specify a value for
2:12:49text so I didn't do a and write
2:12:51something else here it defaulted to the
2:12:54value of two now we can prove that by
2:12:57just simply printing out text and
2:13:00showing you that it is actually
2:13:01two so we have Tim and two just like
2:13:05that um if I wanted to change this value
2:13:08now all I would have to do is simply put
2:13:11a comma here in the function and change
2:13:13it to whatever I want so in this case
2:13:15I'll do
2:13:1667 and if we print it again you'll see
2:13:19we get
2:13:2067 so that's how the optional parameter
2:13:22works now all you have to do to set an
2:13:25optional parameter is simply type an
2:13:27equal sign after your parameter so if I
2:13:31have X and I'll set it equal to three
2:13:33like that now when I call the function I
2:13:36actually don't have to give it any
2:13:38parameters because it already has the
2:13:40optional parameter of three and of two
2:13:43which are going to be the default values
2:13:44if I don't write anything in my function
2:13:47argument here so inside of these
2:13:49brackets and you'll see again we get
2:13:51three and two because those are the
2:13:53default
2:13:54values now there is one tricky thing
2:13:57that you have to understand about these
2:13:59optional parameters say you wanted to
2:14:01have X stay at the default value so you
2:14:05wanted it to be three and you didn't
2:14:06want to type three in the brackets like
2:14:08that but you wanted to change the text
2:14:11optional parameter to something like
2:14:14seven or eight or maybe another string
2:14:17well you can't actually do that in order
2:14:19to change this text optional parameter
2:14:21you would have to also set a value for x
2:14:24here so if I try to I'll say I just want
2:14:27to set the text to three so I'll do like
2:14:29three
2:14:30here it's actually not going to change
2:14:33um sorry what do you call it it's I'll
2:14:35change it something else so we can we
2:14:37can see it better five let's do five
2:14:39it's actually not going to change that
2:14:41second variable right it's going to
2:14:42change the first one automatically
2:14:44because that's just the order in which
2:14:46they're defined so if you want to if you
2:14:48have more than one optional parameter
2:14:51you will have to set a value for the one
2:14:53before in order to change the one
2:14:55afterwards like this so that's the only
2:14:58tricky part about these optional
2:14:59parameters other than that they're
2:15:01extremely useful again you would use
2:15:04them if you're constantly using a
2:15:06function and you don't want to have to
2:15:07keep writing in parameters you only want
2:15:09to give it a parameter in a very
2:15:11specific case then you would have that
2:15:13optional parameter already set so you
2:15:15don't have to keep typing a certain word
2:15:17you don't have to keep giving it a value
2:15:19like that so this is been a shorter
2:15:21video but this is the video on how to do
2:15:23an optional parameter they look just
2:15:25like this they're very simple and pretty
2:15:27basic and I hope you learned something
2:15:29today if you did please like And
2:15:31subscribe and I will see you again in
2:15:33another
Try and Except (Error Handling)
2:15:39video hey guys welcome back to another
2:15:42video this is the 18th video in my
2:15:44Python Programming series and today
2:15:46we're going to be talking about
2:15:47something called a try and accept block
2:15:50of codes so just like we talked about
2:15:52the if else and L if block of code um
2:15:55and like we talked about the for Loop
2:15:57which is a block of code and like
2:15:59functions which are blocks of code uh
2:16:00we're going to be talking about the try
2:16:02and accept so in Python we use try and
2:16:05accept as our keywords in other
2:16:07languages you may have heard of try and
2:16:08catch statements um this is the same
2:16:11thing except in Python instead of catch
2:16:13we use accept Okay so let's talk about
2:16:16what a try and accept statement is
2:16:18sometimes in Python you want to do an
2:16:20operation or you want to try something
2:16:23but you don't know if it's really going
2:16:24to work or not there's some different
2:16:26variables that may come in sometimes it
2:16:28may work sometimes it may not work
2:16:30usually this depends on user input or
2:16:33what the user is doing in the program so
2:16:36a good example of this is maybe if we're
2:16:38validating a form so we'll do an example
2:16:40here we'll say text is equal
2:16:42to uh input like this and then we'll say
2:16:48username okay now in typical usernames
2:16:51you're only allowed certain characters
2:16:53maybe you can't do um a comma you
2:16:56couldn't do a star certain things like
2:16:58that right maybe we only want it to be
2:17:00text or we only want it to be numbers uh
2:17:02we just want to validate this we could
2:17:04use a huge if statement to do this um
2:17:07but
2:17:08instead there's something called a try
2:17:10and accept that we can use so in this
2:17:12case I want my username to only be
2:17:15numbers so I don't want it to be a
2:17:17string at all I want it to only be
2:17:19numbers so what I'm going to do is I'm
2:17:21going to make a new variable here and
2:17:23I'm going to call it number and all I'm
2:17:26going to do is I'm going to try to int
2:17:28the text like this okay so if you
2:17:31remember what the int does is that
2:17:33simply converts our string into an
2:17:35integer and then I'm going to print that
2:17:37to the screen so I'm going to print
2:17:38number to the screen now what what
2:17:41happens right here is uh if I try to
2:17:43type in a word like this you see we get
2:17:46an error it says there's an invalid
2:17:48literal for INT with base 10 now you
2:17:51don't have to understand what this error
2:17:52is pretty much all this means is that we
2:17:54can't convert hello into a number
2:17:56because there is really no number there
2:17:58how how does it know what number to make
2:18:00that into right so in this case we would
2:18:03use a try and accept look because if the
2:18:06user types in something that's
2:18:09invalid well we want them to type in
2:18:11something else we don't want the program
2:18:13to crash like it just did there so here
2:18:16I'm going to put a try like this
2:18:19followed by a colon and then I'm going
2:18:21to indent these two blocks right here so
2:18:25that means we're going to try this block
2:18:28of code okay and then underneath here
2:18:31I'm going to type accept just like that
2:18:34followed by a colon now there is a more
2:18:36advanced way to do this we can accept a
2:18:38specific type of error um but I'm not
2:18:41going to talk about that in this video
2:18:42this is just a very basic try and accept
2:18:44block and then under here uh I'm going
2:18:47to type what I want to happen if this
2:18:49block of code doesn't run meaning this
2:18:51block of code returns an error um and we
2:18:54can't actually execute it so in this
2:18:56case I'm just going to do uh I'll just
2:19:00print to the
2:19:01screen
2:19:03invalid username because remember we're
2:19:06only going to have our username
2:19:07contained numbers all right so now if I
2:19:10run the program and I try to type in
2:19:12something that is not a string so just I
2:19:14mean that is not a uh number like this
2:19:17it'll just say invalid username rather
2:19:20than crash ing the program which is what
2:19:22happened
2:19:23before and if I do try to type in
2:19:25something that is valid so maybe I type
2:19:271 2 3 4 it just gives me that username
2:19:30back right so it works like that um so
2:19:33that's pretty much how the try and
2:19:35accept works again this is a very basic
2:19:38example and that's what I try to do in
2:19:39my videos just give you the basics and
2:19:41then you can try to apply it to some
2:19:43projects you're doing um and some more
2:19:45advanced things so again what happens is
2:19:48it tries this block of code
2:19:51if that block of code doesn't run or an
2:19:53error happens instead of Simply crashing
2:19:56the program and giving us that red
2:19:57message it's going to go to this accept
2:19:59block and it's going to do whatever is
2:20:01in here now in this accept block again
2:20:04we could ask them to type in a new
2:20:05username we could display another
2:20:08message we could maybe um close the
2:20:10program on them we could do whatever we
2:20:12wanted to do in this accept block and
2:20:14it's extremely
2:20:15useful um so yeah that's pretty much how
2:20:18the try and accept works
2:20:21if you learned something please like And
2:20:23subscribe and I will see you again in
2:20:25another
Global vs Local Variables
2:20:32video hey guys welcome back to another
2:20:34video this is the 19th video in my
2:20:36Python Programming series and today
2:20:39we're going to be talking about a fairly
2:20:41complex topic some people find kind of
2:20:44confusing in their programs um it's very
2:20:46useful to understand and it's Global
2:20:48versus local variables so Global um
2:20:53pretty much means everywhere can be used
2:20:55in anything and local means specific to
2:20:58a certain uh block of code or to a
2:21:01certain class so let's just dive in
2:21:04right away with an example so in our
2:21:06program we have things called variables
2:21:08again we've talked about them before so
2:21:10I'm just going to say VAR up here equals
2:21:129 we'll say our um true this is
2:21:16lowercase here uh is just equal to true
2:21:19or actually let's call it Loop in
2:21:20instead just not to confuse anyone so
2:21:22Loop is equal to true and then maybe
2:21:25let's make a function we're just going
2:21:26to call this function Funk we'll give it
2:21:28a parameter of X and we're just going to
2:21:31say if x is equal
2:21:34to oops 2 equal sign 5 then
2:21:39return true and we'll set a variable up
2:21:42at the top of this function and we'll
2:21:44just call it uh
2:21:48Newar is equal to
2:21:51seven or yeah seven let's do that and
2:21:54then instead of returning true we'll
2:21:55actually return new VAR my bad new VAR
2:21:57just like that okay so here we have
2:22:01three variables we have the variable VAR
2:22:04which is equal to nine the variable Loop
2:22:06which is equal to true and then the
2:22:08variable Newar which is inside of our
2:22:10function and that's equal to
2:22:127 uh and then the parameter X which
2:22:15again can kind of be considered a
2:22:16variable but it is uh more specifically
2:22:19a parameter so I just want to show you
2:22:21right away what tries to happen if I
2:22:24print out the variable Newar so outside
2:22:28of my function in its own line I'm just
2:22:30going to say print Newar like
2:22:34that we click F5 and we get an error
2:22:37here it says the name Newar is not
2:22:40defined but how can that be possible we
2:22:43just made Newar right here in our
2:22:46function well this is where we get into
2:22:48local versus global variable
2:22:51so the variable Newar is actually known
2:22:54to be a local variable to the function
2:22:57uh Funk so that means the only thing
2:22:59that's able to access the variable
2:23:01change the variable and use the variable
2:23:03Newar is the function Funk so again if I
2:23:08put the print statement inside of my
2:23:09function and then I call my function it
2:23:12will work so here if I type Funk now and
2:23:15I'll give it value of
2:23:18two then Newar does print to the screen
2:23:21and we don't get an
2:23:22error so that is how uh the local
2:23:25variable works so again it's the same
2:23:28thing in any other type of function so
2:23:30if I write another function I do Define
2:23:33other Funk this one I won't give it a
2:23:36parameter and I'll just
2:23:37say
2:23:40Newar is equal to five like that so
2:23:43different than seven and then this
2:23:45function will print Newar well do we
2:23:48think it's going to print out seven or
2:23:49do we think it's is going to print out
2:23:51five so we'll show you like
2:23:54this if we
2:23:57run uh take zero positional arguments
2:23:59other fun oh my bad there's a two in
2:24:01there
2:24:02sorry it prints out five right so it
2:24:05doesn't look at this variable here it
2:24:07looks at the local variable in here
2:24:09called Newar now same thing if I put
2:24:12Newar up here outside of all of these
2:24:15functions and I set it equal to
2:24:1723 and then I click F5 here to run it's
2:24:22still going to print out five that's
2:24:24because these
2:24:25functions they can see these variables
2:24:27up here but if there there's a variable
2:24:30already declared inside of their
2:24:32function they're going to use that one
2:24:34instead I know it's kind of confusing
2:24:36I'm going to talk a little bit more
2:24:37about how this works I just want to give
2:24:38some examples to start
2:24:40off okay so now we'll talk about global
2:24:43variables so local variables are ones
2:24:46that are declared usually inside of a
2:24:48function so that means outside of the
2:24:50function so again if we tried to print
2:24:52Newar out here um and I got rid of this
2:24:55up here we get the error like we got
2:24:57before right it says it's not
2:24:59defined so what about global variables
2:25:02well these Global variables are ones
2:25:04that anything in the program can see so
2:25:06VAR and loop would be known as Global
2:25:09variables in here if I try to print out
2:25:12our variable
2:25:14VAR and then I'll just call that
2:25:16function by doing Funk and I'll give it
2:25:19a value of two once again
2:25:22it will be able to see VAR and it does
2:25:24print it to the screen as nine that is
2:25:27because this is global it's not defined
2:25:30inside of a function or inside of a
2:25:31class and classes we'll talk about later
2:25:33don't worry about that um same thing if
2:25:35we try to print out Loop so if I change
2:25:38this to loop again it should
2:25:42work we get true printed out to the
2:25:44screen just like that so now there's the
2:25:47issue with changing variables in inside
2:25:50of a function so typically you want to
2:25:53avoid this you don't want to be
2:25:55dependent on these Global variables up
2:25:58here to make your functions work this is
2:26:00because if you remember in my other
2:26:02video I talked about modular programming
2:26:04where we can reuse
2:26:06functions well if I have a variable
2:26:08defined up here and my function is
2:26:12referencing that variable meaning it
2:26:13requires that variable to work then if I
2:26:16try to use this function in another one
2:26:18of my programs it's not not going to
2:26:20function properly because that Loop
2:26:22variable or that Global variable that I
2:26:24try to use is not going to be uh defined
2:26:27in that other program
2:26:29right so let's do an example here of how
2:26:33we can actually change a global variable
2:26:37so if I go here and I say
2:26:40Loop is equal to 7 so I call my function
2:26:44and it's going to set loop equal to 7
2:26:46it's um and then I try to print out the
2:26:49variable Loop Loop well what do you
2:26:52think is going to print to the screen
2:26:53give me a guess it's either going to be
2:26:55seven or it's either going to be true um
2:26:57what do you think right so I'm printing
2:26:59it underneath after I call this function
2:27:02Let's
2:27:03see we get true now even though we what
2:27:08looks like changed the variable in this
2:27:10function instead of actually changing
2:27:12This Global variable instead we created
2:27:14a new variable which is local to the
2:27:16function called Loop if we want to
2:27:19change the variable we have to use
2:27:21something called the global keyword so
2:27:24in Python what we have to do is at the
2:27:25top of our function we just type the
2:27:27word Global followed by the variable
2:27:30name that we want to change so in this
2:27:33case I'm going to say
2:27:34Loop now if we run this
2:27:38program we print out seven that's
2:27:41because this tells our function that
2:27:43we're going to look outside of the
2:27:45function here for the variable if it
2:27:47exists then we're going to change it
2:27:49just like like we did there so I know
2:27:52this has been kind of confusing I've
2:27:54kind of jumped around try to play around
2:27:57with these things and see uh if you're
2:27:59getting errors see if you can figure out
2:28:01what's going to print to the screen here
2:28:02if you want to change a global variable
2:28:04so one that you define at the top of
2:28:06your program here or outside of your
2:28:08functions then you must type the global
2:28:11keyword inside of your function followed
2:28:14by the name here again if I try to
2:28:16change the uh the variable down here so
2:28:18not inside of a function it will work so
2:28:19if I I do Loop is equal to
2:28:24false then uh oh we get seven again just
2:28:26because I called it up here so I won't
2:28:28call this function this time if I say
2:28:30Loop is equal to false then we will get
2:28:32false because it's not inside of a
2:28:34function so it is able to see and change
2:28:36this
2:28:37variable so really using the global
2:28:40keyword uh it's just inside of functions
2:28:42and just remember that if you do create
2:28:44a variable inside of your function it's
2:28:46local to your function meaning that
2:28:48unless you type this Global keyword
2:28:51first and then followed by the name of
2:28:54that
2:28:54variable everything outside of the
2:28:56function will not be able to see that
2:28:59variable so if you liked if you uh like
2:29:02this video and you learned something
2:29:04please like And subscribe and I will see
2:29:06you again in another video
Introduction to Objects
2:29:20hey guys and welcome back to another
2:29:22YouTube video so in this video I'm
2:29:24actually starting a new series on
2:29:26classes and objects in Python now I'm
2:29:28going to start off fairly simple and
2:29:30just explain to you kind of what is a
2:29:31class what is an object how can we
2:29:33create our own and in future videos I'm
2:29:34going to get a lot more advanced and
2:29:36show you some really cool things that
2:29:37you can do with classes and objects that
2:29:39not only make your life a lot easier but
2:29:41are really cool in my opinion and I
2:29:43didn't even know you could do them until
2:29:45I was kind of searching around before
2:29:46making this video so make sure if you
2:29:48guys are interested in bringing your
2:29:50programming kind of to the next level
2:29:51and stepping out of the basics of python
2:29:53and moving into more of an intermediate
2:29:54to Advanced topic that you guys watch
2:29:56through the series because you're going
2:29:57to learn a lot and you're going to have
2:29:59a fundamental understanding of python
2:30:01that you never uh understood before if
2:30:03you can understand the topics I'm going
2:30:05to talk about okay so let's start by
2:30:07talking about objects what is an object
2:30:11well in Python an object is pretty much
2:30:14anything and you can see on this my
2:30:15screen here I have two variables x = 5 y
2:30:19= string
2:30:20now to us without any understanding of
2:30:22objects and classes this just looks like
2:30:24X is equal to an integer that's five and
2:30:27Y is a string and that string value is
2:30:29string now I just want to show you
2:30:31quickly and you might have seen that you
2:30:32can do this before if I print out the
2:30:35type of X and what do you call it we'll
2:30:38just do the same thing here with Y run
2:30:41our program you see we get class int and
2:30:44class string now this is pretty trivial
2:30:47you figured this was going to happen but
2:30:49what does this class actually mean here
2:30:52well pretty much whenever you create a
2:30:54new object in Python so whenever you
2:30:56create a variable something like that it
2:30:58automatically creates an instance of an
2:31:01object now X the way that you can also
2:31:04read this is X is equal to an instance
2:31:07of the in class and its value is five
2:31:10whereas Y is equal to an instance of the
2:31:12S Str class and its value is string now
2:31:15these classes allow us to have built-in
2:31:18functionality in Python now you might
2:31:19might take for granted the fact that we
2:31:21can do something like x + 7 right and
2:31:24obviously we know we're going to get 12
2:31:25when we do that or if we do like x - 6
2:31:28or x / 4 but someone actually coded this
2:31:32functionality into Python and you just
2:31:34can't see it when you're coding here um
2:31:36because it's kind of underneath and I'm
2:31:38going to actually show you a way that
2:31:39you can look at this built-in um code
2:31:43okay so just know that anytime you
2:31:45create a variable and anytime you kind
2:31:47of use any functions like for example if
2:31:49you use uh I don't know say like H is
2:31:52equal to what's something we can do like
2:31:55int y okay I know that's not going to
2:31:57work but whenever you use functions like
2:31:58this someone already coded these
2:32:00functions for you and they're typically
2:32:02built into a class of a certain type
2:32:05when you do something like y. strip you
2:32:08just take for take for granted the fact
2:32:10that you can use this strip method um
2:32:13and this is called a method but someone
2:32:15actually built this in and the reason
2:32:16you can use this is because it's a
2:32:18method on the CL class of St Str if I
2:32:21tried to do something like x. strip well
2:32:23clearly that's not going to work because
2:32:25X is not a string object so just
2:32:28understand that whenever you create
2:32:29anything in Python it is actually an
2:32:31object without you really knowing it or
2:32:33not okay so now I'm going to show you
2:32:36I'm just going to run this in debug mode
2:32:38so I can print some stuff out to the
2:32:39screen okay so I go console and python
2:32:43console okay so I'm using Pym but this
2:32:45works perfectly fine with regular idle
2:32:48in Python okay so if I type this help
2:32:51function and I pick a class and I'm
2:32:53going to pick int like this okay I type
2:32:55help on int you can see it prints me all
2:32:58this stuff now what does this actually
2:33:00mean well once we get to the top you can
2:33:02see it says class int of type object or
2:33:07inherit from object so pretty much
2:33:09whenever we create an integer and the
2:33:11way we create an integer in Python is
2:33:12simply by just saying like x = 5 xal 470
2:33:17whatever like that it automatically
2:33:19defaults this x is a type int and you
2:33:22can see here that it actually tells us
2:33:25exactly how this class works and this is
2:33:27really useful if you don't know
2:33:28documentation you can just type help on
2:33:30something and it'll read out all the
2:33:32class and everything for you so convert
2:33:34a number string to an integer return
2:33:36zero if no arguments are given that's
2:33:38what happens when you call the int
2:33:39function and when you give a base well
2:33:41then it tells you what it does there
2:33:43okay now it says methods defined here
2:33:46well these are actually all the methods
2:33:48that you can use on on the class integer
2:33:52so if you have abs you have ADD and bu
2:33:55seal div mod equal to Flo floor FL TI
2:33:58and I'm going to show you what all these
2:33:59actually mean uh and why they have the
2:34:01underscores on them later on okay so
2:34:04just wait for that you can see that
2:34:05there's a ton of different methods and a
2:34:07ton of functionality already built into
2:34:09this integer class size of string and
2:34:12you can just keep going two bytes and
2:34:15there's a ton and then class methods
2:34:16defined here we're going to talk about
2:34:18what those are later now I want to show
2:34:19you if I do the same thing on string so
2:34:22I say help on oops on S strr like this
2:34:27then we get the same thing except for
2:34:29string and this one is going to be what
2:34:31do you call it a little bit longer
2:34:32because there's more things you can do
2:34:33with string but you can see we have
2:34:34class string and it inherits from object
2:34:38and we'll talk about what all this means
2:34:39later just uh follow along for now
2:34:42create a new string object from a given
2:34:43object uh if encoding and so on okay and
2:34:46then it goes down here add contains
2:34:47equal and you can go through all of the
2:34:49different MTH methods that it has now
2:34:51obviously this is the same for Boolean
2:34:53values and for what do you call it uh
2:34:55floats and all other data types in
2:34:58Python now if any of you have ever seen
2:35:00Turtle before you know that if you
2:35:02create for example import Turtle okay
2:35:06wow I can't type today and you create
2:35:08like Tim
2:35:09equals Turtle do turtle like this what
2:35:13you're actually doing is creating a new
2:35:15instance of a turtle object so in this
2:35:19Turtle module there's a class named
2:35:21turtle and when you call this what's
2:35:23known as a Constructor you're actually
2:35:24creating a new turtle object and you're
2:35:27just naming it Tim or storing it in the
2:35:29variable Tim okay so what else should we
2:35:32talk about with objects here yeah so
2:35:34let's talk about the difference between
2:35:35methods and what do you call it
2:35:38functions quickly so a function everyone
2:35:39here should know is created with the
2:35:42Define key okay or like Define keyword
2:35:45so I say Define Funk give it a value X
2:35:48and we'll simply return
2:35:51x + one now we know to call this
2:35:53function what we can do is we can simply
2:35:55say like print and then Funk of five and
2:36:00we know that that should give us a value
2:36:02of six right because it just simply adds
2:36:04one to what we're giving and this is a
2:36:05function there's no question about it
2:36:07okay now a class is actually what you
2:36:10call with the dot operator so this sorry
2:36:14not a class a method so this dot uh
2:36:16right here turtle. turtle is a method
2:36:19that creates a new turtle object and
2:36:20I'll show you what I mean here so if I
2:36:22do something like print y do uper well
2:36:28this upper right here is not a function
2:36:31it's actually known as a method and it
2:36:33only applies to the class St Str so I'll
2:36:36just print this quickly to show you we
2:36:38get string in all capitals okay if I
2:36:40tried to do something like x. uper and I
2:36:43click the play button here well X has no
2:36:46uh method upper and you can see int
2:36:48object has no ATT upper okay and that's
2:36:51because well X is an object of type int
2:36:54and remember when we typed help on int
2:36:56well we didn't see any upper method
2:36:57there so we actually can't use that and
2:37:00that's why we can only use methods on
2:37:01their correct data types right so if you
2:37:04create a float and you do like uh I
2:37:06don't know like f equals like 5.5 then
2:37:10obviously if we try to do f. uper here
2:37:12this is going to crash as well because
2:37:14well float object has no attribute upper
2:37:17right it had doesn't have that class or
2:37:20that method sorry in it okay so a method
2:37:22is anything you're calling on an object
2:37:24itself right and a function is something
2:37:26that's going to take an object and apply
2:37:28an operation to it right so you can see
2:37:30here that uh five is passed into the
2:37:33function we don't have to do like X do
2:37:36function we just pass it in there and
2:37:38then it returns us a value now methods
2:37:41can return us values as well this one is
2:37:42returning us a value of all capital
2:37:44string it's just different how we
2:37:46actually call those things and they are
2:37:48very similar just the difference is the
2:37:51way that one of them you call on an
2:37:52object and one of them you have to just
2:37:54call and then pass an object through and
2:37:57sometimes these methods can take objects
2:37:59as well for example let's see if I do x
2:38:02dot or F do replace I think that's a
2:38:06thing all I'm going to have to do y.
2:38:09replace y. replace like this okay and
2:38:12then I'm going to pass it a I don't know
2:38:15what should we pass it let's pass it a s
2:38:19and we'll replace it with a blank string
2:38:21okay so we're passing a object of type
2:38:23string and we're passing another object
2:38:25of type string and that's what this
2:38:26method takes as well as we have to call
2:38:29it on the instance right so string so if
2:38:31I call this we get string and six
2:38:34because it simply replaced s with
2:38:37quotations okay so I think I'm going to
2:38:39cut the video here uh I hope you have a
2:38:42little bit of an understanding of what
2:38:44an object is because in the next video
2:38:46we're going to create our own objects
2:38:48and our own class classes uh and we're
2:38:50going to talk about that it just was
2:38:51fundamental that you guys understood
2:38:53this before moving on to the next video
2:38:55okay so that's been it for this video If
2:38:56you guys liked please make sure you
2:38:57leave a like And subscribe and I'll see
2:38:59you again in the next
Creating Classes
2:39:04one hey guys and welcome back to the
2:39:07second video in this object and class
2:39:10tutorial Series in this video we're
2:39:12going to be going over creating your own
2:39:13objects and your own classes and what
2:39:16that looks like in Python so just go
2:39:18ahead and delete this code from the last
2:39:19video uh and start fresh so if you guys
2:39:22remember in the last video we talked all
2:39:24about objects talked about methods uh
2:39:26and that's all cool we kind of already
2:39:27understood a lot of that stuff like
2:39:29we've used those methods it Mak sense
2:39:30how they work uh but now how do we
2:39:33create our own and this is the whole
2:39:34goal of this tutorial series to create
2:39:36your own classes and your own objects
2:39:37and add functionality to your program so
2:39:39to create a new class and this is going
2:39:41to be a new like data type uh something
2:39:43like that we'll talk about that in a
2:39:44second you just simply type class and
2:39:46then whatever you want the name of your
2:39:47class to be so in Python they have in
2:39:49string list whatever okay we're going to
2:39:51create a class and I'm just going to
2:39:53call it dog now you probably have seen
2:39:55this example before if you do anything
2:39:58um what do you call it with like uh
2:40:00classes or objects because people like
2:40:02to use animals but it's a really good
2:40:03example so this is now creating a new
2:40:06class of dog now what we have to do
2:40:09whenever we create a class is we have to
2:40:11uh add some methods into it so remember
2:40:13I talked about methods or things that
2:40:14you call on the actual class itself so
2:40:17I'm just going to type
2:40:18Define nit uncore uncore and we're going
2:40:21to talk about what this does in a second
2:40:22and make sure you don't forget this self
2:40:24keyword uh this object keyword you can
2:40:26leave out if you want I just like to
2:40:27type it because I don't know it's just a
2:40:30habit right because we have class dog
2:40:33Define the NIT and I'm going to make one
2:40:35more method and we're going to call it
2:40:37speak okay and make sure you don't
2:40:39forget this self keyword we're going to
2:40:41talk about what all this does in a
2:40:42second okay pass and in here I'm going
2:40:44to say pass all right so we've now just
2:40:48created our own class
2:40:49it does not do anything but we've
2:40:51created our own class so what we
2:40:54actually do here is we're creating well
2:40:57new class it's equal to dog it inherits
2:40:59from object don't worry about that and
2:41:02this is what's known as The Constructor
2:41:04method now this uncore uncore nit uncore
2:41:08uncore needs to be in most of your
2:41:11classes okay so if you guys want
2:41:12anything to happen initially when you
2:41:14first create a class you need to make
2:41:16this a nit class and what happens is
2:41:18when we create a new object and the way
2:41:20we can do this is by doing something
2:41:21like I don't know Tim equals dog like
2:41:25that this method is automatically going
2:41:28to fire it's automatically going to go
2:41:30off we don't have to say Tim
2:41:34doore netore uncore we don't have to do
2:41:37that whenever we create a new object of
2:41:39type dog this is automatically going to
2:41:42happen so for example if every time I
2:41:45created a new object of dog I wanted to
2:41:46print out uh kn nice you made a dog like
2:41:52this then every time that we do this so
2:41:55if we say like
2:41:57fred equals dog as well then he's going
2:42:00to print this to the screen and I can
2:42:01prove this to you by simply running so
2:42:03he says nice you made a dog nice you
2:42:05made a dog all right we didn't have to
2:42:07call this a nit it just automatically
2:42:08happened whenever we created this dog so
2:42:11let's talk more about this a nit and
2:42:13what we can do with it so in classes
2:42:16there's things called attributes and
2:42:18methods all right methods are anything
2:42:20that you create using Define okay and
2:42:23they look just like functions except you
2:42:25have to call them using uh an object
2:42:28like we talked about in the last video
2:42:30and attributes are kind of like
2:42:32variables that belong to a certain
2:42:35object so to create a attribute you need
2:42:40to use this self keyword all right what
2:42:42this self keyword actually stands for is
2:42:45it stands for the instance that you're
2:42:48calling so him is known as an instance
2:42:51of type dog or of class dog now Fred is
2:42:55another instance of type dog now I'm
2:42:58just going to do this and then hopefully
2:42:59you guys can understand how this kind of
2:43:01works uh because it is somewhat
2:43:03difficult to explain okay so now what
2:43:06we're saying is we've created this
2:43:08initialization all right and we have now
2:43:12that it takes a name okay so what that
2:43:14actually means is we have to now type a
2:43:17name into uh our initial initializations
2:43:19whenever we create a dog we have to give
2:43:20it a name so in here I'm going to give
2:43:22it a name Tim and in here I'm going to
2:43:24give it a name Fred all right and don't
2:43:27worry if you guys are confused I'm going
2:43:28to explain this all in depth in a second
2:43:29okay and I'm just going to do this so
2:43:32that we can actually see what our name
2:43:33is
2:43:34print hi I am
2:43:38comma self. name and we'll talk about
2:43:40what self does in a second okay let's do
2:43:44tim. speak and Fred dope as well okay
2:43:49so let's print this and it says hi I'm
2:43:51Tim hi I'm Fred okay so how does this
2:43:55work so in this initialization this
2:43:57fires automatically when we create dog
2:44:00okay so by calling this we're
2:44:02automatically calling a nit so if we put
2:44:05a parameter in a nit so name that means
2:44:07that we now have to pass a parameter
2:44:09when we create our dog and just like a
2:44:11function we can create multiple
2:44:12parameters we can do like name age color
2:44:16kind we can do as many as we want okay
2:44:18uh and that works fine now what does
2:44:21this self do so this self actually
2:44:24represents the instance right so if I'm
2:44:26if I call dog equals Tim or dog Tim
2:44:30whatever and I set that equal to Tim
2:44:32well Tim is what's being passed into
2:44:34this self parameter here and notice how
2:44:36we have two parameters but we only pass
2:44:37one thing that's because self always
2:44:40needs to be here except in like really
2:44:42specific cases that we're going to talk
2:44:43about later so when we call self. name
2:44:48we're saying that tim. name is equal to
2:44:51whatever name we put in here and that's
2:44:54the same thing with Fred right so when
2:44:56we call it on Fred then we say well
2:44:59fred. name is equal to Fred all right if
2:45:03we didn't do that and we said something
2:45:04like name equals name if I tried to
2:45:07print self. name that would just crash
2:45:10it wouldn't work because we don't know
2:45:12which instance has what name okay so
2:45:15this self means belong it kind of means
2:45:17like belongs to the in that you're
2:45:19calling it on I'm trying to explain this
2:45:20as good as I can because you you really
2:45:22to understand this I'm going to do
2:45:23another parameter here too okay so self.
2:45:26age equals H all right and let's just
2:45:30print this out and I
2:45:35am self.
2:45:37AG years old all right okay so let's
2:45:41print this out and see what we get
2:45:43missing one age sorry I got to put age
2:45:45in here
2:45:4755 3 okay
2:45:49so now if I print this say hi I'm Tim
2:45:51and I'm 55 years old hi I am Fred and
2:45:53I'm 3 years old okay so each of these
2:45:57are instances of class dog and they each
2:45:59have a name and an age and you're able
2:46:01to call the speak method on them now
2:46:04with the speak method does is notice how
2:46:06when I call speak it automatically takes
2:46:08self because it has to know what
2:46:10instance I'm calling it on and that way
2:46:12it's able to access the name and the age
2:46:16now in here you can actually change the
2:46:18name and the age so I'm going to create
2:46:19a new method I'm going to say
2:46:22change like this okay and notice how it
2:46:24takes self no matter what it
2:46:26automatically types it in for me and
2:46:28here I'm going to do H all right now
2:46:30what I can do is I can say self. AG is
2:46:33equal to H and if I call let's see here
2:46:37tim. change age and I give it something
2:46:40like five now if I speak you can see
2:46:44that we get hi I am Tim and I 5 years
2:46:45old and we say hi I am Fred and I am 3
2:46:48years old
2:46:49now obviously you guys can create as
2:46:51many methods as you want you can do call
2:46:53them whatever you want just make sure
2:46:54whenever you create them they first of
2:46:56all have self because we have to know
2:46:57what instance we're calling it on so we
2:46:59can access those attributes all right
2:47:01and you can change these attributes
2:47:03however you like within those methods
2:47:05now I will show you what else you can do
2:47:07okay so say you wanted to access Tim's
2:47:10age and you didn't want to print out
2:47:12like hi I am all this you didn't want to
2:47:14speak you just want to know what his age
2:47:15is well what you can actually do is you
2:47:18can simply print tim.
2:47:21H and now you can see if I run the
2:47:23program we get five down at the bottom
2:47:25and you can do the same thing with name
2:47:27so tim. name and it gives us Tim like
2:47:31that so we're actually able to access
2:47:34the attributes of our object uh from
2:47:38just simply calling whatever that
2:47:39attribute is and in this case its name
2:47:42so whenever you want something to be
2:47:43like public to the uh instance so that
2:47:46they can see it throughout all the
2:47:47methods you need to simp you need to do
2:47:49self. and whatever you want to call it
2:47:51so I could uh say like self dot I don't
2:47:54know let's say Li it's going to stand
2:47:56for list could have a list of like 1
2:47:58three four and now if I call tim. Li you
2:48:04can see we get 134 and obviously if I
2:48:06call friend. Li well this is going to
2:48:09work as well we get
2:48:11134 now the main benefit of classes is
2:48:15that you can create things um you can
2:48:17create multiple objects of a class so if
2:48:20I wanted to store like the name the age
2:48:23and a list for 300 dog objects if I
2:48:26wasn't using classes what I would have
2:48:28to do is I'd have to do
2:48:29like dog one name equals Tim and then
2:48:35I'd have to do dog one age equals 5 and
2:48:38then I'd do dog 2 Age equals Fred and
2:48:40you'd have to continually keep doing
2:48:42that with a ton of variables and
2:48:43obviously you don't want that so these
2:48:45classes allow you to create infinite
2:48:47amount of objects of that type class
2:48:48class and have all of these properties
2:48:50and attributes apply to
2:48:52them okay so what else should we talk
2:48:55about here uh maybe creating new
2:48:57attributes okay so attributes are
2:48:59anything that have this like self. name
2:49:00self. AG self. Li whatever and methods
2:49:03are with these defined Keys okay so if I
2:49:05create a new one and I say Define and
2:49:08add uh what do you call it weight okay
2:49:13say add weight I'm going to give it a
2:49:16weight and then we can say self. weight
2:49:19is equal to weight and what this does
2:49:22now is it creates a new instance
2:49:24attribute um that is going to be applied
2:49:27to this dog class now you see it's
2:49:29giving us an error here it says instance
2:49:31attribute weight to find outside of a
2:49:32nit that's because it wants us to Define
2:49:35that inside of this initialization but
2:49:37it's fine you can actually do it out
2:49:38here so now if I do Tim dot add weight
2:49:42and I give it a weight of like 70 okay
2:49:46and I'll print
2:49:50tim. weight like here let's see what we
2:49:52get we get 70 okay now what if I try to
2:49:56print fred.
2:49:57weight well let's see what happens we
2:50:00get Fred has no attribute weight or dog
2:50:02object has no attribute weight and
2:50:04that's because we have not yet added
2:50:06this by calling add weight on our
2:50:08instance Fred okay so I think I'm going
2:50:11to cut the video off here hopefully you
2:50:13guys have a basic understanding of how
2:50:16classes worked it is kind of difficult
2:50:17to explain it if you guys have any
2:50:19questions leave them down below cuz I
2:50:20feel like I might have butchered the
2:50:21explanation on some of it uh other than
2:50:23that in the next video we're going to go
2:50:25over I think inheritance and creating
2:50:27some more advanced methods and talking
2:50:30about how those work okay so if you guys
2:50:32enjoyed please make sure you leave a
2:50:33like And subscribe and I'll see you
2:50:34again in the next
Inheritance
2:50:39one hey guys and welcome to the third
2:50:42video in my python object and class
2:50:45tutorial series now in today's video I'm
2:50:47going to be going over inheritance and
2:50:49this is an extremely important and a
2:50:50little bit more complicated topic
2:50:52dealing with inheriting attributes and
2:50:54methods from other objects I just want
2:50:56to start off by quickly saying though if
2:50:58you guys are finding the videos a bit
2:51:00too slow for you and uh I'm kind of
2:51:02explaining things a lot of time the
2:51:03reason I do that is because I'm not sure
2:51:05the level of person that's going to be
2:51:07watching the video and I want to make
2:51:08sure that the people that will
2:51:11understand quickly can get the
2:51:12information within like the first few
2:51:13minutes and then the people that really
2:51:15need some extra reinforcement have that
2:51:17opportunity to watch through the entire
2:51:19like 11 or 12 minutes and have it
2:51:21explained to them multiple times so if
2:51:23you guys are finding it's a bit too slow
2:51:25um and you want to move faster just fast
2:51:27forward the video or skip uh to the next
2:51:28one if you guys are done with whatever
2:51:31I've been talking about because like in
2:51:32the last video for example I watched it
2:51:33back and I kind of explain the few uh
2:51:35the same thing a few times that's just
2:51:37for the people that need a bit of extra
2:51:38help and that's kind of what I aim to do
2:51:40with a lot of my videos so that people
2:51:42that need help can you know grab that if
2:51:44they want to okay so let's get started
2:51:47and I'm just going to modify this class
2:51:49just delete Li uh we'll get rid of
2:51:52change age and add weight and we're just
2:51:54going to have name age so on like that
2:51:56okay we can get rid of all this stuff
2:51:57down here now what I'm actually going to
2:51:59do is I'm going to create a new class
2:52:02and I'm going to call this cat okay now
2:52:06cat is going to have very similar
2:52:07attributes as dog in fact let's just
2:52:11copy them over okay so we'll paste them
2:52:13like here we have name age hi I am
2:52:16whatever okay now I'm just going to add
2:52:17one thing and in this case I'm going to
2:52:19add let's see color okay say self. color
2:52:24is equal to color okay now this cat
2:52:27object is obviously going to work the
2:52:28same as dog I don't think I need to show
2:52:30you now just take note of what I just
2:52:32did I just copied all of the stuff from
2:52:35here into cat now if you know anything
2:52:38about programming you know that
2:52:40programmers don't like to copy and paste
2:52:41and that you really shouldn't be copying
2:52:43and pasting code so python has actually
2:52:45come up with a solution to what I just
2:52:47did that allows us to not have to do
2:52:49that and that is called inheritance so
2:52:51I'm going to delete all this and I'm
2:52:52going to go back into cat and I'm going
2:52:54to type dog here now what this little
2:52:56bracket up here means is it means parent
2:53:00so whenever you're talking about
2:53:01inheritance you always have a parent or
2:53:03a super class and then you have a child
2:53:06or a like derived class okay and there's
2:53:09a bunch of different words for them
2:53:10they're not really that important just
2:53:11know parent and child is the most basic
2:53:13okay so in this case cat is going to
2:53:15inherit from dog which means dog is the
2:53:19parent class and cat is the child class
2:53:22and it is derived so it's taken from dog
2:53:25so that means if I do class dog and if
2:53:27all I do in here is just type one thing
2:53:29so I type netore uncore and in here I'm
2:53:33just going to put name age and color and
2:53:38I'm just going to do something that's
2:53:39going to look confusing I'm going to
2:53:40explain it after okay super
2:53:43doore
2:53:45netore uncore and I give it name age say
2:53:50self. color
2:53:53equals color and then I create a new
2:53:56object of cat so I'm just going to say
2:53:58Tim equals cat give it a name agent
2:54:01colors in this case we'll do Tim H5
2:54:05color blue blue cat I know okay so we do
2:54:09that we Press Play No errors okay I'm
2:54:12just going to show you if I do Tim
2:54:15dosp what do you think is going to
2:54:17happen take a guess
2:54:19says hi I'm Tim and I'm 5 years old now
2:54:21how the heck does all this work and what
2:54:23did you just do I know that's probably
2:54:24the question you're asking should
2:54:25probably fix that say color so the way
2:54:27this works is when you inherit from
2:54:29another object or another class sorry
2:54:32you actually inherit all of the
2:54:33properties and attributes and methods of
2:54:35that class so when I do tim. speake you
2:54:39can see well Tim is an object of type
2:54:42cat and Cat well I don't see speak here
2:54:44how does that work well since the speak
2:54:46method belongs already in the dog class
2:54:50and we're inheriting from the dog class
2:54:52it is actually carried on to the cat
2:54:55class and we're able to use it like that
2:54:57okay so Tim dope now what I did here
2:55:00with this super. anit I know this looks
2:55:03confusing but pretty much in the
2:55:05initialization of cat which is unique to
2:55:08cat because it has this added um
2:55:11parameter color I simply called the
2:55:13initialization of dog first which means
2:55:16that when I gave it name and I gave it a
2:55:18age it automatically added self. name
2:55:21and self. AG to my cat object of Tim you
2:55:25see how here I didn't type self. name
2:55:27equals name I didn't type self. AG
2:55:28equals age all I did was call The
2:55:31Constructor method or the initialization
2:55:32method of the superclass and the super
2:55:35class simply means dog um and it did
2:55:39that for me now in this example it seems
2:55:41like well why in the world would you
2:55:43want to do that um that like that
2:55:45doesn't make sense there's not that many
2:55:47lines whatever if you have really long
2:55:49methods and really long classes it makes
2:55:51a lot of sense to inherit so in this
2:55:53example it's hard to show but in long
2:55:55ones you do want to inherit now I want
2:55:57to show you that you can add and
2:55:59overload things from the uh parent class
2:56:04so for example the parent class has
2:56:05speak I'm going to add one more method
2:56:07in here I'm going to say Define let's
2:56:09just say talk okay and in here it's
2:56:12going to
2:56:13say print uh bark cuz it's a dog okay
2:56:18now obviously in our cat class if we
2:56:21want to talk we don't want to bark right
2:56:23and that's what's going to happen right
2:56:24now if we do tim. talk it's just going
2:56:26to say bark so if we wanted to change
2:56:28this we can actually
2:56:29overload this or override this method so
2:56:32if I do Define talk in here I can print
2:56:37and in here I'm not going to do bark I'm
2:56:38going to do meow okay like that little
2:56:42explanation point so now if I do tim.
2:56:45talk like this you can see that we get
2:56:48meow instead of bark and that goes for
2:56:52anything in inherited classes okay so
2:56:55anything that we do in here is going to
2:56:58override whatever is happening in this
2:57:00dog class so say we wanted to change
2:57:02speak we could say Define speak in here
2:57:05and then whatever we put in this speak
2:57:06method would overwrite what's in here
2:57:09now that's not to say that it's going to
2:57:11change if you create a dog dog object so
2:57:12if I do like I don't know let's say gym
2:57:16is equal to dog give it a name give it
2:57:20gy clearly age this an old dog we're 70
2:57:23okay and we do jy. talk well we're still
2:57:27going to get if I spell JY correctly
2:57:29bark right so we get bark and meow so we
2:57:32don't actually change anything in this
2:57:35class we just say that well if you
2:57:36create a class or if you create a cat
2:57:38object then it's going to have this talk
2:57:41method which overrides the talk method
2:57:42that we created in dog and this is the
2:57:44same if you do something with uh like
2:57:46these attributes so if I didn't like
2:57:48self. name and I just statically type in
2:57:51a name like I type uh Tech okay uh then
2:57:55when we do
2:57:57tim. speak now you would think that I
2:58:00it's going to get hello I am Tim because
2:58:03that was the name right and I am 5 years
2:58:05old but instead we're going to get Tech
2:58:07so it says hi I am Tech and I am 5 years
2:58:10old and that is because although we set
2:58:12name up here to be equal to whatever
2:58:14name we type in the in as a argument uh
2:58:17we are changing it to Tech and that
2:58:18overrides whatever happens in this dog
2:58:20class okay uh so I hope that makes sense
2:58:24now what else can we override from those
2:58:26classes well we can inherit like
2:58:28multiple times and we can inherit
2:58:30different classes from upper class I'm
2:58:32going to talk about the way you would
2:58:33want to do an inheritance okay so the
2:58:35way you typically want to inherit is you
2:58:37want to have one really General class
2:58:39that kind of applies that's a bit
2:58:41smaller that applies like a bunch of
2:58:43different classes so I'm going to delete
2:58:44these and I'm just going to type out
2:58:46them and show you an example of what I
2:58:47mean by General class so if I created a
2:58:49class and I called it vehicle like that
2:58:53okay is that how you spell vehicle I
2:58:55don't care we're just going to leave it
2:58:56like that okay and then I created an
2:58:59initialization in here okay and the
2:59:01initialization is going to take like uh
2:59:04price uh gas and color okay I know these
2:59:08are random but just just follow along
2:59:10self. price equals price self do gas
2:59:13equals gas and self
2:59:15do color equals color okay now I'm I'm
2:59:18just going to give it a method I'm going
2:59:19to call Define fill up tank like this
2:59:23okay and what's to say self. gas equals
2:59:27and we're just going to say that each
2:59:28gas tank has a maximum of 100 okay say
2:59:32Define um empty tank and this is just
2:59:36going to say self. gas equals z and
2:59:40we're going to say
2:59:42Define gas left and this is simply going
2:59:45to return self. gas okay
2:59:49and the reason I have this is because
2:59:51well in all vehicles we're probably
2:59:52going to want to have um gas right all
2:59:56of our vehicles are going to have gas if
2:59:57they're not electric obviously we're not
2:59:59going to deal with that for now um so
3:00:00fill up tank empty tank and gas left is
3:00:03probably something we want to know for
3:00:04all of our different Vehicles now for
3:00:06example if I create another class and I
3:00:08say class car okay and car is going to
3:00:12inherit from vehicle like that okay and
3:00:15all we're going to do for the init is
3:00:17we're just going to
3:00:18call the vehicle init so how we do that
3:00:21is do super doore uncore anitore uncore
3:00:25and obviously this needs now a
3:00:29price gas and a color so we're going to
3:00:33pass it price gas and color like that
3:00:38and then that's our initialization and
3:00:40in car all we're going to have is we're
3:00:42going to have a method and it's going to
3:00:44call
3:00:45let's actually let's add one more thing
3:00:48here speed okay self. speeed
3:00:52equals speed okay and then here as a
3:00:56method let's just have like beep okay
3:00:59and all this is going to do is simply
3:01:02print beep beep okay now I know these
3:01:05aren't great examples but just follow
3:01:07along so what we have now is we have the
3:01:08vehicle class okay and it has these
3:01:11things that do with gas and obviously
3:01:13gas is going to work for all of our
3:01:14different types of vehicles and and then
3:01:17we create a car class class that
3:01:18inherits from vehicle and in here all it
3:01:20does is it has an extra attribute called
3:01:21speed and it has a method called beep
3:01:24now if I create another class instead of
3:01:27car I want to do truck now truck is not
3:01:31going to say beep beep it's going to say
3:01:34honk honk like that okay and instead of
3:01:37having self. speed we're going to have
3:01:40self. tires okay equals tires because
3:01:44that's a a property that we're going to
3:01:45care about for our trucks because
3:01:49sometimes they get like six tires 18 you
3:01:51know what I mean that's what I mean by a
3:01:52truck okay so you can see that we have a
3:01:54really General class vehicle that simply
3:01:56has a cut a gas and a price and
3:01:59obviously within our truck and car class
3:02:01we're going to care about those
3:02:02attributes so price gas color but in
3:02:05truck we care about tires and when we
3:02:07click beep we want to say hwk hwk and
3:02:10for car we care about the speed that the
3:02:12car can go and when we uh call beep we
3:02:16want to print beep beep
3:02:18okay so that is an example of where you
3:02:21would use inheritance I know this is a
3:02:23really small example a lot of the
3:02:24classes you're going to end up building
3:02:26are going to be like massive and inherit
3:02:29from uh like other classes and you're
3:02:31going to have like large files full of
3:02:33classes but this is the best example I
3:02:34can kind of give you of when inheritance
3:02:36would be useful and same thing if I
3:02:38wanted to do like Define motorcycle then
3:02:41you could do the same thing there and
3:02:42for Wheels you do like self Wheels
3:02:43equals 2 like stuff like that okay so I
3:02:47think I'm going to end the video here
3:02:49hopefully you guys have an idea of how
3:02:50inheritance works one quick thing if I
3:02:52wanted to I could inherit from car as
3:02:55well so if something is inheriting from
3:02:56something you can also inherit from it
3:02:58so that would mean that now truck has
3:03:00all the attributes of car and all the
3:03:01attributes of vehicle because well car
3:03:03has the attributes of a vehicle so since
3:03:05car has it we're inheriting from car
3:03:07truck is going to get it as well just
3:03:08wanted to add that in before we end
3:03:09anyways make sure you guys leave a like
3:03:11And subscribe and I will see you again
3:03:12in the next video
Overloading Methods
3:03:18hey guys and welcome back to another
3:03:20object and classes tutorial video in
3:03:22today's video I'm going to be going
3:03:24overloading default python methods and
3:03:27why these are extremely useful now if
3:03:28you don't know what I'm talking about
3:03:29just stick around for one second and I
3:03:31will explain it so first of all I just
3:03:33want to talk about what I've already
3:03:34coded here I just have a very basic
3:03:35point class and you can see that our
3:03:37Point object has three kind of
3:03:39attributes in x y and then our
3:03:41coordinates which is self.x and self.y
3:03:43we have a very basic method that can
3:03:45simply move us by x and by y we type it
3:03:48in okay I've created four points down
3:03:50here and these are what we're going to
3:03:51use to kind of test out some examples
3:03:52that I'm about to create so in Python uh
3:03:55remember in my first video I was talking
3:03:57about the fact that if you create
3:03:58integer objects so for example I say
3:04:00like IAL 5 and like zal five if I wanted
3:04:05to add these together all I simply have
3:04:07to do is well put a plus sign right and
3:04:09python knows that this plus sign means
3:04:11Okay add these two things together now
3:04:13how does it know this and how does it
3:04:15know what to do well with integers is
3:04:17pretty straight straightforward but what
3:04:18if we start doing things like strings
3:04:20right if you do s and then you go like 6
3:04:247 8 how does python know how to add
3:04:27these two strings together well it would
3:04:29make sense to it just appended on top
3:04:31but someone actually had to code this
3:04:33functionality in and that's exactly what
3:04:35we're going to be doing in this video
3:04:36except with our custom Point object okay
3:04:39and you'll see what I mean in just a
3:04:40second so pretty much what we want to
3:04:43eventually do at the end of this video
3:04:44is be able to add multiply subtract um
3:04:48and compare points without having to
3:04:51reference their attributes outside of
3:04:53the class so right now uh if I do P1
3:04:56plus P2 our program is going to crash
3:04:57because it doesn't know what that means
3:04:59but by the end of this video we're going
3:05:00to be able to do that okay so that's
3:05:02what I'm going to show you right now so
3:05:04in Python there's a bunch of default
3:05:07kind of operations and methods that you
3:05:09can apply on classes and by default uh
3:05:13they're not defined right on our point
3:05:14class if we try to add to things right
3:05:15now that doesn't make sense so what
3:05:17we're going to is add that method so to
3:05:19do this to add the add operation you're
3:05:22just going to do Define underscore add
3:05:25okay and then in here you need to give
3:05:27another Point object so what happens
3:05:30when I try to add a point and another
3:05:33Point object well whatever we're going
3:05:36to return a new Point okay that is
3:05:40simply equal to self.x plus p.x and then
3:05:44self.y + p.y so what this is doing is
3:05:47since we're passing another Point object
3:05:50so when we do like P1 + P2 P2 becomes p
3:05:54and P1 becomes self so we're going to
3:05:57grab the coordinates of P1 add them to
3:05:59P2 and return that in a new Point object
3:06:02okay now the same works for subtract
3:06:06multiply and Division and for a few
3:06:08others as well so I'm just simply going
3:06:10to copy this okay and paste it one more
3:06:12time and instead of add now you could
3:06:14probably guess I'm going to put sub and
3:06:17what this is going to do is allow us to
3:06:19subtract points so it's going to be the
3:06:21exact same except now we're simply going
3:06:24to subtract the coordinates now with
3:06:27multiplication this one is simply Define
3:06:29underscore uncore Moore underscore and
3:06:32this going to allow us to use the ASX or
3:06:34the star uh to multiply two different
3:06:36point objects so same thing it takes a
3:06:38point and in this case when we multiply
3:06:41points I could return a new point with
3:06:42multiplied coordinates but the way it
3:06:44actually works if you know anything
3:06:45about vectors is we're simply going to
3:06:47return the scalar product which means
3:06:49you multiply the first two x coordinates
3:06:53get a value in this case we would get
3:06:54nine and then you actually add it to the
3:06:57y-coordinates multiplied together so in
3:06:58this case we'd have nine um what do you
3:07:01call it plus 8 and that would give us
3:07:0317 we're not returning a new Point
3:07:05object it's a scaler or like just a
3:07:07number value okay so what we're going to
3:07:09do here is we're going to return
3:07:11self.x multiplied by p.x plus self.y
3:07:16multiplied by
3:07:18p.y and those are the three that I'm
3:07:20going to stick with right now and I'm
3:07:21going to show you a bunch more that we
3:07:23can use to compare Point objects using
3:07:25greater than equal then sign in just a
3:07:26second so I want to test if this is
3:07:28working so let's create a new point I'm
3:07:29going say 0 five is equal to p1+ P2 and
3:07:34I'm going to say 6 is equal to P4 minus
3:07:38P1 okay and then we'll even go as far as
3:07:41creating p7 and multiplying P2 and P3
3:07:46just to make sure everything's working
3:07:47so now I'm simply going to print to the
3:07:50screen P5 P6 and p7 and let's see what
3:07:54we get so you can see here I get main
3:07:58point object main point object and nine
3:08:02now the way that this worked and the
3:08:04reason that we're not actually getting a
3:08:06coordinate value is because I have to
3:08:07show you another method that we can use
3:08:09so that this was actually going to give
3:08:10us something meaningful because right
3:08:12now when we point print out our Point
3:08:13objects right cuz 0 5 is a new Point
3:08:16object cuz when we add point1 and point
3:08:182 we have returned sorry a new Point
3:08:21object right so if we want to make this
3:08:25meaningful and not just show us the
3:08:26address in memory uh where the point is
3:08:28stored which it currently is right now
3:08:30we need to add another method and this
3:08:32one is called Str Str okay and what this
3:08:36is going to do is this is going to be
3:08:38called every time we try to convert our
3:08:40Point object into a string so when we
3:08:43try to print our Point object it
3:08:45automatically looks for Str Str and and
3:08:47if it doesn't find it it gives us this
3:08:49kind of uh gibberish right here but if
3:08:51it does find it it's simply going to
3:08:53well use the value that we returned so
3:08:55in this case we want to return I think
3:08:58we'd want to return from string uh
3:09:00probably the self dot coordinates right
3:09:02so I want it to look something like this
3:09:04we have brackets and then Plus St Str
3:09:09and then
3:09:09self.x plus a comma Plus St Str
3:09:16self.y and plus and then another bracket
3:09:19like this okay so now if I try to print5
3:09:226 and 7 you can see what we get and
3:09:25there we go so now instead of getting
3:09:26that gibberish we get 66 -3 and -3 now
3:09:30keep in mind you can make this anything
3:09:32you want but you do have to return a
3:09:34string value here for this to
3:09:37work okay so the next methods we're
3:09:39going to talk about I'm just going to
3:09:40put them above string here just for good
3:09:43practice is going to be comparing two
3:09:45points so this is great now we can add
3:09:46we can subtract we can multiply but what
3:09:49if we want to compare so what if we want
3:09:51to see if0 one is greater than 2 or if 3
3:09:54is greater than point 4 well how do we
3:09:55do that we first have to determine how
3:09:59are we going to compare points are we
3:10:01simply going to compare the x's and the
3:10:03Y's or are we going to find like the
3:10:05magnitude of a point like what are we
3:10:07going to do to compare which is
3:10:09larger in my case I want to find the
3:10:12length of a point from the origin so in
3:10:14this case the way this would work um I
3:10:17think I can just bring up a little grid
3:10:18program here and just draw it for you
3:10:21quickly so if you have an origin like
3:10:24this okay I'm just using my mouse right
3:10:25now and you had a point here and a point
3:10:27here well from the origin this would
3:10:30have a distance and from the origin this
3:10:32would have a distance obviously this
3:10:33could be like a distance of seven this
3:10:35could be a distance of two and seven
3:10:37would win right because we don't really
3:10:38care about the negatives if you had
3:10:40something all the way over here with a
3:10:41larger distance then well we would want
3:10:43that to win right um so that's the way
3:10:45that we're going to compare them and
3:10:46I'll talk about that and how we do that
3:10:47in one second so to compare we have four
3:10:52major comparisons I think that we can do
3:10:54anyways I'll type them and we'll see so
3:10:56one of them is greater than and that is
3:10:59GT okay and then you can do self and you
3:11:02also need another Point object like this
3:11:05another one is greater than or equal to
3:11:07and greater than or equal to is simply
3:11:09GE okay and then same thing we need P we
3:11:13have less than so Define and you can
3:11:16probably guess LS or sorry LT what am I
3:11:20saying less
3:11:21than and then we also have less than or
3:11:24equal to so l e like
3:11:28that and we have one more and this is
3:11:30simply equal to so this is if we do the
3:11:32double equal sign then it's going to
3:11:34give us a comparison now in these
3:11:36methods we need to return a true or a
3:11:38false value so in greater than remember
3:11:41the way I said I wanted to compare them
3:11:43is to get like the magnitude of the
3:11:45point from the origin so I'm just going
3:11:46to add a method in here I'm just going
3:11:48to call it uh let's do length maybe okay
3:11:53and then in here I'm simply actually we
3:11:55don't need to take anything we're just
3:11:57going to
3:11:58return the math.sqrt and I'm going to
3:12:02import math right up here and this is
3:12:04simply how you get the length from the
3:12:06origin okay
3:12:08of
3:12:09self.x to the power of two
3:12:14plus what am I doing self.y
3:12:17the exponent to and this is going to
3:12:18give us the length Okay so import length
3:12:21so when I'm doing greater than I want to
3:12:23see if return self.x or what am I saying
3:12:29self. length is greater
3:12:32than P do length like this okay and I
3:12:35believe we do actually need these square
3:12:37brackets here so all this is going to do
3:12:39is it's a Boolean condition it says well
3:12:40if the length of ourself is greater than
3:12:43the other length well then we're simply
3:12:44greater than so let's just copy this and
3:12:46put it in here and in this case we're
3:12:48just going to do greater than or equal
3:12:49sign because this is greater than or
3:12:51equal to we'll copy this again change
3:12:53this around to less than and one more
3:12:56time and this is now just going to be
3:12:58less than or equal to now if we're
3:13:00seeing if two things are equal to each
3:13:01other well that's pretty easy as well
3:13:04all we can do is simply take this or
3:13:07actually the way we're going to do this
3:13:08is just see if the coordinates are the
3:13:10same because if we try doing the math.
3:13:12square root and we get like a large
3:13:13decimal number sometimes decimals kind
3:13:15of mess up in Python and they don't get
3:13:17the same Precision so let's just
3:13:20return if self.x equal equal p.x and
3:13:25self.y equals p.y because that would
3:13:28mean that they are the same points right
3:13:30okay so let's do some comparisons now so
3:13:32let's just simply print if P1 equals
3:13:36equals P2 and then we'll print if P1 is
3:13:41greater than P2 and we can print if P4
3:13:45is less than or equal to
3:13:48uh P3 okay and just test these out okay
3:13:51so we get false true and true and you
3:13:53guys are welcome to mess around with
3:13:55these and kind of figure those out for
3:13:57yourself so I will show you I'm not
3:13:59going to go through all of the methods
3:14:01because there's a lot of ones that you
3:14:02can overload but I'll show you how you
3:14:03can have a look at all of the different
3:14:06methods okay so I'm just going to pull
3:14:07up a web page here that has like a large
3:14:10list of all of the kind of built-in
3:14:12methods that you can override or like
3:14:14what do you call it overload um there is
3:14:16a way in Python to like see all of them
3:14:19and print them to the screen but I
3:14:21completely forget the command so I
3:14:22apologize one of you guys knows that
3:14:23command please let me know in the
3:14:24comments because I've been looking for
3:14:26it and I can't find it but anyways you
3:14:28can see here there's a ton like I think
3:14:31there's like a hundred or something of
3:14:32built-in ones you can use like ad sub Mo
3:14:34div true div Flor div can read through
3:14:37here and see what they all do some
3:14:39useful ones I will mention is probably
3:14:41Len I probably should have talked about
3:14:43that one but it's if you call the Len
3:14:45function on
3:14:47something so actually I'll show you Len
3:14:49really quickly just because I feel like
3:14:50I should have talked about so instead of
3:14:51doing length here I could have just done
3:14:54Define underscore uncore lencore uncore
3:14:58and what this would have done now is if
3:15:00I call like Len of P1 it's just going to
3:15:04return uh whatever value I have here so
3:15:06math. square root okay that's like a
3:15:08really useful one too so I don't know
3:15:09why I didn't talk about that but anyways
3:15:11so we go back to this page for one
3:15:12second and see real numbers slice slice
3:15:16can be useful if you're do deal with
3:15:17kind of like list objects that you're
3:15:19creating items and slices you can go
3:15:21through all these and kind of read them
3:15:23yourself I'm not going to talk about all
3:15:24of them CU I'm sure these you guys
3:15:25probably won't end up using very much um
3:15:27the ones that are very important are the
3:15:29ones I talked about yeah so you can see
3:15:30built-ins like there's something you can
3:15:32do with like dir built-ins to see them
3:15:34all uh and yeah I'll leave this link in
3:15:37the description in case any of you guys
3:15:38are interested in having a look at these
3:15:40but there is some way in Python again if
3:15:42you know that way please let me know
3:15:44okay guys so that's been it for this
3:15:45video in the next video I'm going to be
3:15:47talking about static and class methods
3:15:50and then in the future video I'm going
3:15:51to be talking about private private and
3:15:53public classes in Python so if you guys
3:15:56enjoyed the video please make sure you
3:15:57leave a like And subscribe and I'll see
3:15:58you again in the
Static Methods and Class Methods
3:16:03next hey guys and welcome back to the
3:16:06fifth video in my python objects and
3:16:09classes tutorial Series in today's video
3:16:11I'm going to be talking about class
3:16:13methods static methods and class
3:16:16variables now these are very important
3:16:18and very useful especially if you're
3:16:19coding large projects with multiple
3:16:21different classes and they are fairly
3:16:24complex but I'm going to try my best to
3:16:26explain them right now so pretty much
3:16:28you can see here I've already set up a
3:16:29class and I've already actually coded
3:16:31all the methods and class variables and
3:16:33all the stuff we're going to deal with
3:16:34and I'm just going to go through and
3:16:35talk about it because it just saves a
3:16:37bit of time and makes things a bit
3:16:38easier so first of all let's talk about
3:16:40class variables Now to create a regular
3:16:43variable within a class you can see that
3:16:46we have something like self. name equals
3:16:47name we've been over this we know how
3:16:49this works now when you create a class
3:16:51variable typically you do this at the
3:16:53top of the class and you just simply
3:16:56write like a variable just not inside of
3:16:58one of your methods so you can see up
3:17:01here I have a list says dogs equals and
3:17:04then a list if I wanted to have
3:17:05something like X I could say xal 5 or x
3:17:08equal 5 and I could put that at the top
3:17:11of my uh what do you call it class and
3:17:14then to reference that you have to be
3:17:16inside of the class so this sometimes is
3:17:18useful if you want to have variables
3:17:20that every object in your class is going
3:17:22to need to use and you don't want to
3:17:24have to do like self.x equal x within
3:17:27your initialization and it's just better
3:17:29practice to put variables that are going
3:17:32to be like statically uh used inside of
3:17:36the class if that kind of makes sense so
3:17:38the way that you reference these
3:17:40variables is the same as you would
3:17:41reference uh an attribute within your
3:17:44regular class so you can see here like I
3:17:46do self name equals name to create a new
3:17:48variable self. name when I want to
3:17:50reference it later I would have to type
3:17:52self. right it's the same way to
3:17:54reference these class variables so you
3:17:56can't or I guess you could but you
3:17:58wouldn't want to have um the same name
3:18:01as this like in here and if you did that
3:18:02it would just overwrite this uh so
3:18:05you'll see what I mean in just a second
3:18:06but anyways what I'm essentially doing
3:18:08here inside of my initialization is I'm
3:18:10appending every single dog object that
3:18:13we create into the list dogs now this
3:18:17list dogs be begins to the class dog not
3:18:20to the actual instance of dog so Tim and
3:18:23Jim if I reference dogs and I print that
3:18:26out will be the same value it doesn't
3:18:28change dogs or like dogs is not specific
3:18:30to Tim and it's not specific to Jim it's
3:18:33specific to the entire class uh for
3:18:35every dog object so it's the same for
3:18:38all that an XC like five is going to be
3:18:41the same for obviously Tim and Jim and
3:18:43all other dog objects I hope that kind
3:18:46of makes sense we'll walk through it in
3:18:47just a second okay so let's just first
3:18:50of all just print and figure out like
3:18:52what's the point of this uh class
3:18:54variable like what can we do with this
3:18:55well first of all the good thing about
3:18:58class variables is if you want to access
3:19:00them you don't have to have an instance
3:19:03of the dog object to do so so to see
3:19:07this typically like if I wanted to call
3:19:08so if I remove this class method I
3:19:10remove the static method you want to
3:19:11call method on a dog object you'd have
3:19:13to do the name of the dogs like Tim and
3:19:15then Dot bark or Tim do uh I don't know
3:19:19whatever else that we have like do add
3:19:21do add weight whatever you want to do
3:19:23okay but in this case we can just
3:19:25actually call it on the name of the
3:19:26class so if I do dog Dot and then you
3:19:30can see it's already coming up here do
3:19:31dogs like this we should be able to
3:19:33print this to the screen and you can see
3:19:35that we do end up being able to print
3:19:37this to the screen with a dog object and
3:19:39another dog object so the main
3:19:41difference here with class variables is
3:19:43that you can call them with you can
3:19:45still call like I could still do tim.
3:19:48doogs but it I can also call them with
3:19:51the actual name of the class and that's
3:19:53what makes it useful so you don't have
3:19:55to have an instance uh to call that what
3:19:58do you call it variable if that makes
3:20:00sense and I guess that's all I'm going
3:20:02to explain kind of for class variables
3:20:03because they're not that complicated and
3:20:04if you play around with them you should
3:20:05be able to figure that out figure them
3:20:07out okay so next we're going to talk
3:20:09about static and class methods now you
3:20:12can see here I have at static method and
3:20:14at class method these are known as
3:20:17decorators and you put these above your
3:20:20method like directly above them like
3:20:22this if you want to indicate that they
3:20:23are going to be a special type of method
3:20:25and that's what these two methods are so
3:20:27I'll start I'll first start talking
3:20:29about class methods because we kind of
3:20:30already have touched on it with class
3:20:31variables so the way that a class method
3:20:34works is that you can actually call it
3:20:36on simply the name of the class so you
3:20:38can see inside of here uh my parameters
3:20:41I only have one thing and it's called
3:20:43CLS it's not called self it's called CLS
3:20:46what this this means is the name of the
3:20:48class so if I remove Tim and Jim so I
3:20:52guess I can just comment that out like
3:20:53that whatever then what I can do is I
3:20:55can say dog Dot and then what do you
3:21:00call it numb dogs like this and this
3:21:02will actually work and you can see that
3:21:04I'm not calling it on an instance or
3:21:06like of the dog class or like a object
3:21:09of dog class I'm just simply calling it
3:21:11on the class so if I do this you can see
3:21:14that it gives me zero and that that
3:21:16works right now again you can call it on
3:21:21uh what do you call it like you can call
3:21:23it on an instance like if you say Tim
3:21:25equals dog and name Tim I can still call
3:21:29it on Tim and this is still going to
3:21:31work fine it's just that you don't need
3:21:34to do that right you can do it by just
3:21:37calling with the name now static methods
3:21:40are a little bit different they actually
3:21:42don't need the class to uh to be called
3:21:46so so it doesn't pass in the class so
3:21:49that means that you can't reference
3:21:50anything within the class so the whole
3:21:51point of having this class passed in
3:21:53when you call this method is so that you
3:21:56can use class variables and you can use
3:21:59other methods with in with inside the
3:22:01class so for example if within numb dogs
3:22:03I wanted to use a static method or I
3:22:05wanted to call an initialization or I
3:22:06wanted to do something that revolved
3:22:08around the dog class I would need the
3:22:10class name right so that's why it's
3:22:12passed through but with static methods
3:22:14we only pass whatever parameters we want
3:22:16so we don't have to have a self we don't
3:22:18have to have a class and we don't even
3:22:20have to have any parameters but in this
3:22:22case I'm giving one parameter n so I'm
3:22:25going to show you how this works so I've
3:22:27completely removed any instances of dog
3:22:29in my program there's no objects we're
3:22:31not printing anything whatever okay what
3:22:34I'm going to do now is I'm going to call
3:22:35Bark without passing through any object
3:22:39or any class name and you can see how
3:22:41this works so what I have to do to do
3:22:43this is it has to know obviously where
3:22:45bark is right so it's in inside the dog
3:22:47class so I have to start by typing dog
3:22:48but then I can type bark like this and
3:22:52give it a number and you can see here I
3:22:54just wrote a little comment barks's end
3:22:55time so if I run this you can see we get
3:22:57bark bark bark bark now notice that with
3:23:00inside this bark class I don't do
3:23:02anything like I don't touch any
3:23:04attributes I don't touch any class
3:23:05methods and there's actually no way for
3:23:07me to do that so if I were to try to say
3:23:10I wanted to get the value of the length
3:23:12of dogs right with inside this static
3:23:14method well I would be unable to do that
3:23:16because I don't have self right self is
3:23:20it's not working I don't have a class so
3:23:22how am I going to be able to call a
3:23:24class um variable or another class
3:23:27method I actually can't do that so the
3:23:29point of static methods is when you're
3:23:31just going to be using them as a
3:23:32function but you want to organize them
3:23:34within a class so a really good uh use
3:23:37of static methods for example is say you
3:23:39were created like a class and you called
3:23:42it math okay in here instead of having
3:23:45like an initialization ation and all
3:23:47this stuff you just had a bunch of
3:23:48static methods now why would why do you
3:23:50put them in this math class why not just
3:23:52create them as functions well the thing
3:23:53is you want to be able to import modules
3:23:56within python you guys have probably
3:23:58seen this before right you can import
3:24:00other files that you've created into
3:24:01like main files so when you create a
3:24:03bunch of um like math methods or
3:24:06whatever what You' do is you'd say class
3:24:09math and then you just say like at
3:24:12static method and then you do Define and
3:24:14you'd say like add like this okay and
3:24:17you say like X and X2 and then you could
3:24:20return x +
3:24:22X2 and that way you don't have to create
3:24:25an instance of math you don't have to
3:24:27say like m equals math right you can
3:24:29just say math. add give it two values
3:24:31and it will return it to you and you can
3:24:33do math. subtract and you can organize
3:24:35all like the functions that you wanted
3:24:36to use within that class and then class
3:24:40methods right are more used for like if
3:24:41you want to access class variables
3:24:43without having to pass in an object
3:24:45because it's just going to automat pass
3:24:46whatever the class name is that you're
3:24:48giving it I hope that that makes sense
3:24:51now these I'll touch on these really
3:24:53quickly in case I didn't really talk
3:24:54about them these are known as decorators
3:24:57um I know it's kind of a weird name but
3:24:58pretty much this just denotes the fact
3:25:00that you are creating a class method and
3:25:04you are creating a static method because
3:25:05otherwise it's probably going to get mad
3:25:07at you and tell you that you need to
3:25:08type in self or you need to type in like
3:25:10another parameter or something like that
3:25:11so by doing this not only is it a visual
3:25:13representation for anyone that's reading
3:25:15your code but you're also so just um
3:25:18telling python that this is going to be
3:25:19a static method this is going to be a
3:25:20class method obviously you can have as
3:25:22many static methods as you want as many
3:25:23class methods as you want and they're
3:25:25actually really useful so anyways I'm
3:25:27going to wrap up the video here if you
3:25:29guys have any questions about any of
3:25:30this stuff make sure you leave a comment
3:25:32down below and in the next video I'm
3:25:34going to be talking about public and
3:25:35private classes in Python anyways if you
3:25:38guys enjoyed the video please make sure
3:25:39you leave a like And subscribe and I
3:25:41will see you again in the next one
Private and Public Classes
3:25:47hey guys and welcome to the fifth and
3:25:49final video in my python classes and
3:25:52objects tutorial Series so in today's
3:25:54video I'm going to be talking about
3:25:55public and private classes and how to
3:25:58import your own classes from other files
3:26:01into like a main file uh you'll kind of
3:26:03see what that really means as we move
3:26:05along so the first thing to talk about I
3:26:08guess would be what is the difference
3:26:09between public and private so I've
3:26:11brought an example here I've just kind
3:26:13of written this random example in Python
3:26:16to try to illustrate this to you so in
3:26:17other programming languages there's
3:26:19things known as private and public
3:26:21classes now private classes typically
3:26:23can only be used within the same file or
3:26:26within like a certain scope where public
3:26:29classes are accessed or can be accessed
3:26:32by everyone so private kind of means
3:26:34within something like it's not
3:26:36accessible outside of that whereas
3:26:38public means it's accessible everywhere
3:26:41now I have just a definition I found on
3:26:42Google that I'm just going to read
3:26:44because it's the best way to kind of
3:26:45explain it and I can talk about qu it
3:26:46after so in terms of java and we'll talk
3:26:48about this in a second uh public versus
3:26:51private class and this is the same for
3:26:52most languages means this so public is a
3:26:54Java keyword which declares a member's
3:26:56access as Public public members are
3:26:58visible to all their classes this means
3:26:59that any other class can access a public
3:27:01field or method further other classes
3:27:04can modify public Fields unless the
3:27:06field is declared as final okay so what
3:27:09does that all mean and why did I just
3:27:11give you an example in Java well in
3:27:13Python there's actually no such thing as
3:27:16private or public classes there is
3:27:19conventions that we use to declare like
3:27:22a pseudo like private or like a public
3:27:25class um but there's not really anything
3:27:27restricting us from using private
3:27:29classes so hopefully I can explain this
3:27:33but pretty much this first class up here
3:27:35is private and what makes this private
3:27:37is the fact that we have one underscore
3:27:40at the beginning of the class name now
3:27:43in python or in Java for example when
3:27:45you create a class you would do like
3:27:46like you could do something like this
3:27:48like public static class and then that
3:27:51would mean that this is a public class
3:27:53you could say like private class now in
3:27:55Python we don't have those so the way
3:27:57that we Define this just as a convention
3:28:00is you start with one underscore and
3:28:02then the class name now to create a
3:28:04public method or like attribute of a
3:28:07class it's the same thing you just use
3:28:09one underscore so for example you can
3:28:11see I have two display methods here one
3:28:13is a private method as it has an
3:28:15underscore and the the other one is
3:28:16public as it doesn't have an unders
3:28:18Square again like I've said it doesn't
3:28:21actually matter like you can still use
3:28:23this the regular way you'd use method by
3:28:24calling doore display but by putting
3:28:26this underscore here you're essentially
3:28:28telling other programmers and even maybe
3:28:30yourself later on in the future not to
3:28:32use this and you're saying this is
3:28:34private I don't want you to use it
3:28:36obviously if they decide to use it
3:28:38that's their decision but it's just
3:28:39telling you like don't mess with this
3:28:41don't use this it's private for a reason
3:28:43and typically when you private things
3:28:45that's because you don't want other
3:28:46classes to be able to use it and like
3:28:48mess with the class or do anything like
3:28:50that so same thing here when you create
3:28:51an entire class as private that means
3:28:54everything within the class is also
3:28:56private so I'm going to go over to this
3:28:58file now and you can see I have one file
3:29:00mod and one file tutorial one um now mod
3:29:02is this one that we just went through
3:29:04tutorial one is a new file and you can
3:29:05see that I'm actually importing mod uh
3:29:08so I'm importing this file now when I do
3:29:11that what that does is Imports all of
3:29:13the classes and functions that are
3:29:15within this file so for example if you
3:29:17wanted to organize your program you
3:29:19would have a bunch of different files
3:29:21that contain a lot of different classes
3:29:23and you would import them by simply
3:29:25typing this at the beginning of like
3:29:26your main script this way you can keep
3:29:28everything organized clean and neat and
3:29:30then you can see from Mod I'm importing
3:29:32not private so the way that this works
3:29:34in terms of importing individual classes
3:29:37is you type the name of the Python file
3:29:38in this case mod and then the name of
3:29:41the class or function that you wanted to
3:29:43import and in this case I want to import
3:29:44not private so I can simply do that now
3:29:47say I remove this line I say import from
3:29:50Mod import not private I'm still able to
3:29:52access not private just in a different
3:29:53way you can see now this goes red the
3:29:55way I would now do this is I'd say Mod
3:29:57Dot not private because it's saying
3:29:59we're looking within the mod file for
3:30:01the class or function not private but
3:30:04it's just easier to do it this way so
3:30:05that you don't have to type Mod Dot
3:30:07before all that stuff okay so I'm just
3:30:09going to show you this working I guess I
3:30:12have uh this not private class you can
3:30:14see we have two displays
3:30:16and when I run it we get high now I also
3:30:19want to show you the fact that although
3:30:20I told you that this class is private
3:30:22and that this method is private that we
3:30:24can still actually use them so if I
3:30:26simply do
3:30:27testore display you can see that this
3:30:30now works perfectly fine and it gives us
3:30:32hello so I just felt like I had to make
3:30:35this video although it's not really an
3:30:36important aspect in Python just because
3:30:38if you guys are going to be learning
3:30:39different programming languages you have
3:30:41to understand the difference between
3:30:42private and public classes it's very
3:30:45difficult to illustrate in Python
3:30:47because there's nothing that's truly
3:30:48public or private but just know that if
3:30:50you ever run across some classes that
3:30:52have underscores at the beginning of the
3:30:54name or underscores before method names
3:30:56for example like anit then that means
3:30:59that they are typically private or want
3:31:02to be private at least so you should
3:31:03probably not mess with them and not use
3:31:05them that goes for you as well if you
3:31:07are programming and you want to create
3:31:09stuff that you don't want people to kind
3:31:10of mess with just put an underscore
3:31:11before the name it's just a typical
3:31:13convention in Python uh to do that to
3:31:16create a private or public class anyways
3:31:19that's been it for this video and for
3:31:21the series I hope that you guys did
3:31:23enjoy the series and you did learn a lot
3:31:25if you did please consider subscribing
3:31:26and hitting that like button as it would
3:31:28mean a lot to
Optional Parameters
3:31:40me hey guys and welcome back to another
3:31:44YouTube video so in today's video I I'm
3:31:45going to be starting a new um tutorial
3:31:48series on intermediate programming with
3:31:51python um so that being said a
3:31:52prerequisite for these videos is going
3:31:54to be that you know a little bit about
3:31:56python you know the basics you know if
3:31:57statements for Loops lists so on um as
3:32:00I'm not going to be reteaching and
3:32:01touching on a lot of those aspects um
3:32:04which I'm going to consider trivial now
3:32:06um I'm going to be teaching some more
3:32:08advanced topics so if you guys are
3:32:09interested in that make sure you watch
3:32:11until the end of the series and you go
3:32:13through all the videos as a lot of the
3:32:14stuff I'm going to do is going to add
3:32:15add on um as we keep going on videos so
3:32:19something I show in the previous video
3:32:20I'm going to add on to it in the next
3:32:21one um and these videos are going to be
3:32:23a really good way to enhance your
3:32:24programming knowledge and to learn a lot
3:32:26more about python that being said a lot
3:32:28of the stuff I'm going to show here is
3:32:30specific to python um and probably will
3:32:32not work in other programming languages
3:32:35that being said let's get ahead and go
3:32:37ahead and get started with our first
3:32:38video and this one is going to be
3:32:40optional parameters now this is probably
3:32:42one of the simplest topics that I'm
3:32:43going to cover in this series but I
3:32:44figured it would be a good starting
3:32:46point so let's just um go up go ahead
3:32:49and go over what a parameter is um so
3:32:51you should already know this but just to
3:32:53catch some of you guys up I have a
3:32:55function here I've defined I call it
3:32:57funk it returns X to the^ of two um and
3:33:00our parameter in this case would be X
3:33:03anything in this bracket is a parameter
3:33:04note you can have multiple parameters so
3:33:06I can have x y z and so on now what we
3:33:10want to do is we want to create
3:33:11something called an optional parameter
3:33:14now again sorry the argument here um so
3:33:17when we I call my function I said call
3:33:18equals Funk five our argument five is
3:33:21passed into the parameter x x is used
3:33:24here and then if I run my program so I'm
3:33:26running it here uh oops have to drag my
3:33:29console over it's on a different window
3:33:31right now oh and I've accidentally
3:33:32opened up something else now
3:33:35have uh one second guys sorry about that
3:33:38okay I have my console which is here uh
3:33:41it prints 25 to the screen uh like
3:33:43expected okay so now we want to create
3:33:46something called an optional parameter
3:33:48so the the whole point of an optional
3:33:50parameter is so that we don't always
3:33:51have to keep typing in parameters um
3:33:54especially if we have uh more than one
3:33:56that we have to type in it can get
3:33:58tedious so the way that we can do this
3:33:59and it's actually really simple and it's
3:34:01extremely useful and I'll show you in a
3:34:03different example later simply beside
3:34:05your parameter so in this case X just
3:34:07put an equal sign and then put what you
3:34:09want it default value to be so in this
3:34:11case I'm just going to put uh one okay
3:34:14so now what what happens is if I call my
3:34:17function and I don't give it a value for
3:34:19x it's simply going to use one so we can
3:34:22show that so again run the program and I
3:34:24get one because one to the^ of two well
3:34:26is one and I didn't put anything in here
3:34:29and I didn't get an error now notice if
3:34:31I get rid of this equals one and I try
3:34:33to call we get an error because it's
3:34:35missing a uh potential argument X right
3:34:38so let's put that back xal 1 now what
3:34:41happens if I do put something in the
3:34:43brackets here of my function call so for
3:34:45example I pr x 5 when I run the program
3:34:48now we get 25 so if you ever put
3:34:51anything in uh your function instead of
3:34:55the uh like default instead of just
3:34:57leaving it blank in this case then it's
3:34:58going to overwrite this default
3:35:00parameter and it's going to make x equal
3:35:02to 5 instead of one now you might say
3:35:04well why is this useful I'm going to
3:35:06show you an example in just a second but
3:35:07I want to show how you can use multiple
3:35:09optional parameters um and how you can
3:35:11mix them with non-optional parameters so
3:35:14let's just rewrite this function um and
3:35:16in this case I'm going to take two
3:35:17parameters I'm just going to say like
3:35:19word
3:35:21and currents or let's say frequency like
3:35:25this okay and now all I'm going to do in
3:35:27this function is I'm simply going to
3:35:28print to the screen word multiplied by
3:35:34frequency like that okay and then here
3:35:37in my function call my word I'm going to
3:35:39say is Tim and frequency five now this
3:35:44should just print to the screen right do
3:35:46this and it does we get Tim Tim Tim Tim
3:35:49now what if I wanted to make the
3:35:51frequency um an optional parameter so
3:35:54all we have to do here is I'm going to
3:35:55change my frequency to be a default of
3:35:57one so that means now I have a parameter
3:36:01that's not optional which is word and a
3:36:03parameter that is optional frequency so
3:36:06when I run the program by just putting
3:36:07in my needed parameter I get Tim and
3:36:10just one time because that's the default
3:36:12value of frequency again we don't get an
3:36:15error um like you might expect and then
3:36:18same thing if I want to change the
3:36:20frequency so maybe I want to make the
3:36:21frequency 10 all I do is I type in a
3:36:24number for frequency so corresponding
3:36:27and then when I run the program I get
3:36:29Tim and then 10 times like that now what
3:36:33happens if I do something like this and
3:36:35I put 10 here um instead of frequency
3:36:38well you'll see what happens we get 10
3:36:41because 10 now is word and frequency is
3:36:431 and 10 * 1 well is equal to 10 okay so
3:36:48now let's go to multiple optional
3:36:50parameters um so now I'm going to say
3:36:53add and frequency and I want add to also
3:36:56be an optional parameter so in this case
3:36:58I'm going to say add is equal to 5
3:37:00frequency is equal to 1 okay um so what
3:37:03I'm going to do now is I'm just going to
3:37:04say word is
3:37:06times
3:37:08frequency plus add like that um and now
3:37:13in this case same thing I'll just do
3:37:16word in this case I'll say hello um so
3:37:18our default value for ad is five our
3:37:21default frequency for uh our default
3:37:23value for frequency is one so we should
3:37:26have 5 + 1 um which is six multiplied by
3:37:29word so we should get hello six times
3:37:31and we do and now again I can put put in
3:37:33a value um for add so in this case I'm
3:37:36going to say add is equal to zero
3:37:38frequency is still going to be one so
3:37:41now I get just one time to the screen
3:37:44hello so the way it we're is when you
3:37:45have optional parameters is say I wanted
3:37:48to type in a value for frequency but I
3:37:50wanted to leave add as the default value
3:37:53I'm not actually able to do that because
3:37:55of the order in which I've set these
3:37:56parameters so if I wanted add to still
3:37:58be five but I wanted to change frequency
3:38:00I would have to put five for ad and then
3:38:03I could put a value for frequency like
3:38:05three or something like that okay um and
3:38:07again if we show this ad is going to
3:38:09stay at five frequency is going to be
3:38:11three so we should get hello eight times
3:38:14in which we do
3:38:16um now if I wanted to change that around
3:38:18um and I wanted to say okay well I want
3:38:21ad to always be defaulted to a value
3:38:23like five and I'm hardly going to change
3:38:25that but frequency is going to be
3:38:26something I change a lot when I use my
3:38:28function then we would just flip these
3:38:30around so we' say frequency equal 1 add
3:38:34equals 5 like that so now this is going
3:38:36to uh go to frequency and this is going
3:38:39to go to add um like so I hope that
3:38:42makes sense so now I'm just going to go
3:38:43down and I'm going to show um some
3:38:46better examples to why this is kind of
3:38:48more useful so I've just created this
3:38:50class here um called car all right and I
3:38:54have you can see a bunch of parameters
3:38:56so I have five up here um not including
3:38:58self and then just one here in its
3:39:00method called display now these optional
3:39:02parameters can be used inside of methods
3:39:04as methods really are just functions
3:39:06that apply to a class right so I'll show
3:39:08you right now how this class kind of
3:39:10works um I've just created a new object
3:39:12called whip it's a new car object and we
3:39:14have four Fusion 2012 new um and these
3:39:17are the parameters that it takes right
3:39:19so it takes the make the model year
3:39:21condition and kilometers all of these
3:39:22are required I need to type these in no
3:39:24matter what the next one that I have my
3:39:27method is display um and what this is
3:39:29going to do is simply just print out to
3:39:30the screen um one of two messages if
3:39:33show all is equal to true it's going to
3:39:35print out everything including the
3:39:37condition and the kilometers of the car
3:39:39if not it's just going to print out the
3:39:40make model and year of the car so let's
3:39:42watch this uh run on the screen so this
3:39:44car is a Ford Fusion from 2012 it is new
3:39:46and has 0 km um like so so you can see
3:39:49that this is working well now what if I
3:39:51wanted to say um I want condition and I
3:39:55want kilometers to both be optional
3:39:57parameters so typically when you buy a
3:39:59car most people buy new cars so I'm
3:40:02going to say the condition is going to
3:40:03be defaulted to new and kilometers is
3:40:06going to be equal to zero because if
3:40:08you're buying a new car well then
3:40:09kilometers should be equal to zero so
3:40:12only if um I want to I'm going to change
3:40:15that so now same thing here if I go like
3:40:18this and I run the program we should get
3:40:21the same thing so this car is a Ford
3:40:23vision from 2012 it is new and has zero
3:40:25kmers and indeed we do we didn't need to
3:40:27type in those parameters because again
3:40:29they're optional um next one I'm going
3:40:31to show you is down here in display same
3:40:33thing I can set this equal to something
3:40:35like true so show all equals true and
3:40:37now when I call my
3:40:41display um it automatically shows all
3:40:44unless I specify otherwise by typing in
3:40:47false like so and then we get this car
3:40:50is a Ford Fusion from 2012 now this is
3:40:53really useful if you have a lot of
3:40:54different parameters it is especially
3:40:56useful when you're typing and working
3:40:58with classes so that's why I wanted to
3:40:59show you this example I hope that you
3:41:01guys are able to implement this in your
3:41:03programming um anyways that's been it
3:41:06for this video uh make sure you guys
3:41:08stay tuned for the next one which should
3:41:10be coming out in just a few days
Static and Class Methods
3:41:18hey guys and welcome back to another
3:41:21YouTube video in today's video uh the
3:41:23second video in my uh intermediate
3:41:25python tutorials I'm going to be going
3:41:27over static and class methods um so
3:41:30pretty much these are just uh different
3:41:32things you can use uh within a class uh
3:41:35and I'm going to explain what they do
3:41:36and why they're useful so without
3:41:38further Ado let's go ahead and get
3:41:39started um so because these two things
3:41:42uh methods obviously a part of a class
3:41:44use a class we need to First create a
3:41:46class so in this instance I'm just going
3:41:47to make a class I'm going to call it
3:41:49person um it is of class object and then
3:41:53I'm just going to start off by making my
3:41:55initialization uh method here give it
3:41:58self I'm also going to give it name and
3:42:01age and we move to the next line so
3:42:03let's just assign our variables here
3:42:05here self. name equals
3:42:07name and self. AG is equal to H okay the
3:42:12next method I'm going to make um this
3:42:14one is going to be called get
3:42:18population this going type self um
3:42:21actually here I'm going to type CLS and
3:42:23I'm going to get into what this does uh
3:42:25in a little bit and I'm going to return
3:42:27CLS do population and now I just
3:42:31remembered that I actually forgot to
3:42:33make this up here I'm going to make a
3:42:35class
3:42:36variable um where I'm just going to say
3:42:39population is equal to 50 like so okay
3:42:43one more method this one I'm going to
3:42:44call the fine and then is adult and then
3:42:49in here I'm just going to put age I'm
3:42:50going to say
3:42:52return age greater than equal to 18 okay
3:42:57I'm also going to make one more method
3:42:59down here just to show you the
3:43:00difference between a bunch of these and
3:43:01I'm just going to call it Define
3:43:03um
3:43:04display okay and so it's going to take
3:43:08self um and that's actually all we need
3:43:11we're just going to print to the screen
3:43:13in this one we're going to say self do
3:43:16name and then
3:43:19comma
3:43:22is and then comma self. AG comma years
3:43:29old so this should if I did this right
3:43:31it's going to say whatever the person's
3:43:33name is is and then their age years old
3:43:35so for me 18 years old so now let's
3:43:39create a new instance of this class down
3:43:41here um so I'm just going to call this
3:43:43one new person going to set it equal to
3:43:47a person whose name is
3:43:50Tim and whose age is 18 like so and now
3:43:54I'm just going to go through this class
3:43:55and kind of uh go through what we've
3:43:58actually done here so pretty much what
3:44:00I've started off by doing is I've
3:44:01created a class variable population
3:44:03equals 50 I've done my uh Constructor
3:44:05method um which is just going to
3:44:06initialize my variable's name and age
3:44:08and then I've created two methods in
3:44:09here which actually aren't done and
3:44:11they're called get population um and is
3:44:14adult
3:44:15now the name of this video is class
3:44:17methods and static methods so one of
3:44:19this is a class is a class method and
3:44:21the other one is a static method um go
3:44:23ahead and guess which one you think is
3:44:26uh
3:44:27which so I'm going to tell you right now
3:44:29the first one is actually a class method
3:44:32and the next this one here is a static
3:44:34method and the way that we denote these
3:44:36in Python is by putting something called
3:44:38a
3:44:39decorator above these methods um so it's
3:44:42just an at sign and then the name uh so
3:44:45in that case static method or class
3:44:47method in this case static method now
3:44:49what is a class class method and what is
3:44:52a static method and the best way to
3:44:54explain these is to use an example um so
3:44:56pretty much a class method means that
3:44:59you can call it on any um instance of a
3:45:02class so you don't need to have uh for
3:45:06example I don't know what to say you
3:45:08don't have to have an object already
3:45:09created of that class you can just call
3:45:11it on the class so for here for example
3:45:14I've done something like new person and
3:45:15it typically if I didn't have any other
3:45:18um any static methods or class method I
3:45:20would just say like new person. display
3:45:22or new person.get population what this
3:45:24class method allows us to do is do
3:45:26something like this so we can do
3:45:29person.get
3:45:31population now person is simply the name
3:45:34of our class it's not the name of an
3:45:36object of that class so new person is an
3:45:39object of class person whereas person is
3:45:41just the name of the class and if I do
3:45:43that and I print this to the screen so I
3:45:45print person.get
3:45:47population assuming I have no errors
3:45:49here um oh it says it's cuz I haven't
3:45:52put uh the little brackets here my bad
3:45:54doget population it pops up to the
3:45:56screen and it gives us the value of 50
3:45:58and again we didn't have to create an
3:46:00object to use that method because it is
3:46:03a class method um I hope that makes
3:46:05sense I'll try to explain it maybe one
3:46:07more time really quickly pretty much you
3:46:09don't need to create an object of the
3:46:11class to use any methods that are
3:46:13decorated as class method
3:46:15now also in class method all that's
3:46:18passed to the class method um is well
3:46:21the class because it's not actually an
3:46:23object we don't need this self parameter
3:46:25here like we need um in our
3:46:27initialization method and in any other
3:46:29regular methods that we have um we just
3:46:31need one variable we can call whatever
3:46:33we want in this case I call it CLS which
3:46:35is going to store well what class you're
3:46:37getting pretty well and then we can add
3:46:40any other parameters that we want so I
3:46:42can add X could add y um Ely many after
3:46:45that we just need to make sure that we
3:46:47have at least one in there because for
3:46:49example if I remove this um CLS like
3:46:52that it's going to say take zero
3:46:54positional argument but one was given
3:46:56because the class name is automatically
3:46:58sent into um this method when we call it
3:47:02okay the next type of method is static
3:47:05method um and this one is similar to
3:47:08class method um except it can be called
3:47:11without using that class I want to say
3:47:14so doesn't take a self parameter and it
3:47:16doesn't take a class parameter um so you
3:47:18don't actually need anything in here in
3:47:20this case I have age just cuz I want to
3:47:21be able to compare age um but it doesn't
3:47:25need any parameters if I did something
3:47:26like this it would work fine so the way
3:47:28we denote that again is with the at sign
3:47:30static method just above where the
3:47:32method is defined um and to use this
3:47:35method I can do something like person do
3:47:38is adult and then in here if I put
3:47:41something like five we're going to get
3:47:43back the value of false which you can
3:47:45see here if I do a value like 21 we get
3:47:49a value of true now why is this useful
3:47:52what's different than class method um
3:47:54static method is just used when you
3:47:56don't need self and you don't again need
3:47:59that actual object and it's just a good
3:48:01organizational way of storing a bunch of
3:48:03methods so for example if you had like I
3:48:06don't know maybe a math class that you
3:48:07created um and you wanted to have a few
3:48:10math objects and then you wanted a bunch
3:48:12of static methods that were all stored
3:48:14under math so for example when you use
3:48:16the math module in Python you use
3:48:18math.round or math. so on that's an
3:48:21example of possibly a static method or
3:48:23just a function within the math class um
3:48:26you want to organize all your static
3:48:28methods which in within that class um so
3:48:30you would call like whatever the class
3:48:32name is math and then dot the method and
3:48:34then you give it an argument and it'll
3:48:35return that and work fine uh I hope that
3:48:38kind of makes sense it is hard to
3:48:40explain if you don't have like a lot of
3:48:42good use cases for it um class method
3:48:44meod it just takes the actual um class
3:48:47and then it can access anything within
3:48:49the class um that's public to the class
3:48:52so here you see I had do cs. population
3:48:56and population is a variable defined up
3:48:58here this static method it can't access
3:49:00this population um variable because um
3:49:04it doesn't have access to the class name
3:49:06right it it doesn't it's not pass that
3:49:07information um so it can only use the
3:49:10parameters that you pass it it can't use
3:49:12any that are defined within the class um
3:49:15and that's really the best way I can
3:49:17kind of explain it to you guys um and a
3:49:19good way to understand how to use this
3:49:21um and why they're useful is to use them
3:49:23in your own um use cases so again static
3:49:25method um You can call just by calling
3:49:28the person uh the name of the class and
3:49:30then that method um given whatever
3:49:32parameters it doesn't need any
3:49:33parameters if I do something like this
3:49:36and I'll just put like five greater than
3:49:38or equal 18 this still works um you
3:49:41don't need any parameters whereas a
3:49:43class method you need one parameter at
3:49:45least at minimum um which is going to be
3:49:47that class name and it has access to
3:49:50anything within the class so any
3:49:51variables you define um or other static
3:49:54methods within like within this class
3:49:55method you could call another static
3:49:57method um and so on so yeah I hope that
3:50:01makes sense to you guys it is kind of
3:50:02hard to explain the static and class
3:50:04methods but they are really useful um
3:50:06especially if you're trying to organize
3:50:07things um and when you get further on
3:50:10with object orientated programming in
3:50:11Python you definitely notice that you
3:50:13will use these a lot anyways that's been
3:50:15it for this video um please make sure
3:50:16you guys leave a like on the video and
3:50:18subscribe and I will see you again in
3:50:20the next
Map Function
3:50:25one hey guys and welcome back to another
3:50:28YouTube video in today's video this is
3:50:30the third video in our uh intermediate
3:50:33python uh tutorials and I'm going to be
3:50:35going over the map function um so pretty
3:50:38much the map function is an extremely
3:50:40useful tool that allows us to well apply
3:50:43a function to list and then create a new
3:50:46list um with those new values and you'll
3:50:49understand what I mean as I go through
3:50:50an example here so before I even start
3:50:52using the map function I want to just
3:50:54present us with a problem um that the
3:50:56map function can solve and this is kind
3:50:57of the best way to understand it so I'm
3:50:59going to create a list um I'm just going
3:51:01to call it Li of integers up to uh 10
3:51:05like this so 9 and 10 okay um and now
3:51:08I'm going to create a function and I'm
3:51:10just going to call it funk and it's
3:51:11simply going to take one parameter X and
3:51:14all we're going to do is we're going to
3:51:15return x to the exponent x very simple
3:51:20okay now what this problem is it's very
3:51:24simple um I want you or I want to be
3:51:27able to apply this function X to every
3:51:30value in the list here so one uh to 10
3:51:34um and then have that stored in a new
3:51:36list so intuitively the way that you
3:51:39would want to do this U or the way that
3:51:40you would try to do this at least is you
3:51:42make something like new list is equal to
3:51:44this
3:51:45you'd write a for Loop you say 4X in Li
3:51:49and then You' say new list. aend um and
3:51:53then you would put well Funk and X like
3:51:57that and then if you printed out your
3:51:59new
3:52:00list you would get that value I don't
3:52:04know why print is showing up in a
3:52:05different color now but anyways um and
3:52:08there we go so we get 1 4 27 and so on
3:52:11and so forth okay um so now what we want
3:52:15to do is use the map function to do this
3:52:18faster so this is a very valid way to do
3:52:20this this works fine but I can actually
3:52:21shorten all four of these lines into one
3:52:25um by using the map function so let's go
3:52:27ahead and do that now so I'm going to
3:52:29start by typing uh actually we're just
3:52:31going to print because I want to print
3:52:33first starting with I'm going to do list
3:52:36um like that and I'll tell you why in a
3:52:38second I'm going to write my map
3:52:39function like so uh and now your map
3:52:42function takes two arguments um so it
3:52:45takes a function and it takes a list so
3:52:48let's give it our function which in this
3:52:49case is going to be called Funk and then
3:52:52let's give it our list so I actually
3:52:53don't need these double brackets here
3:52:55excuse me let's give it our list which
3:52:57in this case is Li so let's talk about
3:53:00what this really is doing right now so
3:53:02the map function takes a function which
3:53:05is Funk so the name of our function and
3:53:06it takes a list which is Li what it's
3:53:09going to do is it's going to apply this
3:53:11function to every element in the list
3:53:14and we can watch this happen we run the
3:53:16program and you see we get the exact
3:53:18same value as we did before um and
3:53:22that's extremely uh extremely useful in
3:53:24Python because a lot of times you want
3:53:26to apply a function or even possibly
3:53:28multiple functions
3:53:30onto um a VAR or onto a list or every
3:53:34element of the list and you don't want
3:53:35to have to type out that whole for Loop
3:53:37yes you can do that it works fine but
3:53:39the map function is just a shortcut to
3:53:41be able to do that um so let me just
3:53:43show you now uh maybe a few other
3:53:46examples that we can use so like using
3:53:49this map function there's another way
3:53:50that we can create this and it's totally
3:53:52preference which one you want to use I
3:53:53like using map just CU it's kind of cool
3:53:55um you can also use something called
3:53:56list comprehension um and this is
3:53:59something I would have shown in some my
3:54:00previous python tutorials uh like from
3:54:02way back like years ago on my channel
3:54:05but I'll go over it quickly now um and
3:54:07the way that this works is you can do
3:54:09the same things you can say funk X for X
3:54:13in
3:54:15Li now this is actually going to give us
3:54:17the exact same value as what we have up
3:54:19here with this map uh function Li for
3:54:22example because what we're going to do
3:54:23is we're going to say we're going to
3:54:24take this right here so whatever this
3:54:27expression is um and we're going to do
3:54:29it uh for every value of x in the list
3:54:32so that's exactly what this map function
3:54:34does um and it's just going to turn it
3:54:36into a list because we put our square
3:54:37brackets here so if I print this out see
3:54:39we get again the same value um and
3:54:42that's how list comprehension works now
3:54:44for list comprehension you can also add
3:54:46um like an expression in here so I can
3:54:48do something like if um let's say
3:54:52x uh modulus oops modulus 2 equals
3:54:57equals z and now it's only going to do
3:54:59this uh or put this into the list if x
3:55:03is divisible by two uh syntax eror oh I
3:55:07forgot my square bracket here my bad I
3:55:09don't know how even got rid of that
3:55:11let's run
3:55:12that oh it's because that is why all
3:55:16right excuse me on that okay so now we
3:55:18get 4 2 5 6 so it's only doing it for
3:55:21the even numbers of X um so yeah those
3:55:24are two really cool ways to kind of
3:55:26change a list or modify them apply a
3:55:28function to them in Python say Obviously
3:55:31you might want to use a function that's
3:55:32more advanced than this um this is just
3:55:34for a basic example and if you wanted to
3:55:35apply maybe two functions to it you
3:55:37could have X be changed here and then
3:55:40you could return like function two of x
3:55:44to the X here and then that would put um
3:55:47that value obviously in the map so again
3:55:50map takes two parameters a function and
3:55:52a list um and it will apply uh the
3:55:55function to every value in that list I
3:55:58hope this has been useful for you guys
3:56:00if you did enjoy the video please make
3:56:01sure you leave a like And subscribe and
3:56:03I'm going to be moving on to some more
3:56:05complicated and um other examples using
3:56:08map and other functions um in the next
3:56:10video so make sure you stay tuned for
3:56:12that
Filter Function
3:56:18hey guys and welcome to the fourth video
3:56:20in my intermediate python tutorials and
3:56:23in today's video I'm going to be going
3:56:24over the filter function so this one is
3:56:27very similar to what I did in the last
3:56:28video which is the map function um but
3:56:31obviously it has a few differences but
3:56:32the filter and map function are nice to
3:56:34learn back toback because they can be
3:56:36used together um and a lot of people do
3:56:38use them together when they're making uh
3:56:40programs so I'm just going to go ahead
3:56:42and start off by typing out two fun
3:56:44functions here uh that we're going to be
3:56:45using in our examples so I'm just going
3:56:48to make one function called add seven
3:56:50which is simply going to return seven
3:56:52and I'm going to make another one which
3:56:53is called is odd and it's just going to
3:56:56return if the number is an odd number so
3:56:58the way we do that again is X modulus 2
3:57:00does not equal zero like that okay
3:57:04pretty straightforward now I'm just
3:57:06going to make a new list in this case
3:57:07I'm just going to go 1 2 3 4 5 6 7 8 and
3:57:10nine and then we'll even add a 10 in
3:57:13there and and now I'm going to go over
3:57:15uh what the filter function does so I
3:57:18want to assign our uh a new variable
3:57:20here create a new one I'm just going to
3:57:21call this B and I'm going to make it
3:57:23equal to
3:57:24list filter which is the name of our
3:57:27function just like this and now filter
3:57:29function actually takes the same
3:57:30arguments that our map function did in
3:57:33the last video it takes a function and
3:57:35it takes an iterable uh list so we can
3:57:38take a string as well um but typically
3:57:40we just pass it a list something that's
3:57:42iterable and that you can go over um so
3:57:44what I'm going to put in here for our
3:57:46first function is I'm going to put is
3:57:48odd and then I'm going to give a list a
3:57:52now the way filter function works is if
3:57:54this value so it's going to it's going
3:57:56to do the same thing the map function
3:57:57does it's going to pass every element in
3:57:59our iterable item uh in this case the
3:58:02list to the function is odd so it's
3:58:03going to start with one it's going to
3:58:04pass one in there it's going to say one
3:58:06modules 2 does not equal z um which is
3:58:09true so it's going to give us a true
3:58:11value and then it's that's going to be
3:58:13added to the list because this function
3:58:16returned a true value now say we put two
3:58:18in here and we go and we say 2 is X two
3:58:21modules two well that does equal zero so
3:58:24we get a false value returned here now
3:58:27two is not added to the list this is
3:58:29essentially filtering out elements based
3:58:32on um a predefined function so obviously
3:58:35you can make your function that you want
3:58:36to check um a lot longer and a lot more
3:58:40extensive so you're filtering out more
3:58:41items but this is extremely useful when
3:58:43you're solving problems so rather than
3:58:45going through a for Loop and checking
3:58:46every single item um like we might have
3:58:49done with the map function to add things
3:58:51into a list we can just simply call
3:58:53filter um give it the function that we
3:58:55want to filter um based on and then a
3:58:57list and it's going to return that new
3:58:59list so let's just go ahead and run the
3:59:00program here and make sure that
3:59:01everything's working fine um so I'm just
3:59:03going to print out a to the screen and
3:59:05I'm going to print out B and we can see
3:59:08that B has essentially filtered out all
3:59:10of the elements um that were even so we
3:59:13get 2 6 8 10 they are all gone so now I
3:59:16want to show you how we can implement
3:59:18this with our map function um why is
3:59:20this useful what can we use the map
3:59:22function for so I'm make another list
3:59:24here and I'm going to call it C and this
3:59:27time I'm just going to do list map and
3:59:30then inside a map I'm going to do filter
3:59:33but before filter I'm just going to
3:59:34apply another function so in this case
3:59:36I'm going to do add seven and then in
3:59:39filter here I'm actually just going to
3:59:40change this to B because I'm going to
3:59:41type the same thing now let's go over
3:59:43what I just did here
3:59:44so essentially what's going to happen
3:59:46now is we're using the map function
3:59:48which if you don't know go back and
3:59:49watch the previous video um and we're
3:59:51going to apply this new list B which has
3:59:54been filtered um so we filtered out this
3:59:57original list U now we have 1 3 5 7 n so
4:00:00far and based on that list we're going
4:00:02to add seven to every element in that
4:00:04list now again if you wanted to save a
4:00:06line I could just get rid of B here and
4:00:09I could simply paste that in here
4:00:12removing list like that um and this
4:00:15would work fine so let's go ahead and
4:00:17see what actually happens here again
4:00:19we're just taking this new filtered list
4:00:22and now we're applying another function
4:00:23to it um in this case add seven so let's
4:00:25make sure that this is indeed working I
4:00:27haven't made a mistake here so we'll
4:00:28print a again and I will print C and you
4:00:33can see um that we do indeed get that so
4:00:36our one add seven we get eight our three
4:00:39add seven we get 10 5 7 and so on you
4:00:42can see how this works
4:00:44now these are extremely straightforward
4:00:46examples but if you're doing a list and
4:00:48for example you wanted to filter out any
4:00:50elements that contained a certain digit
4:00:52or um that met a certain criteria then
4:00:54you could create a more advanced filter
4:00:56function uh function to filter based off
4:00:58of that had a whole bunch of criteria
4:01:00and then it's going to return a true or
4:01:01false value so I'll show you here if I
4:01:03do something like just return
4:01:05true uh and I don't return a condition
4:01:08per se um then every element in my list
4:01:10is going to be um sent through because
4:01:12nothing's going to be filtered out so
4:01:14again if I print C
4:01:16here all of our elements are here so we
4:01:18have 10 elements because we're always
4:01:21returning true same thing if I always
4:01:22return
4:01:23false then none of our elements are
4:01:25going to be printed because well it's
4:01:28always false so the way to think about
4:01:30it is um I'll uncomment out this for a
4:01:33second is what this filter function does
4:01:35is it's going to apply a function that
4:01:36gives us a true or false value um to
4:01:40every element in the list if when that
4:01:42element is applied to that function we
4:01:44get a True Value then it's going to be
4:01:45created in that new list which filter
4:01:48returns to us um like that now again
4:01:51yeah this is really useful for solving
4:01:52problems and I just want to show you
4:01:53what happens if I do something like
4:01:55return one um and I'll print C to the
4:01:58screen
4:01:59here you can see we get every um every
4:02:02element out of here so pretty much the
4:02:04way is uh in Python something is said to
4:02:07be true as long as it's not zero if it's
4:02:09a number so like if I pass something
4:02:10like high well if you say is high true
4:02:14technically high is true um again if I
4:02:17print
4:02:18C uh we're still going to get every
4:02:20element although I didn't pass the value
4:02:22true um High technically evaluates to a
4:02:25true value in Python the only thing
4:02:27that's not going to evaluate to a true
4:02:28value is something like zero so now if I
4:02:31print C you can see we get an empty list
4:02:34um just wanted to add that in at the end
4:02:36there um teach you something you might
4:02:37not have known there um so anyways
4:02:39that's been it for the filter function
4:02:41in the next video I'm going to tie these
4:02:43all together with something called
4:02:44lambdas and what they look like is that
4:02:48uh you can see it highlights here as a
4:02:49keyword and pretty much this is a kind
4:02:51of function that we can use so we don't
4:02:52have to keep creating all these other
4:02:54ones at the beginning of our program um
4:02:56and they're extremely useful and they're
4:02:57really cool so make sure you guys stay
4:02:59tuned for the next video and I'm going
4:03:00to be explaining how we can use those
4:03:01with list uh maps and on their own
4:03:04anyways if you guys enjoyed please make
4:03:05sure you leave a like And subscribe and
4:03:07I will see you again in the next video
Lambda Function
4:03:15hey guys and welcome back to the fifth
4:03:17video in my Advanced Python programming
4:03:19tutorials in today's video I'm going to
4:03:21be going over Lambda uh I think that's
4:03:24the way you say it at least uh excuse me
4:03:26if I'm going to say it wrong this whole
4:03:27video which pretty much stands for an
4:03:29anonymous function now these are
4:03:31extremely useful um and they don't take
4:03:34up very much space in your program which
4:03:36is really good I'm going to show you
4:03:37exactly how they work um and what they
4:03:39do so I first want to just start off by
4:03:41defining a regular function again this
4:03:43is one example I've been using a lot I'm
4:03:44just going to say Define funk say like
4:03:46return x + 5 that's what our function is
4:03:49going to do it's simply going to add
4:03:50five uh to the number whatever we give
4:03:52it so right if I print something like
4:03:54Funk of two we should get
4:03:57seven there we go and we get seven to
4:04:00the screen now for an extremely basic
4:04:02function like this um often times you
4:04:05don't want to have to write a function
4:04:07definition and take up uh like two lines
4:04:09of code like this um it's easier to
4:04:11write it in a different way and it looks
4:04:13uh like more smooth maybe more slick in
4:04:15your program and I'm going to show you
4:04:16how to do that so that's one way that we
4:04:18can do it by making a function like that
4:04:20another way and this is using Lambda is
4:04:22to do something like this so the name of
4:04:23our function say funk I'll say funk 2 is
4:04:27equal to and then we're going to type
4:04:29our keyword Lambda like this you can see
4:04:31it highlights um in Orange just like our
4:04:34definition key here for our function
4:04:36we're going to give it a parameter or
4:04:38multiple parameters I think you can use
4:04:40multiple parameters at least and then
4:04:42we're simply going to State what it
4:04:43returns
4:04:44so the way this works is you type Lambda
4:04:47the parameter um or multiple parameters
4:04:49which we're going to try in a second uh
4:04:51a colon and then whatever value you're
4:04:53going to return so the thing with Lambda
4:04:55is it's used when you have one uh return
4:04:59or one expression in your function so
4:05:02something like this so again I can do
4:05:04return x + 5 over 4 like I could do a
4:05:06whole expression as long as that
4:05:08expression fits on one line um then
4:05:10that's perfectly fine to return and I'll
4:05:12show you how this works so if I say
4:05:16print Funk 2 and I just give it number
4:05:21like
4:05:22nine there we go we get 14 and seven so
4:05:26it works just like a regular function um
4:05:28and it kind of just looks like a
4:05:29variable so we're saying function two is
4:05:31equal to an anonymous function that's
4:05:34what Lambda stands for anonymous
4:05:35function with the parameter X and simply
4:05:37going to return x + 5 now these are
4:05:39really useful um for using with the map
4:05:42function and the filter function which
4:05:43I'm going to show you later so you don't
4:05:44have to constantly create a new function
4:05:46up at the top of your program and you
4:05:48can also use them inside of other
4:05:50functions so let's let's uh start by
4:05:52doing that so we're going to say funk 2
4:05:55simply just going to copy it and paste
4:05:57it into my other function and then I'm
4:05:59going to return Funk 2 of
4:06:03X+ 85 let's try that and now if I call
4:06:08my
4:06:09function we can see that we get 92 like
4:06:12so now this again is really useful
4:06:15because you want to um often times
4:06:18create another function or use something
4:06:20multiple times within a function um but
4:06:22you might not want to like write a new
4:06:24one up here like Funk two um you just
4:06:27want it to be only used within that
4:06:28function and in that case you would use
4:06:30Lambda as it keeps it all contained and
4:06:32at the top of your function you can
4:06:34write a bunch of other mini functions um
4:06:36that you can then use multiple times
4:06:38within your program it's hard to see the
4:06:39use case for it in small examples like
4:06:41this but they are extremely useful um
4:06:43especially in like more in-depth code
4:06:46longer code so now I'm going to show you
4:06:48with multiple parameters so I believe
4:06:51and I'm not sure but we'll see if we can
4:06:53do this um so we're going to try and
4:06:55we're going to make Funk 3 is equal to
4:06:58Lambda x y and then we're simply just
4:07:01going to return X+ y so let's just see
4:07:03if we call fun three and let's give it a
4:07:06value like five and five and we'll print
4:07:10that to the screen as well
4:07:14see there we go so that does work as
4:07:16well so you can give this infinitely uh
4:07:19an infinite amount of parameters just
4:07:20like you'd be able to do something like
4:07:22this in our function um and you can also
4:07:24I believe do optional parameters so I
4:07:26can do something like yal 4 and then if
4:07:29I put five we should be getting a value
4:07:31of nine here and there we are so you can
4:07:33use optional parameters um you can do
4:07:35everything that you'd be able to do with
4:07:36a regular function except you can just
4:07:39return one expression um like that okay
4:07:42so let's now use this with our map
4:07:45function and our filter function so in
4:07:49earlier videos uh I used the map
4:07:51function and to recall what that does
4:07:53I'm just going to create a list quickly
4:07:54say a
4:07:56equals 1 2 3 4 5 6 7 8 9 oops n and 10
4:08:01like that and I'm now just going to say
4:08:04let's say new
4:08:07list equals list
4:08:12map and then we're going to have a
4:08:13function in this case I'll just write
4:08:15funk for now and then we give it a list
4:08:17right like a so uh what I want to do now
4:08:20is actually want to just put a function
4:08:23in here uh without having to create a
4:08:25new one up here like I don't want to
4:08:26Define Funk so this is a perfect example
4:08:29where we can use Lambda we don't even
4:08:31have to make a variable like equal to
4:08:32the function anymore we can just type
4:08:34Lambda right in here so Lambda and then
4:08:37we so X and we'll say x + 5 so now we
4:08:40again should simply just be adding five
4:08:42um to each of the El elements in a and
4:08:44then print so we're just print new list
4:08:45to the screen now and see if this is
4:08:48indeed working and there you go we can
4:08:50see we started at one so six uh all the
4:08:52way up to 15 like so again that saves us
4:08:55now having to make a function at the top
4:08:58of our program that we're only going to
4:08:59use for one specific case which is this
4:09:02map function um and again we also don't
4:09:05have to create like Funk equals Lambda
4:09:09up here but you can if you want if
4:09:10you're going to use it in a different
4:09:11case you can just type it right in this
4:09:13same line as uh your map function or as
4:09:16your fil with your filter function so
4:09:17this works the same with the filter
4:09:18function I'll do
4:09:20filter like so um it's going to be our
4:09:23function I guess uh yeah I believe this
4:09:27works with the filter function oh yes it
4:09:29does okay so what this is going to do
4:09:31now is we're just going to return X
4:09:34modulus 2 equal equal Z so again now
4:09:37given X we're going to see if it's
4:09:38divisible by two uh if it is we return
4:09:40true we add that element to the list
4:09:42otherwise we will not we check now we
4:09:44get 2 4 6 8 10 and there we go so you
4:09:47can see why Lambda is extremely useful
4:09:50it's really cool um especially if you
4:09:51want to create a lot of mini functions
4:09:53or you want to create functions within
4:09:54functions um so keep everything nice and
4:09:56organized and it's a nice trick to be
4:09:58able to use in Python so with that being
4:10:00said that's been the end of the video
4:10:02today if you guys did enjoy and you did
4:10:04learn something please make sure you
4:10:05leave a like And subscribe to the
4:10:06channel and I will see you again in the
4:10:08next tutorial
Introduction to Collections
4:10:14hey guys and welcome back to another
4:10:16YouTube video so this is the sixth video
4:10:19in my python intermediate tutorials and
4:10:21today's video I'm going to be talking
4:10:23about Collections and more specifically
4:10:25counter which is a part of the
4:10:27collections module so what is the
4:10:29collections module pretty much it's a
4:10:31built-in module in Python that's going
4:10:33to allow us to have different kinds of
4:10:35data types so that we can store
4:10:36information sort through information um
4:10:38and do some cool things these are
4:10:40extremely useful um the second I found
4:10:42out about them I using them all the time
4:10:44because a lot of the stuff that would
4:10:45usually take you a few for Loops or a
4:10:47few lines um you can do in one line or
4:10:49two lines with these uh new collection
4:10:51types so I just want to first off and
4:10:53start by saying that in Python it has
4:10:56something called containers um so
4:10:58containers are pretty much uh a data
4:11:00type or an object that's going to store
4:11:02multiple objects so it's like a
4:11:04container like a list is an example of a
4:11:06container uh a set is an example of a
4:11:08container a dictionary and a tuple as
4:11:10well um these are the four main ones
4:11:12with pyth
4:11:13and in the collections module they
4:11:15introduce five new ones um which are
4:11:17similar to these but they all have their
4:11:19own methods um and cool things you can
4:11:21do with them so the one I'm going to
4:11:22talk about today is counter uh but the
4:11:24other ones are listed here DQ named
4:11:26Tuple ordered dictionary and default
4:11:28dictionary you can kind of get an idea
4:11:30what some of them are by reading them um
4:11:32but let's get into the counter one for
4:11:34right now okay so first thing we need to
4:11:36do whenever we're using collections as
4:11:37we need to import it um so this first
4:11:39line up here is unnecessary but I just
4:11:41did that to show you and we're going to
4:11:43import import counter from collections
4:11:44so we can reference it directly without
4:11:46doing collections. counter okay so I'm
4:11:49just going to start by creating a new
4:11:50counter so I'm going to say C equals
4:11:52counter um now when you create a new
4:11:54counter what goes in here as the
4:11:56arguments um can just be any uh I want
4:11:59to say like collection data type or any
4:12:02uh container so for example you put a
4:12:03dictionary set um tle you can put a list
4:12:07uh and you can also do something which
4:12:08is weird with these keywords I'll show
4:12:10you that in a second so for example I
4:12:12could create a counter like this by just
4:12:15putting a string and it's going to count
4:12:16all the letters in the string and return
4:12:18them to me and I'll show you that in a
4:12:19second I could create another counter
4:12:21where in here I have a list so I can say
4:12:24like a a b c and we'll just do one more
4:12:31C like that and I could put a dictionary
4:12:34so I could do something like this I'll
4:12:36just make a small one so we don't take
4:12:38too
4:12:41long uh oops like that
4:12:44B2 and you can also put uh these
4:12:46keywords which is what I was talking
4:12:47about it's kind of cool because you
4:12:49don't have to type out or like use a for
4:12:51Loop to create a list like this um I
4:12:52could do something like oops cats equals
4:12:564 dogs equals seven like that and I can
4:13:00continue on with keywords um and I don't
4:13:02have to actually put the quotation marks
4:13:03around these I can just name them
4:13:04exactly what they are so let's just go
4:13:06ahead and start printing some of these
4:13:07to the screen to see what actually
4:13:09happens um when I create a new counter
4:13:12object and what does that look like in
4:13:13Python so we'll just print all these um
4:13:16just to give us an idea of what they
4:13:17really
4:13:18are okay so there we go so it says we
4:13:20have a counter object uh it is its own
4:13:22object it's not like a list or
4:13:24dictionary it's its own thing um it says
4:13:26a we have two uh I believe this is an L
4:13:29we have two G1 D1 um and then same thing
4:13:32here A B C dogs cats so you can see it
4:13:35looks like a dictionary in terms of it
4:13:37has a key value pair but it works a
4:13:39little bit differently so now if we want
4:13:42to reference um a specific item which is
4:13:44typically what we want to do from our
4:13:46counter we can just put the square
4:13:48brackets and put the name of that item
4:13:50or the key so in this case I'm going to
4:13:52put cats and you can see now we get four
4:13:55as that's the Direct Value related to
4:13:57cats um and something cool with this
4:14:00counter and a reason why I use it
4:14:02specifically is I can put an item in
4:14:04here um that doesn't exist in the
4:14:06counter it doesn't have a key set and
4:14:08that will actually not return an error
4:14:10like might happen with the dictionary so
4:14:12for example I put ay like I don't know
4:14:14let's see
4:14:16pet then it just gives me zero instead
4:14:18of returning an error so if I were to do
4:14:20this with a dictionary let say d equals
4:14:22like I don't know uh Cat 2 and then I
4:14:27Tred to do D pet like this and get the
4:14:30value uh we get an error uh because pet
4:14:34the key pet does not exist in our uh in
4:14:37our dictionary okay so let's move on to
4:14:39some more methods and why these are
4:14:41extremely useful um so we can see here
4:14:45um one of the methods that we can use is
4:14:47we can actually just list all of the
4:14:48elements out and this is useful if you
4:14:50want to like sum something um so let's
4:14:52just say list and then c. elements like
4:14:57that and what this is going to do if I
4:14:59print it to the screen is it's simply
4:15:01just going to print out all of the
4:15:02elements in our counter so we say cats
4:15:05cats cats cats dogs dogs dogs like that
4:15:09um and since here we said cats equals 4
4:15:12dogs equal 7 um then it prints it out
4:15:15like in a made list with that many uh
4:15:17indices which can sometimes be useful
4:15:19I'm just going to change this to be
4:15:20named D so that we can now print out the
4:15:22one like this with A1
4:15:24B2 and you can see we get a b b um like
4:15:28so I if I do the same thing I'll change
4:15:30this to e we print it again um then
4:15:33again we get another list that just has
4:15:35all of the elements in it so that's one
4:15:37useful method uh c. elements another
4:15:40useful element uh or method sorry is
4:15:43most common now this one by far is
4:15:45probably one of the most useful ones um
4:15:47and you can simply just type this so the
4:15:49name of your counter object and then
4:15:53most underscore common and then in the
4:15:54brackets here you're going to put how
4:15:55many elements you want so if you wanted
4:15:58to find the number one most common
4:15:59element you'd put one if you want to
4:16:01find two you put two so let's print this
4:16:03to the screen and see what we
4:16:05get
4:16:07so and you can see we get the most
4:16:09common elements are a and c and it
4:16:11actually also returns us a count of
4:16:14those elements as well so it says a
4:16:16which occurs two times and C which
4:16:18occurs two times and that's returned to
4:16:20us in a tupple or Tuple however you want
4:16:22to call it uh which is really useful
4:16:25okay so now another one we're going to
4:16:27use uh I'm just going to copy something
4:16:29I have open here just to save a spit of
4:16:30time in terms of creating counter
4:16:31objects so I'm going to delete all this
4:16:33and I'm going to make uh one counter
4:16:35which is equal to a uh or C and then I'm
4:16:38just making a list which is uh D and
4:16:40it's a b b c like this okay so you can
4:16:43do something um as well with these
4:16:44counter objects you can subtract Counts
4:16:47from them uh from using other idal
4:16:49objects or you can add uh like the count
4:16:51of objects to them so one method is
4:16:54called subtract oops like this and it
4:16:59does pretty much exactly what it uh it
4:17:00says so we're going to take this counter
4:17:02object this has to be called on a
4:17:04counter object by the way um it says
4:17:06with these counts and we're going to
4:17:07subtract whatever the counts are from D
4:17:10of similar items so we have 1 a two B's
4:17:13and a c so if I do this and I print just
4:17:17the screen we should let's do a quick
4:17:19calculation here get a is equal to Three
4:17:21B is equal to z c is z and d is still -2
4:17:25uh none oh it's CU I didn't convert this
4:17:27to a list sorry about
4:17:30that let's do this
4:17:33maybe non type object is not ital all
4:17:35right just give me a second guys there
4:17:37is a way that we can oh it's because
4:17:39we're doing this so C do subtract
4:17:42doesn't doesn't actually return anything
4:17:44we just have to print C after we call
4:17:46that method okay so there we go and we
4:17:48do get um what we had there okay so yeah
4:17:50C is Nea 1 because 0 - one gives us
4:17:52negative 1 um and there we are so it is
4:17:56working exactly like it should be okay
4:17:58the next one is update so I'll run this
4:18:02now c. update and this is the exact same
4:18:04thing as subtract except it's simply
4:18:07just going to add the counts of whatever
4:18:08iterable object you give it so in this
4:18:10case I'm going to put D and note here
4:18:12that I'm using a list but I could be
4:18:13using like a dictionary could be using
4:18:15another counter object um I could be
4:18:18using a set uh a tuple uh whatever you
4:18:21want to say there it's still going to uh
4:18:23like work for update so let's see here
4:18:25and oh I didn't print
4:18:28C let's do
4:18:30that and there we go so we went
4:18:32originally so we changed our counter by
4:18:35subtracting and then we just updated by
4:18:36adding so it should be the same as our
4:18:38original counter which it is okay so
4:18:40that's working well the next one that
4:18:42I'm going to talk about now is clear um
4:18:44this one does pretty much what it states
4:18:47uh so if you see here we do c do CLE and
4:18:51then I'll print C to the screen and all
4:18:53it's going to do is just remove all of
4:18:55the counts so we now have an empty
4:18:57counter object that we can use to count
4:18:59something else to add to to subtract so
4:19:01on okay so that's it for the methods
4:19:04pretty well there's one or two more but
4:19:06I don't find them very useful so I'm not
4:19:07going to show them right now um but
4:19:09there's something cool that you can do
4:19:11with these counters and there's a few
4:19:13operations that are applied to them so
4:19:15you can actually add counters using the
4:19:17plus sign you do like this C plus D um
4:19:19you can do c minus D you can subtract
4:19:22them you can add them and then the two
4:19:24operations which you wouldn't
4:19:25necessarily uh know intuitively is you
4:19:27can do something called intersection and
4:19:29Union of counters so let's just first
4:19:31show what happens when I add these two
4:19:33counters together so C plus d oops and
4:19:36this needs to be a cter
4:19:38now because this isn't going to work if
4:19:40it's a list there we go so make that
4:19:42account counter and I'll print C minus
4:19:46D and then we'll print another one after
4:19:48that so if I add them and subtract them
4:19:51um you can see that uh it works like
4:19:54this so when I add them we get the
4:19:56counts added up like this and when we
4:19:58subtract uh we get it subtracted as
4:20:02well now some of you might be uh
4:20:04wondering here why we don't have the
4:20:06elements B C or D shown in our counter
4:20:10uh and that is because if the ele count
4:20:13is less than Z or equal to Z it's not
4:20:16going to be shown in our counter when we
4:20:18do these operations by adding and
4:20:20subtracting so because of that um you
4:20:23can see like why that happened because B
4:20:25is two here and we have two BS so that
4:20:28created zero C is already zero and D was
4:20:30-2 so when we subtracted it's not going
4:20:33to bother showing those uh in our new
4:20:36counter now the next operations that we
4:20:38can do is something called Union and um
4:20:42inter intersection so the first one I
4:20:44guess I'm going to show is intersection
4:20:46and the way that you can think of
4:20:47intersection is like the minimum
4:20:49elements in each of the uh the list so
4:20:52we're going to have C intersecting with
4:20:55D um in this case we should get AAL 1
4:20:58Bal 2 and C 0 D -2 so let's just see if
4:21:02this works and there we go we get B is 2
4:21:05a is 1 and we're not showing the other
4:21:06ones again
4:21:07because they um like they're equal to
4:21:10zero or less than zero so they not being
4:21:12shown and this is the and sign is what
4:21:14I'm doing to do this intersection
4:21:16between the counters the next one that
4:21:19we can do is called Union and this one
4:21:21you can think of as the max Elements
4:21:22shown in each of our counters um so it's
4:21:25going to take a equal to 4 because
4:21:26that's our Max it's going to take b
4:21:28equal to two that's the max between here
4:21:30Cal 1 and then again it shouldn't be
4:21:33showing us D because that's -2 so there
4:21:36we go we get a is four B is 2 and C is 1
4:21:39so this is pretty much going through
4:21:40looking at the counters and just taking
4:21:42maximum element whereas before when we
4:21:44did our intersection it's taking the
4:21:47minimum shown of all those elements so
4:21:50that has pretty much been it for this
4:21:52video and counters uh from The
4:21:55Collection there's a few things that you
4:21:56can do with them as well a few other
4:21:58methods if you want to learn about those
4:22:00uh just go to just look up collections
4:22:02counter python uh you can read through
4:22:04the documentation and you can look at
4:22:06some more examples that I didn't show
4:22:07here specifically other than that I hope
4:22:09you guys enjoyed the video if you did
4:22:11play please make sure you leave a like
4:22:13And subscribe and I will see you again
4:22:14in the next
Named Tuple
4:22:19one hey guys and welcome back to another
4:22:22video in today's video I am going to be
4:22:24continuing with my intermediate python
4:22:26uh tutorials and we're going to be
4:22:27moving on to something called named
4:22:30Tuple uh or tupple whichever one you
4:22:32prefer um which is a part of the
4:22:34collections data type or module class
4:22:38whatever you want to call it so in the
4:22:39previous video I introduced you kind of
4:22:41to the collection module in Python and I
4:22:44showed you counter which is really
4:22:45useful um today I'm going to be showing
4:22:47you named Tuple which is another data
4:22:49type or class whatever you want to call
4:22:51it within that module um and pretty much
4:22:54this is the description of it I'm just
4:22:55going to read it right off of the uh
4:22:57documentation here because it explains
4:22:58it better than I could come up with um
4:23:00so let's go ahead and do that named
4:23:02tupes assigned meaning to each position
4:23:04in a tuple and allow for more readable
4:23:05self-documenting code they can be used
4:23:08wherever regular tupes are used and they
4:23:09add the ability to access field by names
4:23:11instead of position index so you should
4:23:14understand a bit of that but if you
4:23:15don't obviously I'm going to go through
4:23:17exactly how this works and how we can
4:23:19use it so the main difference between a
4:23:20name Tuple and a regular tupal is you
4:23:23can access things by element and it's
4:23:24just a lot nicer to read in your program
4:23:27uh so they might come in handy if you're
4:23:28coding large things or if other people
4:23:30are going to be reading your code so
4:23:32let's go ahead and first off start by
4:23:33importing collections like this and then
4:23:36we're just going to from
4:23:38collections we are going to import named
4:23:41Tuple
4:23:44oops okay so now that we've done that
4:23:48what we're going to do down here is I'm
4:23:50just going to create something that I'm
4:23:51kind of going to go and show you what
4:23:53exactly it does so I'm just making new
4:23:55variable point I'm going to set it equal
4:23:57to a named Tuple with name
4:24:01point and then it's going to have Fields
4:24:04X Y and Z and I'm going to show how this
4:24:07works okay so what I've just done here
4:24:10is I've said point is going to be equal
4:24:12to a new object which is a named tupple
4:24:15or tubble um the name of the tub is
4:24:17point and it has Fields x y and z now
4:24:20this looks a little weird you might be
4:24:22confused what we're going to do whenever
4:24:24we want to create a new point that is in
4:24:26the form of this name tubal is we're
4:24:28going to treat this like a class and
4:24:30we're going to do something like this
4:24:31we're going to say new p is equal to
4:24:34point and then we need to give our uh
4:24:37parameters so in this or arguments so in
4:24:38this case in X Y and Z so I'm just going
4:24:40to say 3 four five like so
4:24:43now you might be looking at this be XYZ
4:24:45this is one string um this might be the
4:24:48name of one parameter the way that this
4:24:50works and it's actually really useful is
4:24:52that it's automatically going to break
4:24:54up this string XYZ into three different
4:24:57parameters so I'm just going to print
4:24:58this to the screen to kind of show um
4:25:01what I mean here so I'm just going to
4:25:02print new p and you can see that we have
4:25:04x = 3 y = 4 and Z equals 5 now what
4:25:08happened here is when we gave an
4:25:09iterable object as our different
4:25:11parameters
4:25:12you can see that we get uh it just
4:25:14breaks it up by space so I do something
4:25:17like gy and then I run this here you can
4:25:19see now g y is equal to 4 I can add
4:25:21another one um on the end like
4:25:24H uh oh and then we're just missing one
4:25:26argument so I'd have to add another
4:25:28number in here so that we get H is equal
4:25:30to 8 I can also do the same thing with
4:25:32the list per se so if I have a list and
4:25:33I want all of these to be the names of
4:25:35like so like X Y uh and let's just do
4:25:39like L and we run this takes four okay I
4:25:43keep forgetting to fix this anyways uh
4:25:46you can see we get x y l any iterable
4:25:49object will work I'm not exactly sure
4:25:51how a dictionary works but I'm pretty
4:25:52sure it takes the keys so let's just go
4:25:54ahead and have a look at this one we'll
4:25:56just say x um
4:26:00zero y z um Zed zero now this might
4:26:07crash but I just want to see if this
4:26:09actually works oh yeah it does work so
4:26:10it just takes the key names ignor the
4:26:12values we get X Y and Z and then I guess
4:26:16you can do the same thing with any other
4:26:17iterable object those are the only ones
4:26:18I can think of that are useful right now
4:26:21so anyways when we want to create a new
4:26:22tupple uh or a new name tupple what
4:26:24we're going to do is just say whatever
4:26:27the name of that's going to be is equal
4:26:29to point which we've set up here to be
4:26:31this type um with these like parameters
4:26:34attributes and then we just give it
4:26:36those parameters now there's a lot of
4:26:38really cool things and methods that go
4:26:40along with this named tle and that's
4:26:42that's why it's useful so first of all
4:26:43you can do something like this new p.x
4:26:47um new p.y and new p oops. z like so and
4:26:53now what this is going to do is it's
4:26:54going to allow us to access each element
4:26:57by its uh index so by its name which you
4:26:59can't do with a regular tle so you see
4:27:02we get three four five to the screen
4:27:04like that and we can use the same
4:27:06operations that we use on basic Tes so I
4:27:09can do something like new P0 like that
4:27:12um and oops my mod thing just popped up
4:27:14somewhere else and you can see we get
4:27:15three um because that's the first one
4:27:17that shows up we can also print this so
4:27:20it looks uh in the form of a dictionary
4:27:22so I'm just going to say underscore is
4:27:25dict like this I believe this is the
4:27:27method or not is as sorry and when we
4:27:30print this to the screen you'll see what
4:27:32shows
4:27:33up and it says an order dictionary and
4:27:35it gives us a list and then it gives us
4:27:37the all the Tes within so X3 y 4 Z 5 um
4:27:42kind of a different form if you want to
4:27:43play with that and use that we can also
4:27:45print out all the field names um which
4:27:47is useful say you forget the fields we
4:27:49can do new P do and then fields and it
4:27:52gives us a tle with those fields in it
4:27:55the next method that we can use is the
4:27:57replace method um this one's pretty
4:27:59straightforward but all you have to do
4:28:00simply is type I don't know let's see
4:28:02here new P doore replace and just make
4:28:07sure you guys remember to put these
4:28:08underscore here all the methods pretty
4:28:10well for this uh required underscore
4:28:12don't ask me why but that's the way
4:28:14they're written they need underscore and
4:28:15then you're going to put a key name so
4:28:17say I wanted to replace Y and I can say
4:28:19equals to 6 uh so we'll run that and
4:28:22then if I just go here and just to show
4:28:24you that we did change that let's print
4:28:27new P like that and you can see oh why
4:28:30did not change to uh to six that's weird
4:28:33let's let me just see if I print this to
4:28:34the screen if this is going to
4:28:38change it should have worked
4:28:42new P like so let's check and Y is not
4:28:45changing to four um that's very
4:28:48interesting why that is not working okay
4:28:52so I just had a quick look at the
4:28:53documentation and the reason why I can't
4:28:55do what I'm trying to do here with this
4:28:57replace uh is because this replace
4:29:00doesn't actually change the Tuple object
4:29:04it it's not capable of doing that so
4:29:06what we have to do is we have to assign
4:29:09a new variable or new object to that so
4:29:12pretty much what this does is it returns
4:29:13a new named tupal uh which we then can
4:29:16set to in this case the same name it
4:29:19it's going to do the same thing that we
4:29:20tried to do except we just need this new
4:29:22P equals to hope that makes sense so now
4:29:24if I run the program we can see that
4:29:27finally this is working we are changing
4:29:29X or I was trying to change y before but
4:29:31I switched it to X to see if there was
4:29:33an issue there uh you could see that
4:29:34that is working like that so anyways um
4:29:37that is pretty much it I'll show one
4:29:40last method here if you guys are still
4:29:42watching so pretty much we can do
4:29:43something like a new a new point so say
4:29:45P2 is equal to point doore
4:29:49make and then I'm going to put a little
4:29:52list in here I'll just say like a b c
4:29:56now what this is going to do is same
4:29:58similar thing to up here is it's just
4:30:00going to automatically grab all of these
4:30:02elements in our list and assign them to
4:30:05XYZ accordingly in our named tle so you
4:30:08can see if I print P the screen now
4:30:12and we give it a run no errors
4:30:14everything's working fine xal a yal b
4:30:17zal
Deque
4:30:22c hey guys and welcome back to the eth
4:30:24video in my intermediate python
4:30:26tutorials in today's video I'm going to
4:30:28be continuing with the collections
4:30:30module and we're going to be talking
4:30:31about a data type known as the deck so
4:30:35yes this says DQ right here and the
4:30:37title of the thing is spelled DQ um but
4:30:39the way that you pronounce it is deck uh
4:30:42and I just found that from reading the
4:30:44documentation otherwise I was going to
4:30:45go through this whole video saying DQ
4:30:47anyways uh deck is the proper way to say
4:30:50this word so if I do end up saying DQ
4:30:53throughout the video please don't leave
4:30:54an angry comment about it uh I don't
4:30:56know why I just keep saying it but
4:30:58anyways deck is the proper way so why
4:31:00would we want to use a deck over uh a
4:31:02typical list so pretty much it kind of
4:31:03looks the same as a list and I'll show
4:31:05you in just a second but the reason why
4:31:06we' use a deck is because it's faster in
4:31:08terms of adding elements to the end and
4:31:11the beginning of a list so rather than
4:31:14using a list you want to use a deck
4:31:15because it's going to be faster whereas
4:31:17if you're trying to randomly access
4:31:18elements within uh a container so a list
4:31:21then you're going to want to use a list
4:31:23over a deck and if that's confusing
4:31:25don't worry I'll kind of show you how we
4:31:27uh as we go through so let's start by
4:31:28creating a deck and see how that works
4:31:30so I'm just going to say d and that's
4:31:32going to be stand for a deck is going to
4:31:33be equal to a deck and then this just
4:31:36takes an iterable argument so in this
4:31:38case I'm going to take type in hello but
4:31:40I could give it something like a list a
4:31:41DI iary uh so on so anyways D equals uh
4:31:46deck hello now if I run the program
4:31:49press F5 and I simply print D to the
4:31:52screen here you can see that we get deck
4:31:54and then h e l l o i just split that up
4:31:58into what looks like a list but it's
4:31:59actually a deck okay so now let's go
4:32:02through uh a few methods that we can use
4:32:05on this so obviously we can use
4:32:06something like
4:32:07append so we can append an element to
4:32:09the end so I can append something like
4:32:11four
4:32:12uh I can pend another element do
4:32:14something like five and if we run this
4:32:16and then print it
4:32:18again you can oops didn't mean to do
4:32:22that then you can see we get hello and
4:32:25then four five so on so you can append
4:32:28uh a nice thing is you can actually
4:32:29append to the beginning of the list as
4:32:30well so I can say append left instead of
4:32:34just regular append and now you can see
4:32:36when we run the program and we print D I
4:32:40don't know why that didn't work let's
4:32:41see here we get five and then hello four
4:32:44so that's a nice method as well we can
4:32:46do the same thing with popping which is
4:32:48removing elements from the deck uh so I
4:32:51can do pop in this case and I'm not
4:32:53going to do four in here so I do d. pop
4:32:56and then I can also do d. poop left and
4:32:58what pop left is going to do is it's
4:33:00simply going to remove the first element
4:33:02in Our Deck rather than the last so pop
4:33:05uh this works on lists as well and
4:33:07dictionaries and stuff you can remove
4:33:09the last element or whatever index you
4:33:11type in here like five two whatever
4:33:13that's the one it's going to remove pop
4:33:15left simply removes the first one so
4:33:17I'll do this and
4:33:19then print D and you can see that we are
4:33:21left with eell because we removed o and
4:33:24we removed the H okay another method
4:33:27that we can use is clear uh this one is
4:33:29simply going to remove everything from
4:33:31the deck so that's pretty standard if I
4:33:35print D
4:33:37again then you can see that we have an
4:33:39empty deck now okay the next few that
4:33:41I'm going to talk about here I believe
4:33:43you can use on some other containers in
4:33:45Python but they do work on this deck as
4:33:47well so let's go through that so now
4:33:48that we already cleared Our Deck let's
4:33:49just go ahead and add some more things
4:33:51to it so what we can do is we can do
4:33:53something called extend and what extend
4:33:56is going to do is it takes an iterable
4:33:57argument which means anything that's a
4:33:59container so like a list a string uh
4:34:01something like that and it's going to
4:34:03put it at the end of our list so if I
4:34:06have something like
4:34:08456 and I run the program
4:34:12and I print D you can see we get four
4:34:14five6 at the end of our list now to show
4:34:16you what happens if you already have
4:34:17something in I called it a list but our
4:34:19deck is if I extend again I'll extend
4:34:22something like so we can see it better
4:34:25hello then what we get
4:34:27is print D we get 456 and then hello so
4:34:32this is a way that to add multiple
4:34:34things into your deck so again if I make
4:34:37like a list and I go like 1 2 3 this is
4:34:39going to work as well so we should get
4:34:41four five 1 2 3 and print D and there we
4:34:46go we get that as well so if you ever
4:34:47don't want to run through like a for
4:34:49Loop to add an entire list into a deck
4:34:52then you can simply use extend extend
4:34:54also works really nicely um with extend
4:34:58left which is going to add to the
4:34:59beginning of our list so now we have a
4:35:01list of 4 5 6 1 2 3 let's see what
4:35:04happens if we extend the word hey um to
4:35:06the left side of the list so I'm going
4:35:10to print that and you can see we get y
4:35:1345 61 2 3 you might say well why didn't
4:35:16we just get H well it's because we're
4:35:18actually extending to the left so we're
4:35:20going to add this in the reverse order
4:35:23and that should make sense because if
4:35:25you're extending this first and then you
4:35:26extend this and then you extend this y
4:35:28should be the first element on the left
4:35:30side of Our Deck like that okay so now
4:35:34we're going to use um a few other
4:35:36methods so this one is probably the most
4:35:38useful one that the deck has and the
4:35:40reason why you would use it if
4:35:42especially if you're going to be dealing
4:35:43with things at the beginning and the end
4:35:45specifically and it is known as
4:35:47rotate so what this does is it takes a
4:35:51integer in here either positive or
4:35:53negative if you put a positive in
4:35:55integer in here it's going to rotate all
4:35:57of the elements by that amount to the
4:35:59right otherwise it's going to if you put
4:36:02a negative element in there rotate
4:36:03everything to the left so just see what
4:36:05happens if I put negative 1 in here to
4:36:07rotate to the left and then we'll print
4:36:09down here sorry it's at the bottom of
4:36:10the screen right now and you can see
4:36:12that we went from this one which was the
4:36:15last uh D or whatever when I printed it
4:36:17and now we've rotated everything over so
4:36:19three's moved over to the position of
4:36:21Two And when we get all the way over to
4:36:23here we've switched e and Y so E's moved
4:36:25over here and Y has been rotated back to
4:36:28the end of the list now you can actually
4:36:30rotate with other uh numbers as well so
4:36:32rotate by two and then you can see if I
4:36:36print D down
4:36:37here that we get a rotated two elements
4:36:40over now and this is is really useful
4:36:42when you're trying to solve certain
4:36:43problems uh to be able to use this
4:36:45rotate again you can do the same thing
4:36:46with positive so if I just rotate it by
4:36:48one now and I
4:36:51simply print D you can see that now
4:36:53we've rotated to the right side so three
4:36:55which was originally here has now been
4:36:57moved up to the beginning of the list
4:37:00okay so now that we've talked about that
4:37:01there's one last really useful thing um
4:37:04that I left out on purpose because I
4:37:05wanted to show now that we can do with
4:37:08these decks so when we initi iate Our
4:37:12Deck here we give it an iterable object
4:37:13we don't have to give it anything it'll
4:37:15just create a blank one if we do that
4:37:17but there's something that it has called
4:37:19Max Len now if I do something like Max
4:37:22Len 5 and I'm just going to remove all
4:37:24this and I'll show you how this works so
4:37:27I have a max line of five and I have a
4:37:29string that has five characters in it if
4:37:32I try to add something to my deck here
4:37:35so I'll just do I'll just simply I'm
4:37:36going to print it here just so it saves
4:37:37us some time going to print D and then
4:37:40I'm going to go d. and let's do one and
4:37:43then print D again let's just Watch What
4:37:46Happens so you can see we get we have a
4:37:49max line of five we start with hello and
4:37:51then when we add one H is actually
4:37:54removed from our deck now the reason
4:37:56that happens is because when we set this
4:37:58max length when we start adding things
4:38:00into the list it needs to remove
4:38:02something to maintain that um attribute
4:38:04of a maximum length of five so it
4:38:06removes the first element from our deck
4:38:10uh and that's really useful as well if
4:38:11you're solving problems because then you
4:38:13don't have to deal with removing things
4:38:14every time you add something to the end
4:38:15or removing like a slice of for example
4:38:18list if you're going to be adding more
4:38:20than one thing so if I
4:38:22extend like something like let's see
4:38:24here one two 3 now we should see that h
4:38:28e and l should all be removed and we
4:38:30should have L1 23 so let's try it and
4:38:32there we go we get hello and we get
4:38:35l123 and we didn't have to deal with
4:38:37figuring out how many elements we need
4:38:38to take from the left side from the
4:38:40right side because of this Max Len now
4:38:43just to note you can't change this Max
4:38:45len by doing something like d. Max Len
4:38:48equals five it's uh or I guess we'll do
4:38:51like six because it's already five you
4:38:53can only access what the max Len is by
4:38:56just doing is simply this
4:39:01print ma d. Max L and this should just
4:39:03give us five uh and it does but if I try
4:39:06to do what I was showing you there so d.
4:39:08Max Len equals 5 you'll see the error
4:39:11that comes up and it says this attribute
4:39:13of Max Len is not writable so we can't
4:39:15actually change it after we've initiated
4:39:17that value so anyways that's pretty much
4:39:20been it for deck within collections this
4:39:23is really useful and you might not see a
4:39:24use for it now but if you start solving
4:39:26some more advanced problems using this
4:39:28deck will make your life a lot easier
4:39:29with those methods that I went through
4:39:30and I'm going to copy all of them down
4:39:32uh into like a little paste bin thing so
4:39:34you can click the link in the
4:39:35description there if you want to see all
4:39:36of them in a list um so you don't have
4:39:38to specifically memorize that other than
4:39:40that you can always just go to the
4:39:42documentation and have a look if you
4:39:43just search up like DQ collections
4:39:45python it goes through what all of these
4:39:47do again I forgot to talk about reverse
4:39:50but it's pretty straightforward you can
4:39:51use reverse and if you're followed up to
4:39:53here you probably probably already know
4:39:55that one so anyways that's been it for
4:39:57this video um if you guys enjoyed the
4:39:59video please make sure you leave a like
4:40:01And subscribe and I will see you again
4:40:03in the next one
Overview of Python
4:40:16hello everyone and welcome to the first
4:40:18video in our python expert level
4:40:20tutorial series now what I'm going to be
4:40:22doing in this series is teaching you
4:40:24about some expert level features in
4:40:25Python how they work how to implement
4:40:28them and then getting into how you can
4:40:29actually use them and why you would use
4:40:31them in your python code now it's worth
4:40:33noting that a lot of the features I'm
4:40:34about to show you as much as they're
4:40:36very useful and complicated and you know
4:40:38Advanced and expert level you don't
4:40:39necessarily need to use them there's a
4:40:41time and a place and as you guys learn
4:40:43this you should refrain from using these
4:40:45features just to almost show off and
4:40:47actually make sure that you understand
4:40:49the purpose of using them and use them
4:40:50in the correct context that's what a lot
4:40:53of the focus of this series is going to
4:40:54be is giving some meaningful examples
4:40:56that try to tell you when you would
4:40:58actually use this feature or at least
4:40:59help you understand really why it works
4:41:02so that you could think about a
4:41:03situation when it might want to be
4:41:04implemented now quick disclaimer here if
4:41:06you guys are not python you know
4:41:08intermediates already or you're not
4:41:10familiar or fluent in python this is
4:41:12going to be a difficult tutorial series
4:41:13for you to follow along with you know
4:41:14you're welcome to as always but just
4:41:16keep that in mind I do have a lot of
4:41:17python tutorials on my channel that a
4:41:19more beginner level if you want to
4:41:20follow along with those before maybe
4:41:22jumping into something like this now in
4:41:24this video what I'm going to be doing
4:41:25specifically is talking about some very
4:41:27interesting and cool features in Python
4:41:30these are not anything specific they're
4:41:31nothing you may not have seen before but
4:41:34it's something that you need to kind of
4:41:36understand before we can move into the
4:41:37more advanced things so we're just going
4:41:39to be talking about how python code
4:41:41actually runs and why we can type out
4:41:43some weird lines like I'm going to show
4:41:45you um in the next few minutes anyways
4:41:48what I'm going to do now is briefly
4:41:49discuss how python code is actually ran
4:41:52and executed now I will say right now
4:41:54I'm not going to go into extreme detail
4:41:55I'm hopefully going to give you enough
4:41:57information so that you understand on
4:41:59kind of a surface level but if you do
4:42:01want to read into this I would recommend
4:42:02it because understanding how your
4:42:04program is actually compiled interpreted
4:42:06and eventually ran on your machine is
4:42:08very important and it definitely helps
4:42:09you understand why a lot of the things
4:42:11in the language work the way they do so
4:42:14most of you would have probably heard
4:42:16that python is an interpreted
4:42:17programming language but what does that
4:42:19really mean well to understand that we
4:42:21need to define the difference between a
4:42:22compiler and an interpreter and talk
4:42:24about how they work together because
4:42:26although python is interpreted it is
4:42:28actually compiled as well into bite code
4:42:30before it is actually interpreted so the
4:42:32way that python works and actually we'll
4:42:34go back to the definitions first so a
4:42:36compiler what does a compiler do well a
4:42:38compiler takes some highlevel code typ
4:42:41Ally high level code and translat
4:42:43translates it into a lower level so for
4:42:46example the python compiler that we use
4:42:48is going to take our python code which
4:42:50would be considered highlevel code
4:42:51that's furthest away from the hardware
4:42:53right and translates that into something
4:42:55called bik code which is a little bit
4:42:57harder for us to understand and closer
4:42:59to the computer's level so that it can
4:43:01actually be used and ran on our machine
4:43:03so what does an interpreter do well an
4:43:05interpreter takes some kind of code
4:43:08typically B code and interprets and runs
4:43:11that that code so it will read that
4:43:12codee and translate it on the fly into
4:43:15machine code that can be executed by our
4:43:17computer rather than doing this
4:43:19translation beforehand when you use
4:43:21something like C what you typically do
4:43:24and depending on the compiler that
4:43:25you're going to use there's tons of
4:43:26different versions and methods of doing
4:43:27this you will compile your code directly
4:43:30into machine code which means that it
4:43:31can be ran directly on the operating
4:43:33system that you're using and you don't
4:43:35actually need an interpreter to
4:43:37interpret your code and run it now I
4:43:39know this seems like a lot but this is
4:43:40something that we kind of need to
4:43:42understand so now I'm going to go into
4:43:43the way that Python's kind of execution
4:43:45chain is when you actually you know
4:43:46press that F5 button or do whatever it
4:43:48is you're going to do but essentially
4:43:50what happens in Python is you write your
4:43:52highlevel code which is going to be on
4:43:54your desktop you know in your text
4:43:55editor your python code and then when
4:43:58you press that run button or you go to
4:44:00execute that code what happens is it's
4:44:02translated into something called bite
4:44:04code now this translation is simply just
4:44:07a tool that checks all of the Syntax for
4:44:09your python code and then converts it
4:44:11into some equivalent code that can be
4:44:13read by The Interpreter that we're going
4:44:15to use so what happens is if you have
4:44:17invalid syntax like say I just type CL
4:44:20or something and that's not defined or I
4:44:21mess up and I don't have a colon or
4:44:23something like that then we get an error
4:44:25because the translation can't happen
4:44:27because the format of our code is
4:44:28incorrect and hence you know invalid
4:44:31syntax that's the error that you would
4:44:32get now assuming that your syntax is
4:44:34correct then what happens is you'll move
4:44:36on to the next stage where everything
4:44:38will be translated into bite code and
4:44:39then that bite code will run through the
4:44:41compiler and the compiler will translate
4:44:43that bite code in like a live time into
4:44:46machine code that runs and executes on
4:44:48your machine so this is why there's a
4:44:50lot of issues with python running on say
4:44:52mobile devices or on other devices that
4:44:54don't have a python interpreter because
4:44:56this bik code cannot be directly
4:44:58understood by the CPU you need some kind
4:45:00of interpreter to run it so hopefully
4:45:02that clears it up a little bit on how
4:45:04that works the basic summary is you know
4:45:06you write your high level code
4:45:07translated into bite code that bite code
4:45:10is then translated through an
4:45:11interpreter in live time as it's reading
4:45:14line by line so one at a time not all at
4:45:16the same time into machine code where it
4:45:19is executed and that's how your program
4:45:21runs this is different than a lot of
4:45:23other languages and each language has a
4:45:24very kind of complicated process of
4:45:27compiling and interpreting and running
4:45:29code so anyways I hope that kind of
4:45:31cleared it up a little bit but this
4:45:33feature of being interpreted and having
4:45:35a lot of things happening live is what
4:45:37we're really going to dig into here so
4:45:39the first thing that I want to show
4:45:41is something that's unique in Python
4:45:43that's not unique in a compiled language
4:45:45and that is the fact that a lot of
4:45:46checks and a lot of things that you
4:45:48typically can't even write with you
4:45:50could not compile code that exists these
4:45:52types of things in other languages you
4:45:54can do in Python so let me just show you
4:45:56what I mean all I'm going to do is
4:45:58create a class dog and inside the anip
4:46:01method um I'm simply going to call a
4:46:04method that does not exist called bark
4:46:06so this is my file um intro. piy we have
4:46:09a class dog a nit and a method self.
4:46:11bark that's being called but that method
4:46:13does not exist in the class body now for
4:46:15any of you that program in other
4:46:16languages you'd say okay well this is
4:46:18not going to work right if we don't have
4:46:21this method the compiler is going to
4:46:22pick that up it's going to say okay we
4:46:24cannot compile this code you need to
4:46:25Define that method or you need to remove
4:46:27the statement whereas in Python we do uh
4:46:31what was this python intro. py no issues
4:46:35we're totally fine we're good to go this
4:46:38is something that's unique about python
4:46:40is that a lot of the code is actually
4:46:41executed at runtime not at compile time
4:46:45all that the compiler really does for us
4:46:47in Python is just translate this code
4:46:49into B code it doesn't necessarily check
4:46:51that all this code is a valid now this a
4:46:53good thing and this is a bad thing we'll
4:46:55talk about why later on but that's
4:46:58something to understand that you can
4:46:59make errors like this in your python
4:47:01code and they will not be caught until
4:47:03runtime which can actually be a huge
4:47:05problem when you know you can't test
4:47:07every single aspect of your application
4:47:09your app gets through goes into a
4:47:11production environment something happens
4:47:12in runtime you didn't check it cuz the
4:47:14compiler didn't pick it up and now all
4:47:16of a sudden your code has crashed right
4:47:18so that's something to consider Okay so
4:47:20we've done that um that's fine that's
4:47:22just something I wanted to discuss and
4:47:23show you and that's kind of the way that
4:47:25python Works everything is actually
4:47:26happening kind of live it translates but
4:47:29then the runtime environment is what
4:47:31picks up on a lot of these errors okay
4:47:34so now let's get into some cooler things
4:47:35and talk about some strange things that
4:47:37we can do in Python that we can't really
4:47:38do in other languages so I'm going to
4:47:40Define a function and I'm actually going
4:47:42to make this function called make class
4:47:45so Define make class now what I'm going
4:47:47to do in here is get a parameter called
4:47:49X and I'm actually going to define a
4:47:51class inside of this function called dog
4:47:54just like we did before so I'm going to
4:47:56say class dog let say Define uncore
4:47:58uncore netore uncore we'll take a self
4:48:01value we'll take a name and then we'll
4:48:04just say self. name equals name okay now
4:48:08what I'm actually going to do is say
4:48:09Define print underscore value what I'm
4:48:12going to get this to do is simply print
4:48:14the value of x so we'll say print X like
4:48:18that and then I'm going to go down here
4:48:20and I'm going to actually
4:48:22return uh the dog like this now notice
4:48:26that I didn't return an instance of the
4:48:28dog by putting the brackets I simply
4:48:30returned dog itself which is actually a
4:48:33reference to the class not actually the
4:48:36uh the object so what I'm going to do
4:48:38down here now is I'm going to say CLS
4:48:40equals make uncore class and I'm just
4:48:43going to print out CLS and show you what
4:48:45this is so let's say CLS let's give the
4:48:47value 10 in here let's go and let's see
4:48:50first of all is this going to work does
4:48:52this not work why does this work what is
4:48:54this even doing well we'll talk about it
4:48:57okay so this actually worked so see what
4:48:59happened here we had this function
4:49:01called make class took an argument X and
4:49:03then we had a class dog that we defined
4:49:06inside of this function now first of all
4:49:08this should look strange to you but but
4:49:10the reason we can do this is because
4:49:12again the python compiler does not check
4:49:15if this is valid or not it doesn't
4:49:17really care so long as you have valid
4:49:20syntax so long as the format is correct
4:49:22your python code can run and it can
4:49:23execute and that's fine and that's
4:49:26exactly what's Happening Here we can
4:49:28define a class inside of a function
4:49:30because that's just the way python works
4:49:32we read from the top to the bottom left
4:49:34to write and that's totally fine that's
4:49:36totally valid now what have I actually
4:49:39done inside this function though well
4:49:40I've created a class that uses a value
4:49:43from the actual function argument here
4:49:46and then we actually return the class
4:49:47itself not an instance of that class now
4:49:50notice that's something that we can do
4:49:52in python as well since python code is
4:49:54running live it's kind of executing on
4:49:57the fly all of these things that we've
4:49:59defined here are actually being stored
4:50:00in memory so this um dog class here
4:50:03actually has a memory location for it
4:50:06that's why we can actually see this same
4:50:08thing with this function same thing with
4:50:10our VAR variable um and same thing with
4:50:12anything else that we Define that's
4:50:13something that's true about python is
4:50:15that all these things are actually
4:50:16defined and stored in memory which means
4:50:18we can interact with them live which is
4:50:20what we're doing right now so when I
4:50:23printed CLS we can see that I got
4:50:25main.m class. local.dog now let's just
4:50:28translate this a little bit although I'm
4:50:29sure most of you understand this main
4:50:31simply means this came from our main
4:50:33module which is a module we ran make
4:50:35class is the name of the uh I guess what
4:50:38is it function that we defined here
4:50:39locals Define what's inside of this
4:50:42function and then dog tells us the
4:50:44actual name of the class so that's what
4:50:45we printed out here but now let's see
4:50:47how we can actually use this so what I'm
4:50:49going to do is actually make an instance
4:50:50of this now I'm going to say d equals
4:50:53CLS name Tim and I'm actually going to
4:50:55say d. printor value like that and let's
4:51:00run this now so now we actually get the
4:51:02value 10 so what I've done is I've said
4:51:05okay well this CLS variable is actually
4:51:07a class since it's a class what I can do
4:51:10is use the way that I would typically
4:51:11use a class it's literally just another
4:51:13name for dog so we'll put our brackets
4:51:15we'll create an instance of it which
4:51:17makes self. name um you know equal name
4:51:20I can show you that as well by just
4:51:21printing d.name and then what we'll do
4:51:24is call the method on our object now the
4:51:26prints the value that we gave when we
4:51:28actually created this class so you can
4:51:30almost think of this um function that
4:51:32we've defined here as a I guess class
4:51:35Constructor where it's creating the
4:51:37class for us then we can use the class
4:51:40so I think that's pretty interesting
4:51:41that's something that's cool and again
4:51:43if we wanted to I could Define another
4:51:45function inside of here we can do
4:51:47another class inside of this class I can
4:51:49make seven classes inside of this
4:51:51function there is no limit to what you
4:51:53can do so long as you're using the
4:51:55correct syntax and you know it's not
4:51:57going to crash at run time right so
4:51:59that's something to consider now let's
4:52:00show some other weird cool things that
4:52:02we can do in Python um that's actually
4:52:04using some other kind of syntax so what
4:52:06I'm going to do now is I'm actually just
4:52:07going to make a for Loop and you might
4:52:09kind of be thinking about what I'm I'm
4:52:10going to do here so I'll say 4 I in
4:52:12range 10 what I'm going to do is Define
4:52:14a function I'm going to call this
4:52:16function just uh I don't know let's do
4:52:18show and all this is going to do is
4:52:20simply print the value I and let's just
4:52:23do I * 2 why not make it a little bit
4:52:25cooler now I'll call the function show
4:52:27here and let's run the code and no
4:52:30surprise to any of you when I run this
4:52:32that this should work fine and we get
4:52:34all of our values printing out so we can
4:52:36obviously do things like this I can
4:52:38define a function inside of a for Loop
4:52:40what I've done here is well only one
4:52:41show will actually ever exist at the end
4:52:44of this kind of runtime but I've defined
4:52:4610 different shows that do 10 different
4:52:48things and I've called them directly
4:52:50after and then they get overridden and I
4:52:52do them again so that's something that's
4:52:54cool and again another feature of python
4:52:55that you can't really do in some other
4:52:57languages now what happens if I take
4:52:59this show and I put it outside of the
4:53:01for Loop so it's not running at the same
4:53:02time as this function is defined at well
4:53:04let's run this we see we just get 18 the
4:53:07last show that was defined is the one
4:53:09that exists and the one that's created
4:53:11now why would you ever do something like
4:53:12this I don't really know maybe if you
4:53:14had a lot of content inside of this for
4:53:15Loop and you had a function you were
4:53:17going to use it multiple times but only
4:53:18locally you could Define it up here
4:53:21again doesn't make that much sense to me
4:53:22to do that but just showing you some of
4:53:23the features of the language okay so now
4:53:26let's show some other things that we can
4:53:28do um I'm trying to think of something
4:53:30that would be cool okay so let's do
4:53:32another one here let's say Define Funk
4:53:35what I'm actually going to do here is
4:53:36take some value let's just say x and I'm
4:53:38going to return a function from this
4:53:41function if the value of x is a certain
4:53:43value so I'm going to say if x equals
4:53:46equals let's just say one then what
4:53:48we're actually going to do is make a new
4:53:50function we'll say Define uh I'll just
4:53:52say like RV which will just be return
4:53:55value and then inside here we don't need
4:53:57any parameters all we'll do is say
4:53:59print X is equal to 1 okay otherwise
4:54:06we'll do is say Define RV and then we'll
4:54:10say
4:54:12print X is not one okay now we'll return
4:54:16this function RV and if you guys
4:54:18remember from what we've done before
4:54:19what I can do is say Okay newor Funk
4:54:23equals Funk of let's just do value one
4:54:26and then I can actually call my new
4:54:27function like that because notice if I
4:54:30don't do this right then let's have a
4:54:32look at this here nothing gets printed
4:54:34out to the screen but if I call my new
4:54:36function by putting my two braces
4:54:38outside of it here then what we get is X
4:54:41is is equal to one and we could change
4:54:43this obviously and then we would get a
4:54:45different value where we get X is not
4:54:48one so we can do interesting cool kind
4:54:50of like paradigms like this again I
4:54:52don't know why you would use this
4:54:53there's easier ways to accomplish this
4:54:55but it's something to consider is that
4:54:57this can actually happen and any
4:54:59arguments that we use here obviously we
4:55:00can use inside of these functions as
4:55:03well and we can get into some pretty
4:55:04complicated things when we start
4:55:05defining functions inside of functions
4:55:07inside of functions Okay so last thing
4:55:10that I want to show you is just a cool
4:55:11thing that we can do to actually look at
4:55:13some of the details of our objects
4:55:15because remember that I said everything
4:55:17in Python pretty much Happens Live this
4:55:20function is an object um this function
4:55:22is an object our Val variables are an
4:55:24object any classes are an object which
4:55:26allows us to do what I'm doing where
4:55:27we're passing them through and around
4:55:29the program they'll have their unique
4:55:31memory address and so long as we can
4:55:32reference that we can use it so let's
4:55:35actually see how we can inspect and look
4:55:37at some of our functions so first of all
4:55:39I'm just going to show you you know you
4:55:40probably seen this before we can look at
4:55:41the ID of our function if we'd like to
4:55:44just by doing this um now if I print
4:55:46this out this should give us the memory
4:55:48address location of our function which
4:55:49we can get right here um and there's a
4:55:52lot of other things that we can do as
4:55:54well and one thing that I want to show
4:55:55you is actually by importing the inspect
4:55:58module so the inspect module can show us
4:56:01some pretty cool things because of the
4:56:03fact that all of our python objects
4:56:05classes all of that are actually live we
4:56:07can interact with them we can inspect
4:56:10them we can ask questions about them um
4:56:12and get that value back from The
4:56:13Interpreter so what I can actually do in
4:56:15here is say print inspect dot uh what is
4:56:19this we can do I'll do get members for
4:56:21now just to show you so get members I
4:56:24won't really discuss exactly what this
4:56:25does but it's just going to show you
4:56:27some details about the function so let's
4:56:29look at this here we can see we get all
4:56:31of this little information and we can
4:56:32obviously use this if we wanted to let's
4:56:34see if we can pick anything out in
4:56:36particular um that stands out nothing
4:56:40okay so here we go function. Funk locals
4:56:42RV at this memory address location so it
4:56:45just gives us all this details that's
4:56:47called new funk is down here the name of
4:56:49it so we can get some stuff like that um
4:56:51not extremely useful we can't use that
4:56:53at other times another really cool thing
4:56:55is we can actually get the source which
4:56:57is the source code of a specific
4:56:59function method class whatever you want
4:57:01so here when I do this it actually tells
4:57:03us the source code of the specific
4:57:05python function now why would this be
4:57:08useful well if we wanted to see exactly
4:57:10what was happening in some kind of thing
4:57:12that we were using that was maybe
4:57:13Library code we couldn't access that
4:57:15actual code we could have a look at the
4:57:17source code like this so now I want to
4:57:20show you another useful thing of this
4:57:21inspect module if I were to import some
4:57:24module and this is kind of a good use
4:57:26case of it say like Q right which is a
4:57:28python
4:57:29built-in um what is it from from Q in
4:57:32for Q This is a built-in data structure
4:57:34from Python and I wanted to look at the
4:57:35code of it obviously there's other ways
4:57:37to do this but if you wanted to just
4:57:39quickly do it in your terminal
4:57:41then what you could do is do something
4:57:42like that and now we can actually see
4:57:44all of the code that's been used by
4:57:46python itself to create this object or
4:57:48create this class so I think I'm going
4:57:50to wrap it up here it's been about 15
4:57:51minutes I hope that this gives you guys
4:57:53a good understanding of python a little
4:57:55bit more how we can pass objects around
4:57:57how we can look at their values we can
4:57:59inspect things we can look at the source
4:58:01code and just remember that everything
4:58:02in Python is pretty well happening live
4:58:04all these objects are defined in memory
4:58:07and well since they're defined in memory
4:58:08we can do a lot of cool things with them
4:58:10like pass them around the program and
4:58:12since the python is an interpreted
4:58:13language we can do all these crazy
4:58:15things where we Define classes inside of
4:58:17functions functions inside of classes
4:58:19inside of functions that we can go crazy
4:58:21and we'll continue to do that as we get
4:58:23further into this tutorial Series so if
4:58:25you guys enjoyed make sure you leave a
4:58:26like subscribe and I will see you guys
4:58:27in another expert Python
4:58:33tutorial hello everybody and welcome to
Dunder/Magic Methods
4:58:36video 2 in the python expert tutorial
4:58:38Series in this video we're going to be
4:58:40doing is talking about Dunder slashmagic
4:58:42methods and the python data model now
4:58:44this is very important this is something
4:58:46that I guarantee you've seen before but
4:58:48you probably just haven't understood
4:58:49what it actually was or why it was
4:58:51working the way that it did and this is
4:58:53actually going to give you a great
4:58:54insight into the way that a lot of
4:58:55things in Python actually operate
4:58:57because until you see this you kind of
4:58:59just take them for granted so what I
4:59:01want to do is give you a very basic
4:59:02example of two objects in Python um just
4:59:05kind of being used and interacting
4:59:06together I'm going to show you how we
4:59:08can create the same thing with our own
4:59:10python objects and even modify existing
4:59:13python objects to implement uh
4:59:15implementations or operations that we
4:59:16want so what I'm going to do to start is
4:59:19I'm going to just make two uh variables
4:59:20here I'm just going to make a list I'm
4:59:21say 1 2 3 and I'll make another list
4:59:24that just says you know four five like
4:59:26that now we know that with lists we can
4:59:28add them together by using the plus sign
4:59:31right we know that that's fine that
4:59:33we're allowed to do that so let's
4:59:34actually have a look at how that looks
4:59:35we get that list and it adds them
4:59:37together now why does that work we don't
4:59:39really no we kind of just assume that
4:59:41that's a feature in Python that we can
4:59:43add list together right and that's
4:59:44totally fine for our intermediate kind
4:59:46of understanding so what about length
4:59:48right I can get the length of a list
4:59:50that works fine too so if I look here I
4:59:53get a length of three and there's all
4:59:54kinds of things that I can do on a list
4:59:56like I can index something on a list I
4:59:57can put you know X1 and that gives me
5:00:00some value here and you're like well why
5:00:02are you showing me all this stuff
5:00:03because you're going to see that this
5:00:05kind of syntax that we're using right
5:00:07here on this object because this list is
5:00:11actually an object and I'll prove it to
5:00:12you just by printing the type of it and
5:00:14telling you that is actually a part of a
5:00:15class you can see class list is
5:00:18implemented under the hood now you might
5:00:21not understand what I mean by that right
5:00:22now but I'm actually going to import
5:00:25inspect and show you and then we'll get
5:00:27into a little bit deeper of this
5:00:28understanding so what I'm going to do is
5:00:30actually just say
5:00:32print
5:00:34inspect doget source of list Now list
5:00:38this is going to be ridiculous when we
5:00:40look at this in the terminal um list is
5:00:42a buil-in class oh it won't let me uh
5:00:43look at the source of the buil-in class
5:00:45okay that's fine we don't need to look
5:00:46at it for now um but anyways the whole
5:00:49point of this is that these objects here
5:00:53this is an object this list is they're
5:00:54both objects we take for granted the
5:00:57fact that we can perform certain
5:00:58operations on these objects using some
5:01:01special python syntax right like even
5:01:02being able to multiply list together
5:01:04like I can do X multiplies by 3 right
5:01:07that's kind of a special syntax why is
5:01:09it that we're able to to do that well
5:01:10that's because this operation is
5:01:12implemented on the list object itself
5:01:14and it tells the list object how to
5:01:16behave such that it sees a
5:01:19multiplication sign right after it or
5:01:20such that it sees a plus sign or it sees
5:01:22this index or it sees a call we
5:01:25Implement that functionality and because
5:01:27we can do that we can Implement that on
5:01:28our own objects now let's do one more
5:01:31example so obviously we saw when we
5:01:33printed out the value of this right we
5:01:35printed out X um we just printed like it
5:01:38just looked exactly like this it printed
5:01:401 2 3 right or in fact actually let me
5:01:43show you if I do a space here and I just
5:01:45print X let's have a look at what we get
5:01:47here we can see that this gets uniformly
5:01:50spaced even though there wasn't a space
5:01:52here so it's not the fact that it's just
5:01:54printing out exactly what this looks
5:01:55like there is something under the hood
5:01:57that's telling python what this list
5:02:00object looks like and what to print when
5:02:02we decide to print it out so let's
5:02:04create our own object now actually so
5:02:06I'm just going to say
5:02:08class person like this and let's just
5:02:11Define in a knit method now this is
5:02:13actually a double underscore method or a
5:02:15Dunder method or magic method whatever
5:02:17you'd like to call it we'll talk about
5:02:18how this one works in a little bit
5:02:20although I'm sure you guys understand so
5:02:22we'll just make our object uh person I'm
5:02:24going to say p equals person like this
5:02:27Make an instance give it a name of say
5:02:29Tim and then if we print the value of P
5:02:32well if we were you know if we looked at
5:02:34the list before and we saw that the list
5:02:36printed 1 2 3 4 just printed exactly
5:02:38what it looked like we we would assume
5:02:40that when we print P it should just
5:02:42print person Tim right it should print
5:02:44this well when we look at it it doesn't
5:02:47do that and I'm sure you've seen this
5:02:48before it prints the memory address
5:02:50location now the reason it does that is
5:02:53because we have not told person what to
5:02:55do when we try to print it it does not
5:02:58know what that what we should show so by
5:03:00default it shows the information that it
5:03:02thinks would be valuable which is simply
5:03:04just its memory address location right
5:03:06that's its like
5:03:07representation internally in the python
5:03:10program so how can we change this well
5:03:12we can Implement what we call a Dunder
5:03:14method or magic method which is called
5:03:17repper now I'm sure you've seen this
5:03:19before but essentially what rapper does
5:03:21is allows us to define the string
5:03:23representation of an object um from
5:03:26inside of it so what I'm going to do is
5:03:27make an F string and I'm simply going to
5:03:29say person and then in here self. name
5:03:33so now when we decide to print this out
5:03:36what we get is person Tim and I'll just
5:03:38do it one more time instead of getting
5:03:41that crappy you know just gibberish of
5:03:43memory address location so this is the
5:03:45first thing that we can do is we can
5:03:46Implement methods such as this on our
5:03:48own objects um to implement some kind of
5:03:50functionality now let's show a few
5:03:53others and why this is so powerful so
5:03:55there's actually a whole list of what we
5:03:57call Dunder methods and this is what's
5:03:59part of the Python data model and some
5:04:01people call these just data model
5:04:02methods and essentially um you guys can
5:04:04read through this if you want I'll leave
5:04:06the link in the description someone
5:04:07remind me if I forget if we scroll scr
5:04:10down to the bottom here we'll start to
5:04:12see that there's all kinds of these
5:04:14double underscore methods that we can
5:04:15actually Implement on our own objects
5:04:17and obviously I'm not going to go
5:04:18through all of them because there's just
5:04:20so many to kind of talk about uh but
5:04:22let's keep going down here so for
5:04:23example new nit Dell repper right we
5:04:26have this one here string bytes format
5:04:30uh ltle e EQ NE GT G we'll talk about
5:04:33what all those are hash um bull right
5:04:35get attribute set attribute dur Dell
5:04:38attribute multiplication there's all
5:04:39these kind of things that we can do so
5:04:41what I'm going to do is actually do a
5:04:42weird one and I'm going to U implement
5:04:44the multiplication Dore method so what
5:04:47this tells python to do actually um is
5:04:51what happens when we use a
5:04:53multiplication operation on objects of
5:04:56this type on objects of type person so
5:04:58what I'm actually going to do in mole
5:05:00here I'm going to assume actually X is
5:05:02going to be some integer and what I'm
5:05:04going to do is just take the person's
5:05:06name and multiply it by whatever the
5:05:07integer is and that's the operation
5:05:09we'll implement when we use the star or
5:05:11the asteris on the person object so what
5:05:13I mean by this is I'm just simply going
5:05:15to say
5:05:17selfname equals self. name time x and
5:05:21I'll even do a little just um thing up
5:05:23here I'll say if uh Type X is not int
5:05:29then we'll simply just throw an
5:05:30exception so we'll say
5:05:32raise exception we'll just
5:05:35say invalid
5:05:38argument must B in uh or in yeah
5:05:41whatever it doesn't really matter what
5:05:42we put there but that's fine okay so
5:05:44we'll say p equals person now what I'm
5:05:46going to do is simply print p uh we
5:05:49won't print that actually we'll just say
5:05:50p * 4 and then we'll print p and let's
5:05:53have a look at what this gives us okay
5:05:56person tim tim tim tim tim right so we
5:05:58can actually Implement whatever
5:06:00functionality we want using this upper
5:06:02level python syntax by implementing some
5:06:04kind of lower level Dunder method and
5:06:06that's what the whole point of this
5:06:07video was to do was to show you that
5:06:09everything that we use in Python all of
5:06:11these different symbols can be
5:06:13implemented on a lower level on our own
5:06:15objects and these are again our like
5:06:16data model uh methods right so we can
5:06:18create objects such that we can add them
5:06:20we can multiply them we can have
5:06:22representations we can do a call on them
5:06:24we can do all kinds of crazy things for
5:06:26example let's do Define uncore call
5:06:29let's just do self let's take some
5:06:30argument why not why and let's just
5:06:32print this value so what I'm going to do
5:06:36is now instead of printing P I'm just
5:06:38going to do p and put value four in
5:06:41there and now let's watch what happens
5:06:43so if I go like this we get the value
5:06:46four printing out we could even change
5:06:48this called this
5:06:51function right four so what happens is
5:06:55we can implement the fact that these two
5:06:57brackets what these two brackets do if
5:06:59they are on an object which is really
5:07:01cool it allows us to make objects that
5:07:03are much more usable much more readable
5:07:05and just almost seem like they fit in
5:07:06with the standard python documentation
5:07:08because I don't need to necessarily call
5:07:10a method I could actually just put the
5:07:11brackets now anything you can think of
5:07:14with this kind of syntax you can
5:07:15probably implement for example Define
5:07:17underscore uncore lencore uncore self
5:07:21return the Len of self. name maybe
5:07:25that's how long a person is unless
5:07:26you're going to ask for their height or
5:07:27something like that then what we could
5:07:29do is print the Len of P right so if we
5:07:33go like this we get three as that's the
5:07:36name so that's kind of the idea behind
5:07:38these Dunder meth in these data model
5:07:40methods the whole point is everything
5:07:42that's above even something like
5:07:44division you know greater than or equal
5:07:46to um less than whatever it is equals
5:07:49index all of these things can be
5:07:51implemented and if you want to see the
5:07:52implementation details of how all these
5:07:54work then you can read through this data
5:07:56model U documentation because there is a
5:07:58ton of them the whole point is you don't
5:08:00need to memorize these you just need to
5:08:01know that they exist and then you can
5:08:03think about for example if I have a
5:08:05class and I want to implement some kind
5:08:07of functionality maybe I have a
5:08:08polinomial class I want to add them or
5:08:10multiply them together then rather than
5:08:12having to make my own method called
5:08:14multiply what I can do is simply
5:08:16implement the mole um double uncore
5:08:19method and then that will allow me to
5:08:20use a star on it now notice you're going
5:08:23to want to make these types safe because
5:08:25obviously I multiplied by an integer but
5:08:26you could technically multiply two
5:08:28people together um you can do all kinds
5:08:30of things such as that okay so now I'm
5:08:32just going to show you something
5:08:33interesting this is just maybe to kind
5:08:34of inline you a bit or to show you
5:08:36something that we could actually do in
5:08:37theory so what I'm going to do is import
5:08:40actually let me say from Q import q and
5:08:46import inspect now what I'm going to do
5:08:49is just make a q object so I'm just
5:08:50going to say qal Q if you don't know Q
5:08:53it's just a built-in data structure in
5:08:54Python works as a q pretty
5:08:56straightforward and print Q okay so I
5:08:59just want to print Q to just show you
5:09:01that Q does not actually Implement a
5:09:03rapper method so let's just do this um
5:09:05and you can see that we're getting some
5:09:07random you know kind of memory address
5:09:09out to the screen so if I wanted to
5:09:11actually see why this wasn't giving me
5:09:14that representation I could have a look
5:09:16into Q itself so I can
5:09:18say
5:09:20print
5:09:22inspect uh doget Source like that and
5:09:25then I could print my just Q object like
5:09:28this not Q object sorry Q class have a
5:09:31look at the source code and we'll see
5:09:32that if we scroll through it here we
5:09:35don't have any Dunder methods that are
5:09:36implementing any functionality so let's
5:09:39say you wanted to make your own Q class
5:09:41that worked very similar to the python Q
5:09:43class but you wanted to do things like
5:09:44allow the plus sign or you wanted to do
5:09:46things like allow the minus sign well
5:09:48what you could do is you could say from
5:09:50Q import Q as Q like that then you could
5:09:54say your own
5:09:55class class Q is extends from q and then
5:10:00you can Implement your own Dunder
5:10:01methods so that you could override this
5:10:03so for example Define uncore rapper like
5:10:07this and then what I want to do is maybe
5:10:09I want to show a q but I want to say how
5:10:11many elements are in the Q well then in
5:10:14that case what I would do is return q
5:10:17and then I would say q and then I guess
5:10:19in this case going to be self
5:10:21doore Q length or Q size something like
5:10:26that now now what I do if I create a q
5:10:29say uh you know qu equals Q like so and
5:10:34I decide to print out
5:10:36qu like that so let's have a look
5:10:40and we can see we get Q with zero items
5:10:42now the reason I knew to use Q size is
5:10:43because I read through the source code
5:10:44and I saw that Q size returns a length
5:10:46of self doq and I see here we have self.
5:10:49q equals DQ uh what else do we have all
5:10:52these other things so this Q actually
5:10:53just implements a DQ object uh where's
5:10:56the init it's right here it can read
5:10:58through you can understand how this
5:11:00works so if you wanted to actually add
5:11:02something to the que right using maybe
5:11:04the plus sign and you didn't want to
5:11:05implement something else then what I
5:11:07could do is say Define underscore uncore
5:11:08add
5:11:09_ self um item like that and then what I
5:11:14would simply do here is okay let's add
5:11:16that to the Q so we'll say self dop put
5:11:20and then item so now if I want to add
5:11:22something to my Q I can say Q + 9 and it
5:11:25doesn't even need to be equals I can
5:11:26literally just do+ 9 and if we come here
5:11:29and we run this now we have an object CU
5:11:31with length one right and I could keep
5:11:33doing this I do Q + 7 right like that
5:11:38and then if I wanted to I could
5:11:39literally Implement a negative sign so I
5:11:41can say Q minus we have to put something
5:11:44here but if I want then I'd say Define
5:11:45uncore
5:11:47subcore
5:11:48self item and then all I can do is say s
5:11:51let's just say
5:11:53self. getet like that and that's all we
5:11:57need to do so now if we run this we
5:11:59should see Q as length one and even
5:12:01though I mean I don't like I just put a
5:12:02minus sign like this actually I'm
5:12:04curious if this will even work um Q
5:12:06minus imbal syntax so I could do like Q
5:12:07minus none if I wanted to uh and then
5:12:10that still will remove that item for me
5:12:12so this is the idea right these aunder
5:12:13methods allow you to implement these
5:12:15kind of syntax things this higher level
5:12:17syntax and this is what's called again
5:12:19the data model in Python this is how all
5:12:21these different objects work and these
Metaclasses
5:12:23Dunder methods can be very useful
5:12:25there's a lot of different ones I'm not
5:12:26going to go through all of them but the
5:12:27whole point is that you understand that
5:12:29each one of these kind of unique pieces
5:12:31of syntax in the higher level of python
5:12:33maps to a lower level Dunder method that
5:12:35implements that imp implements that
5:12:37operation right so that's the way that
5:12:40this works so yeah so hopefully that
5:12:42kind of cleared things up on Dunder
5:12:44methods and Magic methods they're really
5:12:46not that complicated they're very useful
5:12:48and when you're creating classes where
5:12:50these operations make a lot of sense to
5:12:52do like adding subtracting multiplying
5:12:54especially working with numeric values
5:12:56being able to avoid having to create
5:12:57your own methods like Define add x y and
5:13:01being able to just implement the dunder
5:13:03method add and use the plus sign can
5:13:04make things a lot more intuitive um and
5:13:06just easier to read in your programs so
5:13:08anyways that has been it I hope you guys
5:13:10enjoyed this video on Dunder methods
5:13:12magic methods and the python data model
5:13:14as always if there's anything you'd like
5:13:16to see in the future videos please do
5:13:17leave a comment down below like
5:13:19subscribe and I will see you in the next
5:13:25video hello everyone and welcome back to
5:13:27another python expert tutorial series
5:13:29what we're going to be doing in this
5:13:30video is covering meta classes a fairly
5:13:32complex but very interesting topic in
5:13:34Python so we're going to start from the
5:13:36very beginning and start understanding
5:13:38how classes are actually created
5:13:40instantiated um the way they work on a
5:13:42lower level in Python and then from
5:13:44there we're going to move into what meta
5:13:45classes are how they actually work and
5:13:48why we would even want to use them now
5:13:50I'm just going to preface this video by
5:13:51saying meta classes are an extremely
5:13:53complex thing I'm only going to be
5:13:55showing kind of the basic implementation
5:13:57of meta classes here if you really do
5:13:59want to use these you're going to have
5:14:00to look up the documentation and
5:14:02implementation details on your own and
5:14:04there's very few instances in which you
5:14:06need a meta Class A lot of what I'm
5:14:08about to show you can be accomplished in
5:14:09other ways and typically you're going to
5:14:11want to use another way other than a
5:14:13meta class just because these are seen
5:14:14as very complicated and almost sometimes
5:14:16bad practice depending on what you're
5:14:18trying to do so these should really only
5:14:20be used by experts I mean I'm showing
5:14:22them to you guys so you can get an
5:14:23intuition on how they work and if you
5:14:25see one you'll understand what it does
5:14:27but you shouldn't really be putting
5:14:29these into your own code and using them
5:14:30unless you absolutely need to so before
5:14:32we get into the content again a quick
5:14:34thank you to kite for sponsoring this
5:14:36video series they have a download link
5:14:38in the uh in the description down below
5:14:40you can download them for free they are
5:14:41essentially a very good python
5:14:43autocomplete so you can see if I start
5:14:45typing some things like this we get a
5:14:47kite completion thing popping up and
5:14:48anyways that's what they are they're
5:14:49free you can download that from the link
5:14:51in the description all right so let's go
5:14:53ahead and get started now the first
5:14:55thing that I want to do is really
5:14:57discuss how classes work and what
5:14:59classes are so we saw in the previous
5:15:02example and I believe two videos ago I
5:15:04did something crazy like this right and
5:15:06I made a class and I find it inside of a
5:15:09function and then in that function I
5:15:11actually just returned the class itself
5:15:14and this is totally fine this is valid
5:15:15syntax you can see I just ran the code
5:15:17down here we don't get any errors that's
5:15:19fine the reason we can do something like
5:15:22this in Python and not in other
5:15:23languages is because in Python classes
5:15:26are actually objects now the property of
5:15:29an object essentially means that we can
5:15:30interact with it at runtime we can pass
5:15:32it around through um parameters through
5:15:35variables we can store it we can save it
5:15:37we can modify it we can interact right
5:15:39that's what an object is now a lot of
5:15:41people are going to say well how is a
5:15:42class an object I thought a class
5:15:45created objects for us well that's very
5:15:47true a class does create objects for us
5:15:50but that doesn't mean that it's not an
5:15:52object itself and if a class is an
5:15:54object if you believe what I'm saying
5:15:56and I'll prove it to you in a second
5:15:57then that means we must have had some
5:15:59higher level class that created that
5:16:01object for us right just like if I Make
5:16:03an instance of high and I do something
5:16:05like that that means we must have this
5:16:07class High such that we can make an
5:16:09object you know and have the instance
5:16:11for it so what we're going to be getting
5:16:13into here with meta classes is the basic
5:16:16idea is a class defines the rules for an
5:16:19object right it defines the attributes
5:16:21the parameters the methods the things
5:16:22that are allowed the operations that can
5:16:24be performed that's what a class does
5:16:26for an object what a meta class does is
5:16:29Define the rules for a class so when you
5:16:31create a class you will use the meta
5:16:34class to create it this happens
5:16:35automatically you don't need to type
5:16:37anything specifically but we're going to
5:16:38hook in to it later and see how it
5:16:39really operates and that meta class
5:16:42defines how this class is created and
5:16:44that's the basic concept here so I'm
5:16:47going to make another class and I know
5:16:48this is confusing but as we go through
5:16:50here you're going to start to slowly
5:16:51understand with these examples and all
5:16:53I'm going to do is just print an
5:16:55instance of this class so let's look at
5:16:57it we can see this tells us it's an
5:16:59object main test object at some
5:17:01gibberish location so now let's print
5:17:04above this just the class
5:17:07itself okay look at that class main.
5:17:11test now the reason we're able to do
5:17:13this again is because this is an object
5:17:14so I know this doesn't explicitly say
5:17:16object but this class is an object now I
5:17:19want to look at something else so we
5:17:21know we have this method in Python
5:17:23called type and I can look at the type
5:17:25of a uh a class or an instance or an
5:17:28object and I see what it is so let's
5:17:29look at it when we do the type of test
5:17:32we get that is in class main if I were
5:17:34to print say the type of an integer
5:17:37right like the type of two let's have a
5:17:40look that's class int right and we could
5:17:42print you know the type of whatever we
5:17:43want we could print the type of a
5:17:45function so we say
5:17:47Define Funk like that and then just
5:17:50we'll just say pass in there and we do
5:17:52Funk what's that that's class function
5:17:55right so that exists in a function so
5:17:56this is an object this function is an
5:17:58object everything in Python is an object
5:18:00and that means they have a type and they
5:18:02were created somehow so what is the type
5:18:05of a class well let's look at it so
5:18:08we'll delete all this and then we'll
5:18:10just
5:18:11print the type of and I somehow hit
5:18:15insert accidentally there of test so
5:18:18what do you think this type is is it of
5:18:20type test is this class of that type no
5:18:23it is of Class Type now I know this
5:18:25seems ridiculous this means nothing to
5:18:27you right now we're going to dive into
5:18:28what this is but essentially this is
5:18:30what we would refer to as the meta class
5:18:32so this type is what essentially defines
5:18:35the rules and creates this class for us
5:18:38when we type this class syntax we will
5:18:41call a type Constructor using the
5:18:44different things in our class to make
5:18:46this class object that we can then use
5:18:48to make objects and use to interact with
5:18:50in the program so what is Type how does
5:18:53this type thing work well if I told you
5:18:55that this class all this is doing the
5:18:57syntax is just calling this type class
5:19:00then we should explicitly be able to
5:19:02create our own objects without typing
5:19:04out the syntax like this we should just
5:19:06be able to use the type class itself
5:19:08which is true and what I'm about to show
5:19:10you so I've just created class test it
5:19:12it has passed it has nothing in it right
5:19:14now I'm going to show you how we can
5:19:15make this without actually having to
5:19:17type this kind of syntax and writing out
5:19:19class in fact and this seems ridiculous
5:19:22I agree python should have done a better
5:19:23job when they defined this but I believe
5:19:25it's something to do with just backwards
5:19:27compatibility we can make a class by
5:19:29doing this and just watch and then we'll
5:19:31talk about exactly how it
5:19:35operates so this line of code is
5:19:38completely equivalent to this these two
5:19:42things are completely equivalent there's
5:19:43absolutely nothing different about them
5:19:46and I can use test here the same way I
5:19:48can use test here just like I can create
5:19:50an instance of test I can create an
5:19:52instance of this other test so let's do
5:19:54it let's let's prove it to you so if I
5:19:56print test and Make an instance boom
5:19:59main test object Works totally fine no
5:20:02issues with that what else can I do I
5:20:04can print the class and again same thing
5:20:06we have that object totally fine that's
5:20:08because this function here essentially
5:20:11creates the class for us using these
5:20:14different arguments a name this is the
5:20:17internal representation of the class any
5:20:19bases which means anything that we
5:20:21inherit from so like a super class or a
5:20:24parent class and then any attributes so
5:20:26let me start showing you what I mean uh
5:20:28and then hopefully this should make some
5:20:29more sense so I'm going to set an
5:20:30attribute of five I'm going to say t
5:20:33equals test Make an instance and just
5:20:35print that attribute uh go like this of
5:20:39t.x so just like this works on a regular
5:20:42class we can do that we get our value
5:20:44five now just like in regular classes as
5:20:46well I can Define attributes outside of
5:20:48the class so you say something like t.
5:20:51Wy why not set that equal to let's just
5:20:54say hello want to print Wy sure let's go
5:20:58for it uh T.W one second
5:21:03guys oh T is not defined so that needs
5:21:06to go above so I can explain meta
5:21:08classes but apparently cannot just
5:21:09create regular classes okay so that's
5:21:10totally fine that works we have t equals
5:21:13test and then T.W equal hello we can
5:21:15print that attribute that's totally fine
5:21:17so that's how we can create classes
5:21:18using type now what about methods what
5:21:20about sub I'm going to show you all that
5:21:22so we'll do that now uh and then we'll
5:21:24get into actually meta classes and how
5:21:26this is even useful to us so what I'm
5:21:28going to do is make another class I'm
5:21:30just going to call this class Fu let's
5:21:31just make a method in here just called
5:21:33print uh actually not print let's do
5:21:35like show and then all we'll do in here
5:21:37is take one attribute we'll say self and
5:21:40just
5:21:41print hi why not right just do that so
5:21:44if I want to now have an inheritance
5:21:47what I can do is simply type the name of
5:21:49the Class A Comma just to make sure this
5:21:51registers as a tble Here and Now test
5:21:53will inherit from F which means that if
5:21:55I want to use the method show I can do
5:21:58that and I can run that like that and we
5:21:59get that now I shouldn't have printed
5:22:01that out because I forgot that wasn't a
5:22:02return value but you can see we get high
5:22:04that works totally fine this is how you
5:22:06do this right now what if I want you add
5:22:09methods right so to add a method is a
5:22:10little bit different but essentially
5:22:12what you're going to do is just Define a
5:22:13function that will be the method
5:22:14signature so in this case let's say you
5:22:17know add
5:22:19attribute put self like that then here
5:22:22we just say self. Z equals 9 why not
5:22:25right we'll do that and then in here
5:22:28we'll Define our attribute so we'll say
5:22:30addore
5:22:31attribute colon guess what we're going
5:22:33to do add attribute no brackets now make
5:22:36our instance let's call t. add attribute
5:22:40and then let's
5:22:42print t. Z like that and let's look at
5:22:45this and there we go we get the value
5:22:47nine that works fine so this is how we
5:22:49use type this is how we create a class
5:22:52and this is very important to understand
5:22:53as we get into the next part of the
5:22:55tutorial here because if you can
5:22:56understand how we use this to create our
5:22:58own classes then you're going to
5:23:00understand the next part and why when we
5:23:02have a syntax like this all this really
5:23:05does is pass that information to another
5:23:08class called type that creates the class
5:23:11for us and returns an object that
5:23:13represents that class now I know this
5:23:14was a lot I hope this you guys
5:23:16understand at this point now we're
5:23:18actually going to get into meta classes
5:23:19this was just the underlying information
5:23:20we need to understand all right so
5:23:22welcome to the next part of the tutorial
5:23:24what we're going to be doing now is
5:23:25actually getting into meta classes and
5:23:27showing how they work so again the basic
5:23:29principle here is to remember that a
5:23:30meta class is above the classes you're
5:23:33creating yourself so if you create
5:23:34something like class dog right and you
5:23:37do that what's actually happening is all
5:23:39this information that you typee out the
5:23:41syntax gets passed to some meta class
5:23:44the thing above it that actually takes
5:23:46that and returns to you an object that
5:23:48represents that class and that's the
5:23:50whole point here so what we're going to
5:23:51do is actually make our own meta classes
5:23:53so rather than using that built-in type
5:23:55class and you could see on all of our
5:23:57objects when we looked at the type of
5:23:59them they were of type type we're simply
5:24:01going to make our own meta class that
5:24:03inherits from type so does a very
5:24:05similar thing but we're going to change
5:24:07slightly how objects are constructed so
5:24:09you can really understand how this works
5:24:11now you might get lost a little bit at
5:24:13the beginning when I start typing some
5:24:14of this stuff out but hopefully you
5:24:15should start to follow along as we go
5:24:17through and explain how all this works
5:24:19so let's get started I'm going to start
5:24:21just by creating a meta class and I'm
5:24:22going to call this meta now whenever you
5:24:24make a meta class what you have to do is
5:24:26make it inherit from type uh not
5:24:29necessarily but for our purposes that's
5:24:30what we're going to say it has to
5:24:32inherit from type to make a meta class
5:24:34the reason for that is because type does
5:24:36some other things when it's creating an
5:24:37object right and if you don't inherit
5:24:39from this well your class is just going
5:24:41to be not a meta class now because it's
5:24:43going to be using type to create this
5:24:45kind of class if that makes any sense
5:24:48whatsoever but hopefully that does what
5:24:50we're going to do now is I'm actually
5:24:52going to define a new method like this
5:24:55and for those of you that don't know
5:24:56what new does as a Dunder method or
5:24:58magic method like we talked about before
5:25:00essentially this is called before the
5:25:02init method so this is the first thing
5:25:04that is always called when a object is
5:25:06created or instantized or whatever you
5:25:08want want to call it this is always
5:25:09called first so what you can actually do
5:25:11here is hook into this and modify the
5:25:13way an object is constructed by changing
5:25:16what happens in this new method whereas
5:25:18the init all the init is doing is after
5:25:20this new method gets called and the
5:25:22object is constructed what it does is it
5:25:25um initializes uh it changes the values
5:25:28it takes some parameters in like that's
5:25:29what it does right whereas new is just
5:25:32called beforehand to modify the
5:25:33construction of the object okay so what
5:25:35I'm going to do in here is I'm going to
5:25:37take self
5:25:38I'm going to take class name and
5:25:40remember this is what's going to be
5:25:42called whenever we create another class
5:25:44that uses this as a meta class we're
5:25:45going to say class name we're going to
5:25:47say bases and bases is what we put
5:25:50remember in that type argument when we
5:25:52want to do inheritance we have the name
5:25:54we had the bases and we have the
5:25:55attributes and guess what this last
5:25:57one's going to be it's going to be
5:25:58attributes so essentially we're saying
5:26:00okay when we create this new method the
5:26:03construction of a class which happens in
5:26:06this meta class needs to contain the
5:26:07class name the bases and the attributes
5:26:10so that that way we can look at them we
5:26:11can do something with them and we can
5:26:13return to you a class that has those
5:26:15pieces of information right as an object
5:26:17so what I'm going to do now is simply
5:26:19just return the type of in this case
5:26:22it's going to be class uncore name bases
5:26:26and htrs now this is very simple we're
5:26:28not really going to do anything other
5:26:29than this but I'm also just going to
5:26:31print out the
5:26:33attributes so you can see what they look
5:26:35like okay now what I'm going to do is
5:26:37I'm going to make another class I'm
5:26:38going to call it dog this is going to
5:26:40have a meta
5:26:42class that's equal to meta like that and
5:26:47then in here all we'll do is just say x
5:26:49= 5 y = 8 just so we can look at some
5:26:51things when we print these attributes
5:26:53out and then we'll create an instance of
5:26:55dog so we'll say d equals dog like that
5:26:58and run the code all right so look at
5:27:01this what has happened well actually I'm
5:27:04even going to get rid of this and just
5:27:05show you that even if I don't create an
5:27:07instance this still runs right you can
5:27:08see no instances this is still running
5:27:11the reason for this is because the
5:27:12default meta class of dog is type right
5:27:15that's what it runs and that's how the
5:27:17class is created what we've done is
5:27:19we've overridden that we've changed it
5:27:20to our own meta class and essentially
5:27:23we've added our own little piece here
5:27:25that just prints out the attributes and
5:27:27then returns to us an object remember
5:27:29this is an equivalent way of creating
5:27:31this class by just literally typing this
5:27:33line and this is what happens this is
5:27:35what we get so we can see that we have
5:27:36the attribute module is equal to main
5:27:38the qual name dog so that's the name of
5:27:41this class right and then we have our
5:27:43attributes so X5 Y8 that we've defined
5:27:46down below now what if we Define a
5:27:48method called hello and we
5:27:52print say hi inside of here well then
5:27:57you can see we have dog. hello at some
5:27:59memory location that's mapped to by
5:28:01hello and that's a function so that's
5:28:03how this works now knowing this
5:28:05information meta classes can be very
5:28:07powerful because for example let's see
5:28:10like I can hook into the construction of
5:28:12this class and I can modify the
5:28:14construction of it if I want now this is
5:28:16something you would never do what I'm
5:28:18going to do is actually change all of
5:28:19the attributes that I have here to be um
5:28:24what do you call uppercase so I'm going
5:28:25to change this x to be a capital x this
5:28:27y to be a capital Y and this function
5:28:29hello to be Capital hello so all
5:28:32capitals I won't bother changing I can't
5:28:34change these underscore methods but I'll
5:28:36show you how we do this okay so let's
5:28:38start programming this so what I'm going
5:28:39to do is I'm going to make a blank
5:28:41dictionary I'm just going to call it a
5:28:43because notice down here that all of my
5:28:45attributes are actually in a dictionary
5:28:47right so what I'm going to do is modify
5:28:49this attributes dictionary based on the
5:28:51attributes that we passed in and make
5:28:52them all capital assuming that they're
5:28:54not they don't start with two
5:28:55underscores so what I'm going to do is
5:28:56say for item in hrs. items and I'm not
5:29:00sure if the syntax is 100% correct so
5:29:02there might be a mistake here we're
5:29:03going to say if item do starts with and
5:29:07in this cas case it's going to be
5:29:09underscore
5:29:10uncore then what we'll do is simply just
5:29:12add it back into the dictionaries
5:29:14actually yeah we'll just add it back
5:29:15into the dictionary so we'll say a in
5:29:17this case item equals
5:29:20ATS and then item like that so
5:29:23essentially since this is dictionary the
5:29:25way this is going to work right is we're
5:29:26just assigning this item to this new
5:29:28dictionary it's just literally the same
5:29:29thing right okay otherwise so if it
5:29:31doesn't start with a double underscore
5:29:33we need to change it to be uppercase so
5:29:34we're going to say a and in this case
5:29:36item. uper like that equals
5:29:41atrs item then we'll change the
5:29:44attributes here to be a which will be
5:29:45our modified attributes and that means
5:29:47when we return this dog class this
5:29:48should work assuming I haven't made a
5:29:50mistake which is likely but let's look
5:29:51at it and let's see what I got wrong all
5:29:54right so I guess I forgotten the fact
5:29:56that when we do something like this I
5:29:57need to actually Loop through with name
5:30:00value rather than just item so name will
5:30:02be the attribute name value obviously
5:30:05the value mapped with it just means
5:30:06we're going to have to change a few
5:30:07things here which we say name so having
5:30:09to do that we can change that to Val we
5:30:11can say name and that's yeah so name
5:30:13here this will be Val like that and I
5:30:17think we should be good on that front
5:30:18let's run that um and let's have a look
5:30:20at this air name Val is not defined of
5:30:23course I've made it value let's run and
5:30:25there we go we get good okay so let's I
5:30:29haven't proved to you really what
5:30:30happened but let's just step through
5:30:31this code cuz I did make a few errors so
5:30:33it might be confusing so we started by
5:30:35printing the attributes we then made a
5:30:37blank list uh a blank dictionary sorry
5:30:39that's going to represent our new
5:30:40attributes and then we've looped through
5:30:42all of the attributes so we just do do
5:30:43items to get uh the name and the value
5:30:46and we said if the name starts with a
5:30:48double underscore just add the proper
5:30:50value back in otherwise what we'll do is
5:30:52add the uppercase attribute with that
5:30:55corresponding value so we've changed
5:30:56them to Upper uh to uppercase
5:30:58essentially so let's actually print the
5:31:00values down here so the new values just
5:31:02have a look at the difference and you
5:31:04can see if we scroll up here that notice
5:31:07hello has been changed to Capital hello
5:31:09X to capital X and Y to Capital y so now
5:31:13let's do another proof just to make sure
5:31:14that all of you believe me that this is
5:31:16okay to do and let's go down here and
5:31:19say d equals dog create an instance and
5:31:22let's try to say see what d.x is so just
5:31:24print
5:31:26d.x well what is the error let's look at
5:31:29this here dog object has no attribute X
5:31:33what's but we defined X up here that's
5:31:35because we've changed it to be a capital
5:31:37X and notice that when I do that that's
5:31:39totally fine and same goes with hello I
5:31:42can't access that but when we change
5:31:43that to Capital hello we're totally fine
5:31:46because again we've modified the
5:31:48construction of the object all right so
5:31:51now that we kind of understand how meta
5:31:53classes work we've seen that we can hook
5:31:55into them we can change the way that
5:31:56objects are created we can en Force
5:31:58certain constraints I can change all of
5:31:59the attributes to be capital I can do
5:32:02whatever I want right I could change the
5:32:04bases I could remove an inheritance if I
5:32:06didn't want that to be allowed
5:32:08essentially this is why they call it
5:32:10magic because with this kind of hook
5:32:12into the creation of classes you can
5:32:15really enforce quite a bit of
5:32:17constraints on how classes are created
5:32:19so for example if you want every single
5:32:21class in a specific module to never be
5:32:24allowed to use a certain attribute or to
5:32:26only have or to have to have that
5:32:28attribute you could set meta classes for
5:32:31those or for that actual specific module
5:32:33which I'm not going to talk about now
5:32:35and then when that module's run it will
5:32:37check The Meta class right it will go
5:32:39through the meta class that's how we
5:32:40create the classes itself and make sure
5:32:42that all of these classes kind of
5:32:44conform to that structure that you've
5:32:46defined now this is specifically nice
5:32:48for when you're writing kind of Library
5:32:49code and you want your user code to be
5:32:51very specific let's say they're
5:32:52inheriting from a specific type um you
5:32:55could check if they're inheriting from
5:32:57you know whatever type it is that you
5:32:58want if they do inherit from that type
5:33:00you can enforce the fact that they have
5:33:01a specific function or specific method
5:33:04in that class that's needed to make that
5:33:06work so that's some of the things that
5:33:08you can do with meta classes again
5:33:10they're very complicated there's not
5:33:12really like a need to use them so you
5:33:13don't necessarily need to implement them
5:33:15into your program but sometimes they're
5:33:17cool and understanding how this class
5:33:19construction Works to me is even a
5:33:20better benefit of using the meta class
5:33:22itself so I believe there was one last
5:33:24thing that I wanted to cover here
5:33:27although it's not coming to me at the
5:33:28top of my head I mean I think that's
5:33:30pretty much it I don't know what more
5:33:32more I can show you guys you can have
5:33:34meta classes that inherit other meta
5:33:36classes you can do very very complex
5:33:38things with that but I think honestly
5:33:40for now we're going to wrap the video up
5:33:42at this I hope you guys enjoyed I hope
5:33:44you understand now how classes are
5:33:46created how meta classes work and what
5:33:48the heck they even are and I hope that
5:33:50you understand that you don't need to
5:33:53use this and the point of this video is
5:33:54to introduce you to the concept and make
5:33:56you more familiar with the way that
5:33:57classes and objects actually work in
5:33:59Python so anyways that has been it I
5:34:01hope you enjoyed make sure you leave a
5:34:02like if you did subscribe to the channel
5:34:04and I will see you in another expert
5:34:05Python tutorial
Decorators
5:34:11hello everybody and welcome back to
5:34:12another expert Python tutorial so in
5:34:15today's video we are going to be talking
5:34:16about decorators which are a pretty cool
5:34:18and useful tool they're not super
5:34:20complex but essentially they allow us to
5:34:22modify the behavior of a function
5:34:24without actually changing any of its
5:34:26code now this is useful because
5:34:28sometimes you want to be able to add and
5:34:30remove decorators while debugging a
5:34:32function or maybe you want to change the
5:34:35behavior of all of your functions and
5:34:36rather than going in and changing all of
5:34:38the code you could create a decorator
5:34:41which will allow you to Simply you know
5:34:42use one line of code to change the
5:34:44behavior of all of these different
5:34:46functions so I'll show you how this
5:34:47works and what a decorator actually is
5:34:49to start by just going through a bit of
5:34:50a kind of recap of how python passes
5:34:53objects around and why we can actually
5:34:55even use decorator a quick reminder
5:34:57before we get started that kite is the
5:34:59sponsor of this series kite is the best
5:35:01AI auto complete for python on the
5:35:03market and you can get it for free in
5:35:04the link in the description it
5:35:06integrates with the all the popular
5:35:08idees and text editors so subline VSS
5:35:10code adom Vim you name it chances are
5:35:13kite is you know compatible with that
5:35:15again you can download that from the
5:35:16link in the description so the first
5:35:18thing I'm going to do is I'm going to
5:35:19create a function called Funk now what I
5:35:21want to do is just show you that if this
5:35:23function takes a string I can actually
5:35:26return another function in here that
5:35:28does something with this argument and
5:35:30we've seen this before in the previous
5:35:32expert python tutorials but I want to
5:35:33just make this very clear on how this
5:35:34works so I'm going to call this function
5:35:36a wrapper because it's in side of this
5:35:38function it's not going to take anything
5:35:40as an argument and what I'm simply going
5:35:42to do in here is say
5:35:44print
5:35:46started
5:35:48print string and
5:35:52print ended like that and we'll do a
5:35:54capital and I guess actually this string
5:35:56sorry does not need to be in a string it
5:35:58needs to be like that so we print the
5:36:00variable and then what I'm going to do
5:36:01here is simply return the wrapper
5:36:04function like that so if I do this now
5:36:06what happens if I say x equals Funk like
5:36:09that and I put in my string let's say
5:36:11hello and I run this program you can see
5:36:14we get started hello and end
5:36:17it now the reason that happened is
5:36:19because we returned here the wrapper
5:36:22being called right these two brackets
5:36:24but if I remove this and I do this now
5:36:26nothing happens so what we've actually
5:36:28done if I print out the value of x is
5:36:31you see we store a function in here that
5:36:34was returned from this function which is
5:36:36actually equal to this and then if we
5:36:38wanted to call this function we could
5:36:39put our little braces like that and here
5:36:42we go we get started hello and ended so
5:36:44I just want to show you that this is
5:36:46possible and the reason this is possible
5:36:47is because functions in Python are
5:36:49objects which means that we can pass
5:36:51them around we can throw them around our
5:36:53program and you can see up here that
5:36:55this is the location of the object you
5:36:57know function wrapper from funk. locals
5:37:01okay so now that we understand that
5:37:03let's bring it up a little Notch and see
5:37:05what happens if we actually instead of
5:37:07just just passing a string in here pass
5:37:09another function so I Define a fun
5:37:12function I'll call it funk two like that
5:37:14and all we'll do in here is just
5:37:16print something we'll say I am Funk 2
5:37:21why not I'll get rid of this and now
5:37:23rather than passing a string in here why
5:37:26don't we actually just call some
5:37:28function so let's put F in
5:37:30here let's put I don't want Funk two I
5:37:32just want F let's call whatever function
5:37:35is called so we'll do print started call
5:37:37whatever ever function was in here and
5:37:38then print ended and then return this
5:37:41function so let's see how this works so
5:37:44now if I can move this down a bit let's
5:37:46say x equals
5:37:49Funk Funk 2 let's print X and then let's
5:37:54call X so what we're doing here for
5:37:56anyone that's confused is we've created
5:37:58this function which accepts a function
5:38:00as an argument right or as a parameter
5:38:02then what we do in here is we Define a
5:38:04function that prints started calls
5:38:06whatever function was passed in prints
5:38:09ended and then returns this function so
5:38:12this is not actually going to do
5:38:13anything until it is called so when we
5:38:15return this we'll store it in a variable
5:38:17X and then when we want to use this
5:38:20interior function here we call it with
5:38:23this right the X and the brackets so
5:38:25let's have a look at how this works
5:38:27contrl B and you can see started I am
5:38:29Funk to ended fun Funk uh rapper locals
5:38:32right and the idea here is that I can do
5:38:35the same thing with say funk three right
5:38:37right if I print let's
5:38:40say I am Funk 3 like that now rather
5:38:44than passing Funk two I can pass Funk
5:38:46three and we can see now we get the same
5:38:48thing happening except Funk three and in
5:38:50fact we could do it you know I could say
5:38:52y equals Funk Funk 2 and then we can do
5:38:57the same thing and just call Y down here
5:38:59and now both of them are going to work
5:39:00and they're both going going to be
5:39:01different so started I am Funk 3 started
5:39:04I am Funk two ended so with this in mind
5:39:07we can start looking at kind of what a
5:39:08decorator is now almost but I just want
5:39:10to show you a way that let's say you
5:39:12know this function right here which
5:39:14we're going to call our decorator we
5:39:15don't want to have to do this weird call
5:39:18right where we use our decorator
5:39:19function to call function three we just
5:39:21want to call function three and have it
5:39:24do this behavior and you know do
5:39:26whatever function three is right how
5:39:28would we get that to work how would we
5:39:30get that to happen we rather than me
5:39:31having to call Funk and pass Funk 3 and
5:39:34Call Funk and pass Funk 2 how can we do
5:39:37this another way well what I can
5:39:40actually do is kind of a cool sneaky
5:39:41line here in Python where I do this I
5:39:44say funk three is equal to Funk at Funk
5:39:483 now this seems like a weird line of
5:39:52code but essentially what this is going
5:39:53to allow us to do now is rather than
5:39:56having to call function and pass Funk 3
5:39:58in we can actually use Funk 3 which is a
5:40:00variable now which stores the function
5:40:04that is returned from the call of Funk
5:40:07with function three which means that if
5:40:09we
5:40:10call Funk 3 like that then what's going
5:40:13to happen is it's going to run this
5:40:15function that was created from this call
5:40:17so this is a way that we can kind of
5:40:19change that behavior we can say okay so
5:40:22Funk 3 I always want this whatever's
5:40:24inside this wrapper function to happen
5:40:26whenever I call it so all I need to do
5:40:28to Define that is write this line of
5:40:31code and now every time I call Funk 3
5:40:33from anywhere in my program it'll do
5:40:34this and if we run this we can see that
5:40:37happens and that works fine and we can
5:40:39do the same thing with funk 2 um yeah
5:40:42Funk 2 equals Funk Funk 2 and then we'll
5:40:47call Funk two like that and we can see
5:40:49that it works for both of them so this
5:40:52is where we now get into the concept of
5:40:54a decorator things are going to get a
5:40:55little bit more complicated but now all
5:40:57I'm going to show you is what a
5:40:58decorator does versus what we've done so
5:41:01this kind of line of code here is weird
5:41:03right like it's not something you really
5:41:05want to write you don't really know
5:41:06where to put it it's like just not ideal
5:41:08to have to write this line of code so
5:41:10python has thought of this for us and
5:41:13what they've introduced is a syntax that
5:41:15essentially allows us to do this um just
5:41:17in a better way and all it is is using
5:41:19this at sign and putting the name of
5:41:21your decorated function so like that
5:41:23just on top so now this line here pretty
5:41:26much just replaces this line like they
5:41:29are exact identical copy they do the
5:41:32same thing this just looks a little bit
5:41:33nicer and it's a little bit easier to
5:41:35understand what's happening when you
5:41:36have you know this decorator syntax
5:41:38above the function and you can actually
5:41:40decorate your function with more than
5:41:42one decorator if you want we're not
5:41:43going to talk about that right now uh
5:41:45but that's something to note so pretty
5:41:47much to replace these lines of code all
5:41:49I have to do is put at the name of the
5:41:51function that I want to run before right
5:41:54and then now if I do fun three and fun
5:41:56two we can see we get the same thing so
5:41:58we get started I Funk 3 ended started I
5:42:00am fun two ended so this is how this
5:42:03works so whenever you're creating a
5:42:04decorator function you need to kind of
5:42:06implement this raer functionality where
5:42:08the first thing takes the function
5:42:11itself and the second function here well
5:42:14this just takes the um or what is this
5:42:16this creates the function that's going
5:42:17to be called when this is returned right
5:42:20or when you actually call this function
5:42:21so essentially you're changing your said
5:42:23Funk 2 is now going to be equal to
5:42:25whatever this is and usually inside of
5:42:27here you're going to use the function
5:42:29now there's a lot of problems that I'm
5:42:31sure some of you can think of here so
5:42:32what happens if I say fun 2 takes an
5:42:35argument X and instead of saying uh I am
5:42:38fun 2 all it does is just print out x
5:42:41what happens now well if I run this and
5:42:44I even let's give an argument let's give
5:42:46five here you can see we're getting an
5:42:47error right and the reason we're getting
5:42:49an error this huge error what it say
5:42:51rapper takes zero positional arguments
5:42:53but one was given so the issue is that
5:42:56when we actually call Funk two what
5:42:58we're actually going to be calling from
5:42:59this line of code is this wrapper
5:43:01function so this wrapper function needs
5:43:03to have the same amount of arguments
5:43:05that function two has so how can we do
5:43:07that how can we fix that well we could
5:43:10just put an X here like this right and
5:43:12then let's comment out fun three let's
5:43:15run this uh oh sorry my bad we got to
5:43:18throw an X in here that was what the
5:43:20issue was before so we have our X's
5:43:22there and then this works right so I put
5:43:23an X in the wrapper I put an X here
5:43:25which means that when I call this x will
5:43:27be passed to this wrapper X and then we
5:43:29can use that to call the function and
5:43:32that's totally fine but what happens now
5:43:34when I want to use Funk three and Funk 3
5:43:36does doesn't take any arguments but this
5:43:38takes arguments the whole point of
5:43:40decorators is to be able to use this on
5:43:42multiple functions and we get missing
5:43:44one required positional argument x uh
5:43:46Funk three there's an issue you know
5:43:48things are happening so what what do we
5:43:51do here how do we fix this well this is
5:43:52where we use what we call the unpack
5:43:54operator I believe or the Splat operator
5:43:56or something but essentially you can put
5:43:58star args star star
5:44:01quars like this and what this will allow
5:44:03you to do is accept any number of
5:44:06position or keyword arguments so
5:44:09essentially this says okay we don't know
5:44:10how many arguments are going to be
5:44:12coming in here there could be an
5:44:13infinite amount so rather than just
5:44:15trying to guess or figure it out or
5:44:16write a different rapper function for
5:44:18every single amount what we'll do is
5:44:20simply write star args star star quars
5:44:22which tells us except all of the
5:44:24arguments that are passed in except all
5:44:25the keyword arguments and what we can
5:44:27actually do is simply pass whatever
5:44:29these are to this function so let's do
5:44:32this here star star quarks so now what's
5:44:35going to happen is any arguments pass in
5:44:37we'll just automatically pass to F same
5:44:40with quars and if there's none we just
5:44:41won't pass anything so this kind of
5:44:43handles any amount of arguments and now
5:44:45we'll see that when we run this we don't
5:44:47get any error and this works fine for
5:44:49any number of arguments so even if I
5:44:51were to add you know X Y in here
5:44:53something like that and then we call
5:44:55this with like
5:44:5656 and run that still same thing this is
5:45:00working doesn't matter how many
5:45:01arguments we have we're perfectly fine
5:45:04okay so that's good but what happens
5:45:05when we return a
5:45:07value from function 2 well what happens
5:45:10then so let's say x
5:45:13equals print X and right now so we can
5:45:16see that we're returning y from function
5:45:182 right but if I call this and I print X
5:45:22we don't get y return to us so how can
5:45:24we fix this right so what we actually
5:45:26need to do is since we're calling the
5:45:29actual function here inside of the
5:45:31wrapper we need to store the return
5:45:33value I'm going to store that as RV so
5:45:37that I can return it at the end of this
5:45:39wrapper function so since this wrapper
5:45:41function is returned from here this
5:45:44could return a value as well and well we
5:45:46don't want to return immediately so like
5:45:48I could just write the line return F
5:45:51like that and that would work fine but
5:45:52we want to do something after the
5:45:54function is called so to you know solve
5:45:56this we do RV and then we can return RV
5:45:58so store the return value of this
5:46:00function return it at the end and now if
5:46:02I print this you can see that we get our
5:46:03return value of six so that is how we
5:46:06return turn values that is how we pass
5:46:08in different amounts of parameters and
5:46:10now I'm just going to show you a few
5:46:12actual useful um decorator functions
5:46:14that you might want to use so you're
5:46:15probably looking at me like well what's
5:46:17the point of this right now like why do
5:46:18we have this well a good example is say
5:46:21you want to validate input say you want
5:46:23to have a bunch of functions that accept
5:46:25some numeric input and that numeric
5:46:26input is always going to be between the
5:46:28value 1 and 10 right rather than
5:46:31checking the parameters every single
5:46:33time inside of your function what you
5:46:36could do is you can use a decorator that
5:46:38checks them for you right and then all
5:46:39you would have to do every single time
5:46:41you want to validate that your input is
5:46:43correct is throw a decorator over top of
5:46:45your function that says validate input
5:46:46or whatever it's called Write the
5:46:48appropriate code inside of a decorated
5:46:50function and this these kind of
5:46:51functions like this and then you're good
5:46:53to go so that is kind of the idea behind
5:46:56decorators that you can modify the
5:46:58behavior of a function add functionality
5:47:00to it without modifying the code and
5:47:02sometimes you don't want to touch the
5:47:03code of a function because you want to
5:47:05make sure that you're not breaking
5:47:06anything you don't know how it's been
5:47:07written but you want to add something on
5:47:09top for example maybe timing how fast
5:47:11the function runs and this is a very
5:47:13common example I'll show you this as
5:47:15kind of our last example to using
5:47:16decorators um so yeah we'll do a timing
5:47:19one so essentially if I want to time how
5:47:21long a function takes to run every
5:47:23single time right then I can create a
5:47:25timer decorator so you know what
5:47:26actually let's do it from the start
5:47:27let's erase all of this um and do one
5:47:29from the beginning so I'm going to say
5:47:30Define timer which is the name of my
5:47:32decorator it's going to take in a
5:47:34function we'll Define a wrapper this
5:47:37will take STAR
5:47:38args uh star star quars like that again
5:47:42quar stands for keyword arguments what
5:47:44we're going to do here is start by
5:47:46importing time we're going to
5:47:49say uh let's say
5:47:51start equals
5:47:54time.time like that and then we'll just
5:47:58print started or actually we could do
5:48:01something else let's do this let's say
5:48:04uh RV
5:48:07equals Funk like that and then let's say
5:48:10end or guess we'll say
5:48:12total equals time. time minus start and
5:48:17then we'll
5:48:21print time like that and we can put
5:48:25total and then we will return
5:48:29RV and return wrapper like that oops I
5:48:34did not mean for those okay so now that
5:48:35we have that let's create function let's
5:48:37just say
5:48:38defined test and then here we're just
5:48:41going to say 4ore in
5:48:44range 1 th000 let's just say pass and
5:48:48there we go so now if I want to decorate
5:48:49this function I can say at timer and
5:48:52what this will allow me to do now is
5:48:53when I run this function have it tell me
5:48:56how long this took to run so let's go
5:48:58ahead and just actually call test like
5:49:01that and see it ran in 0.0 seconds so I
5:49:04guess this rounded for us but I guess
5:49:06would I be able to do this make it take
5:49:08any longer if I add any more zeros there
5:49:10we go so we can see that is how much
5:49:11time this function took to run and if I
5:49:14wanted to add timer on another function
5:49:16well then I could do another one say
5:49:18test two like that and we can even just
5:49:21say time do
5:49:24sleep why is my capitals on 2 seconds
5:49:27and we'll decorate this one with at
5:49:29timer as well and now when we call test
5:49:32two and we run this give it a second and
5:49:35we should see that this takes 2. o1
5:49:37seconds so this is how decorators work
5:49:39again useful when you want to modify the
5:49:41behavior of a function without actually
5:49:44touching it without changing it you can
5:49:46add other decorators on top of an
5:49:48existing decorator I'd urge you to see
5:49:50you know how that actually works how
5:49:52you'd be able to do that you can test
5:49:53that out for yourself but for now that's
5:49:55pretty much it and a really useful
5:49:57example of decorators is a timer
5:49:58decorator when you're debugging you want
5:50:00to check what is really slowing down or
5:50:02how long something's taking another good
5:50:04example is having a logging decorator so
5:50:07having a decorator that essentially logs
5:50:09the calling of functions or what's
5:50:11happening in the program that's a good
5:50:12one to add here and then obviously
5:50:14validating input and checking return
5:50:16values and stuff like that is always a
5:50:18good example of when you might want to
5:50:19use decorative so with that being said I
5:50:21hope you guys enjoyed if you have any
5:50:22questions as always leave them down
5:50:23below and with that being said I will
5:50:25see you in the next expert Python
5:50:26tutorial
Generators
5:50:31series hello everyone and welcome back
5:50:33to another expert Python tutorial so in
5:50:36today's video we're going to be talking
5:50:37about generators now generators are a
5:50:39pretty straightforward and actually very
5:50:41useful expert level feature in Python
5:50:44and chances are you've probably seen
5:50:45them before now before we get into
5:50:47generators I do need to quickly mention
5:50:49that this uh Series has been sponsored
5:50:51by kite so kite is the AI autocomplete
5:50:53for python that I've been using
5:50:55throughout this entire series and just
5:50:56through my daily coding you can see that
5:50:58when I actually start typing something
5:51:00we get these nice suggestions top um
5:51:02popping up from kite with the little
5:51:03kite icon here it is just the best
5:51:06python autocomplete it works very
5:51:07quickly and actually saves you quite a
5:51:09bit of time and works for other modules
5:51:11that aren't just built-in python things
5:51:13so it's pretty useful if you guys want
5:51:14to download it there's a link in the
5:51:16description and it's free so you know go
5:51:17ahead and do yourself a favor if you do
5:51:19want that auto complete okay so now
5:51:22let's get into generators now actually
5:51:24before I discuss generators I want to
5:51:25show you the problem that generators
5:51:28help us solve so we should understand
5:51:30that in our computer we have a finite
5:51:32amount of memory a finite amount of RAM
5:51:35and when we run a computer program our
5:51:37program is loaded into that memory and
5:51:39when we're storing things manipulating
5:51:41variables lists all of that that's all
5:51:43stored in our computer's memory that's
5:51:45because that is the fastest way for us
5:51:47to retrieve and write data while we're
5:51:49in a program you know we could put some
5:51:52things on a hard drive but that's pretty
5:51:53slow and it really depends on what we're
5:51:55doing but most of the time we're working
5:51:56in Ram and what that means is that we
5:51:59are limited to the amount of ram that is
5:52:01in our physical computer when we're
5:52:03writing a computer program and in fact
5:52:05you know most of us will never even see
5:52:07the error that I'm about to show you but
5:52:09it is possible to actually fill up all
5:52:11of ram right to use all of ram to have
5:52:14nothing left and in fact that's what I'm
5:52:16going to try to do right now so if we
5:52:17look at the example on the screen and
5:52:19I've just started running the code
5:52:20you're going to see this takes a second
5:52:21to run what I'm trying to do is generate
5:52:24the sequence of all of the squares from
5:52:26the number zero up until I want to say
5:52:28this is like 100 million or 10 million
5:52:30or some number like that right and then
5:52:32what I'm going to do is Loop over all
5:52:33those numbers and print them out and
5:52:35you're going to notice that very shortly
5:52:37yes and there we go we get the error we
5:52:39get a memory error which essentially
5:52:41means that no we can't do this these
5:52:43numbers are too big we don't have enough
5:52:45room left in memory we're not allowed to
5:52:46use that much space no that's not
5:52:48allowed so how do we go ahead and how do
5:52:50we fix this well this is where we use a
5:52:53generator now before I actually create a
5:52:56generator I want to look at something
5:52:57here if all I'm doing in this specific
5:53:00application is just printing or
5:53:02processing say one value at a time which
5:53:04is what I'm doing when i'm looping over
5:53:06this list right one value at a time I'm
5:53:08processing it I'm not accessing other
5:53:10values I don't need values that are in
5:53:12the future I don't need values that are
5:53:13in the past then is it really necessary
5:53:16for me to create a list like this to
5:53:18Loop through and the answer is no
5:53:19obviously right in fact what I could do
5:53:22is replace this for loop with just a
5:53:24print statement right so I could replace
5:53:25this entire thing actually we'll just do
5:53:27it down here so let say 4 I in range and
5:53:30then we'll just type that same number
5:53:31whatever that's close enough and then
5:53:33what I can do is print I the exponent
5:53:36two like that right and this is the same
5:53:38thing and this is the idea behind
5:53:40generators this is not what we're going
5:53:42to do precisely to generate this
5:53:45sequence but generators allow us
5:53:47essentially to look at one value at a
5:53:49time and to not store the entire
5:53:52sequence of numbers when we don't need
5:53:53to do that and I'm actually going to
5:53:56code out our own kind of generator
5:53:58pattern here that just uses a class and
5:54:01some Dunder methods to illustrate what's
5:54:03actually happening inside of a generator
5:54:05when we use it now I understand that
5:54:06most of you probably still don't know
5:54:08what a generator is but after I write
5:54:10this class and start showing you
5:54:11hopefully this should make sense so what
5:54:13I'm going to call this is just gen and
5:54:15what I want this to do is the exact same
5:54:17thing that we did before where it's
5:54:18going to generate a sequence of all of
5:54:20the squares up to some number let's say
5:54:22that number is ADD so in this case I'm
5:54:24going to say let's define uncore uncore
5:54:27nit uncore uncore let's take self Let's
5:54:30Take N let's say self. n is equal to n
5:54:34and then let's say self. l is equal to Z
5:54:37so this is going to be the last number
5:54:39that we generated the square for and
5:54:41we're going to use this variable to keep
5:54:43track of the last number we generated
5:54:45the square for so that we know which
5:54:46next number to use and I'll show you how
5:54:48this works we're going to define a
5:54:50actually in this case we're going to do
5:54:52a Dunder method so underscore next it's
5:54:54going to take self and what it's going
5:54:55to do is return self. next now I know we
5:54:59haven't defined self. next yet well
5:55:01we're going to do that now so Define
5:55:02next self and in here what I'm going to
5:55:04do is generate that sequence that I did
5:55:06before except I'm just going to do it
5:55:08using these two variables I'm not going
5:55:10to do it using a for Loop like we had
5:55:12before and storing everything in a list
5:55:14so what I'm going to say is RV equals
5:55:17self. last to the exponent 2 so this is
5:55:20saying our return value is going to be
5:55:21equal to whatever this last number is
5:55:23that we have to the power two right so
5:55:25we're going to find the square for that
5:55:26then I'm going to say self. last plus
5:55:28equal 1 we're going to say if self. last
5:55:33and in this case equals equals self. n +
5:55:351
5:55:37um actually what I need to do is sorry
5:55:39put this at the beginning of my Loop so
5:55:41if self. last here equals equal self. n
5:55:45then what we're going to do is say raise
5:55:46stop iteration stop iteration is just an
5:55:49error that we can raise that essentially
5:55:51tells us hey no we can't go any further
5:55:53and then what we're going to do is
5:55:55return this RV okay so now I'm going to
5:55:57show you how we can use this so this we
5:56:00can actually use in a very similar way
5:56:01that we were kind of looping through the
5:56:02numbers before and we're going to say in
5:56:04this case g equals gen let's say we want
5:56:07to Loop up to you know some massive
5:56:09number like that and now if I want to
5:56:11actually Loop through this entire
5:56:12sequence what I can do and I mean some
5:56:14of you are saying oh this isn't big
5:56:15enough okay let's go bigger we'll say
5:56:17while in this case true we're going to
5:56:21say
5:56:22try
5:56:24print the next value of G right and what
5:56:29this underscore uncore next is allowing
5:56:30me to do is call this next function or
5:56:33method or whatever you want to call it
5:56:35on G here here and we can print the
5:56:37value and then I'm going to say accept
5:56:39stop iteration and then we're simply
5:56:42going to break this Loop like that and
5:56:45we should actually be good with that
5:56:47okay so what this is going to do now is
5:56:48Loop and I'm just going to make this
5:56:49number smaller just so we can see that
5:56:51it does actually just go up to 100 um
5:56:53but I promise you this does work with an
5:56:55infinite sequence and we'll watch and we
5:56:57can see that this works just like we
5:56:59would have before and we can generate
5:57:01all of the squares for these numbers now
5:57:03I can make this number absolutely
5:57:05massive I can make it you know that's
5:57:06super long number and this will be
5:57:07totally fine and in fact that's because
5:57:09we're not storing all of this in a list
5:57:11where we're storing all of the previous
5:57:13values all we're keeping track of is
5:57:15kind of the internal state of the next
5:57:17number we need to generate and this is
5:57:19the idea behind a generator is that we
5:57:21don't need to store every single value
5:57:23what we can do is just store you know
5:57:26almost the last value that we generated
5:57:28and then using that we can generate the
5:57:30next one right or we can figure out the
5:57:33next number to generate and obviously
5:57:35this kind pattern like you can write
5:57:37this if you want but what python has
5:57:38done for us is kind of come up with a
5:57:40pattern that makes this a little bit
5:57:41easier called the generator pattern so
5:57:44what I'm going to do now is pretty much
5:57:46take this class and I'm going to convert
5:57:47it to a generator and I'm going to show
5:57:49you how this works so I'm going to say
5:57:51Define gen and here this will just take
5:57:53the value n what I'm going to say in
5:57:55here say for I in range and in this case
5:57:59we'll say n then we're going to say
5:58:01yield I to the exponent 2 now I know
5:58:05this seems weird but it's essentially
5:58:06the way that this works is we instead of
5:58:08using this crazy pattern that we had
5:58:10before we're going to use the yield
5:58:11keyword now what the yield keyword does
5:58:14is as soon as we hit this it returns
5:58:16this value to wherever this was called
5:58:19from or wherever we were looping through
5:58:21and then it pauses this function so
5:58:24rather than stopping the execution of
5:58:25this function which is what a regular
5:58:27function would do when we hit the return
5:58:29keyword it just pauses it which means we
5:58:32actually keep track of what I was so we
5:58:35still know what that number is we still
5:58:37know you know what n is we still have
5:58:39all of the internal information of this
5:58:41function stored in memory we haven't
5:58:42gotten rid of that but we're just
5:58:45pausing when we hit this yield keyword
5:58:47that's what that means so think of yield
5:58:48as like a pause whereas for example
5:58:51return would be a stop of execution so
5:58:54we can use this generator by doing
5:58:56something like this g equals gen give it
5:59:00some number like this and then what we
5:59:02can do is say 4 I in G and then what we
5:59:05can do is simp simply print out I now
5:59:08this is going to work the exact same as
5:59:10all of the other examples I've seen I've
5:59:11shown you before and you can see that
5:59:13indeed it is working and we could run
5:59:15this you know infinitely and we're not
5:59:16actually going to ever run into a
5:59:18problem and in fact I probably should
5:59:19have made this number a little bit
5:59:20smaller because I feel like this is
5:59:21going to run for a long time but anyways
5:59:23that can go down there so this is the
5:59:25point of a generator and I know some of
5:59:26you are still confused but the way you
5:59:28have to think about it is we create this
5:59:31generator by creating this function gen
5:59:33with uh what do you call it you know
5:59:35100,00 th000 or whatever it is whatever
5:59:37this number is 1 million and then what
5:59:39we can actually do is Loop through this
5:59:42and what happens when we Loop through
5:59:43this is it runs this for Loop up until
5:59:46it sees this yield keyword so it sees
5:59:48this yield keyword I to um squared right
5:59:51and then as soon as it sees that what it
5:59:53does is it pauses the function and says
5:59:55okay we don't need to run this anymore
5:59:57and then it waits until it's called
5:59:59again so until we Loop through it again
6:00:01with this for Loop the next method on it
6:00:03is called and then it returns to us the
6:00:05next year and because it's pausing
6:00:07rather than stopping execution this is
6:00:10totally fine and in fact we can actually
6:00:12look at how much memory this function is
6:00:14using versus if we were to do the same
6:00:16thing say with a list and we'll do that
6:00:18in a second but I want to show you that
6:00:20to use this generator I don't have to
6:00:23just Loop through with a for loop I can
6:00:24use that next method that I showed you
6:00:26before and if I do nextg and I run this
6:00:29you can see we get zero because
6:00:30obviously the first answer is zero and
6:00:32if we do this a few times we get zero
6:00:37149 so this is how this works when we
6:00:40call the next method or the next
6:00:42function on our generator we get the
6:00:44next value so essentially up until it
6:00:46hits the next yield keyword and in fact
6:00:49we actually can use more than one yield
6:00:51keyword in our gen if we want and I
6:00:53could say yield one uh and we could do
6:00:56as many yields as we want so let's do
6:00:59this and then let's actually show what
6:01:02happens when we go past how many yield
6:01:04keywords we have so if we look at get
6:01:06this here we can see we get 10 100 1,000
6:01:0910,000 and then we actually end up
6:01:12getting an error and there was a one up
6:01:13here just so you can see and that error
6:01:15is actually a stop iteration error right
6:01:17here because we don't have any more
6:01:19keywords yield inside this generator so
6:01:22there's nothing more to return so this
6:01:24works the exact same way that I showed
6:01:25you with that class that I did before
6:01:28where it keeps track of the internal
6:01:29state it knows what line it was on when
6:01:31it paused and then it goes to the next
6:01:33one until it hits the next yield keyword
6:01:35and that's when it pauses again and
6:01:37that's how a generator works and they're
6:01:38pretty useful there's some other things
6:01:40that we can do with them and in fact
6:01:42I'll show you um comparing the size of
6:01:44say a generator like the one we just
6:01:46created versus actually generating an
6:01:49entire list you can see in memory how
6:01:50much this is actually using so if we
6:01:52want to generate the sequence right we
6:01:54know this generator works we can use
6:01:55that now so let's run that and what I'm
6:01:58going to do now is import Sy I'm going
6:02:01to actually make that list that I did
6:02:03before so we're going to say x equals
6:02:05and in this case we'll say I to the
6:02:08exponent 2 for I in range and we'll just
6:02:11do uh let's say 10,000 like that and
6:02:14we'll replace 10,000 Here and Now what
6:02:16I'm going to do is just print out the
6:02:18size of both of these so I'm going to
6:02:19say
6:02:20sy. get size of and in this case the
6:02:24first size we'll look at is X which is
6:02:26that list and then we'll get the size of
6:02:29G and I'll show you the difference in
6:02:32memory so what this does is tell us how
6:02:34many bits or actually I believe how many
6:02:36bytes are being used by whatever object
6:02:38is we pass in so in this case the list
6:02:40and in this case the generator and
6:02:41you're going to see that we have a
6:02:42substantial difference so this first
6:02:44list is using 43,81941
6:03:05need it one at a time which is what
6:03:07we're doing when we say Loop through
6:03:08something then it's much better to use a
6:03:11generator to generate this sequence for
6:03:13us now there's a lot more that I can
6:03:15show you with a generator uh we can
6:03:18close a generator we can stop a
6:03:19generator we can send values to a
6:03:21generator but I'm going to leave that
6:03:23for people that are a little bit more
6:03:24advanced the point of this is to
6:03:26introduce you to this concept of the
6:03:28generator whereas when you're
6:03:29programming think about say if you're
6:03:31going to make a massive list like this
6:03:33with a bunch of values do I need of
6:03:36these values am I using one value at a
6:03:38time am I looping through am I printing
6:03:40it am I adding it to something or do I
6:03:42only need say the last value and this
6:03:45value or a few values and obviously you
6:03:47can make your generators more
6:03:48complicated than a simple for Loop you
6:03:50can do some more complex computations
6:03:52you can store variables inside of here
6:03:54because remember that this yield keyword
6:03:56just pauses the execution when this
6:03:58generator is called and what that means
6:04:00is that I can Loop through this this
6:04:02first generator say you know five times
6:04:05and then somewhere later in my program
6:04:07continue looping through it and it will
6:04:09you know resume where it left off and
6:04:12that's a useful kind of construct it's
6:04:13hard to show any examples in this you
6:04:15know short amount of time but hopefully
6:04:17this gave you an idea of when you would
6:04:18use a generator so with that being said
6:04:21I hope you guys enjoyed if you did make
6:04:22sure you leave a like subscribe to the
6:04:24channel and as always let me know if
6:04:26there's any other expert level features
6:04:27you would like to see in the comments
6:04:29down
Context Managers
6:04:33below hello everyone and and welcome to
6:04:36another expert Python tutorial so in
6:04:38today's video we're going to be talking
6:04:39about context managers now context
6:04:42managers are definitely one of the more
6:04:43useful topics that we're going to be
6:04:44talking about or that we have talked
6:04:46about so far and you'll find yourself
6:04:48using them a lot especially as you get
6:04:49into more advanced programming and
6:04:51dealing with things like shared memory
6:04:53and shared resources and unlocking and
6:04:55locking and yeah it just all those kind
6:04:57of things contact managers come in very
6:05:00handy essentially now I'm actually
6:05:02almost certain that all of you have seen
6:05:04context managers before in fact I'll
6:05:06show you a few examples here of times
6:05:08when you've used them and just not known
6:05:10that you've used them and that's kind of
6:05:11a common Trend here that I'm hoping you
6:05:13realize is a lot of the stuff that we're
6:05:14using it's not completely new to you
6:05:17it's not something you've never used
6:05:18before you just never really understood
6:05:20how or why it worked and that's what the
6:05:22point of this Series has been is to give
6:05:24you that appreciation and understanding
6:05:26of how these lower level things work so
6:05:28that you have more control over your
6:05:29code and if you need to change them or
6:05:31write your own versions you now know how
6:05:33or at least have the intuition to do so
6:05:35now again this video is sponsored by
6:05:37kite a big thank you again to them you
6:05:39can see whenever we start typing we got
6:05:40these little kite keywords popping up
6:05:42it's just a really good auto complete
6:05:44and again it's free if you want to
6:05:45download it it's from the link in the
6:05:46description you can find that okay so
6:05:48let's go ahead and get started with
6:05:49context managers now the first thing
6:05:51that I want to do is show you what
6:05:53problem context manager solved to us so
6:05:55I'm going to start or solve for us so
6:05:57I'm going to start by showing us the
6:05:58problem and then the solution with a
6:06:00context manager so essentially let's say
6:06:02we have some file and we want to we want
6:06:04to open a file right so want to open say
6:06:06file.txt we'll open it in right mode
6:06:09which just means create a new file if it
6:06:10doesn't exist yet and then what we'll do
6:06:11is say
6:06:12file. write let's say hello and then
6:06:16we'll say file. close okay pretty
6:06:20straightforward pretty basic this will
6:06:21work run this code nothing wrong with it
6:06:24but what actually is wrong with what I
6:06:26just wrote well looking at this at a
6:06:28surface level it seems like everything's
6:06:30totally fine I mean we've opened the
6:06:31file we've written one line to it in
6:06:33this case hello and then we've closed it
6:06:35now the issue occurs in this code if
6:06:38this line here so this file. right line
6:06:41doesn't work or it doesn't happen for
6:06:43example like if I just error this line
6:06:45by putting line X here and then we run
6:06:47this we can see that we don't make it
6:06:50down to this file. close line because we
6:06:53had some error on this right line so say
6:06:55we open the file and we start trying to
6:06:56write something happened something else
6:06:58had the file open we couldn't access it
6:07:00well now we've opened the file and we
6:07:03haven't closed it and that's an issue we
6:07:05need to make sure whenever we open a
6:07:07file or have some shared resource that
6:07:09we work with that we close it afterwards
6:07:11now a better example would probably be
6:07:13if I open this in read mode um as
6:07:16opposed to write mode because that would
6:07:17mean that this file already exists on
6:07:18the system but hopefully you get the
6:07:20point that the issue occurs when we open
6:07:23the file we do a bunch of stuff and we
6:07:25don't reach that file. close so the
6:07:27point is how can we make sure that we
6:07:29reach this file. close no matter what
6:07:31happens in between the open and in
6:07:33between the close well the way that we
6:07:36could do that is we could use a try and
6:07:38a finally statement right so we could
6:07:39leave what we have right here and we can
6:07:41just add a try like that and then we
6:07:43could add a finally uh down here if we
6:07:47indent that properly for the file. close
6:07:49so what this means is regardless of if
6:07:51this works or not we will eventually or
6:07:54finally close this file so that's good
6:07:56and we've just kind of fixed our area
6:07:57there and that's totally fine but
6:07:59there's an easier and better way to do
6:08:01this that allows there a little bit more
6:08:03functionality and what that is is a
6:08:05Conta text manager now this code right
6:08:07here is completely equivalent to what
6:08:10I'm about to write so with open file.txt
6:08:13we'll open in R mode again we'll call
6:08:16this as file to be consistent and we'll
6:08:19just simply say file. WR and in this
6:08:22case Hello so these two kind of blocks
6:08:25so this block up here and this block
6:08:27actually do the exact same thing and the
6:08:29way that this works is this open method
6:08:32here actually defines what we should do
6:08:35when we use it as a context manager when
6:08:37we exit and when we enter so when we
6:08:40enter what that means is okay when we
6:08:42open this file when we write this code
6:08:45what's the first thing that needs to
6:08:46happen well the first thing needs to
6:08:48happen is we need to open the file and
6:08:50we need to return that so we can store
6:08:52that as the file object that's the thing
6:08:53that's happening when we enter this
6:08:55context manager and we can tell we're
6:08:57using a context manager because we're
6:08:58using the with keyword and that's the
6:09:00most common way and kind of the only
6:09:02examples I'm going to show you here is
6:09:03using the with keyword in pth
6:09:06then what we do is we do something
6:09:08whatever we have inside here we could
6:09:09print something we could you know fil
6:09:11out right we could do a for Loop doesn't
6:09:13matter and then when this is done so all
6:09:15of the code in here regardless of there
6:09:17if there was an exception or not we will
6:09:20call the exit method and what the exit
6:09:23method does is some kind of code that
6:09:25allows us to close our file properly and
6:09:28make sure that everything works so
6:09:29essentially the context manager is kind
6:09:31of like a hidden way to make sure that
6:09:33whenever we do one operation we do
6:09:36another one regardless of what happens
6:09:38in between and again that's really
6:09:40useful for things like opening and
6:09:41closing files and for locking and
6:09:43unlocking shared memory so now I'm going
6:09:45to show you how we can actually write
6:09:47our own context manager that does the
6:09:49same thing that we've just shown here so
6:09:50we have this open F function and we say
6:09:52with open file. txtr as file file. WR
6:09:57hello we understand that after this runs
6:09:59regardless if there's an exception or
6:10:01not we close the file but how does that
6:10:03actually work well let's try to do that
6:10:05so what I'm going to do now is I'm just
6:10:06going to make a class and we're going to
6:10:08write this context manager so I'm going
6:10:10to say class file I'm going to say
6:10:12Define nit and since we just want to do
6:10:14the same thing we did before we're going
6:10:16to take a file name and a method in our
6:10:18nit and we're going to say self. file
6:10:21equals open file name method now it's
6:10:25important to note that when we make a
6:10:27context manager we don't necessarily
6:10:29need to use it as a context manager all
6:10:31the time in fact I'm going to start
6:10:33writing a context manager here and show
6:10:34you the methods we need to implement to
6:10:36do that but right now you know the
6:10:38function that we have here we can use
6:10:40this fine even though you know it's
6:10:41going to be a context manager we can
6:10:43instantiate it and we can use it as we
6:10:45usually would we just aren't going to
6:10:46call these special methods that I'm
6:10:48about to wct so we have the first method
6:10:50which is called enter now remember enter
6:10:52I told you is the first thing that
6:10:54happens and what this function needs to
6:10:56do is return to us some value that we're
6:10:59going to use in the context manager
6:11:01right when we wrote for example with
6:11:03something um as you know F then this
6:11:07something is going to call this enter
6:11:09method so we're going to store
6:11:10whatever's returned from enter in F so
6:11:12that's what we should get there so here
6:11:14what we're going to do on the enter is
6:11:16simply return self. file and again that
6:11:18should be the open file object then what
6:11:20we're going to do is Define an exit
6:11:23method so underscore uncore exit uncore
6:11:25uncore and notice these are special
6:11:26Dunder methods because they're going to
6:11:28be called in a special way automatically
6:11:30from python because we're going to use
6:11:32the context manager syntax so here
6:11:34actually takes three arguments and the
6:11:36three arguments or parameters I guess we
6:11:38have our type I believe we have I'm
6:11:40going to look to make sure I haven't
6:11:41messed this up we have value and then we
6:11:45have Trace back so essentially what this
6:11:48does is regardless of whether or not we
6:11:51get an exception between when we open or
6:11:53so between when we enter and between
6:11:55when we exit if we get an exception it's
6:11:57actually going to call this exit method
6:11:59with that exception so that we can
6:12:01handle the exception in here the way
6:12:02that we would like and I'll talk about
6:12:04how that works a little bit more in a
6:12:05second but let's just do what we wanted
6:12:07to do which is self. file. close so for
6:12:10now we're actually going to omit the
6:12:12fact that uh we're going to deal with
6:12:14these parameters although we'll talk
6:12:15about them later so in here I'm actually
6:12:17just going to write some code um some
6:12:19print statement so that we can see what
6:12:21happens when we enter and when we exit
6:12:23these methods so print enter print exit
6:12:27so now what I'm going to do is I'm going
6:12:28to set up the same thing I did before
6:12:30except this time instead of saying with
6:12:31open I'm going to say with file and the
6:12:34reason I'm allowed to do this is because
6:12:36I have this enter and exit method
6:12:37defining that this is a context manager
6:12:39and I'm going to say with file we have
6:12:41let's say you know file.txt let's put
6:12:45that in say read mode um actually we'll
6:12:48put it in write mode and then we'll put
6:12:50that as F we can say f. write hello as
6:12:55the classic example that we've been
6:12:57using and now let's actually run this
6:12:59code and let's look at these print
6:13:01statements and make sure this works okay
6:13:03so we get enter and exit and inside here
6:13:06I'm actually just going to put a print
6:13:07statement that says middle just so we
6:13:10can see how this works so even though I
6:13:12have it explicitly called this exit or
6:13:14this enter method we can see that they
6:13:16do get called so enter middle exit and
6:13:19they get called in the appropriate
6:13:20timing right so this stuff happens in
6:13:23between the enter and the exit and now
6:13:25what I'm going to do is simply put an
6:13:27exception in here so I'll say you know
6:13:28put some letters here just to make sure
6:13:30this crashes let's run this um and
6:13:33actually sorry that's going to be a
6:13:34syntax see if there's any way that I can
6:13:36get this just to crash uh let's just say
6:13:39raise exception like that and now let's
6:13:42see if we still get um all right yes we
6:13:44do so we can see we get enter middle
6:13:47exit and then we get the exception
6:13:48raised afterwards even though we raised
6:13:51the exception before we got to this exit
6:13:53method so what this essentially says is
6:13:55regardless of if there's an exception or
6:13:57not we're going to call this exit method
6:13:59and the point of this is so that we can
6:14:00actually handle an exception inside of
6:14:02our exit method if we need to so I know
6:14:04this is kind of confusing to see in my
6:14:06Trace down here because there's all this
6:14:07text but look what's happening right we
6:14:09can see that in line 17 an exception was
6:14:12raised but the thing is we still called
6:14:15this exit print um and we still closed
6:14:17the file even though that exception was
6:14:19raised which again is the advantage of a
6:14:21context manager okay so what I want to
6:14:23do now is show you what this type value
6:14:26and Trace back actually is so we notice
6:14:29that what happens is essentially any
6:14:31exception that's raised inside of here
6:14:33actually gets sent to this a exit
6:14:35function where it can be handled and
6:14:37that's why we see this exit popping up
6:14:39here because we enter we do this middle
6:14:42stop some exception was raised in the
6:14:44middle that's fine so we immediately go
6:14:46to this exit function where we pass that
6:14:48exception and we wait to see if it gets
6:14:50handled and that's why all of this code
6:14:52in the exit function runs before we
6:14:54decide that we're going to crash the
6:14:56program or that something's going to
6:14:57happen so what I'm going to do now is
6:14:59show you how we can actually handle an
6:15:01exception inside of this exit function
6:15:02and again what type value and traceback
6:15:04are
6:15:05so now we'll keep printing exit but
6:15:08we're also going to print all of the
6:15:10different values that are here so we'll
6:15:11just use an F string and we're just
6:15:13going to do a bunch of commas so that we
6:15:14can see what all these are so type value
6:15:18and in this case Trace back so let's run
6:15:20this now we're still going to get a
6:15:21crash in our code but now we can see
6:15:23that we have class exception none and
6:15:26then a trace back at some location so if
6:15:29we wanted to we could look at this
6:15:31traceback we could look at the type of
6:15:32the exception and we can actually handle
6:15:34it and in fact the way that we handle an
6:15:37exception from our exit is if we
6:15:39determine that this exception is fine
6:15:41that we shouldn't crash the program that
6:15:43all is good what we're actually allowed
6:15:45to do is return the key value true so if
6:15:49we return true from this exit method
6:15:52here that's going to tell python that we
6:15:54gracefully handed handled this exception
6:15:57which means we don't need to crash the
6:15:59code we don't need to run or you know
6:16:00see all this Trace back in the output
6:16:02here we're fine and we're good to go and
6:16:04in fact if I run this you can see that
6:16:06by simply adding this return true here
6:16:08we no longer get a crash in our program
6:16:10and everything is fine now be careful
6:16:12with this because you don't want to just
6:16:14add return true if there is actually
6:16:16exceptions and they need to be handled
6:16:18properly in fact a better practice would
6:16:20probably be to do something like if type
6:16:23equals equals and then whatever type of
6:16:25exception you were looking for maybe I
6:16:27raised like a file not found exception
6:16:29or something and we were looking for
6:16:30that specific one then what you could do
6:16:32is handle that exception inside of here
6:16:35and then return true in that instance
6:16:37and in fact this still works because the
6:16:38type was exception um but I'm trying to
6:16:41see if I can raise like yeah so let's
6:16:43say file um exists error like let's do
6:16:45something like that and run this and you
6:16:47can see now because that wasn't just a
6:16:49regular exception this crashed because
6:16:51we didn't handle that properly so that's
6:16:54how we can handle errors inside of this
6:16:56exit function you just return true if
6:16:59when you check through this error that's
6:17:01passed in here um you get you know some
6:17:04valid stuff and can handle it right and
6:17:06if you don't return true and there's no
6:17:08error like we've seen that's totally
6:17:09fine so if we don't raise some exception
6:17:12regardless of if we return true or not
6:17:14everything works fine and we're good to
6:17:16go and that is one of the massive
6:17:17advantages of context managers okay so
6:17:20now I'm going to show you how we can
6:17:21actually create the same context manager
6:17:24because this seems a little bit
6:17:25complicated using a generator so we've
6:17:27talked about generators and we talked
6:17:29about decorators what we can actually do
6:17:32is import something called context lib
6:17:35so we can import context lib like that
6:17:37and from Context lib there's actually a
6:17:39decorator in here that allows us to
6:17:42decorate a generator that becomes a
6:17:45context manager and I'm going to show
6:17:46you how that works okay so let's do the
6:17:48same example we did before with opening
6:17:49a file but now using a different kind
6:17:52well not really different kind but just
6:17:53a different way of creating this uh
6:17:56context manager so what I'm actually
6:17:57going to do is I'm going to write this
6:17:59decorator and The Decorator is context
6:18:01li. context manager like that there's no
6:18:04brackets at the end and in fact what we
6:18:06could have done is said from Context lib
6:18:09like this import context manager and
6:18:11this is a built-in library in Python and
6:18:13that will just allow us to do this
6:18:15rather than having that um dot
6:18:17beforehand so let's do that I'm going to
6:18:19say Define file like this and what I'm
6:18:22actually going to do is take again the
6:18:24file name and the method and inside here
6:18:27I'm going to say okay file equals open
6:18:31and in this case we'll say file name
6:18:33method then I'm going to yield cuz again
6:18:35this is a generator the file object and
6:18:38then I'm going to file. close so what
6:18:41this means essentially is that the first
6:18:44thing we're going to do is say file
6:18:45we're going to create this file object
6:18:47we're going to yield this to wherever
6:18:48we're being called from and then when
6:18:50this function resumes again you can
6:18:52think of this as the enter right and
6:18:54this as the exit we're going to close
6:18:56the file and in fact in here again we
6:18:58can handle some exceptions we can do
6:18:59things like that and this is a quicker
6:19:01easier way to actually make a context
6:19:03manager so now I can say with file and
6:19:07in this case let's just say you know
6:19:09what what have we been doing I don't
6:19:10know let's just say text.txt let's
6:19:12switch it up here let's go W mode and
6:19:15we'll say as F and we can say
6:19:18f.r let's say hello like that and if we
6:19:21run this code that's perfectly fine that
6:19:23works and now when we add our print
6:19:24statement so we'll say
6:19:26print uh enter like that and we'll
6:19:30print exit like that and then we can
6:19:32print middle and we can see how this is
6:19:34is actually working okay so enter middle
6:19:38exit so this decorator allows us to turn
6:19:41this generator object into a context
6:19:43manager it works pretty much exactly the
6:19:45same as I talked about before there is
6:19:47more to go in and kind of dive in with
6:19:49this uh this syntax right here I
6:19:52typically recommend to people just to
6:19:53use the class syntax just because it
6:19:55allows you to do some more things into
6:19:57something more people are familiar with
6:19:59but if you want to use this context
6:20:00manager decorator and do some things
6:20:02with this generator kind of syntax um to
6:20:05use a context manager you're more than
6:20:06welcome to and that's why I showed you
6:20:08so I think with that I'm going to wrap
6:20:10the video up here an interesting idea
6:20:13though if you guys want to mess around
6:20:14with context managers is to think about
6:20:16locks in threading so actually from the
6:20:18threading module in Python I can't
6:20:19remember the syntax right now off the
6:20:21top of my head you can have shared
6:20:23memory um and you can have locks and
6:20:26yeah like memory locks which essentially
6:20:27allow you to wait for another resource
6:20:30to give up this lock so that you can
6:20:32access a resource so the point is like
6:20:34say we have one variable X here that's
6:20:36going to be accessed by two different
6:20:37threads well we can't access and change
6:20:39X at the same time so we create a memory
6:20:42lock and what one thread will have to do
6:20:44is wait for that lock to be available
6:20:46before it's able to access the object
6:20:48and you can actually implement this
6:20:49functionality with a context manager so
6:20:52that your threads you make sure they
6:20:54unlock the resource before they finish
6:20:56changing it so that's something that's
6:20:57interesting if you want to mess with
6:20:58that I don't really have that much time
6:21:00in this video so I'm not going to do
6:21:01that um but yeah so with that being said
6:21:04this has been context managers I hope
6:21:05you guys enjoyed if you did make sure
6:21:07you leave a like subscribe to the
6:21:08channel and as always let me know if
6:21:10there's any other expert level features
6:21:11you would like to see