Python As Fast as Possible - Learn Python in ~75 Minutes

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everybody and welcome to python as fast as possible in this video i'm going to be trying to teach you python as quickly as i possibly can that means i am going to go over things quite quickly i'm going to talk very fast and i will note that this is not for absolute beginners if you are looking for a beginner level tutorial series i have many on my channel i will link a ton of resources down below and if you feel that i missed anything or made any mistakes please do leave a comment with that being said let's go ahead and get started and get into python as fast as possible [Music] to get started we need to download and install python please download the most recent version of python from the python website which will be linked in the description if you're using any version of python which is below 3.6 that will not be compatible with this tutorial when downloading please check the box that says add python to path this will make your life easier in the future now python automatically comes with its own environment to write code in this is idle you can use this to write your code if you'd like but i prefer to use visual studio code which is a text editor to download that you can go to the link in the description and just download the stable build for whatever operating system you're on now if you're using vs code there is one little bit of setup we need to do to get started here we simply need to install the python extension go to the extension tab here this will bring us to the vs code marketplace we can type python and then we can see the python extension and simply press install you can see for myself i already have it installed so before we get started i thought it would be a good idea to talk to you about some of the things that python is used for here i am on my channel in the playlist tab and you can see a bunch of different playlists just giving you an idea of some of the things that i've created with python and taught on this channel python is a general purpose programming language it is very easy to learn versatile and really easy to write in the core advantage of python is typically how simple it is how easy the syntax is to read and just how fast development is with it that being said python is mostly used on the web and in machine learning artificial intelligence and data science related tasks now the sponsor of our video knows a ton about that and that is simply life i need to thank simply learn for sponsoring this video and giving you all a discount on their data scientist master program that was co-developed with ibm this program is comprised of six unique courses that implement a unique blended learning experience in a high engagement online classroom environment during this program you will master 30 plus in-demand skills and work with tools and languages like r sas python tableau hadoop and spark you'll master these skills through 15 real life projects and one capstone project during this 12 month comprehensive course you'll also be given 1200 usd worth of ibm cloud credits to use during your 24 7 access to the ibm watson platform after completion of this course you'll be given certificates from ibm and simply learn to testify to your skills as an expert in data science get started by hitting the link in the description i want to start by covering the core data types in python these are int float string and bool and otherwise known as integer now an integer or int is simply any whole number that does not have a decimal point on it so two three seven three seven that was going to be an int negative nine that's an inch negative some random large number is an int so long as it does not have a decimal point on it we consider it an int a float is almost the opposite of that it is simply any number that has a decimal point on it so something like 272.00 this is a float even though you can would consider this a whole number the point zero adds a level of precision to this which makes it a float of course something like 9.7 would be a float whenever you see a decimal point that means the number is a float next we have string a string is simply anything surrounded by single or double quotation marks typically we consider this a sequence of characters or numbers so if i type something like hello and i type something like hello these two strings are actually completely equivalent it does not matter whether you use single or double quotation marks there is no difference i will note that if you want to embed a double quotation mark or a single quotation mark in the actual string itself then you would usually wrap the string with the opposite type of quotation mark so in this case i want to have this double quotation mark inside the string so i wrap the string with single quotation marks now just to note here if i do something like 4.6 even though you might consider this a float this is a string again because it is surrounded by single or double quotation marks remember the definition anything defined or surrounded sorry by single or double quotation marks next we have boolean this is one of two values we have true or we have false some people may consider this one and consider this zero we're going to look at these later on and you'll see why that's important but that is a boolean value and these are the four core data types in python the next topic i'd like to cover is output and printing this is very important and we will be doing this a lot in this video in python there is a simple function called print this is how you output something to the console when you run your program typically when you print something you'll put it in strings so i want to print say hello world so i will print the string hello world like that and if i run my program by pressing this run button you can see down here in the console we get hello world now notice that if i just type hello world like this and i don't put it inside of a string and i run my program i will get an error this is because i cannot simply print something like this the program does not know how to interpret hello world if it is not wrapped in strings that is because this is not one of the core data types and we have no variable or anything that denotes what hello world is that being said if i want to print a number say like 4.5 python knows what 4.5 is because it is a float you can see when i highlight it it says float if i run this it will print 4.5 out and there will be no problems now if you want to print multiple things what you can do is separate them by commas so say i'd like to print 4.5 and then i would like to print hello when i do this it will automatically add a space between the two things let's just put another string here just to show you what i mean so 4.5 hello notice it adds that space in between now we will talk about how you can print things without the space in between later on but that is the basics of a print if i want to print many things on the same line i can go hello i could say end i can say 87 i can say false and i can run this and this will all work hello and 87 false now there's a few arguments that you can pass into the print function if you do a comma and then type end equals you can denote what you want the end of this print statement to be by default the end here is a carriage return which is backslash n what that means is go to the next line after this line is printed now notice if i don't add this backslash enter in fact let me just show you what happens by default so let's say i have hello and 87 i'm going to get rid of this and i print both these things out we get these on two separate lines that is the default behavior of the print statement and prints on a new line now notice if i add end e end equals and instead of a backslash n let's say uh this pipe like that and i run this now at the end of the print statement it simply prints a pipe it does not add the carriage return which means we do not move on to the next line that's a little bit more advanced but essentially the carriage return is that backslash n and again that means just move down to the next line that is the basics of printing and output and now let's move on next let's move on to variables variables are an important part of any program in python they are quite easy to create when you create a variable in python you simply type a variable name a space an equal sign and then assign it to some value this value is going to be some data type some function something else that we'll talk about later on to give you a basic example i'm going to set the variable hello equal to the value 10. think of a variable like a bucket that stores some data or stores some type now if i want to access the value that is stored in hello what i can do is i can simply say print hello what this will do is it will say okay this is a variable i know it's a variable because i saw it earlier on the program let me see what its value is and i will print that value so now i'm going to run and you can see that we get tim let's make another variable let's call this world let's actually set this equal to the value world now let's print hello comma world and let's see what we get we get tim and then space world variables are somewhat complex you can make them equal to other variables if i decide to change the value of world to actually be equal to the variable hello what's going to happen is it's going to say okay hello is a variable what is a low equal to hello is equal to tim so i will print tim now watch what we're going to get we get tim tim now what happens if i come here and i change hello to be equal to no if i do this what's going to happen is world will not change because world was set equal to the value of hello at this line at this point in time line three because our program reads from top to bottom the value of hello was tim so world gets set to tim then hello gets set to no that does not change the value of world so what you're going to see is we're going to have tim and then we're going to have no so let's have a look here and sorry other way around we get no tim my bad though we printed a hello first and then world i have that mixed up that is the basics of variables there's many other things that we can do with them but let me just tell you about the naming conventions for them so in a variable you are not allowed to have any special characters and you cannot start with a number so you are allowed to have an underscore that is the only exception in terms of special characters usually you will use that when you are denoting a space and in python the convention is typically to separate things using an underscore in other languages you may see something like hello world if you wanted to make a variable uh that has multiple words in it but in python stick with snake case which is what this is known as the other one is camelcase and use underscores to separate your variables just to give you an example of what i mean you cannot start a variable with a number so 9 hello notice i'm getting the red line popping up here and i could not do something like hello 9 that does not work but if i want to hello world 32 that is totally fine that is a valid variable name the next topic i want to talk about is getting user input just like printing something is very simple getting input is simple as well what we do is we simply use the input function we have our open and close parentheses and then we type what's known as a prompt inside of these brackets like this now this does need to be in a string unlike the print statement we cannot do something like 4.5 in here that will not work we do need a string so what i'm going to do is i'm going to add a prompt say i want the user to type their name then i will say name i'll do a colon and then i will add a space between the colon and the end of the string the reason for this is that the user will be able to start typing on this line right after the prompt so if i don't have a space then what's going to happen is the user will start typing right where the colon is which of course is not going to be good so let's go ahead and run this and have a look at what happens we can say name colon space and now i'm going to type tim that's great but how do we actually get the value how do we store what the user typed in so we can use it later well we need to assign this input to a variable so what i'm going to do is i'm going to say name is equal to input i just want to note because i forgot to say this previously we could have capitals in our variable name if we want that would be fine but i'm not going to do that and now what will happen is whatever we type in will be returned to us in this statement as a string and it will be assigned to this variable name so if i go ahead and print the value of name what's going to happen is whatever we type in here will be printed out so let's have a look at this name let's type tim and we see that tim is printed out below there now we of course can get multiple user input so i can say something like age equals input age colon and now let's see what happens when i run this so we go name tim age 20 and we could put print those out if we want but i'm not going to actually you know what let me show you one example where i print them out let's say hello and then let's go comma name and then you are comma age years old so this is a way that we can combine variables and strings so that we print out a meaningful response that says hello whatever the name is you are whatever the age is years old so in this case i'll say name tim age let's go 12 and says hello tim you are 12 years old all right so that is it for input now we're going to move on and talk about how we can actually convert say numeric input into a integer or float because it default comes back as a string all right so the next topic i want to cover is arithmetic operators these essentially are like plus minus multiplication exponent how you perform mathematical operations in python now just something to keep in mind when we're performing and using arithmetic operators we have to make sure that the data types on the left and right hand side of these operators otherwise known as the operands are the same data type or are both numbers what i mean by this is if i have something like hello and i try to add 9 to it well that just does not make any sense and we are going to get a error and something's going to be wrong with our program so just keep that in mind i cannot do hello minus 9. you cannot do hello divided by 9. you actually can use the multiplication you can take a guess at what that's going to do but i will show you that later alright so let's go through the basic operators the first one is the plus sign very straightforward we can add 9 plus 3 or in this case i could add x plus y so x is 9 y is 3. we can add those two values together ignore the squiggly when i save that goes away now something to keep in mind if i make this 3.5 this works totally fine even though these are different data types since they are both numbers i can add them together of course i can do the subtraction let's run that i can do the multiplication which is an asterisk and i can do the division we do the division we get some crazy number just keep in mind that the maximum precision in python i believe is 13 decimal points there is ways to get beyond that but that is kind of too advanced i'm not going to cover that right here and when you ever do division or you're dealing with rounding and all of that that's always a problem in computers so just keep that in mind you're not going to get the most precise answers with division like this if this is a non-terminating decimal point anyways hopefully that makes sense now i just want to point something out if i actually have say 9 over 3 of course you know that that answer is 3 and that is a whole number right 3 is a whole number but this is actually returning to us a float not in int even though neither of these data types are afloat the reason for that is because whenever we use the division operator it returns a float because it does not know if it's going to need to represent a floating point or it's going to have to represent some level of precision so just keep that in mind if for some reason you don't want this to be a float of course you can convert the result to an int by just doing int like that okay so those are some of the basic arithmetic operators next we have exponent so that's two asterisks like that this will raise x to the power y so we can see that gives us 729 and then next we have floor division so what i'm going to do here is actually make this a 10 have a guess at what you think this is going to give but this gives us 3. the reason it gives us 3 is because this will give us the integer result of whatever the division is imagine this was like 9.333 or whatever it may be it just simply removes all of the decimal points and just gives or sorry to be 3.333 removes all the decimal points and just gives us whatever that integer result is next is mod this is the percent sign this returns to us the remainder after a division in this case 10 mod 3 is going to be well the answer is 9 and then remainder 1 right so that is why we get 1 from this here i think that's it for those operators now i will simply show you how we can do say order of operations so if we want to order of operations we can use brackets or if we want to evaluate things in a specific order this does follow the simple order of operations which the acronym i know for it is bed mass which is b e d and i hate this autocomplete it's making it very hard to do this m a s okay so what this stands for is brackets exponents division multiplication addition subtraction python does follow those standard order of operations if you just write an expression but of course it's always easier to use brackets and spread things out so i can say something like x mod y and then i can multiply that by two since i didn't say it the integer division and mod operators are in the lowest order of order of operations so if you use mod that is going to be below addition and subtraction pretty sure that's correct but someone can correct me from the in the comments if i'm wrong about that but anyways you get the point you can use brackets and that is how you can perform and do arithmetic in a certain way all right next thing i need to cover here is simply just an example using input so what i'm going to say is num equals input and we'll just say number colon like that and what i want to do is simply print the result of num minus five so i want to take whatever number they type in and i want to subtract five from it and print that to the screen so let's try it num equals five notice that it says unsupported operand types for minus are four sorry yeah minus string and int so what that's saying is that num is a string even though it looks like an into us i'll explain why in a second and we cannot subtract the number 5 from it that just doesn't make any sense the program doesn't know what to do with that so think about this why is num a string we typed in 5 this should be an int value from what we understand about our data types well this input automatically returns to us whatever we type in in a string so just imagine that whatever we do here it's going to give us a string and that means that if we want this to actually be a numer numeric value and be able to perform arithmetic on it we need to convert it to an int so there is this fancy function called ins in python this will take some string assuming that there is an int inside of that string and it will convert it into its integer representation let's have a look now when i type a number let's say 10 of course we get the value 5 it took num which was 10 so the string 10 you know i'll type it out like here we have the string 10 and it simply converted that to the actual integer value 10. now you can imagine we have other functions in python that do this as well we have say float that's going to take whatever we have here and convert it into a float now notice what happens when i do this if i do float 10 we get 5.0 whenever you have at least one float in your arithmetic it's going to automatically return to you a float it needs to keep the level of precision that was in the original you know operation or in the original value so just that's something to keep in mind whenever you're doing you know plus minus multiplication if there is one float as the operand so on the left or right hand side of the operator then you are going to get a float result back all right that is all i wanted to show for this now let's move on so the next thing i want to cover is something called string methods before i do that though i need to define what a method is so let's create a variable let's make this equal to hello and we can just say hello equals the string hello i want to show you this function called type which will tell us the type of a variable so if i print the type of hello well you can imagine what type this is going to be it's simply its data type let's have a look here we get class string you can ignore the angle brackets in the class if you're more advanced you probably already know what this means but essentially what this is saying is that this string or this variable here hello is an instance of the class string again big words you don't have to know what those mean but for anyone more advanced hopefully that makes sense to you all right so how do we use a method on a string and what is a method well a method simply is something with a dot operator so we have dot we have sum and name whatever the method name is in this case it's going to be upper and then we have an open and close bracket or parentheses whatever you want to call them now i just want to show you what these methods do so if i say hello dot upper this is a method that we can call only on strings so since this is a string i can use this dot upper method on it and imagine what this is going to do what this actually does is it upper cases or puts the entire string in completely uppercase so if i have a look now at hello and we print it out we can see we get hello in all capitals now i don't need to call it like this i also can do hello dot upper now the reason this is going to give me the same thing is because well this is storing the value of the string which is hello and then it was just making an uppercase right here now what happens it says okay well what is hello equal to oh well hello is equal to the string hello so you can think of it as a substitution this string just goes where that is makes it upper and then prints it out there we go upper now let's have a look at what dot lower does you can probably imagine already but it simply makes the entire string lower case these are useful when you're getting user input and you want to validate and check if it's equal to something which we will do later on so a few other methods we have dot capitalize you can imagine what this does it simply capitalizes the first letter in the string and then makes the rest of it so it looks good right so i've had a sentence and i said like hello world like that let's have a look at this it's going to give me hello world properly and if i added a capital w like that it removes that and it just kind of formats it as if a sentence would next we have this method called count what count does is it will count a specific string within the string so it looks for a substring in the string and how many times that occurs let's see i wanted to see how many double l's were inside of this string well i can say hello.count double l and then that will count and tell me how many are in here now have a look and guess how many you think are in here well there's actually zero the reason they're zero is because these are capital l's and this was looking for lowercase l's so that's why it's very important to make sure you're differentiating between capitals and lowercases because two lowercase l's are not the same as two uppercase l's but if i wanted to fix this problem i could make the entire string lowercase so i could say hello dot lower and then i could count all of the double l's in the lowercase string so now let's look at this in this case we get the value one let's make this a little bit better and let's look for all of the is there any other ones that we can look for i guess we can look for how many o's there are all right in that case we get two o's so those are a few basic string methods there are some more but i just want to show you how you use these and the fact that you can chain them on each other because what happens is this will return a string you can imagine that the substitution of this just looks like um you know hello world and then it's going to count all the o's inside of hello world and return to us that value which in this case was 2. all right so now we're going to move on to conditions and conditional operators now before i mention that i just want to go over something that i forgot in one of the previous sections this is simply string multiplication and string addition this is important so i'm just going to cover it quickly but essentially let's say i have a variable and that's equal to hello so x equals to a low i have another variable and this is equal to actually we can say 3. well there's this cool thing in python but what i can actually do is multiply a string by an integer and that will simply repeat the string the integer number times so if i say x multiplied by y even though these aren't the same data type which i said before might cause problems since it's a string and it's an integer and the string is on the left hand side we can do this so i can say how this gives us hello because we multiplied the string by the integer so just something to keep in mind that we also can add two strings together because that actually makes sense so if i say hello and then i say yes well i can add these two together this is known as a concatenation so let's have a look at this and i get hello and then yes so you can do that you can add two strings together and you can multiply a string by an integer just need to throw that in there because that obviously is something that's important to understand all right next we're going to talk about conditions and conditional operators so a condition is simply something in python that evaluates or compares two variables or data types and returns to us either a true or false value based on the comparison so to you know give you an example of a comparison is x equal to y is x less than y those are comparisons and the answer to that is either yes or no or in our case true or false so we have that true and we have these false this is boolean so the result of a comparison is simply a boolean value all right so let's look at what these operators are we have two equal signs which checks for equality is the left-hand side equal to the right-hand side we have not equal to which checks for inequality so is the left-hand side not equal to the right-hand side we have less than or equal to greater than or equal to less than and greater than now there's a few other random ones we're not going to go through those but these are the core conditional operators and let me show you an example of condition let's say i have x equals hello and y equals hello i want to check for the comparison so i might do something like print x equals equals y are these two things the same let's see yes they are we get the value true now let's do the not equal to sign are these things not the same no they're not so we get false that is the basics of checking for equality now what if i add a capital l inside of this string well we know that lowercase and capital l's are different so if i do this we get true which means no these two strings are not the same they are different if i do double equal sign then we get false now interesting thing with strings we can actually compare them using the greater than sign and less than sign now it's not super intuitive so i have to kind of show some more primitive examples here to give you an illustration of how this works but if i have something like a and i say is a greater than let's actually go with capital z what do you think the answer to that is well that's actually true now the reason for that is every single character inside of python is represented by what's known as an ascii code we actually have a look at what this ascii code is by looking at what's called the ordinal value of a character so if i print out the ordinal of z we can actually see that that's 90. if i print out the ordinal of a we can actually see that that's 97. so what that's telling us essentially is that this uppercase z is actually represented by the number 90 and the uppercase or sorry the lowercase a is represented by 97. so what we do is we would compare the two ordinal values of these character characters and see that well actually a is greater than z now if we do this so i say a greater than b then that's going to be false because b is actually represented by 98 there is some logical ordering for these characters but i just want you to understand that we can compare strings using greater than less than equal to all of those but just understand that we're comparing the ordinal value of the strings now let's say we have something like a b what we're going to do is simply just go from left to right and compare all of the characters so we would say okay is a greater than b yes it is imagine these two are the same so we had a and then we had d like that if these were the same then we would go on and compare the next character which would be b in this case we get false because obviously the d is greater all right so that is how that works now let's just show a few examples of numbers let's just say 7.5 equals equals 7. is that true obviously that's false let's say 7.5 greater than 7. obviously that's going to be true and what about 7.0 equals equals 7. have a look at that we get true all right that is the basics on the conditional operators and conditions of course you can store these things in variables i can say result equals 6 equals equals 6. and print result like that and we get the value true okay let's move on now to chained conditionals so a chained conditional is essentially combining multiple conditions together to create one larger condition what i have right here is a basic example i have three conditions right i have result one result two and result three result one says is x equal to y result two says is y greater than x and this one said is z less than x plus two you can imagine that these have brackets around it it works the same so all of our conditional operators have a lower precedence than all of the arithmetic operators so if i do something like z minus two less than x plus two well that's not going to say z and then negative 2 less than that it will evaluate on the left hand side and it will evaluate on the right hand side and then it will combine and use sorry this conditional operator so hopefully that makes sense but just keep that in mind that you can actually write expressions on each side of a conditional operator okay so now that we have that essentially what i want to do is say okay how can i make a condition that uses result 1 result 2 and result 3. so obviously all of these are equal to true false true false they're equal to something like that so what i can actually do is use three separate keywords so and or oops and not so these three words are what i can use to chain conditionals together and create larger larger conditions from specific conditions themselves so what i can do is do something like this i can say result 4 equals result 1 or result 2. now what does the or operator do well the left hand side and the right hand side of or have to be a true or false value which they are because these evaluate to true or false what or will do is it will look at the left hand side and right hand side it will say okay if the left hand side is true or the right hand side is true this whole condition is true of course if they're both false that's the only situation where this whole condition here is going to be false so in three scenarios because there's four possible scenarios here it's going to be true and the other scenario it will be false so if both of these are true obviously at least one of them was true so that evaluates to true so let's actually just print result 4 and have a look at it great we get true now what we can do is we can add another or if we want and we can say result 3. now what this says is ok if result 1 or result 2 or result 3 is true this whole thing is true which of course it is now let's see how we can combine other stuff into this we can actually use this not keyword what this does is it takes whatever is on the right hand side of it so in this case it's going to be a true or false value and just flips it so in this case if if result 2 is true which it is it's just automatically going to become false so if i actually i can show you on a more basic level if i just print a instead of result 4 i print not true like that this will print the value false which you can see down there and if i print not false then this will print true now what i can do is do something like this i can say not false or true so let's have a look at that and there we go we get false so it takes whatever this is which is true and then it just reverses it and makes it with the not now those are two that we've talked about so or and not now what about and so and essentially says okay is what's on the left hand side and the right hand side true if both of it is true they are true the whole condition sorry is true otherwise it is false so only in one scenario out of the four possible scenarios here with two variables or two values on the left and right hand side is this going to be true so let's have a look at this and we get true because obviously this is false then we nod it and that turns to true now of course we can do a combination of and and or now when we use these combination of different things right so not and or we have to know which is going to come before what so kind of in the order of operations now the way it actually goes is not is first and is second and or is last so just keep that in mind because that will be important based on the way that you write your expression so essentially if i say false and true or true then what we're going to actually do is evaluate the false and true first and then we will evaluate the or of whatever this is and then that so in this case this would be false so then we would get false or true and then this would turn into true then we would have the not and that would turn that into the false so now we're moving on to the fun stuff which in my opinion is the if else if and else statement now this essentially allows us to actually check if something occurs or if a condition is true and if that is true we can do something specific otherwise we might not do anything or we might do something differently so for the example i'm going to do here i want someone to type in their name and i want to say if their name is equal to in this case tim i will tell them they are good at python i don't know something random so in this case to do that what i will start doing is writing an if statement so the syntax of an if statement is if condition like that i know that's spelled wrong but that's fine condition colon indented block whatever i want to happen in the indented block and then that is the basic syntax of the if statement so for here if i want to check if x is equal to tim i will say if x equal equal to and then the string tim with the capital t i will simply print you are great so now what will happen is if and only if x is equal to tim will i print you are great now in any situation so regardless of if the name is equal to tim or not i will always do this so i will print that statement hopefully that's self-explanatory so let's go ahead and do tim we can see you are great always do this now let's do another name let's go with joe and it says always do this it did not print this statement now the addition to the if statement is of course i can add more stuff underneath this indented block so i do another print statement and that would add a space so let me actually just show you how this works if i do tim now you see we get that space by just printing an empty print statement essentially okay now what we can do though is we can say okay how about i only want to print you are great or i want to print some other message i don't want to print two things well what i can do is i can print i can add this else this else must come after an if statement and essentially what it allows me to do is say okay if this isn't true do this so it's an if else right if true do this otherwise do this never will i do both so in this case i'll just print no you know not very intuitive or it doesn't really make much sense but that's fine so now i'll go ahead and i'll say name tim it says you are great and let's actually just go with no and then it prints out no all right so that's the basics on the if else now let's say we actually want to check multiple things so maybe we want to check a bunch of different names and if the name is joe maybe we want to say you know thanks joe i don't know something random right well what we can do is use this elif statement the l if statement can be used as many times as we want but it must come after an if and it must be before any elses so i can't just go ahead and start writing these random elifs everywhere it only is going to come after an if statement so there's only ever one if only ever one else but as many lifts as we want and you'll see how this works in a second so the l of syntax is the exact same except it starts with the el so i'm going to say l if x equals equals joe and i'm going to print by joe why not so now let's run this let me just show you what happens when i go with joe it says buy joe and let's just print something random it gives us no now of course i don't need this else statement i can use an life without the else statement that's totally valid in this situation if i do something random just nothing will happen but if i put tim or joe then it will give me the valid response now let's add another lf let's say lfx equals equals i don't know let's go with sarah we can print random okay and now let's try this out and that works now of course we could add the else at the end but that is the basics for the if l if and else now of course you can chain and add stuff inside of another if statement i could put another if statement inside of this but just make sure you keep the indentation correct because that how it that is how it determines what we are going to be printing and what we are doing if this statement is true so in these next few sections i'm going to be talking about collections how we deal with collections looping over collections and all of that fun stuff but what is a collection well collection is simply an unordered or ordered group of elements now the two collections i'm going to discuss here in this section is list and tuples so if we have a list a list looks something like this it is simply square brackets now inside of the square brackets we can have a series of elements elements are simply some data type so i could have the integer 4 i could have the value true i could have some string high and these elements do not need to be the same type like in many other languages so this is a list and the way that the list works is that it can store a bunch of different elements and a list is an ordered collection which means the order in which we enter things into the collection matters and is maintained in fact it's very important in the list so how do i actually access elements in the list how do i deal with the list what are some things we can do with it well of course i can define a list by simply opening up some square brackets and then defining some elements inside i also can just leave square brackets like this empty that would give me an empty list the first thing to look at on a list is this function called len len will tell us the length of the list this also works on strings and a few other things as well but if i look at the len of x that tells me 3. now just to kind of show you here if i say y equals high and i were to look at the len of y after i look at the line of x we obviously get 2. so len is a function in python that we can use to look at the lengths of stuff all right so that is one of the things we can do on a list what we can also do is we can append things to it what a pen does is add to the end of the list so if i want to add say the element hello i can say x dot append now i can print out x and if we have a look we get four true high hello of course we added this element to the end of the list i can also extend the list by another list so let's say i have a list here and inside of this list i have 4 5 and just a bunch of other stuff well when i extend it what that will do is take all of the elements from this list and simply append each of them to the end of the list so you can imagine extend its internal implementation just loops through all of this and appends all of those elements to this list now what about removing something or popping something off of the list well i kind of already gave away what one of these things is but what pop does is remove and return the last element in the list so if i actually go ahead and print x dot pop what this will do is it will print high and it will remove high from the list and you can see that when we print x again so we get high and now our list has high remove because we popped it off of the end now another argument for pop is actually the index of what we want to remove when we look at a list every single element in the list is identified by a number that number is known as its index otherwise known as its position in the list when we start looking at indexes in a list we start counting at zero so the first position in the list is 0 the next is 1 and the last is 2. if we want to know the last index in a list we look at the length of the list and subtract 1. in this case we have 3 elements the last index would be 2 the first is 0. so if i go ahead and say x dot pop 0 what that's going to do is remove the first element from the list which is 4. so now we remove that and all of a sudden if we look at this new list the first element at position 0 index 0 is going to be true now if we simply want to access elements of the list maybe we don't want to remove them we just want to look at them we can say x put these square brackets which means we're going to access something and we can put the index of the element that we want to access in this case we'll access true there we go we print true and that is the basics now what i can also do is do something like x 0 equals and change its value to say hello now when i print x we'll get hello true hi let's have a look and there we go the reason i can do this is because lists are what's known as mutable this means this x right here does not actually store a copy to the list it stores what's known as a reference to the list and the actual items itself are stored somewhere else so for people that are more advanced and understand that i'm hoping if you guys have come from another programming language that makes sense but lists are mutable which means they can be changed i can change all of these elements and if i do something like y equals x i go ahead and i make that change to x but i don't make it to y you're going to notice that x and y will be the exact same so i have hello true high hello true high making a modification to x made a modification to y because x and y are storing a reference to this list not a copy of this list if i wanted to copy the list i would have to do this i would have to do two square brackets a colon inside we'll talk about how this works later but now notice if i made this copy that change does not apply to the other list there's a lot of other stuff that you can do with lists but those are the basics and hopefully that makes sense next i'm going to talk about tuples so tuples are similar to lists except they are immutable so a tuple uses round brackets instead of square brackets and it works pretty much the exact same except we cannot append we cannot remove and we cannot change elements so a tuple is really an immutable list you can think of it like that it means it cannot be changed once it has been defined if we want to change it we actually have to redefine it so what i can do is i can print out say x0 and that's fine that will give me the correct answer so that will give me zero but if i go ahead and do something like x 0 equals 5 and i try to do that you're going to notice we get an error because the tuple object does not support item assignment reason for that is because again it is immutable i also cannot do something like x dot append i can try but you're going to notice that again we'll get another error tuple object has no attribute append so this is a tuple they're not used a ton although they are used sometimes and just to note here you can have lists inside of lists so i can say something like you know i could have a tuple inside of a list i could have a list inside of a list inside of a list and i could have elements inside of this they don't need to be uniform size sometimes in other programming languages when you're looking at a raise if you have embedded lists every element has to be another list stuff like that just wanted to point out that you can do something like that and i think that's all i'm going to cover right now for lists and tuples all right the next topic i have for you is for loops now a for loop if you've learned in other programming languages allow us allows us to iterate a set number of times so we will be talking about a while loop the main difference between a while loop and a for loop is a while loop is running an indefined amount of times we don't know we're going based off of a condition which we'll see shortly a for loop we know how many times we're going to loop at least relatively so what i'm going to do is i'm actually going to show you how we can just create a basic for loop to print the numbers at from 1 to 10. say 4i in range and then inside of here we simply put 10. i'll talk about what this range function does in a second but let me just show you how the for loop actually operates so now i can show you that this prints 0 1 2 3 4 5 6 7 8 9 does not include 10. so the way we set up a basic for loop if we're just going to be iterating through account is we say for i or for some variable this is going to be our iterator or a counter variable whatever you'd like to call it in this is a keyword we always have for i in and then some word here in this case we're going to put range range is a function that actually creates a collection of numbers based on the input that we give it the input to range is the following start stop step so we're allowed to put up to three um and we can have less than three arguments inside of this range function essentially start stop step is saying okay what number do we want to start at what number do we stop at what number do we step at now if we only have one argument so we only pass one number by default that is the stop argument we usually start at zero so by default our range will start at zero and we will go to whatever number we put here most of the times you're just going to put one number and this defines the ending range so in this case it says okay we're going to start at 0 and we're going to go up to 10 but not include 10. notice we didn't print 10. now if we had two arguments that's going to be default start stop so we'll say okay we will start here at 1 and we'll stop at 10 but not include 10. if we add 3 we'll include a step and that tells us how much we should increment every single time by default we increment by 1 but we can increment by 2 and we can increment by a negative number as well let's actually say that we want to start at 10 and we want it to go to negative 1 and we want to step by negative 1. well let's have a look at what this looks like and just test it out okay so running you see we started at 10 right there and then we went all the way down to zero and of course we could start at negative 10 and watch what will happen here we do nothing the reason for that is we started at negative 10 we were going to negative 1 and we're stepping by negative 1. but since we were already past negative one we aren't going to go there obviously and we just immediately stop so that is kind of how the range function works so for i in range again start stop step by default if you just put one argument that's going to be the stop if you put two that's start stop you can mess around with that and see how it works but i think that's enough of an explanation the next thing we can do is actually loop through a list so let's say we have 4i in and now instead of range we'll simply just put a list we'll say 4i in range and we can do 3 4 and just a bunch of numbers here now if we print i it will simply print all of the numbers in that collection or in that list it will step through one at a time and print all of them now let's say that maybe we didn't want to do that maybe we actually wanted to keep track of what index we were at we could say x equals the list we'd say 4i in x and then um well sorry that actually would not be the right way to do we could say 4i in range the len of x and then instead of just printing i we could print x square brackets i let's have a look at this here you see how that works the way this works is we're going to go up to but not including the last index of x or the length of x sorry so in this case 1 2 3 4 5 6. so this right here will say 6. so for i in range 6 that means the maximum value of i is going to be 5. so we say x 0 x 1 x 2 3 4 5 and print all of the values out now there is another fancy thing that we can do if we want to avoid using that it's called enumerate what enumerate will do is create indexes and values for all of the elements in our list so we'd say 4i comma element in enumerate x and then what i can do is i comma element and you'll see how this works what we do is we get 0 1 2 3 4 5 and then all of the elements so this works kind of like a combination of the two for loop styles i just showed you where we actually have a range so we go from zero up to five those are all the indexes in the list and then we print all of the elements for each of those indexes in the list and we just have two variables here that denote that so the next topic to discuss is while loops now while loops are pretty straightforward essentially what they are is while condition equals equals true go ahead and do something so we're going to have some condition here before this colon and then after the colon there's going to be an indented block and whatever's in that indented block will run inside of the loop while that condition is true to illustrate this i will say i equals zero say while i is less than 10 we'll say print run and then we'll say i plus equals one this is another way that you can add one two variables so you could say i equals i plus one or you can do i plus equals one you can also do something like i multiplied equal by 1 or divided equal by 1. so multiplied by equal 2 what that's going to do is say okay multiply i by 2. we could do something like that if we wanted as well just showing you some more syntax for some interesting things that we can do in python but that's the basics of a while loop if i go ahead and run this we can see that this is going to run 10 times and there isn't really much more to talk about for the while if other than the fact that we can implement a break statement so we could also write the same code like this while true i plus equals 1 and then if i equals equals 10 break and that will simply break out of whatever the closest loop is to us imagine we had another while loop like this and we said while true inside of here then this would break this while loop not the other while loop hopefully that's making sense anyways let's move on to the slice operator so the slice operator is personally my favorite part of python there is implementations in other languages but none of them are as nice as python in my opinion and what this allows us to do essentially is to take a slice of a collection like a string or a list or a tuple and do something with it so let me just show you what i mean the slice operator is simply the square brackets like this with a sequence of colons and numbers within it the way that it works is we have a start a stop and a step just like the range function that we looked at before so if i put something like x here and then i go start stop step what i can do is say okay i want to start at zero i want to stop at index these are all indexes that we're putting in here three and i want to step by two now actually let me just stop at index 4 just so we get some more values in here but let me show you what this looks like we get the value 0 2 when i have 0 4 2 in my slice what this is saying again is start at the first index which is 0 go to the fourth index but do not include it which means stop as soon as we hit this and step by two so we're gonna start at zero then we're gonna go to two and then we're gonna go to four but since four is the stop here we're not going to include that in our slice now when we do the slice operator we don't have to include all of these different things as arguments in fact what we can do is just include a stop just include a start and all of that so just like the range function if we don't include anything so we just sorry we just include a colon like that and then we put something on the right hand side that is saying stop at whatever index we put here so if we do this it says start at the beginning and stop at the end now if we do something like 2 colon that says start at 2 and stop at the end so whenever we leave a blank we're assuming it's either the end or the beginning or one in the case of a step so if i did two colon colon maybe i did two colon four colon this is saying okay start at two stop at four and then step by one but i don't even need to include that i could just do two colon 4 but now let's say i wanted to go 4 and then colon 2 colon negative 1 what this is saying is okay start at 4 go to 2 and step by negative 1. so now what this is going to do is start at 4 which is actually this and it's going to give us 4 and then three and then it's not going to include two or any of the other ones so that's what we'll get in fact let's look at this we see we get four three now one of my favorite things with a slice operator is to reverse a list all we have to do is colon colon negative one this means start at the beginning stop at the end and then step by negative one so this will actually start stepping backwards which is kind of interesting so when we look at that we get eight seven six five four three two one zero that is an easy way to reverse a list in python now of course all this works on a string as well let me show you that with s you see we can reverse the string like that and we can also just do the standard arguments that we would typically do let maybe we'll just step by two so start at the beginning stop at the end and step by two we get hlo so that's the basics of the slice operator there's a bit more stuff that we can do with this but that is kind of how it works and i believe this works on tuples as well we can just try it out to make sure that i'm not lying to you guys but if i do that you can see that that does indeed work and we can use the slice on kind of any collection of elements all right so now we are moving on to sets which are an underrated but extremely useful data type in python now a set is simply an unordered unique collection of elements what that means is there is no duplicate elements we do not keep track of order or frequency of elements all we care about is if something is there or if something is not there the reason we do this is because a set is extremely fast to do what's called lookups removals or additions when we're talking about a list let's say we have something that you know has a bunch of elements in it if we want to say remove the first element from the list that actually involves shifting the position of all of the other elements because we're keeping track of the positions of these elements with the set since there is no notion of position adding and removing something is as simple as just adding or removing that element there's no other operations that need to be done which mean it's very fast to use so think about a set to be used only in a situation where you care if something exists or doesn't exist not about the frequency or order in which that item exists so to create a set is pretty straightforward we can say x equals set and we use the open bracket and close bracket now the reason we don't actually write something like say s equals and then this which is really the representation for a set is because what this will do is create a dictionary now technically if we want to create a set we can do something like this and this will create a set for us that initially has the elements 4 32 and 2 inside of it this is what's known as a set literal if you're not going to be creating an empty set it's totally fine to do it like this so long as you add an element inside but if you are creating an empty set then you need to use this constructor otherwise you're going to end up creating a dictionary which is not what you want to prove the simple point to you i will print out the type of just an empty you know set representation and you'll see that it gives me dict like that of course that's not what we want anyways let's have a look at this set and i'll just show you what i mean by unique unordered collection of elements so 4322 if we print that out we get 32 to 4 so obviously a different order than how we added in there and notice that the duplicate twos were removed now the set operations are pretty straightforward we can add something to a set with s dot add our set dot add i'll put five and inside of here and let's just keep printing out s there we go we can see we added in there we can also do s dot remove 5 and that will well since there's nothing in there give us a key error now if there is the element in there so we can do it like this it will remove that element for us and then there's a few other things that we can do as well for a set one of the good ones is actually checking if something is in a set what i can do is say 4 in s what this will do is simply check if the element 4 is in the set and give me a true or false in this case it is true and remember this happens in what's known as constant time which means it's just extremely fast to do this as opposed to a list so we could say 33 in s that's going to give me false now the parallel i want to make here is if i had s equals to 4 32 2 2 like that so actually let's go s2 and i said print and let's just go 2 in s2 this operation the one that's actually in the set is going to happen much much much much much faster than this one of course this isn't a big deal right now because these collections are quite small but as these get extremely large just understand that using a set is very beneficial when it comes to looking things up checking if something's in there removing and deleting there's a few other operations we can do on sets let me just define s2 and i'll just quickly show you a few of them we can do for example the intersection or the union of these sets so i can say s dot union s2 and we'll go ahead and have a look here and we can see we get the union of these two sets essentially adding them together we can do the difference of the two sets if we want we can do the intersection and we can do the symmetric difference and all that other fun stuff that you can do with sets now i'm not going to get into all that stuff just know that these are sets and that's some of the stuff that you can do with them so now i'm going to talk about dictionaries now if you're from another language what may be familiar to you here is a hash table or a map something like that they're very similar to dictionaries but a dictionary is essentially a key value pair you have some key you have a colon and then you have some value that it corresponds to now of course that value has to actually be a valid data type but it could be something like a list it could be a single value like that and the way that you access and create new key value pairs is something like this i can print out say x and then the key inside of here in this case it's actually called key and what that will give me if you look down here is simply the value 4 because well that's what corresponds to this key so you have keys and you have values now to add a new key is pretty straightforward what you can do is you can say x inside of square brackets you can define whatever key you like it can already exist if you want we can say something like key2 and we can make that equal to 5. now note that these keys do not need to be the same sorry same data type so i don't have to have two string keys i can have integer keys i can have whatever keys i want do something like x2 equals 8 and there we go you see that works perfectly fine in fact i can make this a list just to prove to you that the values don't have to be the same either and there you go now just like a set this actually works using a hash which means this is very fast to retrieve add and modify values within the dictionary in fact it's so fast you can kind of assume that it's in constant time although there is a chance there will be something known as hash collisions although i'm not going to talk about that because that's probably a little bit more advanced than what most of you know anyways what we can do here in terms of operations on the dictionaries we can check if something is in the dictionary so i can say okay is some key in the dictionary key in x there we go print that out and that says true a few other useful things that we can do is we can get all the values from the dictionary so we can say x dot values print that out and it says addict values and it gives us a list of four usually when you do this you're going to want to actually create a list out of this because that is a different data type that's returning to us and there you go we get the value 4. now similarly we can do dick.keys so x dot keys and there we go we get a list that has all of the keys in terms of iterating over or deleting values i'll first show you how you delete something to delete something we can say x and then the key that we want to remove in this case it will remove this key so let's go ahead and do that and well i guess it didn't really show you that it was deleted let's just print it out to prove that there you go and we get an empty dictionary of course like i showed you before we can use that in operator and to loop over it there's actually a few interesting ways that we can do this so i can say for a key value in x dot items and then what this will do is simply return to me all the keys and values so i can print key value and you'll see we'll get key four so if i want to loop through all the keys and all of the values i can use dot items if i don't want to do that i can just say four key and then i can say in x what this will do is simply just give me all the keys in x and if i want to access the value of course i could say x key like that and that will give me the value and we get the same thing popping up there that's kind of the basics for the dictionaries there's a few other methods as well but i'm not going to talk about those um that is a dictionary and this next one is a fun one this is comprehensions now as far as i know python is one of the only programming languages that actually has this a lot of people absolutely hate it but i personally love it and if you learn to love it you can use it quite well now to do a comprehension essentially what that is is a one line initialization of a list of a tuple of a dictionary of many other things so let me just show you what i mean i can do something like x equals a list and then i can say x for x in range five now if i go ahead and i print the value of x you're going to see what we get here let's run it like that we get zero one two three four the way this works is essentially we can actually define a for loop inside of this list and what it will do is say okay we're going to loop through as if we normally would and whatever we have on the left hand side here is actually the element that we're going to add into the list so i could do something like x plus 5 for x in range five and there we go we get five six seven eight nine ten i can also do something like you know x mod two and or x mod five there we go i can also you know grab something else i could say zero for x in range five that would give me a list of all zeros you can see how this might actually be useful now if we want to go into a more complex example i could do a list and then inside this list i could say 0 for x in range 100 for x in range five now what that's going to give us is five of these lists that each have a hundred zeros inside of them you can see that's quite large but that is kind of how that works now we can also go ahead and add some more complex stuff to this too we and say 0 for or i can say i for i in range 100 if i mod 5 equals equals 0. so essentially if if i is divisible by five then what we can do is actually add it into the list let's have a look at that and there we go we get all the increments of five so you can play around with that and see how that works but this also works for dictionaries in fact let's just do the same thing here let's make it a dictionary and now we'll just go i colon zero so we have all these keys that are initialized at zero we go there and have a look we can see that dictionary works and of course we could do the same thing for a set we can just say i for i in range blah blah and there we go now we get a set so we can actually do this for tuples as well except we're not going to do it in the way that you might assume if you just go ahead and do this this actually returns to us what's known as a generator object it's a bit more advanced for this kind of video so i'm not going to talk about that but what we can do if we want to make this work for tuple is we actually type tuple we can use the tuple constructor which is simply this and then do the comprehension inside of there now that will return to us the tuple you can see i tried to do it the other way before and that returned to me the generator object anyways that is comprehension these can be used in a whack of different things they're definitely interesting and good to know and one of the best features of python that is not in many other languages so now i'm going to start talking about functions so to define a function in python you simply use the def keyword this means define then you pick your function name in this case i'm going to pick func the function names are the same as variable names in terms of how you name them you can put some positional parameters inside of here if you want or you can leave it empty you add a colon indented block then anything inside of the indent will run when you run the function so let's go ahead and actually just run this and show you the basics of this so that is pretty much how you run and define a function of course you can define multiple functions you can actually define functions inside of functions if you wanted to this is totally valid if i wanted to do something like this and then i could call funk like that now of course this is a more advanced use case but just showing that you can do that because of the way that python works in fact functions are actually objects which means you can technically return them i'll show that in a second but let me talk about arguments first so let's say i want to add the argument x y inside of here of course that means now when i call the function i need to add some value for x and for y so let's go five six and now if i after run want to print those i can print x and i can print y let's have a quick look we get run 5 6. of course we can return something from a function as well maybe i'll return x multiplied by y now if we actually go ahead and print the value of this function what will happen is it will evaluate what is here return that to us and print that out we get the value 30. now note that when you return multiple things from a function which you can do i can return x multiplied by y and maybe x divided by y as well this will return the value to us in a tuple so if you look here we get actually two values we have i guess the x y value here and that's inside of a tuple now if we want to unpack this tuple and say separate this out into different variables we could of course index at zero and index at one but a much cleaner and faster way to do this is to do something like r1 comma r2 equals func 5 6. what this is going to do is take these two values here and simply separate them into the r1 and r2 variable now if i go ahead and i print r1 r2 and i have a look here you can see that we get 30 and then 0.83334 that is the basis for a function if you want to do a optional parameter you can do something like z equals you can set a default value i usually set it as none and what this means is that you do not have to pass that in but if you want to you may and it will override the optional value that you have here so let's just start by actually printing out zed i'm not going to call it with zed to start you can see that oops let's run that for a second we get run five six none and now if i add a z so let's just add seven there we see that instead of none we are going to get seven printing out right there that is the basics of a function and now we're going to move on to some other more advanced aspects of it all right so i'm going to talk about something that's really cool called the unpack operator and then i'm going to talk about star args and star star quarks but i first want to show you an advanced example of a function so if i say define func like this and then maybe i'll say take x and i define func 2 inside of here this is totally valid by the way you can put a function inside of another function and then inside of here what i'm actually going to do is say this function is going to print x and i'm going to return func 2 but notice i don't call it now this is just to prove to you that functions are objects hence they can be passed around just like variables so what i can actually do here is i can go ahead and i can say print or sorry not print but i can call func 4 or func 3 and then i can actually call it like this what this is doing essentially is saying okay i'm going to call this function here with the value 3. what this function does is it returns to me another function so it gives me func 2. notice it's not calling the function it's simply returning it now i'll actually just show you what i mean first before confusing anyone let's print this out before i do the call statement on it but you can see that this actually tells me this is a function object it's a func.local's func 2. so it's pretty much saying that funct2 is defined within func now if i actually call this because that's just the function reference or object it's not actually you know being evaluated if i call it like that we can go ahead and we get 3 and then we get none of course the print statement is going to print none because there is no there's nothing to print from the return of func 2 but just have a look here we get 3. now this is equivalent if i did something like this i said you know x equals func and then on the next line i just called x since x is equal to a function func two i can call it just like i showed you before and there we go we get three anyways that's the advanced aspect i wanted to show now what i want to do is show you about star args and star star quarks uh so i'm going to put in like this all right so before i define what this does what i'm going to do is just show you what the unpack operator is in python so let's say i have a list maybe we'll call it x and we just have a bunch oops of random numbers in this list so what the unpack operator does is actually separate all of the elements from a list or from a collection into individual elements the best example of this is the print statement if i print asterisk x which means unpack x what this is going to do is take all of these elements out so separate them by individual elements and pass them into the print statement as arguments so instead of just printing you know say the entire list what it's actually gonna look like is print one comma two three comma two three six blah blah you get the point that is what this is going to do so just have a look at what happens when i print that i get all of these things separated by spaces like what happened if i just pass them in individually not being in the list now look what happens when i just print x let's have a look see it actually prints the list out so what this does is unpack whatever we have in a tuple or a list or some kind of collection and sends it through as arguments to a function so that's where star args and star star quarks come in although you'll see that in one second so let's say i have you know x y that we need inside of a function we got to pass x y what this will do is simply print x and then print y now let's say i actually have maybe a bunch of pairs that i want to pass this function so maybe i say pairs equals and then i have like one two three four well what i'm going to do is do a for looping and say four pair in pairs and what i want to do is call this function with those pairs now the naive way to do this would be to go func okay pair and then pair 1. great that would work but that is not the pythonic way to do this what we can actually do is do asterix pair and what this will do is take 1 2 and take 3 4 unpack them separate them and pass them as arguments to funk so if you have a look at that that actually works and that is why the unpack operator is really useful you can also use this actually on dictionaries as well and i'll show you how it works with dictionaries a little bit more complicated it involves two asterisks what this does is if i have keys i can say x representing obviously the argument x and then make that equal to 2 and then i can say y and this could represent the argument y and i'll make that equal to 5. now if i run this this works fine i needed two asterisks because this was a dictionary and that is kind of how this works um yeah i don't i guess i really need the for loop anymore to illustrate this but the double asterisk is used for dictionaries the single asterisk is used for a tuple or for a list now i just want to make the point here that let's say these are not in the correct order so it's y and then x this will still work and that's actually why this is really useful because you don't have to have it in the correct order so long as you name the arguments as the keys so now hopefully that will explain to us how star args and star quarks work so star args and star star quarks essentially imagine you have a function you don't know how many arguments positional or keyword arguments you want to accept that's what quark stands for keyword arguments well what you can do is you can use star args and star star quarks and what that will do is allow you to pass in an unlimited amount of regular arguments and keyword arguments so if i actually go ahead and go funk and then i pass like one two three four five and maybe i'll pass some keyword arguments like you know one equals zero two equals 1. i know this doesn't really make any sense but you get the idea now if i actually run this and print it out you can see that when i print args and print quarks we're getting a tuple that has all of the positional arguments which are these and all of the keyword arguments which are 1 and two now if i wanted to actually use any of these what i can do is i can unpack them so first of all i can unpack args and what this does if we have a look here is it will print out one two three four five i could try to unpack quarks but you're going to see what happens when i do this and hopefully if you remember the last example you'll realize why this is not going to work it says 1 is an invalid keyword argument for print so what this is going to do when i unpack quarks is it's going to take all my keyword arguments and it's going to say 1 equals 0 and it's going to say 2 equals 1 and pass that to the print statement now obviously 1 and 2 well those are not valid arguments for the print statement so we're going to get an error anyways that is args and that is quarks you're going to have to look into this on your own and kind of learn about how it works but that is the basics and i just wanted to show that to you so that you're aware that you can do these cool fancy things in python this next one is going to be very fast but this is just defining scope and global so when we look at a function like this there is a notion of a scope so we have x equals tim we have defined func and then we have name as a parameter and what we're doing is we're saying okay we're going to change x to be equal to name so essentially if i was naive and i didn't know any better i would assume that what's going to happen is while this variable up here would change when i pass this name to this function any of you that program before probably know that's not going to work but if you have a look here you can see that x does not change before or after when i call func with the string changed the reason for that is that this x variable is local it is within the scope of this function which means it cannot be used access change from with from outside the same thing here with this x this one is actually global this is on you know not not defined within any scope other than this file which means well i can't just directly change it from this function i could however access it from this function i could print it here but i can't actually change its value because if i do that what's going to happen is i'm going to create a new x inside of here which is equal to name and it will be local to this function it will not modify this however there is one interesting thing we can do in python which is the global keyword if i global x now you're going to notice that the expected behavior will occur now we get tim and we get change what that is saying is okay i want to use x as a global variable so i want to reference x in the global scope never use this this is never good to use and if i ever see a python program that has it i always get upset but this is something that you can use and i feel obligated to show you because there is some very very rare situations where you may need to use that all right so now i'm going to spend about 10 seconds showing you how to actually raise an exception in python there's a keyword called raise maybe in java you've seen something like throw and raise can raise an exception if you wanted to do that so i could raise exception and then inside of here i could just say bad you know whatever i want now if i run the program as soon as we hit that line immediately i'm going to get an exception and it says raised exception bad exception bad of course that is the basics there is more arguments and things you can do with this this is a base class which means you can extend it when you get to object oriented programming make your own exceptions and make all that fancy stuff for our cases that's all we really need to know i could raise say like a file exists error any other kind of error that i want and then again add some description for it by just putting a string inside of the brackets but that's enough for that now let's show how we can actually handle exceptions all right so now that we know how to raise exceptions it makes sense to learn how to handle them so in python rather than having like a try catch we actually have a try except finally block so what i can do is do something like try that means i'm going to try to perform whatever code is inside of here maybe i'm going to get an integer division by zero error so i can do you know 7 over 0 that's going to raise an error then i can create an except block i can accept an exception as e what this is going to do is mean whatever the exception is will be stored in the variable e and then so i could print out that exception and we can continue running the program this is the basic try except block you don't actually need to define something here i can just say try accept like that if i want and also accept a specific exception so i don't have to just put exception this is general this means any exception that occurs i will actually catch you can only catch specific exceptions if you want but again that's up to you so in this case you see we get division by zero it's just printing out but it didn't actually raise or you know show us that error so it did raise it but we didn't actually see it now if i go ahead and just do this what we can do is we can say oh well that is the actual problem and now our program is going to crash and not work but if we have this except block like this of course we're good to go now there also is a notion of a finally block the final block will run no matter what usually you put clean up type operations inside of here maybe you're trying to write to a file some exception occurs and then no matter if this was successful or not you want to close the file after you would put that inside of finally so i could say print finally like that have a look here and you can see we get finally i'm not going to go through this too much i assume if you're going to be using this you understand how try accept and finally work but that is the implementation in python and that is the basics for raising and catching an error and now we're going to discuss the lambdas so lambdas are actually a really cool aspect of python i'm pretty sure they have them in many other programming languages as well but it looks something like this essentially a lambda is a one-line anonymous function what that means is not really a named function you don't define a lambda using the def keyword what you can do is something like x equals lambda x and then x plus five what this means is this lambda is going to take one argument which is x and it's going to return x plus 5 not multiplied by x do i mean to do that that means if i call x like this and i go i don't know x2 and we print this let's just go ahead and have a look at this here oops again did not mean to do that we see that we get the value 7. now i will note that this is not the advised way to use a lambda we're going to see how to use it in just a second in the next section but this is the basics of how it works it's just a one-line anonymous function so i can do something like x y as well and then i can do something like x plus y not capital y and if i go ahead and call x with i don't know 2 3 or 232 sure that's fine let's have a look here we get the value 34. so hopefully that makes sense on how the lambda works it looks intimidating but it's really not that complicated and in the next one you'll see why these are useful and now i'm going to talk to you about map and filter which are two useful functions in python which make use of the lambda functions now you don't necessarily need to use lambda function but a lot of times it comes in handy so let me show you what i mean let's make a list let's add oops i wanted to do a few more commas than that let's add a bunch of elements inside of here to make it kind of nice and long so we get some interesting output and now what i'm going to do is i'm simply going to show you how we use map so what map will do is we'll take all of the elements of a list and use a function to map them into a new list so if i say something like mp standing for you know map i can say map which is the actual function now what i do is i put a function inside of here that i want to use for the map so this will make sense in a second but i'm going to say lambda x x plus 2. so this is essentially saying add 2 to every single one of these elements or add 2 to x in this case and then what i do is simply pass x now i know x is kind of confusing so maybe we'll just change this to i so that's easier to differentiate but what this is going to do is say okay we're going to map all of the elements inside of x so in this list x to this lambda function which means take this lambda function and apply it on every single element of x and then put that into a new list now what i'm going to do is i'm going to print the list representation of mp map actually returns to us a map object we can use that if we're going to iterate over it but it's usually more useful just to convert it right into a list so let's have a look at what this does and there we go now we get three four six and you can see all these elements have been added by two now we can do a more complicated one maybe we can multiply by two you'll see more of a difference and there we go now the next function i want to show you is filter so hopefully you get the idea of how map works but filter i don't want to say it the opposite but it does something a little bit different so this function here which is lambda what it's going to do is instead of actually returning some value it's going to return true or false and it's going to tell us whether or not we should include the item in our final filtered list or filtered object so essentially this lambda function or whatever function we put inside of here has to return true or false based on the value of an item so what i could do is say something like i is i mod 2 equals equals 0. so this means you know only return it if it's even so this is the function take some value i tells us if it's equal to or sorry divisible by 0 and if it is we'll include it so let's have a look now and you can see that we only get all of the even elements in our list now of course what we can do is also define a more complicated function so we can say define func take some value let's go with i and we can do the same thing here we could say return i mod 2 equals equals 0 but we could also say you know i equals i multiplied by 3 so we can do that and then we can say i mod 2 equals equals 0. then instead of using a lambda here we could simply just write func that's totally fine so we just put the name of the function that we want to use and then we run this and it works fine but this is why lambda is useful because a lot of times you don't want to define your own function up here you just want to define it right inside of the map statement or the filter statement so that's kind of where you would use this there's another few use cases for it but this is kind of the most common and where you'll often see it so anyways that is map and filter and for our last and final topic i'm going to be covering something called f strings now f strings are new in python 3.6 if you have any version lower than that you are not going to be able to use f strings but they're a really cool way to actually just manipulate and create strings so what i can do is do something like x equals i can do an uh lowercase f or a capital f it doesn't matter and then a string notice that this will highlight or maybe it'll be a towel size depending on what editor you're using and well what you can do is type a string as you normally would except now if you want to embed an expression what you can do is do that inside of curly brackets so i can do hello and then i could say like you know six plus eight and this will actually be evaluated and now when i print this out it will give me the answer 14. so this is great because if you have some variables say like i have you know i don't know tim equals 89 or something and i want to embed that in the string without having to concatenate it and convert it into a string with str i can simply just use the f string and just embed it with all the formatting uh by putting it inside of these curly braid braces so that is kind of how that works you can go ahead and mess around with this if you want to really see how it works but essentially you just put an expression inside of these curly braces it will be evaluated and then it will turn into a string really great easy way to work with strings of course you can print an f string as well i could print hello and then we can do that and in fact let's run this and just see what we get and there we go we see we get hello 89 so that is f strings so with that being said i'm going to conclude the video here i am officially out of breath i went very fast through this tutorial with the purpose of making it as quick as possible and not wasting any of your time if you guys appreciate this effort please do leave a like subscribe to the channel and of course let me know what you want to see next i will quickly mention that i did miss a few things in this video specifically object oriented programming and advanced language features if you would like to know about those two things i have a ton of resources related to them i will leave them in the description the first is a about an hour long video on object oriented programming in python that covers kind of all the fundamentals and everything you need to know the next one is a whole series on expert level features in python i believe that's about six videos and covers things like meta classes context managers generators all of that anyways as i said i hope you enjoy leave a like subscribe i will see you in the next one [Music] you
Info
Channel: Tech With Tim
Views: 641,202
Rating: undefined out of 5
Keywords: tech with tim, python, python as fast as possible, python speed course, speed course python, python tutorial, python programming, python tutorial tech with tim, tech with tim python, tech with tim python beginner, python speed tutorial, python overview, python summary
Id: VchuKL44s6E
Channel Id: undefined
Length: 79min 41sec (4781 seconds)
Published: Sun Oct 04 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.