Python As Fast as Possible - Learn Python in 75 Minutes

Show video

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. 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

2020-10-09

Show video