Full --2023-- PYTHON course for Beginners

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everybody my name is Dave and this is my python course for 2023 and Beyond of course I say that because we're heading into the new year and we're going to be on the latest version of python which is python 311. so to get started what you're going to want to do is simply download and install python so to do that what you can do is come over to python.org okay and then you can simply hover over the downloads Tab and then you'll be able to download python 311. now um if you are on Linux I believe you're going to want to go over to other platforms but you can see uh depending on what operating system you're on you could just click the tab over here on the left as well okay so after you do that now I already have python installed so I'm not going to do that but after you install python what I want you to do is navigate to Jet brains okay jet brains is a company I believe a Czech company that um they create some of the best Ides in the world um one of the best Ides for python is pycharm and um it's quite popular pycharm is quite popular so what is an IDE it is an integrated development environment and it's just software that allows us to write well helps us write code and it usually has a lot of features and it just makes our job as programmers or developers easier okay so once you get to jetbrains oh and a little information about jet brains that is pretty interesting is that they actually helped Google develop their official programming language which is kotlin okay now once you get to pycharm you're going to want to download the Community Edition okay um it's free and it's it's terrific um you know if you're a professional you know if you want to get the you know pie charm professional or something I don't know you can do whatever you want if you want the professional it does come with a free 30-day trial but the Community Edition has pretty much everything you need so after you install pycharm and you can just pause this video we're just going to hop right into the course you can pause the video here wait for pie charm to fully install after you do that what you're going to do is you are going to create a new project now your screen might look a little bit different because it's the first time you're opening it but I'm going to create a new project and this box will pop up for you okay and right up here is where you want to name your project what I'm going to name my project is course okay and you can actually uncheck this box where it says create a main.py welcome script it may or may not be checked but just make sure it's unchecked we don't need that okay so I'm going to go ahead and create and we'll do this window and there we go we have our first pycharm or python project uh created and what we're going to do is we're going to click on our top directory okay this is our main folder and we're going to right click this and we're going to create our first python file all right so hover over python file if you do happen to click on file you can use this as well but you have to use the dot py extension so this is just a little shortcut just a new python file right here and what we'll do is we'll go ahead and name this uh variables and data types let me move this microphone a little bit maybe a little bit over there okay so let's call this variables actually I'm going to type a little bit slow just to um I don't want to I don't want this course to go too fast okay so variables and we'll just say data types this is a little bit long make sure you have underscores okay you don't want to have empty spaces and there's a reason for that I'm not going to get into it right now they work 90 of the time but sometimes you can run into problems when you have spaces in the name of your file okay so let's go ahead and hit enter and we have our first python file and what we're going to do is talk about data types and variables of course so let's go ahead by talking about variables what is a variable it is simply a container that stores data okay so let's go ahead and create one we can name our variables whatever we want but they should be appropriate and meaningful so I'm going to name this variable name okay and then I'm going to put a space put an equal sign okay and then I'm going to put another space and I'm going to put a data type in here called a string and a string is just a character or sequence of characters to um to write a string you actually have to use either single or double quotes I like using double quotes a lot of developers like to use single quotes because you don't have to hit the shift key but I find that double quotes ends up saving me a little bit of time and we'll get into that a little bit later but I'm going to use double quotes for this course so let's go ahead and put some double quotes here and I'm just going to put any name in here okay so I'll just go ahead and put Dave sorry about that okay and why don't we just go ahead and comment this okay so I'll put a space space pound sign or hashtag another space and then we can actually write a comment okay so why don't we go ahead and say something like um this is a string data type or character slash sequence of characters okay so a string data type again is a character or sequence of characters and it's being stored inside of this name variable remember it's just a container okay so I could have a container um like a sandwich bag and what would I put in probably a sandwich a container is like the same thing think of it as an empty box that you put stuff into all right so let's go ahead and create another variable I'm just going to call this one X okay and I'm going to set it equal to 9. all right now notice I'm not using double quotes here this is a different data type this is an integer okay so I'm going to do space space hashtag space and say this is an integer or well let's say entered your data type or hole number sorry okay so we have our variable which is X okay and it's equal to 9. now what what is this variable it's again it's an empty container and it's holding data what kind of data well it depends on the data type that you put in it this data happens to be a string which is characters or sequence of characters and they have to be surrounded with double quotes okay or single quotes an integer doesn't need those it's just a whole number now I will be a little bit repetitive in this course so if you want to speed it up that's okay a lot of my students need repetition so that's just how I teach now there are probably other courses online where there's no repetition and you just whiz right through it but I'm not going to teach that way so let's go ahead and create another variable and we'll set it equal to let's do a 9.5 okay so this is a float or I will say data type again or a number with a decimal point okay so floats are like fractional numbers they're numbers with decimal points in them okay and you you have to make sure that you understand the difference I mean obviously it's pretty simple to in pretty clear what the difference is but just make sure you understand the difference between the two because it does matter in Python some other languages it doesn't matter like JavaScript they're just all numbers but in Python it matters okay now let's talk about the fourth data type which is a Boolean okay so what I'm going to do is create a variable called is underscore day and I'm going to set that equal to true because right now it is daytime where I am so this is true and let's go ahead and comment this we'll say this is a Boolean data type or let's do it or true or false value and we'll just put a slash in here instead or true slash false value okay and let's go ahead and create another one we'll just call this one is night and of course we're going to set this equal to false now make sure your booleans have a capital you can't use a lowercase t or a lowercase f booleans and python have capitals they're keywords okay and um these are booleans they're true or false values what do they do well they're a little bit more complex they're not super complex they're a little bit more complex than I'm going to explain right now but in their most basic form they kind of direct code for example when something is true python will run code and when something is false python will not run code okay so that's the most basic form of booleans obviously you can write your logic and change your logic up a little bit so that stuff runs when it's false but we're not going to get into that right now so variables and data types okay a variable is a container and the data type is what we're storing in that container now you don't have to store a data type in a container but if you don't it's not very useful for example if I don't store all of my you know Christmas tree ornaments in a box or my holiday ornaments in a box if you don't celebrate Christmas um then it's not super useful they're just kind of all over the place or what I put each individual one in a different it's just not very useful so you usually want to use variables so you can take that data with you throughout your entire program okay if you don't you can't take it with you it's not portable okay all right let's go ahead and stop this lesson and just take a break and then we will come back into the next lesson and I will see you soon hello everybody we are back and in the next lesson we're going to go over something really really simple it's going to be like 30 seconds long and it's called multiple assignment and it's not um it's not always the best to use okay it's sometimes it can reduce readability we never really want to reduce readability perhaps if you have a very very small program and it's only for yourself fine you can get away with it but for the most part we want to maintain readability okay or um increase readability so let's just call this multiple assignment and let's get into it and of course I need to go ahead and get rid of that second a enter and what is multiple assignment well it's when we declare all our variables and put data types store data types inside of those variables on the same line so just like we did in the last lesson I'll say name X comma y comma is day comma and then is night and now I'll put my equal sign and then I'm going to assign corresponding values okay so I'll just go ahead and put Dave I believe X was 9. y was 9.5 and also notice what I'm doing after each comma I'm putting a space comma space now the reason I'm doing this is to maintain styling okay we want to maintain pep 8 okay so basically we maintain Pepe to maintain readability proper styling helps us maintain readability um his day was I believe true because it's still daytime um and then we have false okay so this is multiple assignment it's when we do everything on one line sometimes you can use this it's not a huge deal to use this but I typically avoid it doesn't mean it can't ever be used it just in some in most instances it can decrease readability in some instances it instances it's it's completely fine all right so that was the end of that lesson very short I will see you in the next lesson okay everybody I'm back and I'm ready for the next lesson I hope you are too so let us begin again when we create a file we're going to want to create it in this top directory okay so I noticed that I had multiple assignments I must have misclicked something some it was not in the proper um folder so make sure you click up here okay on the top directory and then create your python file there what we're going to do is we're going to name this um string Methods okay and let us begin so string Methods well a string is a data type and because it's a data type it's also an object don't think too much about this okay I know that seems uh you know a little bit crazy like oh it's a string data type it's a string object like what is it well it's both okay but we're not going to get into objects right now I don't want you to um you know get intimidated by that it is a string data type therefore it is a string object therefore it has behaviors okay what do we call those behaviors we call them methods all right so let's go ahead and create a string data type we'll say name again actually let's do City and we'll just put Boston okay okay again I have a string data type which is also a string object and therefore it has behaviors what do we call those behaviors methods how do we access those behaviors well we type the name of the variable generally we can access them a couple different ways but generally what we'll do is we'll type the name of our variable City represents Boston and then I'm going to use the dot operator okay and there we have it you can see all of the methods and if you look over to the left you're going to see these little pink circles with M's in it and that's just pie charm's way of telling you hey these are all the methods associated with a string data type which is also a string object okay so let's go ahead and just kind of look through these um why don't we start off with an upper okay so you can always tab to autocomplete if you notice that when I just typed that in really fast I didn't actually type it I hit tab the second you see the method that you want at the very top you can go ahead and hit Tab and it will auto complete it for you okay now what you're noticing is I wrote the name so I call I called basically called the name of the variable that I wanted to use I use the dot operator and then I access the upper method now what are these parentheses these parentheses are the call okay they tell the method to occur okay and if you don't have these it'll simply tell you where the method is stored in memory but once you add the parentheses then it will actually call the method and perform that behavior what do we put inside of these parentheses well in this particular method we don't put anything in it okay but in many methods we're going to actually pass in what are called arguments we'll approach those in a minute so let's go ahead and run this and what we can do is actually right click and then you're going to see run string Methods okay now the reason I'm telling you to do this is because we haven't run this file before if you have not run a file before you really should just right click it or you can see you can control shift F10 but I just like to right click for the first time and then subsequent um so when I when I run the file afterwards because it's already been run and it's already been selected up here you can just hit shift F10 all right so I'm running this and we see nothing why do we see nothing well because we told we haven't told python to display anything and to do that we use what's called a function okay and specifically we're going to use the print function so let's go ahead to the beginning and we're gonna we're gonna type print and then I'm going to put a parenthesis and another parenthesis okay so now what did I pass into this function as an argument I passed in City Dot Upper okay I'm not going to pass an argument into the Dot Upper method though because it doesn't require one now if I run this you can see this particular Behavior makes every letter uppercase okay so let's go ahead and move on okay let's go ahead and print a city Dot let's do a DOT title and let's run this now you can just shift F10 it's going to run uh if you're on Mac I think it's command are but I'm not sure some of my I don't have a ton of students on Mac actually most of my students are on windows so um I don't I don't remember the Mac shortcuts anyways you can see that dot title actually creates uppercase now you may be wondering well why don't you just put an uppercase up here I actually like to use all lowercase the reason I like to use all lowercase is it just keeps everything uniform uppercase and lowercase have different values they're two different things and if I keep everything uniform via lower case I just do everything lowercase it's easier for me to find data when I need to okay I don't have to worry about oh was it an uppercase am I going to find an uppercase no I know everything that I do is lowercase and when I want to make it presentable I use methods instead okay all right so we can see that the dot title method again capitalizes just the first character but what I'm going to do is I'm going to put an MA in here too and now let's run it and you can see that the dot title method doesn't just capitalize the first character it also after every space characterizes the first character I mean I capitalize as the first character after every single space let's go ahead and run another method so we'll do a print a city Dot um let's do a DOT capitalize so you can see the difference and you can see that this particular behavior capitalizes only the first character all right and it's different than dot title it doesn't do it to the to the um to the characters after any spaces okay all right let's go ahead and move on let's go ahead and print a City dot what do we want to do here let's go ahead and look through a little bit why don't we do an is digit this one's cool and let's run this and we get false why do we get false well python comes up into this string and it says hey are you a digit string it checks is this a digit string well there's no digits in here okay so we know it's not a digit string what if I put a 9 okay will that make it a digit string let's run this and it still doesn't a digit string has only digits in it okay so that's why we're getting false let's go ahead and move on let's go ahead and print a city Dot let's do a city dot find okay and I'm going to pass in an argument for this one because we have to tell python what to find but to tell this method what to find and this are this actually takes a string as an argument so I'm going to have to put in my quotations okay and I want it to find oh let's go ahead and run this and I get a one what is that what is a one well it's representing the position or the location and we do that by using an index okay so with indexes we start from zero so that means the location of B is at the zero index and the location of o is that the one index s two T3 the second o for and also remember that space has value okay so I believe that would be at the sixth index now you may be wondering well why is it only giving you a one with there's two o's in here unfortunately the dot find method only finds the first instance okay so that's why it's giving you a one and not the um four okay now um let's go ahead and move on so let's go ahead and print a City dot count and let's go ahead and count the o let's run this now what this particular Behavior or method does is pretty simple I'm sure you've already figured it out by now but it just counts how many occurrences there are okay so there are two occurrences of O It's counting that and it's returning a number to you now what is it actually what is this data type that it's returning to you because that's important is it returning a 2 in string form no it's returning an integer to us okay and we'll get into that a little bit later but as you can tell these required arguments these behaviors again these methods they did not require arguments so when will you know whether something requires an argument you have to pass in an argument well typically it just comes from experience but you can always get information and I'm going to show you right now what what you could do so for me what I like to do is I like to call that particular method let's just say I wanted to do um let's do is lower okay now typically what you can do is Click inside of these two parentheses and just hover right over it and you can see we get this box here and this is telling us this is a called a document string oops sorry about that this is called the document string and it gives you more information so return true if the string is a lower case string false otherwise all right and you can see that it also is going to return a Bool or a Boolean which is a true or false value remember and you can tell by this little arrow here also what you can do is you can click on the documents here so let's go ahead and do that and what I want you to do is learn how to read documentation okay because it's crucial you need to be able to read documentation all right and you can see return true if all case characters in The String are lowercase so the documentation is also very useful and you can you can see that this requires no arguments nowhere in this documentation or in that document string did they say this requires an argument all right again most of it just comes from experience but you can always figure out whether or not it requires an argument let's go ahead and print a city dot replace and let's go ahead and replace the O okay now this requires two arguments so this is my first argument and now I'm going to put a comma remember a space after every comma and I have to tell it what to replace the o with and let's just replace it with an i okay let's run this and what does it return to us a piston Mass okay now this also allows you to enter an optional argument okay so you can specify how many eyes you want or O's you want to replace perhaps you only want to do the first o you actually could just put a one here let's run it and you can see it only replaces the first o uh you can also say this had like 15 O's in it you could replace the first five by just putting a five here again this is optional so you don't have to use it all right let's go ahead and move on let's go ahead and print a City Dot and let's just try to find something um what do we want to do here also by the way I don't know if I told you the is lower method is just checking to see if this is lowercase let's do a well how about we end it here we'll we'll end it here now um just one thing to note if you start seeing these donor methods you've gone too far what are Dunder methods double underscore methods don't worry about them right now um you've gone too far you're typically just going to use these regular methods up here okay all right so I'm going to delete this and just to recap the lesson a string data type is also a string object and objects have methods okay those methods just represent the behaviors every data type has behaviors or methods okay so an integer data type is an integer object and it has integer methods a float data type is a float object and therefore it has float methods okay so uh the reason we're going over string Methods is because they're much more useful okay Boolean flow integer methods are just not that useful okay so um that will wrap up the lesson and I will see you in the next one hello everybody um we are going to start our next lesson and to do that we're going to again right click our top directory here hover over new create a new python file and let's go ahead and call this type casting okay what is Type casting very simple we don't want to overthink this type casting is when we take one data type and cast it into another one or change it into another data type okay but it does have to make sense okay python just doesn't just change whatever you tell it to change okay you have to do things logically all right so let's start off by creating a variable called X and let's store a string nine inside of it let's create another variable let's call it Y and let's store an integer inside of it and let's create another variable we'll call it Z and let's just go ahead and put a float inside of it so these are all the same number okay but they're all different data types perhaps we needed to perform some kind of mathematical operation well do you think that I could perform math on a string I actually can but it's different math okay it's not the math that we know we'll get into that a little bit later but basically I I can't I can't do any kind of arithmetic on a string all right so I may need to change it I may need to convert that nine and to do that what I'm going to do is just re-assign so I'm going to say x equals now I'm reassigning because X already exists and I'm going to say x equals int nine uh sorry not nine and X okay so X now equals whatever X was but the integer version of it okay so let's go ahead and print this okay so let's print X and let's run it oh and again because this is a new file you'll want to right click and then run okay and now we have this nine here all right but how can we tell that it's been converted to an integer well obviously we had no errors like we know this has been converted into an integer but we can always ask python we can say hey python print the data type of X and we do that by using the type function and then what comes after a function parentheses and what do you pass in print into parentheses arguments okay so let's go ahead and run this and you can see it is an integer object of the integer class okay what it's telling us is this is an integer okay now let's go ahead and move on let's try to convert 9 integer nine so y equals Str 9 not nine I keep doing that sorry s t r y and let's go ahead and run this we have no error how can we see what data type it is well we can always just say print type Y and let's go ahead and run this and you can see python is telling us hey that is a string so our Typecast online line 8 did indeed work all right let's go ahead and type cast Z so we have a float why don't we try to convert this float into an integer and let's see what happens so Z Now equals int Z and let's run this okay and let me go ahead and actually because we're on python 311 hmm let's just go ahead and print Z and we get a 9. I actually don't think this was possible in Python 310 I may be wrong but typically you'd get an error because there's a decimal point I do believe this may be a new feature I could be wrong if I'm wrong just uh you know put that in the comment comment down below and let me know but I'm pretty sure we were never able to Typecast that so hey great let's go ahead and move on now what I want to do is convert let's go ahead and create another variable let's just call this XYZ okay and let's set this equal to 19. I want to convert 19 to a Boolean okay so I know that sounds a little bit weird but let's just say x y z Now equals um Bool x y z and let's just go ahead and print this oops XYZ now what do we get true so what's going on here how did you just Typecast a number into a true or false value pretty simple and actually it's very useful but any number that is not zero is true you may be wondering well why well because it can be very useful to us okay so perhaps we have a program that depends on how many people are voting and as soon as we have 30 people voting and as soon as that number reaches zero we want the program to shut off okay so maybe the program is true when there's 30 people 15 people vote there's 15 people left the program is still true another 15 people vote the number becomes zero and then it becomes false and the program no longer runs okay so it can just be very useful for us and it's the same thing with strings okay so let's create a string and I'll just say name equals Dave and let's just go ahead and convert Dave so we'll say name equals Bool name let's print name run it and we get true again well the reason it's true is because this has value the only time a string has no value is when it's empty and that applies to some other data types as well that we'll get into but because this is not empty it is true now if I make this empty and run this it becomes false now what happens if I just have a space in here this is still empty right well let's run it and I get true space has value remember that space has value okay now this is typecasting and you're going to see in the next lesson why this is very important so what we'll do is we'll end the lesson here and I will see you in the next hello everybody I am back I hope you are ready for the next lesson I took a nice little nappy poo and I am rested up and ready to go so what we want to do is create another another file so we're going to clip click on our top directory here and let's just call this input okay now taking input is super useful we may need information or data or something from a user okay and the way we do that is by taking input so how do we take input very simple it's just like print okay we call the the name of the function which is input you can tab it if you want remember parentheses after functions and methods and we have our parentheses here and now we're going to pass in an argument here what kind of argument do we pass a string and it will be the prompt it's going to tell the user what kind of information we want from them so I'm just going to ask somebody for their name so I'll say please enter your name and what I like to do is put a colon here and a space okay let's do it without that let's go ahead and run this program remember to right click if it's uh because it's a new file and you can see down here it's asking me and it's allowing me to put input in but you can see this cursor is just blinking right next to the E and A lot of people have a tendency to put a space and then enter their information so Dave remember space has value this can actually break the program okay this little space can break the program never allow the user to have the opportunity to put that space in there and the way we do that is by coming up here and I like to put a colon and a space now let's rerun this stop and rerun and now this looks a lot cleaner okay and now the user is not going to go ahead and try to put a space to give themselves space trust me it happens a lot and let's just go ahead and enter a name so I'll go ahead and enter my name hit enter and everything worked but this program does nothing remember variables we want to use variables that makes our data that makes our data portable so if I want to use that name I would store it in a variable remember you can name your variables whatever you want but they should be appropriate and meaningful I'm not going to store this in a variable called you know farc what does farc mean that it's not useful it's not um meaningful at all so we'll just go ahead and call it name and now what I want to do is when I take somebody's name I want to go ahead and print like a greeting so I'm going to say um hello then I'm going to put a plus sign here okay and then I'm going to put name let's go ahead and run this let's just go ahead and put Dave here and we have hello Dave of course we're missing the space the appropriate space so I could come right up here and I could add that space let's run it again I know I'm repetitive I'm redundant but repetition is what gets us really really good at coding all right so I'm going to put my name again and it looks better but we're kind of doing something that I don't like to do anymore and in fact I hardly ever do it I mean I still do it but I don't do it that often and that's called concatenation see this plus sign well we take input as a string when somebody gives us input it comes in as a string they can write 9 10 11. we know those are numbers but they come in as string numbers remember in typecasting this was a string nine if they put a 9 in this input well it would come in as a string so um when I have a plus sign and I have a string and another string it's called concatenation and it concatenates them together okay generally what will happen is they'll go from two memory locations to one all right now this print statement is simply printing it one thing I want to discuss with you is print has no value we'll go over that a little bit later but just remember that moving forward print has no value it simply displays stuff and that is it okay so how do we do this without concatenation we're going to use what's called an F string okay I want you to use these as much as possible they're amazing they're my favorite we're going to use them the rest of the course as much as possible sometimes you have to use concatenation anyways um we have this app string and what we'll write here is our message so we'll say hello and then what we do is we use curly brackets these curly brackets allow us to put in pipe it allows us to put in code python logic so I'm going to just simply put the variable name here and let's run this I'll put in my name and we get hello Dave okay so let's go ahead and make this a little bit more complicated I don't want somebody to enter their name I want somebody to enter their dog's name so please enter your let's just say dog 's name now this brings me to why we use double quotes well why I like to use double quotes because I have this single quote here it would actually interfere with my single quotes out here had I used them so you would have had to fix that by either using what's called an escape sequence or Escape character or use double quotes I like to use double quotes it just makes things easier for me again this would interfere with those single quotes that you used outside all right so we're going to ask them that dog's name and now I'm going to ask them to enter their dog's age okay so I'm going to say uh please enter your dog's age remember colon in space and what I'm going to do is change this F string I'm going to do something like this I'm going to say name the dog is age years old let's go ahead and put a exclamation point there and let's run this so now we're taking two inputs and we're storing them in two different variables and I'm just going to put Spike and the dog's age is let's just say the dog is seven okay and we could Spike the dog is seven years old but perhaps we wanted to present the dog's age in dog years well we know I don't know if it's scientifically proven but I think it's universally accepted that dog's age by seven years for every one human year so what we could do is multiply age by seven and this is the beauty of an F string it allows us to add python code and python logic into our strings and the output ends up being a string Okay so let's do age times seven and let's go ahead and run this so let's put Spike let's put seven and look look what we get a little bit strange remember in one of the earlier lessons I forget which one but I said well we can do math with strings but it's not the math that you and I know it's not arithmetic okay this is string math it's weird math and all it's doing is taking the string seven and it's multiplying it seven times so how do we fix that well we can use typecasting remember what would I what I would do is wrap the variable okay so let's go ahead and run this Spike seven and it works however there's a little bit of a problem here this works it's fine but every time I want to convert age I would have to actually wrap it I'd have to Typecast it every single time unless I Typecast the source where's the source right up here now whenever I use it it will oh it will remain a uh an integer okay so let's run this Spike seven works perfectly and we did this the right way by typecasting the source now sometimes you may not want to Typecast The Source but typically it's a much better approach all right let's go ahead and create a area calculator okay so what I'm going to do is I'm going to highlight all of this and I'm going to teach you something control forward slash this has now been commented out python does not it ignores comments okay The Interpreter it ignores comments so it sees the hashtag it ignores it it's not considered code and create that area calculator and we need two things to calculate area length and width so let's go ahead and do that length equals input and let's just go ahead and ask them so we'll just say please enter the length of the object colon space and what I actually like to do and this is a feature of pie charm is I like to hit Ctrl d and it will duplicate code for me okay because the only thing I'm going to change here is width and then um right here width all right now that I have the the length and the width what I can do is I can create another variable I could say area equals length times width all right and why don't we just go ahead and print something so the user can see their area so we would say something like this the area of your object is curlies area let's go ahead and run this program okay so let's just go ahead and put a 15 here and let's put a 78 here okay so we get an error and we get an error here I want you to try to figure this error out I'm going to go ahead and give you a second okay so pause the actually pause the video and try to figure it out all right so what's going on here is we're not typecasting now typically you would want to Typecast this as a float but as we learned I believe python 11 actually will still Typecast um it floats into an integer now which is strange but actually let's just go ahead and Typecast this into a float and same with this one okay let's go ahead and run it again and we'll put a 56.9 and a 23.76 okay and there is the object of our as the area of our object now you notice it's not in feet or inches or anything like that this is just kind of like a broad area they can enter whatever they want in here okay so that is taking input we will wrap the lesson up here take a little break and I will see you in the next lesson okay everybody I hope you're ready for the next lesson let's go ahead and get into it so what we're going to do is create a new file again top directory click on python file let's just go ahead and name this one math module okay now this is less about the math module and more about learning how to use modules okay now this particular module is part of the base standard library that means it comes with python all right so typically if something doesn't come with python you'll want to come to the terminal here now if your terminal says Powershell what I actually recommend doing is clicking this down arrow okay going to settings and then Shell Path what I would do is switch it from Powershell to command okay I don't really like using Powershell in fact I think Powershell is horrible I'm much more of a bash kind of person okay so let's go ahead and click OK and then what you would want to do is exit out of the terminal that you're in and then just open it back up and from now on it will only open up the command prompt okay now if it's not part of the base standard Library you're going to have to run a pip install okay this is the python package installer we're not going to do it and then you'd put the name of the module or the package that you're trying to install but we're not we're not going to do that right now we're just going to work on the math module so again this comes standard in the in the in the library so what we're going to do is just simply import math now how do we use the module first before we get into that you're going to notice this is gray that's pie charm's way of telling you hey you've imported a module and you're not using it and that is a waste of resources in Python because python will run this entire module first it sees line one it says oh you're importing math I'm going to run completely run math and then it runs your script okay so basically we don't want to import stuff if we're not going to use it now if I was to right click on math and click go to and declaration or usages let's just scroll down you can see it's going to run 470 lines of code unnecessarily because again we're not using it so how do we use it well we simply type the name of the module then we use the dot operator again this is much like calling a method or accessing a method but there's a difference here okay these are functions we're typing the name of the module not a variable and then we're accessing its functions or its variables okay now let's go ahead and do a math.co okay and let's just go ahead and put a 9.1 here and let's run this and remember to right click if it's your first time with this module open so you'd right click and run it and we can see that there's nothing here remember we have to use the print function to see stuff if we want python to display stuff we must use the print function okay I purposely will leave the print function out just so I can repetitively tell you about it okay so um because again repetition is what makes us good let's go ahead and run this now and you can see we see it so if math.seal rounds up to the nearest whole number what do you think rounds down to the nearest whole number what is a whole number it's an integer in Python so if you guessed it congratulations it is map.floor so let's just go ahead and put a 9.9 in here let's run it I ran the debugger sometimes I believe if you hit shift F9 it runs the debugger so make sure you hit shift F10 okay again rounds down to the nearest whole number or the nearest integer you about math.pal now um this comes with python now you do not need to import the math module to use pal all right but we're going to go over it anyways so math.pal let's this is exponential math let's do 5 to the power of 9. again this takes two arguments so how do you separate those arguments with a comma okay what comes after a comma a space that's proper styling let's go ahead and run this exponential math is always huge so we get some astronomically large number let's go ahead and move on okay so let's go ahead and print a math dot square root okay and let's go ahead and do let's say 45. okay so we get the square root of 45. pretty simple this is how we use modules now one thing I just want to show you is nested functions now we already have a nested function right in front of us but we're going to make it a little bit more complex so I want to print a math dot square root I want the square root of math dot pow 6 to the power of 7. and actually why don't we say 6 to the power of math dot pi now you may be wondering well why do you not have parentheses after math.pi isn't that a function no it is not it's simply a variable inside of the math module actually put a dot and a p here you can see it's got that little orangey circle with the V it is a variable inside the math module that simply holds Pi 3.14 okay so let's go to Pi let's go ahead and run this and that's a nested function now the reason I wanted to show you this is because you know you will use nested functions and it's nice to just kind of get used to them this is how python works but also because I want you to know that the innermost function works first what's our innermost function math.pal so this function is going to work first it's going to return a value then we're going to get the square root okay that we're going to get the square root of whatever value this function produced and then we're going to print it okay that's our very last function all right so this is just a simple import let's talk about using an alias okay now math it's pretty simple sometimes our modules have a little like longer names but sometimes for a shortcut we can use what's called an alias and we would just use as m now you can see that these are all underlined with red squigglies all right that's because math no longer exists it's been renamed we're using an alias it's m now so I'm going to use a little keyboard Magic first so I'm going to double tap control and then I'm going to use the down key double tap there we go and I'm going to delete now because it's renamed as M it's m dot seal m dot floor m dot pal so the Alias can be useful okay that just saved us um you know uh 12 characters okay so yeah um 12 characters now you're like well that keyboard magic didn't work down here no it didn't so I'm going to show you another thing that's really useful what we can do is hit Ctrl R this brings up our replace menu and what I can do is type in math Dot and I want to replace all math dots with an m dot and I'm going to click replace all much faster okay now again this is using an alias if we run this let's hit run and everything is working the same okay so an alias can be very useful a lot of developers use it you don't have to but I like using them sometimes I don't I'm going to show you another way to import that is not necessarily the best way to do things okay in my opinion it reduces readability and it it's harder to debug okay so let's go ahead and change this import statement to from math import and let's just go ahead and import then we name the function we want to import or the functions so I'm just going to say seal okay now the only function that I have that's going to work is Seal so if I was to delete this seal is going to work what about floor doesn't work I didn't import it I can put a comma here and I could say floor and now floor will work this is not a good way to do things Okay the reason is because if you have a very large program and say you know you want to debug it or perhaps another developer is trying to debug it they're going to have a hard time finding every instance in which you used math whereas like before they could probably hit Ctrl F and they could say oh he's using the math module let me go find every instance and this is going to show them every instance in which you use math or in this case we used remember m so I could just say m dot now every instance where you use m that's going to come up your developer is going to be able to find it if you use this from math import it you you don't have that feature okay so let me go ahead and delete that and now I just want to quickly show you you can actually import every single function with the asteroid all right and now I would just hit Ctrl R and every time I see an m dot let's replace it with nothing okay so replace all and this works too the only function in here that you that would work regardless is pow and that's because it's been moved over into actually being a part of python so let me go ahead and delete that and again this is not something you do not want to do it this way okay so what I would do is actually we can't even fix it now with the replace method um but yeah I would typically avoid this unless you have like a really you know like a really small script now one question that I get a lot is hey if you import a specific function are you saving resources and the answer is no it still runs the entire script so why even use it don't use it okay all right so that actually will wrap up the lesson that's how you use modules again if it's part of the base standard Library you can just import it if not terminal and you got to run a pip okay pip install I believe there's another way to do it in pie charm where you can go to file settings actually I don't know where it is um maybe it's down here I'm not sure where it is actually I'll find out where it is and then I'll show you another way where you can actually install with pycharm but I never use it I always use pip okay all right so I will see you guys in the next lesson hello everybody I hope you are ready for the next lesson so let's begin now again we're going to right click new python file and let's go ahead and call this um arithmetic operators this will be a quick lesson okay so what are arithmetic operators let me first off clean this up a little bit get rid of some of these tabs and I also want to clean this up I have two programs running you can see right here this will actually show you which programs are running and you can just click the X and you can terminate those programs okay so you know the um the main arithmetic operators okay I'm sure you do it's going to be the plus sign the minus sign uh the multiplication sign is the asterisk and how do we do division well simple forward slash okay so let me go ahead and let me comment these so I'll say um Edition just do subtraction multiplication and division okay now the reason I'm showing you this is because I want to show you modulus exponential math and floor division it's not about these okay so how do we do floor division well floor division is pretty simple it just rounds down so if I was to say print nine let's just do like 900 divided by 32. and let's run this again right click and we get 28.125 all right now if I was to do floor division what it will do is actually round that down Okay so let's run this again and it rounds it down to the nearest whole number which is an integer okay so floor division can be super useful now let's talk about modulus and I love modulus so let's just go ahead and do 900 modulus 32 okay let's run this and what does it do it divides 900 by 32 and then it gives us the remainder okay so modulus is super useful for finding like even numbers odd numbers or multiples of a particular number okay so I could see if something is a multiple of a particular number if this equals zero so let's just go ahead and put um say 33. see what we get okay it's got a remainder of nine um let's do 34. I'll remain oh geez sorry about that 34. let's run it remainder of 16. 35 remainder of 25 so you can see it's just giving us the remainder I don't know all of the multiples of of 900 so um I don't know if we're going to find zero let's try 36 we'll do one more zero okay so 36 is a multiple of 900. again modulus is very useful for finding multiples evens odd numbers okay now let's quickly address exponential math now before in the math module we were using the pal function okay why who cares just use two asterisks that's what I like to do now you could make the argument that pow is more readable because most people understand that as power okay which represents exponential math but I just like to use two asterisks okay very simple short lesson I will see you in the next one hello everybody I hope you're ready for the next lesson so let's begin let's start off by creating a new file again and we'll call this conditionals okay oh forget the D let's go ahead and rename that now anytime you change a file it's actually called a refactor okay so if you do anything you make changes that is a refactor all right now what are conditionals well basically conditionals allow us to execute a block of code if a certain condition is met okay so let's go ahead and create a variable called age and let's set it equal to input and I want to Typecast that to an integer and let's just say please enter your age okay so what I'm going to do is I'm going to evaluate this age and what I'm going to start off with is an if statement so I'm going to say if age is greater than or equal to 21. we're going to go ahead and print a message now notice what I'm doing I'm putting a colon at the end of my condition this is my condition this is my if statement and then I'm tabbed over anything that's tabbed over will execute if the condition above it well it will execute if the condition above it ends up being met or is true so let's go ahead and print you are legal to drink okay so let's run this program let's right click run it and let's just go ahead and put um 23. okay so our program is working perfectly but what about anybody who's not greater than or equal to 21. let's run this program and just put like 19 here nothing executes okay if you want to execute something for anyone who's not greater than or equal to 21 well then you have to use an else statement so we say else and then we don't put a condition okay there's no reason to have a condition for anything else for example if I'm in my garage and I have a bunch of paint and the only paint that I care about is the red paint well I may say move the red paint all the way to the right and everything else can stay put I don't need to have a condition okay I don't care what color paint if it's not red it's staying where it is okay so again we enter down we're tabbed over and let's just go ahead and print you are not legal to drink and let's go ahead and run this now so if I put a 19 here you can see that this condition is not met therefore it's false and then this condition is going to execute no matter what okay because there are no conditions if the condition above it is not met there are it's it's going to execute now perhaps we had another condition well we can use an e-lif okay eliff is short for else if all right so when you see e-lift I want you to think of the words otherwise if and if you see the else statement I want you to think of the words everything else now e-lift statements have conditions so I'm going to say elif age is greater than say 95 let's go ahead and print you are far too old to be drinking okay Ctrl Z and let's go ahead and run this and I'm going to put 96 here what's going on this is not working well what's happening is this condition is not structured properly okay what I like to do is put conditions in either ascending or descending order or I like to put extreme conditions at the top okay now you could classify this as an extreme condition but I'm just going to go with descending order so I'm going to cut that out with Ctrl X I'm going to paste it up here and we see that we have an error we have red squigglies here now the reason is because you have to start with an if okay so you have to have the if statement and now this will become an e-liff now let's run this let's go ahead and put let's get rid of that box too let's go ahead and put a 96. everything's working let's just double check by putting say a 20 let's just go ahead and put a 95 okay because this is greater than it's not greater than or equal to so this should trigger that you are legal to drink and it does one thing you don't want you don't want these blank spaces in in between your conditions all right so again we want to structure an ascending or descending order or put extreme conditions at the top remember the first condition must be an if statement what's happening is python will see this if this is true it will execute and it will shut both of these conditions off okay if python sees this and it's false what it will do is it'll evaluate the next condition if that's true it'll shut everything else off all right so that's how conditionals work now we can have as many e-lifts as we want remember ellips are else if statements and I want you to think of the words otherwise if so let's say something like this otherwise if age is greater than um actually let's do less than 19 okay let's put a colon there and let's just go ahead and print and we'll say um your your not there let's say just yet okay so let's go ahead and run this program now let's shift F10 let's just go ahead and put 18 and we can see you're not there just yet all right so again we can have as many e-lift statements as we want but we should always try to to structure these and and like like I said ascending descending order or extreme conditions at the top all right so what if you had more than one condition but you wanted it on the same line so you could do an and statement you could write an and statement you could say and age is greater than 16. okay now what you're seeing here is yellow squigglies okay there's another way to write this and believe it or not I for a lot of my students I like to tell them to just write it like this because it's really clear and easy to read but you could simplify this okay there's a there's a a chain comparison that we can use so let's go ahead and simplify chain comparison and you can see this does the exact same thing all right so you can use either one I actually think the other one's more readable and I like to emphasize readability when it comes to programming it's it to me readability is crucial if your code is readable well then in 10 20 years from now people will still be able to use it it's just readable you know developers can work on it forever if it's not readable you know you're going to have issues okay it's not going to stand the test of time in my opinion so readability okay so you can see we have two conditions here let's go ahead and run this and let's put 18. and it's still working but let's go ahead and put 15. and you can see you are not legal to drink okay so these are how conditionals work let's go ahead and comment these out and let's do one more okay let's go ahead and remember to comment out you can highlight what you want to come out and control forward slash okay so let's go ahead and use conditionals for the area calculator again let's go ahead and ask somebody for the length of their object and we'll say length equals float input and we'll say What is the uh we'll say the length of your object okay and I'm just going to hit Ctrl d and we'll change this to width okay we'll say width here let's go ahead and create that variable for area so we'll say area equals I'm getting a little bit tired so I'll probably wrap it up after this lesson and area will equal length times width okay all right so we have the area and now what we're going to do is ask the person for the area of their room so we'll just create another variable called room area we'll set it equal to input and what we'll do is we'll just ask them for the area of the room so please enter the area of your room okay and now what we're going to do is just compare the two okay if the area of the object is too big we're going to say hey this will not fit in your room and everything else will fit in the room remember everything else we use the else statement so I'm going to say if area greater than room area well then we'll simply print this will not fit into your room and everything else will so we'll just go ahead and print this will fit into your room okay so we basically did the same thing up here except we're really analyzing two variables which happen to be input all right well really three but we pretty much change these two inputs into one variable okay so we're comparing area against rum area and we're using conditions to evaluate whether or not an object will fit in the room all right so this will wrap up the lesson I hope you enjoyed it and I will see you in the next lesson hello everybody uh I hope you're ready for the next lesson and let us begin so of course as usual we'll go up to our top directory and let's just go ahead and create a file called while Loops okay we will be going over while Loops in this lesson now what is a while loop it's B it basically allows us to execute code over and over again or repeat code okay this can be very very useful in coding or programming okay now while Loops we have to be a little bit careful with because they will just continue to run so we have to usually provide it with a condition and once that condition is met then the loop will break or we can do something else and I'll go over that as soon as we go over the condition approach so let's go ahead up that's done I will be playing that soon let's go ahead and create a variable called votes okay and let's set this equal to zero now what do we want to do we want to run a while loop while votes is less than 5. okay so what this Loop is going to do is it's going to ask somebody for their vote it's going to increment this vote by one and then once this variable reaches 5 then this while loop will break so why don't we just go ahead and ask somebody for their vote we'll set it equal to input and we'll say please enter cheese or um let's say cheese or pineapple okay and this is just kind of like two random Pizza flavors um mom and dad are going out to get pizza and they want to know whether you we want cheese or pineapple okay and why don't we do something like this too why don't we make this interesting let's just say Pine equals zero and then we'll say um cheese equals zero okay and what we're going to do is we're going to evaluate vote we're going to say if vote equals equals cheese then we're going to increment cheese by one so we're going to say cheese plus equals okay this is a reassignment so it's taking cheese whatever the value of cheese is right now it's zero and it's reassigning it and incrementing it by one at the same time all right so let's go ahead and put an e-lift here we'll say elif vote equals equals um what is it pineapple then what we'll do is we'll increment pineapple by one okay so we'll say pineapple um opine I used Pine plus equals one and then what do we want to do at the very end of this while loop we want to increment the votes by one okay so we'll say votes plus equals one all right and now what we'll do here is we will evaluate Pine and cheese okay so what we could do is we could say if Pine is greater than cheese what we can say is print we'll print pineapple it is else that means cheese one and we'll say cheese Pizza for dinner okay and I'll go ahead and do that so excuse me for my typing actually my I damaged the tip of one of my fingers lifting something heavy up in the garage it's not a big deal but it's kind of messed up my typing a little bit anyways let's go ahead and run this program and see how it works stop and rerun what is the length of your object oh we have to right click and we're going to enter let's go with cheese the next one will be pineapple let's do pineapple again let's do cheese again we have a tie so this is the tie breaker right here and I'm going to upset a lot of you I'm going to go with pineapple okay now the while loop is going to break because the votes was incremented and now it's become five which is not less than five so this condition ends up being met the while loop breaks and then it goes down to line 14 and it evaluates these two variables that we made up Pine and cheese okay so um let's go ahead and write another while loop that doesn't have a condition okay perhaps we don't know what condition it's going to be so let's comment this out sometimes we don't have a condition okay we don't know what the condition is going to be and there's two approaches to this we can use a flag or we can use what's called a while true Loop now they're both kind of frowned upon but I would rather use a wild true Loop any day over a flag because they're both not that great to use but I feel like they're both unavoidable because you just don't always know when you're what the condition is going to be and if anything the wild true at least that reduces one line of code for us so let's go ahead and use a while true and let's avoid using a while while loop of the flag so let's say while true now this is going to run infinitely but we have a solution okay and I'm just going to ask someone something very simple okay so I'm going to say um I'm going to ask them to enter their name so I'm going to say uh name equals input please enter your name and then what I'm going to do is I'm also going I'm also going to tell them they can enter Q to quit anytime okay and let's put the colon in the space here and let's just put a dot dot dot oops sorry all right so let's go ahead and print their name and now let's go ahead and run this program all right so I'm just going to put Dave Dave Steve Steve this code is going to run infinitely it's never going to stop so what we have to do is use the keyword break okay so what I would say is this I would evaluate name let's evaluate their name first we'll say if name equals equals Q then we're going to use the break keyword and what that will do is tell python to break this Loop otherwise what we'll do is we'll just print their name so let's go and print name and let's go ahead and run this let me get rid of some of these spaces you should never have more than two empty lines to maintain proper pep 8 styling okay why do we maintain proper pep 8 readability we always want to be able to read our code let's go ahead and run this stop and rerun and I'm just going to put Steve um bill let's do Sarah let's do Amy let's put my name and now why don't we just go ahead and use Q okay enter and Q ends up so when they enter Q it ends up breaking the code because this evaluated to true and therefore the logic that's tabbed over underneath it is going to execute okay so again these are while Loops we don't want to have an infinite while loop so we just got to make sure that we're writing our logic properly if you don't know the condition use a while true over a flag maybe some other developers out there say use a flag I hate using Flags I'd rather use this any day but typically you'll want to try to come up with a condition okay all right so that's the end of this lesson and I will see you in the next hello everybody I hope you're ready for the next lesson because we are going to be going over lists these are very important Python and why don't we just go ahead and as usual create our file here at the top and we'll just call this list okay now what is a list a list is just a collection of items okay and it can have as many items as you want and each item is separated by a comma okay now a list can also be empty or just have one item that's possible too and also the data inside of it is typically related we can have any data type we want we could have a string an integer but typically like I said the data is related it doesn't have to be though all right they just work a little better when they are now let's go ahead and create our first list and we use square brackets to do it so I'm going to create a list called cars okay and then I'm just going to use square brackets and right now I have a list okay it's an empty list but we're going to populate it so let's go ahead and add a Tesla comma space and then we'll go ahead and add a forward let's do a Chevy and then let's just do uh Toyota yeah we'll do it on Toyota okay so I have a list with four items in it all right and they also have index numbers just like strings okay so Tesla is going to start at the zero index number Ford one Chevy Two Toyota three three index numbers for a total of well not actually Four index numbers but four elements index number zero through three all right so now that we have this list let's go ahead and talk about a list and how it's mutable okay so I don't know if you've heard this term you may or you may have not heard this term but it simply means that I can alter this indirectly all right I do not have to come up here click to add another item I don't have to do that I can do it indirectly from a separate line of code and generally what we do is use methods to do that but I'm just going to show you some manual selection first so if I wanted to just select the Ford I would have to write the name of the list okay and then I'm going to use square brackets right up against the list now when you use square brackets right up against the name of the list like this it becomes an index operator okay so now I have the index operator here and I'm just going to put the index number all right and I think I was going to select actually let's just go ahead and select Toyota which is going to be the last element and now what I could do is I have Toyota selected so I could just print it okay and make sure you right click run and there we go we've printed Toyota okay now I can also use the manual selection to update a value okay and this is very useful so what I would do is put cars square brackets three and I could change the Toyota to say a Honda and now if I printed this list so we'll go ahead and print cars and run it you can see the Toyota has been changed to Honda all right so this is a manual selection all right now we can also use that to delete so we would use the Dell keyword here okay and I want to delete say the Ford okay so I'm going to say dell cars index operator and then one because we know Ford sits at the one index now before we move forward into methods I just want to quickly discuss negative indexes okay so this is zero through three but the last element you can also start from the last element whereas this would be negative one so everything has a negative index number as well but we start from negative one not zero okay so negative one would be Toyota negative two Chevy actually Toyota has been changed to Honda but um not until line five okay so oh that's one thing I just want to quickly discuss python reads from top down so it's not until line five that the Toyota becomes a Honda from line four all the way to line one in this list the Toyota stays a Toyota all right again Toyota it starts from negative one Chevy negative two four negative three Tesla negative four all right now let's just go ahead and print um cars and run this and you can see that this Dell keyword did indeed remove the um what do we want to remove the four okay great so what I'm going to do is I'm going to comment these out just so we know which lists we're working with I don't want any of the elements to change I just want to see them up here all right and let's just go ahead and start working with some of the um the methods we'll try to get through as many as possible so the first thing I want to show you is how to add to a list okay and we typically use a method called dot append all right so I'm going to say cars.append and then I'm going to pass an argument in and it's going to be a string and what do I want to pass in let's go ahead and pass in a let's pass in a Lambo okay now if I was to print cards and run this you can see at the end there's the lambo so when we use the dot append method we just have to be aware that it does add to the end of the list now there is a method to add an element at whatever index you want and that's called insert however I don't use it that much I typically just use a pen so we would say cars.insert and then I want to put the index number where I want to insert a car so let's just say at the two index comma space for our second argument and at the two index I want to insert a Bugatti okay so now if I was to print cars you can see at the second index we have a Bugatti the Bugatti and everything shifts to the right all right so let's go ahead and move on let's talk about removing from a list again we can always use the Dell keyword up here but I actually like to use the method when I'm removing so what I would do is say cars.remove and then you pass in whichever car you want to remove and let's just go ahead and remove the Ford again all right and let's go ahead and print cars okay and you can see that the dot remove method also removes just like the Dell keyword does now it's important to realize that the Dell keyword or just using delete and removed this takes the value out of the list and it's gone forever all right totally gone you can never use it again it's been thrown away but there is another method that we can use that doesn't throw the value away it simply pops it out of the list gives it to us so we can we can have it it Returns the value to us and then we can do whatever we want with it now a good example might be something like this what I want to remove the Toyota and throw it away because it didn't pass emissions no what I could do is pop it out and I can have this separate list okay called failed emissions and what I'll do is pop the Toyota out and I'll put it in fail the missions until it gets fixed okay and then I could put it back into the cars list and that might be like a kind of a I guess somewhat of an example of using the dot pot method I'm not going to throw the Toyota away just because it failed emissions I want it I want it to be fixed so let's go ahead and do that so I'm going to say cars.pop okay but the dot pop does not support actually that passing the name of the element you have to pass the index number so I want to remove the Toyota and we added a Lambo and a Bugatti we removed a Ford so um if we look down here okay yeah this is our latest list okay and it's going to be at the third index so I'm just going to pass a 3 here and now that I've done this I can actually just store this in a variable because it gets returned to us so I'm going to say popped equals cars.pop3 and then how do I add to a list well I want to put it in the failed emissions list and it's highly unlikely that the Toyota would fail emissions because we know they're very reliable and also it's unlikely that the Tesla would fail emissions too so it'd probably be like the Ford of the Chevy or actually the lambo or the Bugatti because I don't know how reliable they are anyways let's go ahead and put that in a list again we use the dot append method you can use dot insert but I wouldn't so we'll do failed emissions let's say failed emissions dot append and then we'll just put popped in there and now let's print both lists okay so we'll print um cars and then print popped let's go ahead and run this and you can see it's been removed from the cars list and now it's just inside of the popped list all right so oh sorry I didn't mean to print popped I wanted to print failed emissions and you can see that I didn't print the list to failed emissions I actually just printed the Toyota because there's no square brackets around it so let's go ahead and run this and you can see we have the Toyota okay all right so that's the dot pop method in the dot remove method let's go ahead and look at some other methods one thing I actually like to do is use pie charm to my advantage I mean it is your IDE you should be using it to its fullest potential or as much as much as you can alright to help assist you in developing software so what I'd like to do is type the name of the list and use the dot operator and then I just like to look at the methods hey is there something in here that I want to use let's go ahead and do a um let's do a sort okay now what is this method do this method permanently sorts the list okay so it'll be in like alphabetical order and I believe numbers come first but we don't have to worry about that in this list it's just all lowercase again I like to use lowercase because it keeps things uniform all right I know that I'm not looking for uppercases sometimes in lower cases other times and there's ways around that but for me it's just easier to just use lowercase when I want to use uppercase I use methods to beautify it all right so let's go ahead and print cars now that it's been sorted and we can see up here that's the last instance of the list before we sorted it and then if you look down here it's been sorted so the Bugattis first now okay so that's the dot sort method but perhaps you don't want to permanently sort it one thing you can do is you could just like print sorted and then pass it in here okay this is a non-permanent way of doing it temporary it's a temporary sort all right I'm going to actually get rid of that though because it's already sorted for us and let's go ahead and continue on so I'm going to do a cars Dot and let's go ahead and talk about extend okay I find this method a little bit useless I mean I'm not saying it is useless but I just I don't use it that often and what extend allows us to do is add multiple items how do we do that we just pass another list here okay so what we would do is just add let's say let's add a bunch of vehicles let's add two two not a bunch um I don't know Pagani I'm just adding a bunch of like cars that I've seen on Forza um let's go ahead and add a Corvette okay I don't even know if that's how you spell Pagani well we're gonna go with it okay I'm actually now that I'm using this I'm seeing um an instance where this would actually be more useful but let's go ahead and print cars so let's run it and you can see what does the dot extend method do it also adds to the end but again it just takes multiple elements I I think you can actually you can put as many as you want in here but typically I don't I guess maybe the dot extend method might be a little bit easier now that I'm looking at it but typically what I'll do is I'll just reassign so I'd be like cars equals Cars Plus and then you can actually add this list you could have just taken this list right here and you could add it right here all right and that will do the same thing but I don't know maybe the dot extend method's a little bit better so let's just go ahead and delete that and why don't we continue on so we'll do a cars Dot and what I want to do is I want to go over clear clears Actually I don't even need to show you because what it'll do is it'll just clear our entire list but the list will remain okay so cars will still exist it'll still have a memory location it just won't have any elements in it so I don't really need to show you that because once the list is cleared we won't be able to use the other methods now what I do want to show you is um let's go ahead and do count and then we'll do copy so cars.count this does the same thing as the string method that we worked with earlier and what you can do is just pass in a variable I'm not a variable you can pass in a value and why don't we go ahead and pass in I don't know Corvette so we'll say Corvette and let's go ahead and print this let's run it and you can see it's it's returning one that's because there's one Corvette in this list it does the same thing like I said as the string method it's just gonna count it's just gonna count the occurrences that's all all right let's go ahead and discuss the copy method but before I do what I want to do is create another list okay I'm going to say cars V2 equals cars so now what I'm saying is this list okay or which whichever one it is I think it's I think it's this list either way um what I'm doing is I'm actually referencing the first list okay I'm not creating a copy you might think that this is creating a copy so let's go ahead and print just so I can show you I can print the ID of cars and then I can do it with cars V2 let's run this you can see they're both sharing the same memory location so you may think that you have created a copy and you might start altering this list you might say okay well I don't want to do anything to cars but I have this cars V2 copy and I'm going to add a bunch of cars in there and I don't want anything to happen to the original list cars but because this is a reference you you you're sharing the same memory location anything you do to cars V2 will be done to cars so what how can you avoid that well you can make a copy I believe there's like a little operator to do it as well but I I can't remember what it what it is so what I would do is just say um cars V2 equals cars.copy okay let's run this and now you can see that the memory location is different so now you actually have a genuine copy that will not affect the original list of cars okay so let me just look through to see if there's any methods left so I'm going to put a cars Dot and if there's nothing what we'll do is we'll just go ahead and uh and the and the lesson here okay it looks like there's nothing really left for me to go over so uh great job and I will see you in the next lesson hello everybody I hope you're ready for the next lesson because we are going to go over for Loops okay so as usual we'll come up we'll right click create our file and name it four loops okay so what is a for Loop well it's it allows us to iterate over a sequence of of elements okay what is a sequence of elements well generally it's it's a list okay but it could be um any other iterable object now how do you know if an object is iterable well let's start off by creating a list we'll call this fruits okay we use our square brackets and then I'm going to put Apple I'm going to put um Peach I'm gonna put lime and what else do we want maybe like lemon yeah we'll do lemon okay so we have this list here how can we see if this is an irritable object well generally that just comes from experience you're just gonna know what you can and cannot for Loop through but another way to tell is to use the dur function okay so this is short for directory and then we just pass in the object okay so let's go ahead and run this and if we look through this directory we're going to see um where is it iter okay so we have the inner method here this means that's an interval object and therefore we can for Loop through it okay so let's go ahead and delete this line three we don't need it okay I'm just going to clear this really quick by running again and now let's start off by writing a for Loop so what we do is we use the the keyword for okay and then we name our list our Loop variable okay so I'm going to name this item this is actually pretty common okay and then we use the in keyword and then we put our iterable object here again a list isn't the only iterable object in Python but for now for this lesson let's just pretend it is okay so for item in Fruit we have our Loop variable and this is kind of like a shape shifter okay this is going to become each element for each Loop and then what are you going to do with this Loop variable well sometimes you don't do anything with it but we're going to in this case so let's just go ahead and print item let's run this and we can see apple Peach lime lemon so what's happening here this variable is shape shifting into the apple and then it's printing it and then the loop runs again okay so for the second Loop item shape shifts into Peach and then we print Peach and then we for loop again for the third time item becomes lime we print lime and then again for the fourth time item becomes lemon we print uh lemon and then the we've reached the end of this sequence so we're no longer going to for Loop the for Loop will break okay so we can do a lot of stuff for the for loop I typically like to use for Loops okay to analyze elements in a list but that's not the only way you can use a for Loop in fact you can use for Loops to just generate Co or Loop code for a specified amount of times it doesn't have to be for iterating through you know a sequence okay so let's go ahead and create a condition here okay I'm going to create a condition that says if item equals equals Apple then what do I want to do I'm just going to do something simple I'm going to print I found I found the Apple okay and then I actually can put an else here I'll put an else and let's print I am not an apple okay let's go ahead and run this okay so you can see that the very first line here is I found the Apple all right and that's because apple is the first element so item became Apple first and then everything else of course is not an apple now what if I was to add another Apple at the end let's run this and you can see that the first element is an apple and so isn't the last element now I quickly want to discuss the two equal signs here okay two equal signs is an evaluation one equal sign is an assignment operator you have to understand the difference between the two okay and you know even I still make this mistake sometimes I just put one equal sign just on accident and I don't do it often but it does happen again this is an assignment operator it's how we would store data into a variable whereas this is going to evaluate okay does item actually equal Apple well then if so perform this logic okay so that's a for Loop now this is a traditional for Loop and again there's no there's no um limit on how complex you can make this for loop I mean you know you should try to not make anything complex when it comes to coding because um you know it's coding's already hard enough I know we're working with python and it's a little bit easier to write than other languages but coding is still hard okay so you should always try to reduce complexity if you can but there is no limit to how complex your for Loop can get you can have nested conditions and obviously if this if condition is inside of the for loop it's a nested if condition okay now let's go ahead and enter down and let's do a traditional for Loop okay uh sorry this is this was this is what I would consider a traditional for Loop this is a range for Loop what we're going to do right now so I'm just going to say for I and range okay and now we're using the range function and range will generate I believe a tuple or a list of numbers now we'll get into tuples later but they're very easy same thing as a list um anyways this is going to create some numbers okay and I'm going to specify like 90 here okay so as you can see I am not looping through a sequence all right I'm not iterating through a sequence so you don't have to iterate through a sequence for um to use a for Loop that's not the only thing you use a for Loop for now I is actually going to become um 0 through 89 okay this is non-inclusive so if I wanted to add or include 90 I would put give me 1 through 90 plus one okay and now I'm going to Loop 1 through 90. so let's just go ahead and print I let's run this and you can see I do get one through ninety okay and it's also still running this for Loop but if I was to start here go all the way down yes we do get one through ninety now again I'm not looping through a sequence here so there's a lot of stuff that I could do perhaps I wanted to I knew that there were 90 people coming to my party okay I'm throwing a Halloween party or something and every time somebody comes in I want them to be able to input their name into my program so I can save it to a you know a text file or something well what I could do is I could set up a specified number of Loops okay this program will run 90 times for all 90 people okay I don't even have to use I all right so again this is a traditional for Loop and this is like a range for Loop now what's interesting about using the range for Loop is that we can use this I as an index number so how could we do that well let's go ahead and delete print delete this and what I want to say is I want you to Loop the length of fruits okay so this is going to Loop um five times all right and it's going to give us 0 through 4. well that just so happens to be the index number of each one of these elements so what I can do now is I can kind of use this Range Loop in the same way this traditional Loop works I can say print and then I use that manual selection that we've gone we had gone over in list I put square brackets and then I can put I in here right because I is also like a shape shifter it's going to become zero then it's going to become one then it's going to become two three four and so I'm essentially printing fruits zero for the first Loop run the loop again print fruits one for the second Loop Etc okay so let's go ahead and run this and you can see I get apple Peach lime lemon Apple all right so what's useful about this is that this allows us to manually update like you there is no method to update a value inside of a list so if you need to go through and update each value you'd probably want to use a Range Loop now believe it or not there's a better way to do this it's called enumerate we'll go over that later I typically avoid using range Loops I don't fully avoid them I still use them but enumerates a little better we'll go over that after so I'm going to um go through each element and I'm going to add an S at the end okay so now we have instead of singular fruits we're going to have plural fruits so what I'm going to say is um fruits I put an i here now equals fruits I plus an s okay and what I'll do is I'll print fruits run it and look what we have now so this sometimes can be useful more useful than the traditional for Loop however there's another for Loop that's even better okay and it's just by using enumerate so let's go ahead and do that what it does is kind of combines these two the functionality of both of these Loops together so what I'm going to do is I'm going to say for uh I think it's index so we'll say for index comma value and enumerate now if you look here this has a c okay that means this is a Constructor and what it's going to do is it's going to take our list up here and it's going to construct it into an enumerate object okay so what I'll do is put a numerate here put the parentheses and now I'll pass in the um list and now if I was to just print index comma value and run this you can see that this is very very useful okay because we're getting the index and the value we know that Apple lives at the zero index Peach well actually this actually should be peaches but um Peach s lives at the one index limes lives at the two index Etc okay so this could be very useful I highly suggest uh instead of just going right for a range for Loop you just use enumerate okay now let's do the same thing we did on line 11. okay what we'll do is we'll say fruits index equals value plus s okay a little bit easier because I don't have to um specify fruits I again I don't have to rewrite this this line of code I can just write value and actually let's put a s s s s something like that so we can see that it did indeed work and let's go ahead and print fruits let's run this and you can see this works too and again this is much more useful we don't I don't want to get too complex in this lesson but um if there's a there's a problem where you're like oh I need the functionality of the traditional for Loop but I also need index numbers then just go with an enumerate for Loop okay so that'll wrap up for okay they're very they're dimple we don't want to overthink these okay we don't want to be intimidated by these they're very simple of course they can get complex but we're not going to do that in this lesson okay so great job and I will see you in the next lesson hello everybody I hope you're ready for the next lesson because we are going to be going over dictionaries okay so as usual we'll go ahead and create a file okay and we'll call this dictionaries okay perfect now what is a dictionary well it's a sequence okay it's sort of like a list but lists have an index and a value whereas dictionaries have a key and a value and we get to Define that key so it can allow for very fast lookups okay very fast access I love to use dictionaries so how do we write them well we use curly brackets and let's go ahead and use the cars example again we'll put curly brackets here and now we have a dictionary well actually we have a set right now but don't worry about that it's a dictionary once we start writing key value pairs so let's go ahead and put a key value pair let's do a Toyota and then we put a colon here and we'll do Tacoma okay that is our first key value pair all right and now we'll put a comma we'll put a space and let's add another key value pair so let's just do a Honda colon Civic okay and then lastly we'll just go ahead and add a Ford F-150 okay all right so we have our dictionary here how do we add to the dictionary well first off we need to understand that just like lists dictionaries are mutable that means we can change it indirectly we can manipulate it indirectly I don't have to come up here and add another key value pair I can do it from outside of the dictionary so to do that what we do is we name the dictionary or just write the name of the dictionary use our index operator but in this case we're not accessing indexes we're just adding a key so we'll say we have a four let's just go ahead and add a Tesla and then we put an equal sign and then we put the value okay and what I'll do is just add a plaid okay I think that's their fastest car I'm not sure anyways this will actually add to the end of a dictionary okay so there's no dot append method for dictionaries let's go ahead and run this and remember to right click and you can see that it adds to the end now what if I tried to add another Tesla well we can't do that because you can't have duplicate keys so I'm going to demonstrate that right now we'll say Tesla equals and let's just do a p90d I think okay now if I was to print this list I mean this dictionary sorry and run it you can see it didn't add another Tesla it's simply overwrote the value okay so again we cannot have duplicate Keys all right now let's go over some of the common methods as well all right let's enter down a couple lines now one thing I do want to point out first is that this is underlined with yellow squigglies I think it's because it wants me to use a Constructor but either way I'm just going to show you how uh how we can create a dictionary another way using a Constructor so what I would say is something like this Cars 2 equals and I would say dict okay then we put our parentheses and then we can just add keyword pairs right here so I would just it looks a little bit different but I would say something like this um Dodge equals Ram comma let's do Chevy equals Corvette and we'll just add one more we'll say um what do we want to do Kia equals Sorrento I think it's a Sorrento anyways this is also another way to create a dictionary okay but I typically use curly brackets all right so if I was to print cars too and run this you're going to see it does the same thing okay we have a dictionary with key value pairs okay let's move on and let's get into some of the methods and what we'll do is we'll just use methods on our top dictionary here so let's go ahead and say cars use the dot operator as usual and let's go ahead and do items first it's right here why not now what cars.items does is returns us a list of all of the items or the key value pairs so this would be an item this would be an item and this would be an item and it's going to be in a list so we can print this run it and there you see we have the list with the square brackets and what's inside well inside are these tuples with a key value pair inside of it what is a tuple a tuple is a list it's the same thing but they're they're immutable they cannot be altered okay and that makes them pretty useful for protecting data and they're a little bit faster too okay so let's go ahead and move on that is the dot items but what if I only wanted say all of the keys I could say cars.keys okay let's run this and you can see I get a list of just the keys and then if I want to adjust the values I could say print to cars.values okay and here we go we have a list right here of all of the values all right let's move on to some other methods let's um let's go ahead and say cars dot let me remove this bookmark cars Dot and why don't we do pop well dictionaries they don't have a they don't have a DOT remove method so there's two ways you can delete from a dictionary one is the cars.pop method okay so what I would do is just pass in the key here and let's just pass in Honda and if we print cars and run this you can see the Honda's been removed another way to do it I believe we can use the Dell keyword and what I would say is cars square brackets and let's just go ahead and pass in Toyota then let's print cars run this and you can see that this method works as well so you can use dot pop and perhaps you're going to want to use it because you want to save the value remember dot pop doesn't throw the value away it removes it from the the data structure and then it returns that to you okay so you may want to use it but again it only Returns the value so you're popping Honda out but it's actually going to return um the Honda the Civic okay now another thing we can do is I actually want to comment these out just so we can keep working with this original list okay so I keep calling it a list it's a dictionary I apologize now another thing we can do is cars. pop item what does this do well it requires no arguments and all it does is pop out the last item or the last key value pair and it returns both of them to you so if I was to print this run it you can see it popped out the Tesla p90d okay so again it just pops out the last item so that's a key value pair it doesn't just pop out the value all right let's go ahead and move on let's do a cars.update okay and what update allows us to do is add to the end of the dictionary now I'm going to show you this I'm going to show you another approach after let's go ahead and add by doing something um what do we have in here let's go ahead and add because this is in another dictionary we can add some of these so I would say Dodge Ram okay put a comma and then then we'll just do Chevy Corvette Chevy Corvette okay let's go and print cars now let's run this and you can see that the dot update method adds to the end okay it adds to the end of the dictionary there is another way to do it using the pipe symbol where you might have two dictionaries so for example I have a dictionary right here let's actually cut that out let's comment this out and let's enter down and I'm going to say cars equals cars pipe symbol and then this dictionary okay this will do the same thing so if I run this you can see it did the same thing all right okay let's go ahead and move on so let's do a cars Dot and what have we not done yet um clear is obviously self-explanatory I don't want to go over clear with you because it'll clear the dictionary and then we can't work with it copy well it's the same thing that I talked about in our list lesson okay you don't want to just reassign okay or create a copy by using the assignment operator because what that does is create a reference they share the same memory location and anything you do to your copy or the copy that you think you have is still going to directly alter the original list so again you would use the copy method to create another memory location and now you actually have a real copy of it that you can manipulate that will not affect the original dictionary all right so we're not going to touch copy right now and we're not going to touch clear and it looks like um we can just go ahead and move on I want to teach you about for loops with dictionaries okay we still use the same syntax so we still use the four keyword okay we still name a loop variable and then what we would do is use the in keyword and then just name our dictionary okay our dictionary is called cars let's put a colon let's tab over and let's just go ahead and print item and let's run this okay and you can see that it's only printing the keys so this isn't necessarily ideal a lot of times when you're looping through a dictionary you kind of want to access both the key and the value all right so what I recommend doing is just automatically defaulting to this type of for Loop where I say key value so now we have two Loop variables and we would say cars dot items okay and then we can just go ahead and print the key and value here let's go ahead and run this and you can see now I'm getting both the key and the value now perhaps you just wanted the value we know that key is default so we get all the keys by default but if you want all of the values well you can what you can do is say for Value in cars dot uh let's do dot values and then we could just print the value okay so let's go ahead and run this and you can see now we just get all the values all right so for looping is generally the same with dictionaries it's just a little bit different depending on how you want to work with it if you just want the values you got to use the cars.values method if you want both the key and value cars.items and then by default you can just Loop through the keys okay so that is dictionaries and that will wrap up this lesson and I will see you in the next hello everybody I hope you're ready for the next lesson because we will be going over sets and tuples okay so as usual we'll come up we'll right click and we'll create our file we can call this sets and tuples okay now let's just quickly go over a tuple because there's not a big difference between uh tuples and lists tuples are just um they're they're immutable lists okay so we can't change the data inside unless we do it directly now this could be good for protecting data for example days of the week okay those aren't going to change so we might want to put those inside of a tuple so why don't we go ahead and do that we'll just call it uh days and then what we do is we use round brackets here okay and then we can just put like uh Monday make sure we'll do mom we'll just abbreviate so Monday Tuesday let's do Wednesday let's do Thursday let's do Saturday and then we'll put in Sunday okay so if I was to hit days not hit but type days and then hit the dot operator you can see we we have no methods here the only thing that we can do is count the amount of occurrences of a particular element or get the index of that element okay we can also for Loop through it as well so if you need to for Loop through it analyze each element and then do something with them you can do that but again it's it's immutable so you would have to like take those elements out and put them in a new list and uh whatever you do with those elements you can pass those changes into the new list okay so let's go over sets now all right I'm going to create a set called nums actually I'm going to create two sets so I'm going to put a 1990 these are just going to be like numbers I probably should have named this dates but no big deal we'll do 1990 again let's do 1990 again um let's do 2005 2001 2010 um 2019 29 let's do 2019 again and why don't we do um 2003. okay so I have all of these dates inside of a set and actually I'm going to create another set I'm going to call it nums V2 short for nums version two and let's go ahead and put a 1990 in it then let's put a 2000 let's do a 2001 actually I've already have a 2001 in the up in the the set above so we'll do a 2002 2003 2004. 2006 2007. okay now these are unordered and they do not allow duplicates so if I was just to print nums we know that we have some duplicates in here for example we have three 1990s and we have um two 2019s so let's just run this and you can see it got rid of all the duplicates but it's also unordered all right so every time you run this it could stay in the same order or it could change order it's it's unordered and you can see because we started with a 1990 but if you look down here it starts with a 2001. now let's go over some of the methods okay so I'm going to um type nums I'm going to hit the dot operator and then let's go ahead and check out add okay this allows us to add an element to the list of actually the set it's just like a list it's just like the DOTA pen method and I'm going to add another date so let's just do like 19.95 okay and let's go ahead and print um nums let's run it and you can see 1995 has been added all right now let's go ahead and do a nums Dot and I think I want to do um intersection okay now what this should do let's hover over this I believe it gives us all of the same elements okay return the intersection of two sets as a new set so I want the intersection of nums and nums V2 and actually we don't want that capitalized so I think it's control shift U okay perfect now let's go ahead and print why is my caps lock on print let's print that method actually so go ahead and print it okay now you can see if you ignore this this gives us a new set right and what it does is give us it gives us numbers that are in both sets Okay and it returns it as a new set so that's how the intersection method works now let's go ahead and do let's print a nums dot let's do difference okay and you can probably guess what this is going to do let's run it and it gives us all of the numbers that aren't the same all right now let's go ahead and do another method let's go ahead and print the nums Dot and why don't we do a remove okay now you probably already know what copy is it's the same thing pop is also the same thing but I want to show you remove okay because there's something with remove that I want you to know so if you can't find the number you'll end up getting an error so let's just put a 20 20. okay and let's run this and you can see we actually get up we get an error so if you don't know whether something exists or not inside of a like an element exists inside of the set what you can do is use the discard method so we'll say nums.discard and then let's put a 2020. and you can see it returns none all right so that's why I wanted to show you remove a lot of times it's actually better to just use discard just because you know it's a little bit safer okay now let's go ahead and check out some of the some more methods um let's not print anything just yet let's say nums dot uh one thing we can do is Union okay and what this will do is just actually combine the two uh sets together so I could say um nums V2 in here and now if I was to print this run it you can see it combines everything okay all right let's move on let's print a nums dot clear you probably know what what that does um and it looks like that's really the only thing that we have to go over so that's again they're super useful I mainly use sets because they just don't allow duplicates and it allows me to reduce the amount of data or reduce the amount of redundant data that I'm actually trying to manipulate so I can convert something to a set to get rid of all the duplicates before I continue on with my script and my program or whatever it is I'm trying to achieve with my data okay so that is the end of the lesson and I will see you in the next one hello everybody I hope you're ready for the next lesson because we are going to be going over functions and as usual we'll right click up at the top directory we'll create a new python file we can call this functions okay so what is a function well function's simply like a block of code an encapsulated block of code that runs when you call it so it's kind of like a tool all right now there's a number of built-in functions in Python that we've been using for the entire course okay and one of those obviously is print okay it only prints or there only displays when we call that function and generally we'll pass an argument in to tell python what to display okay so functions are really really useful for reusability okay we can reuse functions over and over again if you have a bunch of functions that you love to use and they're all kind of similar you can just store all of those functions in a file and you could import it and then start using those functions in your code so they they also make our code more modular all right so how do we write a function well simple we use the def keyword okay this is short for Define now in some parts of the world I've actually called heard people call functions definitions but that's not the correct terminology these are functions all right we're going to start off with something very simple let's create a function called greeting okay now we use the def keyword then we name our function and then every function must have parentheses after the name okay and what do we pass into these parentheses well generally we'll pass parameters in here but we're going to leave this one empty so let's go ahead and put our colon and now we enter down and anything that's tabbed over of course is going to be a part of this function so let's just create a greeting okay let's just print hello okay okay so we have written our first function this is not going to run unless we call it all right so we have to call it and the way we do that is by typing the name of the function followed by the parentheses these parentheses are the call if you don't put these parentheses you're not calling the function it is not going to work in fact you're probably if I was to print this it's only going to show me the memory location all right so let's go ahead and run this remember we right click run and there we have it okay so we can just use this function whenever we want it's reusable okay I could do this four times let's go ahead and run this again and you can see we get hello four times now this function isn't very useful okay and there's a number of reasons why but to better illustrate that I think we should create another function that is more useful so let's delete these calls okay we don't care about this function right now and let's create another function called add so we'll call at we'll call it add and this function is going to take two parameters this allows the user to put input into their function okay to pass input into their functions and we can name our parameters whatever we want but I'm going to name them num1 and let's say num2 okay then our colon and then what do we want to do with these parameters okay so what we want to do is let's start off just by printing them so print num1 plus num2 and let's just go ahead and run this we'll call the function so we'll say add and let's run it now you can see it's required to it's requiring two positional arguments okay again these are parameters I want you to think of these like empty boxes or placeholders they're placeholders for your arguments we pass arguments into the function call so what do I want to put inside of this empty box which we called num1 I want to put a 98 in here then I'm going to put a comma and I have to pass in another number for num2 okay so let's just go ahead and pass in 23 and let's run this okay now you can see we get 121 but there's something that's not correct here and that's print we really shouldn't print our functions I'm not saying we can't but we generally want our functions to return value to us print has no value it does nothing it just shows us it that all it does is display it shows us something and we don't want our functions to do that we want them to return a value so that we can use it actually before I return this I'm going to show you why if I was to um store this in a variable let's just say um sum okay now some should equal 121 right well it doesn't if I come down here and I print some and I run this you're going to see it does still print the two numbers but then it tries to store it in this variable that's not possible because we're printing print has no value and then what happens is it returns none okay so that brings me to that actual word return that's what we want to use when we want our functions to give us value back so what I should do here is delete this print statement and I want to return num1 Plus num2 now this function has value and I can take it with me okay whatever this function produces has value and I can take it with me wherever I go inside of my program okay so if I was to run this again you're going to see I just simply get 121. now what I like to do is I typically like to print my functions so I wouldn't really do it like this this works too depending on your needs but I would just print add and let's just do 23 and 56 and let's run this okay and we get 79. all right so let's go ahead and create another function all right and what function do I want to create let's create an average function let's say def AVG which is short for average and this is going to take just one argument I'm going to call this argument args actually why don't we just go ahead and pass a list in instead so our parameter should be a list and we'll just say list now we could do that uh that's that's a reserved keyword we really shouldn't use the word list let's just say list of nums a little bit long but it'll work okay so we know we're going to pass a list into this function and we're going to get the average so imagine a student passes in a list of all of their grades and they want to get their average grade so what we could do is return that way it has value we'll return the sum of the list of nums and what that's going to do is it's going to calculate well it's going to add every single number inside of this list okay and then we want to divide it by the Len of nums a list of nums I should say so list of nums and now if I call this function and by the way the Len function simply Returns the length so if you give me five grades well this is going to produce five and then of course this is going to add all of those grades together so for example we might have 500 divided by five and that would give us the average okay now let's go ahead and call this function by writing its name and then this one requires a list okay so let's go ahead and pass a list in here with square brackets and let's just put a 90 a 98 a 79 and an 87 okay and let's run this now I'm still getting 79 which is 9 line 9 here that it's producing 79. let's comment that out let's run it again and you can see I'm not producing anything again that's because we're returning this does have value but we're not printing it okay so let's just go ahead and print it so we can see okay let's go ahead and run that and you can see we get 88.5 okay now another way to do this is actually by using args okay so what I'm going to do is I'm going to put args here actually it's an asterisk and then args okay and now I would want the sum of args divided by the length of args okay this does the same thing but it basically takes every argument and turns it into a tuple and it's just a little bit faster because now I don't have to pass in these square brackets I can just pass in as many arguments as I want or an arbitrary amount so let's go ahead and run that and it still works the same okay now let's go ahead and talk about um default arguments and positional arguments I'm going to create a function that um what do we want to do here that displays car we'll call this def display car yeah we'll call it display car all right now this function is going to take a make comma model all right now I would consider these to be positional arguments okay and you're going to see why I'm going to actually print this function because like I said you don't have to return every single function so I'm going to print this and I'm going to write an F string I'm going to print an F string that says I drive a curly brackets make curly brackets model okay now let's go ahead and call this function let's comment the call on line 16 out and let's pass in a make and a model okay so let's have them make be a Toyota in the model should be a Tacoma all right let's go ahead and run this and you can see this works appropriately now I would consider the to these to be positional arguments because you really don't want to put the model in first okay if I was to um just reverse these so let's cut this out Ctrl X paste it comma space remove that and run this you can see this just doesn't make as much sense I mean I'm sure you could figure it out but I drive a Tacoma Toyota is just a little bit weird so positional arguments okay they have to go in the right order all right not always for example um these wouldn't really require any position because you're just passing an arbitrary amount in but these are not positional arguments all right it doesn't matter if I put 56 plus 23 it's the same thing as 23 plus 56. not positional okay now let's talk about some default arguments too all right let's go ahead and put a comma here and let's go ahead and add Year all right and I want to make this default okay so I'm just going to put a 2003 okay so now I'm going to say I drive a year make and model now this means that I should pass in two three arguments okay but because this is a default I don't necessarily have to so let's go ahead and run this and you can see it defaults to 2003 if I don't put the year in myself now if I put in a year let's just say 2019 and run this you can see the argument that I pass in overwrites the default argument so this is just a default okay now you could make it optional too okay whereas like there's no default value you either put in a year and it gives you this sentence that displays the year as well or you just don't put in a year and it doesn't display it so let's just go ahead and do something like that I'm going to say year equals and why don't we do something like this I like doing this you can also you can also use none but I'm just going to put an empty string here okay so I drive a um year make or model let's first off let's just go ahead and run this so you can see it looks fine but what if I get rid of 2019 and run this again you can see it's somewhat optional but we do have a bit of a problem here okay we have two spaces this doesn't really look right so what we could do is create a condition inside of here I could say if year what does that mean it means if this is not empty remember if something's empty it's false okay if it's not empty it's true therefore if I pass in a value this is no longer empty and this becomes true all right so then I would print this else I would print um let's print an F string and I'll say I'll copy this and let's delete this let's run it and you can see I drive a Tacoma Toyota again we swap these around so these should be positional let's go ahead and fix that let's run it okay I drive a Toyota Tacoma so what's happening this is no longer empty this becomes true this print statement executes otherwise year is false and this print statement will execute and that takes care of that space for us so let's put in a comma and put in a year and just see how it looks let's just put 2019 back in and run it and it looks good now we have to write some extra code because we're not using return when you return the function ends let me give you an example of that we would no longer need the else statement so what I want to do is delete the print here let's just go ahead and return get rid of this and now I no longer need the else statement because if you're this is going to return this otherwise it'll just simply return what else I put here so let's enter down and just say return here and now I don't need the else all right in fact I have to bring this up all right let's run it and you can see because we're using return we're not getting anything displayed so we print the call run it and now it's working perfectly if I get rid of this 2019 run it again working perfectly so notice how return saved me one little line of code all right now those are optional and default arguments let's quickly talk about type annotations okay so let's go ahead and enter down a little bit now these are called professional functions when we use annotations type annotations people also call them type hints this is called a professional function because it gives our user more information about what our function does in fact let's not even create another function let's just do it right here to use type annotations what we do is we put a colon and then we put the the data type okay we need to tell the user hey we want a specific data type when you give me the bank when you give me the make don't pass a list in don't pass a dictionary in pass in a string okay all right and I think we put a space here and we'll do the same thing with model we want a string for the model so let's just go ahead and put Str all right and now we don't have to use a type annotation on this one because it's a default argument okay so we just don't have to do it now we should also tell the user what this is going to return so we do that by putting an arrow here and it's going to return a string okay and we also want to put a doc string okay so to do that what we can do is we do three double quotes so I'm going to do shift double quote double quote double quote and then I'm going to hit enter and there you can see I think pie charm is doing this for me and what I want to do is just give the user a little bit more information this is a professional function again this is called the doc string and these are type annotations these are not mandatory they're just hints it's telling the user hey what like this is what you should enter all right now let's go ahead and just write something up for the make okay we'll say um takes a string of the VA let's say vehicle make okay I'll copy this takes a string of the vehicle model the year we could say actually because our arguments will overwrite the year you could pass in an integer so I'm just going to say takes an integer of vehicle year okay and what's it going to return it's going to return a string of vehicle info now I'm getting this little red squiggly it's bugging me so let's just try running this and see if that goes away okay it does I don't know why that's there seems a little bit weird if I hover over it a loose punctuation mark okay well we're not going to get too deep into that let's just go ahead and ignore it because it's it's working all right so now if I was to call this function I'm actually going to delete this and I'm going to write the name of the function okay and now you can see I have a little bit of information okay this box pops up I have some information and if I need more information I can hover over the blinking cursor and we can see down here there's a little bit more information all right you can also write something up you know about you know what this function does okay and this is a professional function you don't have to use them you should use them and it just gives more information on how the function works now one last thing I want to discuss with you is if you're using optional or default arguments they have to be all the way to the right okay so if I take this out and I put here put it here you can see I get these red squigglies you they always have to come last okay a non-default argument can never come after a default argument so let's just go ahead and undo that and that will wrap up our lesson on functions again just go over these if you want to just rewind this lesson back because functions are really important okay so great job and I will see you in the next lesson hello everybody I hope you're ready for the next lesson because we are going to go over lambdas so as usual we will right click create a new file and we're going to call this Lambda okay all right so what is a Lambda it's it's simply an anonymous kind of throwaway function kind of like a one-liner function and to better explain obviously I'm going to create a regular function we'll call this add and my parameters will be X and Y and I'm going to Simply return X Plus y okay now how can I do this on one line well I could use a Lambda now lambdas are super useful especially when we use them in higher order functions and we'll go over that in just a bit but let's create our first Lambda and let's say um we want so we use the keyword Lambda and we want to have the same parameters so we'll say x comma Y and then we put a colon and then we just write our expression so basically we want to return X Plus y noticing I'm not using the return keyword lambdas automatically return the expression to the right of the colon and again to the left of the colon those are your parameters now I could store this in a variable okay so I could say add to equals this Lambda but typically we just use lambdas and again higher order functions where we use it and that's it all right so I wouldn't really store this in a variable other than otherwise I would just create a regular function now let's go ahead and call add to just to see if it's working so we'll say add to okay and then we'll pass in 45 67 and let's go ahead and print this all right let's run it and you can see this is working perfectly okay it's just adding the two numbers so Lambda functions work the same way as a regular function except we just it's like a one-liner and a lot of times they're a little bit more clear and concise depending on what we're trying to do all right so let's talk about these higher order functions where lambdas are really useful okay the two ones that are the big ones are map filter and there's also reduce but we're going to go over map and filter all right so let's go ahead and create one and before I do that I want to create a list and I want to comment these out too so I'm going to create a list so I'm going to call it nums equals square brackets 12 34 56 67 34 again and 21. all right and now say I wanted to perform a function on each element well I could use this higher order function or the map function all right so let's say map and then I want to define the Lambda first okay that's going to be my first argument again a higher order function is a function that takes another function in as an argument or returns a function so let's pass in a Lambda and we only really need one parameter okay this is going to represent the element in the list okay so X is going to represent each element every time we iterate we put our colon and what do we want to return I want to return X say to the power of 2 all right and let's go ahead and put a comma and then we pass in our second argument which is going to be our iterable object okay so nums all right now if I was to run this actually let's go ahead and print it oops you know what I did there okay you can see after we print it we just get this map object okay what we should do is typically we'll just convert it to a list so we can see it right so let's go ahead and convert this to a list and now let's run it and you can see that we get every single element in here basically it's squared okay so they're all X it's all exponential math now this is super useful but remember Map works on every single element in the list perhaps you wanted to filter out some elements well that's where filter would come in handy and let's go ahead and create another list called products set it equal to a list and this will be a list of dictionaries okay and the dictionary is going to represent a product so I'm going to say name of the product will be an apple let's do a price and we'll set the apples at say a hundred okay now let's go ahead and put our next product in curly brackets again we'll do name we'll do pair we'll do price at why don't we do 150 for this okay and then we'll put one more product in let's do name again of course we'll do kiwi and the price will be let's do 190. okay so I have a list you can see with the square brackets what's inside of the list a bunch of dictionaries or actually three to be exact each dictionary represents a product okay with its name and its price and I want to filter out every single product um I actually just want to filter products so that it's showing me products that are greater than 100 have a price of of you know greater than 100. so let's go ahead and do that using the filter function Remember map function works on every single element filter filters out elements um whether it's based off whether it's true or false and you'll see how that works right now so let's go ahead and print a list filter and then we'll pass our Lambda in now there's nothing stopping you from passing in a function here as well I mean a Lambda is a function but you could create a regular function up here name it and then pass that name into here but again this is just a little bit more clear and concise okay and honestly you can throw the font it's basically you're throwing the function away after all right so this is going to require one parameter X okay that's going to represent each element and then what we're going to say is we want um let's let's get true if sorry um x square brackets this is how we index into a dictionary okay price is greater than 100 okay and then we put our comma in here and then what we're going to put in the iterable which is going to be products okay now you're seeing a red squiggly here why is that well it's because we have an if statement if you have an if statement and a Lambda it requires you to follow it up with an else statement so I would say else false okay so if it's true they'll store that element in this list and if it's false the element will just not be stored in the list it'll be ignored okay so let's go ahead and run this and it looks like I ran on debug and you can see it did exactly what we wanted okay if it had a price of greater than a hundred then we filtered basically we filtered those elements into the list all right now perhaps I wanted to just show the um the names of anything that had a price of over a hundred now I could actually map those as well so let's just go ahead and print list map okay and what we could do is store this why don't we store this in a variable instead of print it okay we'll say new uh products equals that okay and then I'm going to run the map on this iterable object so I'll pass in the Lambda X and I want um the only thing that I want is X square brackets name okay and now I put a comma and I'm going to do that on new products and now let's go ahead and run this and you can see now it's just giving me the names okay so these higher order functions are really cool they're really useful but believe it or not I don't use them that much because we have something called list comprehension and we're going to be going over uh over that particular concept next so that will wrap up this lesson and I will see you in the next bye hello everybody I hope you're ready for this next lesson because we're going over list comprehensions okay these are really fun so let's go ahead and get started now as usual we'll right click create a new python file and let's call this list comprehension file this comprehension and now I want to write a list comprehension but before I do that I want to write a traditional for Loop Now list comprehensions are really cool because they allow us to write a for Loop inside of a list and we can do it on one line so let's just go ahead and create a list okay we'll set this equal to um one we'll set it equal to a list and we'll put 1 23 45 67 12 34 and 89 okay now we have this list and I want to create another list but I want each element to be squared so I would iterate over this okay and then take each element and put it inside of a new list now we went over lambdas in the last lesson and we basically accomplished that goal by using the map function okay but we're going to use list comprehension instead here and I actually like these better okay I think they're just a little bit more clear and they're easier to read so let's go ahead and create another list okay so we would have to create nums V2 set it equal to an empty list but we're going to populate it here on line five and six so we write our for Loop we want to iterate through nums so we would say for I n nums okay now what do we want to do with I we want to square it and then we want to put it inside of this empty list so we would say nums V2 dot append and then we put the number in so we would say I to the power of 2. okay now if we were to print this list run it let's right click and run you can see this works perfectly okay now how about we put a condition all right so I'm going to say if I is greater than let's say 23 or let's do 34. then I want you to square it okay otherwise I want nums V2 dot append and we'll do it to the power of three so we'll say I to the power of 3. now let's go ahead and run this actually let's do this to the power of four just so we can really see it's a little bit more clear and you can see wherever we did something to the power of four we have these astronomically large numbers okay so basically we have a for Loop okay and this took you know um four lines of code so one two three four five well five lines of code if you don't include the for Loop but we have four lines of code down here underneath the for Loop now what if I could do that all on one line well I can let's go ahead and comment this out let's go inside of our square brackets right here and with a list comprehension the first thing we do is tell python what we want to be in the list and again I want it to be I squared and then I write my for Loop so I would say 4 I and nums now let's just go ahead and run this and what you see is it's going to square every single number okay so this is where the conditions come in how can I write a condition well if I only have one condition I want to write that condition to the right of the for Loop okay so I would say if I greater than 34. let's go ahead and run this and you can see now it's only targeting certain numbers now what's unique about this is we're essentially filtering okay so if we look back at our Lambda we can see we filtered here okay we can accomplish the same goal most of the time with a list comprehension and it's why I use them so often okay now let's go ahead and create two conditions so if I have two conditions what I have to do is put them on the left so I would say I want I to the power of 2. if I is greater than 34 else I want I to the power of 3 for I and nums so again one condition goes on the right side of this for Loop and two conditions have to go on the left side of the for Loop that's just the syntax okay so let's go ahead and run this and you can see this is working perfectly okay um actually is it working perfectly if it's greater than oh I did this yeah but I want it to the power four okay great so let's run this and now we can see I noticed the numbers were off a little bit so sorry about that okay so we're doing everything on one line of code this is terrific and this is actually really easy to read now one thing you need to know you have to be able to identify where a list comprehension will be suitable and time there's times where you just can't use the list comprehension it's just not going to work okay so list comprehensions are great it's a great way to map and filter um but you can't do everything with a list comprehension so keep that in mind sometimes you just have to go with the traditional for Loop okay so that is list comprehension let's actually take one more look okay I want to show you how we can accomplish this goal so remember we basically filtered anything that had a price greater than 100 but then we wanted to map it just by its name how can we accomplish these two lines of code with one list comprehension well let's take our products list over so I'm going to control see this and I'm going to paste it in and now we have this products list and what I want to do is I want to create another list underneath it and we're going to use a list comprehension we'll call this let's use the same name we'll call it new products okay so new products okay now what do I want with new products I want the name so I would say I square brackets name for I in products if I square brackets price is greater than 100. look how amazing this is these are terrific I just accomplished both of these lines of code I got done on one line okay so this is fantastic this is the reason why I use list comprehension so much and they're just really easy to read again they don't they can't accomplish everything but they can do a lot for us so so let's go ahead and run this and uh why don't we print new products okay and you can see we get pear and kiwi now if I come into this file and I run this you can see down here we get pear and kiwi so again two lines of code using uh map and filter and we did it all in one line with a list comprehension okay so that will wrap up this lesson and I will see you in the next one hello everybody I hope you're ready for the next lesson because we are going to go over file handling now we're not going to go too in depth I just want to go over the basics with you because this is a beginner course but file handling is exceptionally important in all of programming because it allows us to work with external files in the data that's in external files now python is the language of data science so we we work with a lot a large amount of data okay and sometimes these data sets are huge we couldn't store them in memory and file handling in Python is really useful because we can actually break that data into smaller chunks okay now as usual what we'll do is we'll right click we'll create a file let's just call this file handling okay now there are two ways to work with files but I don't want to show you one of the ways because it's improper all right so I want to show you how to work with a file using what's called a context manager and to do that we say with okay we use the with keyword and now we use the open function all right this function is going to create a file object for us now what would we what we would want to do is name the file here and then we put a comma and we say what mode we want to open the file in so I would just put um let's put read okay now there are three modes okay three main modes one would be right and it's important that you understand these modes because if you write to a file it will completely overwrite it so you have to be very careful also if you're trying to write a file and it doesn't exist well python will see that and then it will create the file for you now we're going to read from a file that we have but just be aware that writing to a file will completely overwrite it so how could you avoid that you could append to a file so we have read write and append append will add to the end of the file okay so it's a lot safer than writing to a file there's also um read plus W plus a pen plus the only one I ever really use is read plus but we're not going to get into that okay so read plus by the way would be read and write now we've created our file object with this open function and now we need to store it in a variable and what we do is we say as F okay now you can name this anything you want you could say file object people say file obj I just like to use f okay it's simple and you're used to seeing a store variables to the left okay so it would be an equal sign to the left of that equal sign would be the variable and then we'd store this open function inside of it but this context manager is just a little bit different this is just the syntax so this is our variable now okay and we use the as keyword so while we're inside of the context manager we're tabbed over we are within context and the file will will remain open okay as soon as the the context manager is done or you're tabbed back into what's called Global scope which is back here well then the context manager will close that file for you now why is that important well if you just use the improper Way by just using the open function without the entire context manager well you can do that but if you forget to close the file you can run into a lot of issues okay you have to explicitly close the file and that leaves developers it leaves them open to making mistake we all make mistakes you can have problems with file descriptors leakage and you know if you don't close a file the data is still technically in transit and data in transit is inherently less secure than data at rest so again just stick to using a context manager whenever you are doing file handling okay now let's just go over read okay um why don't we start off by discussing some of the ways we can read from a file one of my favorite ways to read from a file is just simple it's just F dot read okay and then what we would do is store this in a variable okay and then I can actually just print contents so let's go ahead and run this and you can see it's reading the file now if I was to open the file down here you can see it's reading it perfectly okay but perhaps I wanted to work with this data now generally each line will represent a new um you know like a new part of that data so what you would want to do is read lines you could read lines run it and now you're storing each line which represents a new piece of data inside of a list this is a little bit easier to work with a lot of times okay now I also like to do this where I do F dot read dot split and how do we want to split it we want to split it by a new line so now if I run this you can see this is a little bit cleaner because it gets rid of that backslash n all right so let's go ahead and talk about specifying how many characters you want to read well let's delete this dot split method and now what I can do is I can pass an argument in here and I can specify how many characters I want to read so let's just go ahead and read 15 and run this and you can see it's only reading the first line let's go ahead and pass in 23. and now you can see it's reading 23 characters okay so you can specify how many characters you want to read again this goes back to python allowing us to break data down into smaller portions okay now of course the file that we're working with is not very large but we're going to pretend it's huge and we're just breaking this down into small little portions okay so this is one way to do it but another way to do it is also we can actually iterate right through our file object so I would say for line and F okay and I would print the line all right let's go ahead and run this and you can see this works perfect okay now one problem is this extra line I wouldn't say it's a problem but if it bothers you what you can do is you can specify keyword argument and you would end instead of ending with a new line you're going to end with an empty string so if we run this now you can see that kind of fixes it for us if you don't like that extra space being in there okay now like I said if you write to a file what will happen is it will end up writing over the entire file so you have to be very careful I quickly want to show you a pen then we'll wrap up this lesson I don't want to go too in depth so let's go ahead and put an a here okay now we're appending to the file so what we want to do is delete this for Loop and we can just write to it but it would write in a pen mode so I would say F dot right and let's just go ahead and write this is the fourth test and let's go ahead and run this now you can see it didn't overwrite the file but it actually appended right to the last T this isn't very desirable so a lot of times you want this to come down here so let's go ahead and fix that let's just say this is the fifth test and what I would do is put a backslash n right here okay let's go ahead and run this and then there you go so that kind of fixes that little issue that we just saw where it added it right to the very end basically concatenated it to the last character so that's file handling feel free to explore it go on Google there's a lot of information on file handling again this is a beginner course so I don't want to get too in depth but I will see you guys in the next lesson all right bye hello everybody I hope you're ready for the next lesson because we are going to go over exception handling okay now what is an exception it is simply an event that occurs during our the execution of our program that will disrupt the the normal flow of our instructions okay so when an exception occurs it is being raised what is an exception again it's just an error okay it's going to break our code so as usual let's go ahead and right click and let's create a new file and let's call this exception handling okay let's go ahead and create a program that is vulnerable to an exception okay so I'm going to ask somebody for their age I'm going to set it equal to input and I'm going to say please enter your age okay and because I'm asking for their age and I want to do some evaluation with it what I'm going to do is I'm going to convert this or Typecast it okay and we'll Typecast it to an integer now let's evaluate this age we'll say if age is greater than or equal to 21 we will print you are legal to drink okay and then we'll just go ahead and add an else block and we can say you are not legal to drink all right so I have this program and on line one this is vulnerable to an exception okay and let's go ahead and run this so I can demonstrate so I'll run this I'm going to put in an age but I'm going to make a mistake for some reason I'm just going to put in my name instead because I'm just doing other things I'm distracted and I'm running this program and I just go ahead and put my age in all right you can see we end up with an error okay or a value error to be specific now how do we fix this well generally what we want to do when we're handling an exception is we want to basically fail gracefully okay we don't want this to happen because users get panicked they don't see what this is it's this is a big deal to people okay it's a big deal to me and I'd like to write a lot of code so I don't like to see errors and what we want to do is handle that gracefully and we can do that using the try keyword and the accept keyword we can also use else and finally but we're not going to go over that okay let's just go over try and accept so what I want to do is I know that this code is vulnerable to errors so I'm going to put it inside of a try block so basically I want to try this code try this code and if it raises an exception well we'll handle that in the accept block okay so I would just go ahead and print you did not um enter a valid age okay so what's going to happen is if if I cause an error in here well what will happen is this accept block will trigger and then it's going to print you did not enter a valid age however if this block of code runs well that's perfect that's all we needed to do anyways and everything will run just as normal so let's go ahead and run this program now and I'm just going to go ahead and put Dave okay and you can see the program handled the exception gracefully now perhaps we wanted to Loop this code so that no matter what if they did not enter a valid age we could just keep looping through the code okay and giving them another chance to enter a valid age I could just simply put this in a while loop so I would say while true now again be careful with while true Loops they're better than using Flags but a lot of times you just want to try to figure out if there's a condition that can be met in this case I'm just not seeing a condition that could be met maybe there is I don't want to spend too much time so what I'm going to do is use while true now if this block of code Works what I'm going to do is break otherwise the accept block is going to run you did not enter a valid age and then it's actually going to run the while loop again okay so let's go ahead and run this and I'm just going to put uh 21 here okay you're illegal to drink everything worked fine the program exited now if I run this again and I put Dave you can see you did not enter a valid age and then because we're in the while loop it's allowing me to have another go at it okay now there's something right here on line nine that you may have noticed and that's that yellow squiggly and this is telling us we have a styling issue and it's because we're using a bear except what is a bear except it's an exception that doesn't Define or handle an exact exception that you want that or that's been caused so let me demonstrate that if that doesn't make sense now this is too broad we're just handling any exception but many times we want to say handle um exception as e now this is still a bit broad this is still going to handle every exception and it's going to print this error but it's also going to store the error inside of this variable so I could print that variable out to so I could say you did not enter a valid age and then I could put like a backslash n here and I could say um what do I want to write I'm doing an F string let's put a backslash n in here sorry and then I'll say error and then I'll put e in here okay and let's just go ahead and run this and see what we get now so we'll stop and rerun okay and let's just go ahead and put Dave here and you can see now we're handling this exception a little bit better okay we're catching any exception and what we're doing is we're storing the error code inside of this variable again this is just how this is proper styling okay just having a bear except is not good enough it just it just isn't now the best way to do this is to actually put the the exception that you know will occur and we know that only one error can occur here and that's going to be a value error so I would say accept value error as e now this is a little bit more readable for example if someone else is working on my code they may come in to this except block and see that a value error can occur okay and that might just give them a little bit more insight into what's going on with my program and it allows them to work with my code a little bit better okay it just gives us more information it's better to do an accept block like this now we can also use an else in a finally block again we're not going to get into that this is good enough for now because this like I said this is a beginner course okay now that will wrap up the end of the lesson but just a quick recap when we don't know if something is going to cause an error or we know it's going to cause an error we can put it inside of a try block okay if the error occurs it was raised and the accept block runs it's that simple okay I will see you in the next lesson bye hello everybody I hope you're ready for the next lesson this is the last lesson and we are going to be going over object oriented programming now we're not going to go too deep into it because again this is a beginner course but why don't we go ahead and start so let's go ahead and first off create a file so we're going to call this oop okay now let's quickly talk about object oriented programming okay object oriented programming is is a programming Paradigm it's a way of organizing and writing your code and it's based off of the idea of objects okay and objects are collections of data and like functionality that represent real world things okay so I may have an object in Python that represents an actual car in the real world I mean I can represent pretty much anything you can think of okay now objects are made from classes okay classes are blueprints for creating objects okay we only build one class and then we can build as many objects from that class as we want okay this class will Define the the attributes and the behaviors that the objects are going to have okay so if I had a dog class well I can create a bunch of different dogs from that class okay and they're all going to have the same attributes and behaviors but they're not going to have the same attribute values okay so I think it's best for us to just start coding okay instead of talking about it so let's go ahead and first off create a class okay so to do that we're going to use the class keyword so we'll say class okay and what I want to do is I want to create a class for cars all right so I'm going to call this class car and it's proper convention to use an uppercase okay now we'll put our colon and the first thing that we generally want to do is create an initializer or a Constructor okay so to do that we're going to say def underscore underscore init right here okay and this is where we're going to pass in our attributes okay user defined attribute values basically and to do that what we'll do is we'll come up here I want you to ignore this keyword this word self for now okay so let's go ahead and put a comma and we know that every car is going to have a make and a model okay and now what we want to do is kind of um uh set their variables so we do that by saying self dot make equals make okay and self dot model equals model okay so we have now created an initializer method okay this is going to run automatically when you create an object okay now what we want to do is Define some behaviors of a car some behaviors might be def Drive okay in this car we'll just go ahead and we'll print um I don't know we'll print the car geez I can't spell today is driving okay all right so we have this one Behavior okay what are behaviors that that are inside of a class those are called Methods all right and now we'll create one more we'll call it def stop so every car drives and every car stops hopefully and let's go ahead and print the car is stopping okay so we have successfully created a class and I can create as many car objects as I want from this class and to do that what we do is we do what's called instantiating an instance okay an instance is an object of a class so I'm going to do that by just saying I'm going to name my instance I'm just going to call it C1 you can name it whatever you want but it should be meaningful and then I'm going to say it equals a car okay and now I want to put parentheses because this car class requires that I put in the make and the model all right so I'm going to go ahead and say um let's do a let's not do a Tesla let's do a Ford and we'll do an F-150 okay so I have now instantiated an instance or I have created an object of the car class let's talk about self what is self self is a reference to the object itself okay and this is how we're able to create many as many as we want actually usually an arbitrary amount of objects based off of this class so the instance name gets passed in first so C1 gets literally gets passed in first into this self okay the self parameter all right and now what's going to happen is we're saying C1 dot make equals Ford and C1 dot model equals F-150 and that again helps me differentiate between all of my objects all right if I had if I didn't have this self parameter nothing would work it wouldn't know which object we're talking about or which instance we're talking about okay so now I have this car instance and what I can do is I can access all of the car methods by typing C1 and then using the dot Operator just like we do with strings or integers or lists or dictionaries it doesn't matter we use the dot object to access the methods now you can see we have two methods here we have drive and we have stop okay so let's just go ahead and call the drive method and we're going to see the car is driving now let's make this a little bit more unique okay so I'm going to make this an F string and I want to put the the actual make and model of the car so I want to say self dot make let's put the let's say the self.make self dot model is driving okay and let's go ahead and run this now and you can see because it's passing in C1 first which itself we know self we know C1 dot make is equal to Ford and C1 dot model is equal to F-150 that's going to get passed in because we're directly working with this instance okay so again that's how we can organize having multiple instances of a class let's go ahead and create a C2 just so you can see a little bit better how it differentiates between the two and let's just go ahead and put a Dodge and we'll do a Dodge Ram okay now if I call C2 dot Drive what's going to happen remember C2 is we're working with C2 now so C2 gets passed in and we know that we have C2 dot make equals Dodge and C2 dot model equals Ram so when I call this drive method C2 dot dot make which is Dodge and then Ram is going to get passed in so let's go ahead and run this and you can see the Dodge Ram is driving so again self just it refers to the instance all right okay so that's just a quick way to create a class again we're not going to get too in depth here because it's a beginner course but what I want to do now is just show you uh inheritance okay so what I'm going to do is I'm going to delete these just for actually I'll comment them out and I want to create another class but it's only for electric cars okay and I want you to pretend that this class is huge there's a lot going on inside this init model now I could create my own class for electric cars and start from scratch and just build it out just like a regular car or I can use inheritance okay which may be much more useful because it's going to save me a lot of code again pretend that this this car class is very complex okay and this init method may be initializing some kind of a database or just anything just pretend it's more complex and we don't want to write a complete class from from scratch what we can do is use inheritance so I would say class Electric car and then I use the parentheses and I inherit from car okay now if I create My initializer Method it's going to look a little bit different because not only do I want to initialize the attributes for the electric car I want to initialize this init method of the parent okay so what I would do is put it in it here and we still have to have the same attributes okay so we Define the same attributes we would say make model and let's just do battery size so we'll say B size okay because that's going to be the major difference between say a car and an electric car all right now what we want to do is call the initializer method of the parent so we use super to do that okay so we'll say super dot init okay and now what we want to do is just pass in the parameters for that actual init method of the parent class which is going to be make and model okay and now the only thing that we need to take care of is the battery size okay now again this is important that we do this because there may be a lot going on in this parent's initializer method and maybe initializing database a database or doing something that's complex that we don't want to rewrite okay so in our child in it method we call the parents init method and you still have to pass these in okay but what we don't have to do is we don't have to kind of uh store them in a variable like we did on line three and four all right so now what I'm going to do is just take care of battery size so I'm going to say self Dot B size equals B size okay so now I have successfully inherited from the parent class okay so let's go ahead and talk about like overwriting methods so basically the child it has all the power when it comes to programming the child is going to have all the power okay so whatever I can access the parent and all of its methods with the child but I can't do it the other way around okay I can't take a parent object and access all of these child attributes and methods it's just not possible okay so if we were to create the same method well what would happen is it would overwrite it but first off let's go ahead and create an electric car okay so we're going to say electric car one equals electric car and this one needs a make model and a battery size so we'll say Tesla we'll do a plaid and let's make the size like just 300 okay a rant some random integer all right that's too many blank lines okay so we have this electric car and if I was to say ec1 and I used the dot operator you can see I have access to the parents methods okay but also if I create the method here so I would say def Drive and down here I would say print what do we want to do we'll say the electric car has a uh let's do something like this self Dot make sell we'll do self dot I can't spell today okay uh self dot model is driving on its I don't know we'll say self.b size is driving on itself dot b size battery we'll do something like that okay kind of random doesn't totally make sense but you know it'll work so now if I come down here and I try to access that drive method again well it's not going to grab the parents okay and you can see over here to the right because we overrode it okay the child takes precedence so it's not no longer going to access the parents drive method it's only going to access its own Drive method okay so if I say drive and run this you can see the Tesla plat is driving on its 300 battery okay so that's just like the basics of inheritance again it's just to um allow for better reusability okay and like I said there could be a lot going on inside of a car class initializer that you just really you and you really want to use all of that code why would you start over from scratch and start writing all of this code when you can just simply inherit and then use this super method to call the parents initializer okay so let's go ahead and move on we're going to uh just use two objects together okay so what I'm going to do is I'm going to grab all of this and I'm actually I'm going to comment I'm going to comment everything except car out in fact I'm actually gonna yeah I'm gonna comment everything out and now what I want to do is I want to create a car and I want to create a garage okay and what I want to do I will say class garage so class garage and what I want to do up here is give every car a horsepower okay so we'll say um HP and we'll say stealth.hp equals HP so self.hp equals HP and that's going to represent horsepower so now when you create a car you have to put the make the model and the horsepower and for garage what we're going to do is we're going to create an initializer and I want to just give it two attributes so we'll say def in it I want to give it a Max we'll do a Max size and then we'll do I think all we need is a Max size yeah we'll just do a Max size okay that's going to be the only user defined attribute but there's going to be another attribute that we're going to write it's not going to be user defined so I'm going to say self dot Max size equals Max size and then I'm going to say self Dot um garage space equals an empty list this is going to represent the space that's in a garage okay if we can park cars inside of a garage I need to be able to represent that space and one of the best ways to do that is by using a list okay so every time I create a garage object it's going to have a user defined Max size but it's going to have a non-user defined list that represents the space inside of that garage and now I'll be able to put car objects that I create inside of this garage okay so let's go ahead and create a method to allow us to insert a car object inside of this garage okay so I'm going to say deaf uh let's say add car and we actually we have to put a parameter here that's going to represent the car so I'm just going to say car and what I'm going to do is say if the um Len of self Dot Garage space if the length is less than self dot Max size we know the garage is not full and therefore we will be able to add a card to it okay so what I'll say is um self.garage dot append right that's how we add to a list remember self.garage space represents a list and what are we going to append the car that they put in okay now if it's full why don't we just print a message okay so we'll just say else print the garage is full okay and now what I want to do is I want to create a behavior okay and this behavior is going to go through each car and it's going to give us the average amount of horsepower that's in our garage so if I have a a Lambo and I have a Corvette a Porsche and a Bugatti it's going to get the average of all of the horsepower that's inside of that garage okay and to do that what we can do is create the method to do it so what we would say is Def AVG let's say HP okay so average horsepower okay and what it's going to do is we need to we basically need to iterate through this list okay so we're going to start with a value we're going to set that equal to zero and then we're going to iterate through our garage space it's a it's a list right it represents the space and there's cars inside of that list so I would say for car in self Dot Garage space value plus equals car dot HP okay car.hp so every single car has an attribute called HP and that's going to be you know maybe like a 500 or 300 so it's going to be an integer and what we're going to do is we're going to add that to the value okay so now that I have the value and I'm adding the car HP to each one this for Loop has done its job so what we can do is we can return the um why don't we do the value divided by the Len of self Dot Garage space okay and this should give us the average horsepower okay so let's go ahead and um let's get rid of everything down here because I want to or we'll create more space we'll create more space so again we want to create an instance or instantiate an instance of both classes okay so the first thing let's let's just go ahead and create a garage first okay so I'm going to say garage one equals garage okay and now I want to just pass in one attribute which is going to be the garage size so there's going to be a Max of four cars okay now I have a garage and I need to create some car objects so I'm going to say C1 equals car and we'll start we'll do all like exotic so we'll do Lambo and the lambo will have a event to door and let's give it a horsepower of like I think it's like 900 or something like that okay and I'm gonna hit Ctrl D okay and I'm gonna do C2 I'm gonna call this a Porsche we'll just give it a not we'll say it's a 911. we'll do C3 we'll do a Bugatti for C3 this person's got a nice garage Bugatti we'll do the shyron I think I think it's got over a thousand horsepower so I'm just gonna put like 1100 here and then one of my favorite cars would just put a classic in here so we'll say let me just control do this we'll say McLaren F1 which I think had like seven horsepower 700 horsepower okay so I have a garage that can hold four cars and I've created four car objects now I want to add these cars to the garage okay so what I would do is I would say G1 dot add car and I have these car objects so I'm just going to go ahead and add C1 and because of the way I wrote this method okay I'm gonna have to do one at a time now you could rebuild this so that it takes all four objects at once but just for the purpose of this lesson we'll stick with this and I'm just going to hit Ctrl d d and I'm going to say C2 C3 C4 okay so I've created the garage I created the four car objects and I added each car to the garage now that's the max if I was to try to add another car it's it's just not going to work okay and it's going to print a message it's going to say the garage is full all right and now what we want to do is we want to get the average horsepower okay so let's go ahead and print um G1 dot average horsepower and let's go ahead and run this and you can see the average horsepower is 900 okay so this is how we use two objects together all right now I don't want to go any further than this again it's a beginner course I know I reiterate that all the time but um I hope that you've enjoyed it that's going to wrap up this lesson okay so we went over just creating a class using inheritance and then using two objects together and what I would recommend doing is either going on Google and you can learn you can learn a lot more on Google obviously but also I want to let you know that I am coming out with a more advanced uh python course it's not done yet but as soon as it is I will make sure that I um put a link to it in the description of this video okay so thanks so much I hope that you guys learned a lot I hope that it was easy and I broke it down in layman's terms for you and I helped you I hope you get a better understanding of um python from this course and thank you so much goodbye
Info
Channel: CodeWithDave
Views: 26,509
Rating: undefined out of 5
Keywords: python, programming
Id: HjGTcasl7jE
Channel Id: undefined
Length: 199min 44sec (11984 seconds)
Published: Wed Jan 04 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.