All 39 Python Keywords Explained

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
this video was brought to you by IND dentle IO learning python Made Simple how's it going everyone in today's video we're going to be exploring all 39 keywords that you can find in Python and just to show you where I'm getting this number of 39 from I'm going to paste in the following code and all it does is imports from keyword the keyword list and the soft keyword list because I'm also going to be including the soft keywords then I have a function that Loops through each one of these and displays the current keyword with its current position and when we run this script what we should get back are all of these keywords as you can see it's enumerated quite nicely and it goes all the way down to 35 plus four soft keywords and it's important to mention that we have 39 as of python 3.12 because prior to python 3.12 type was not considered a soft keyword so on previous versions this would give 38 and of course case and match are also recent additions so you might not have those depending on which python version you are running so yeah I'm going to be showing you all of the keywords that are present in Python 3.12 starting off with number one which is the false keyword and false is one of the two states we can have in Python the other one being true and it's practically a constant for zero so here I have a Boolean called has money which is set to false and then I'm going to print it and to demonstrate that it just just contains the value of zero I'm going to print the integer version of false so that when we run this script in the console you'll see that we will get false back and that the integer of false is equal to zero moving on to number two none none represents an absence of a value there's no value if none is specified so sometimes from the internet you might get some data back and that data might be empty or it might actually contain absolutely nothing and in that casee it's going to give you none and when you print none what you get back is none another example might be that you're trying to select a user in a database and the user you selected does not exist so in Python in general you're either going to get an exception or you're going to get none back which again represents an absence of a value so here we were not able to select a user because it did not exist and that's why we gave it the value of none and if we were to print this selected user we' get none as an output once again keyword number three true true is another constant for the integer value of one all it does is represent something being true for example if I have something such as has money I can choose to set that to true or false to show that I have money or that I don't have money and when we print that to the console what we're going to get back is that Boolean and again since these are just constants for the integer values you can do do something silly such as true plus true and that will work just fine you will get two as a response or as a return because true is just a constant for one keyword number four anytime you have some sort of if check or Boolean comparison you can choose to use and to check that two or more conditions return true and everything inside this check must return true for this to pass for example here we're checking that c is more than b and that c is more than a both of these Expressions have to be true for this print statement to get executed and an easier to understand example might be if you have a name of Bob and an age of 19 and what you want to do here is check that this user is eligible to sign up for your website so of course you will want to make sure that their name has more than zero characters and that their age is more than 18 if both of these conditions are satisfied this code will be executed and this was possible thanks to ant as you can see if we were to run this the user is eligible to sign up because the length of the name is more than zero and the age is more than 18 moving on to keyword number five as as is used to give an alias to a module for example if you're importing math you can import it as M to keep it short so now we don't have to explicitly refer to math each time we want to use the math module we can just type in m and use do notation to use use any of its methods such as cosine and pass in 10 and this also works with specific functionality such as from random we can import the random integer as r i for random integer now we can directly refer to that by typing in r i and passing in 10 and 20 so this will generate a random integer in the range of 10 to 20 both end points being inclusive and another place you'll see the as keyword being used is with context managers for example if you're trying to open a text file you might want to give it an alias such as file when you are working with it or when the file is actually open so here we can type in with open text as file and then work with that file inside here keyword number six assert very often when you're creating a program you're going to have some minimum requirements for what is needed for your program to actually run for example maybe your program relies on a database that is built in to your device if this database is missing at any point in the program then your program was doomed from the start and shouldn't run no matter what so one thing you can do inside your program is use the assert keyword and assert just makes sure that whatever condition you insert is satisfied before moving on otherwise it's going to raise an assertion error so in this example we have a database and we have provided that database so this database should always be here and if we were to run this it will run just fine because we have that database but at any point if that database was to disappear then the next time we were to run our program we would get an assertion error that we cannot run the program without the database and once again you can use the assert keyword with any kind of Boolean expression so we can even perform a check such as this one and here I have a variable called limit which is of type integer and a number which I just called n so what we want to do here is check that the number of n is under the limit and as long as that's true our program is going to work just fine it's going to be able to move on with the code but as soon as this becomes false for example if we change this to 20 it's going to raise an assertion error keyword number seven and for this keyword we're going to have to import a built-in module called async IO and that's because in Python we use async iio to perform asynchronous tasks and this is an example of what it can look like in its bare form here I have a function called main which I want to execute asynchronously so in front of Def I included the async keyword and since this is asynchronous code we need to run it in an event Loop and that's why I imported async iio and asynchronous programming is a huge topic so I'm not going to explain it here moving on to keyword number eight await and await is is actually a keyword that we use with asynchronous programming or with async iio so in this example I'm going to import async iio and from async IO I'm going to import the future type then I'm going to create this async function called my task and it takes a number as an integer and this is just going to simulate that we're starting a task and that we are returning some data but as you can see immediately we have an await keyword inside here and all that's saying is that we want to wait for this to finish before moving on then in my main entry point I've created some tasks which is going to return a future and I'm going to start all of these tasks asynchronously and once again before we actually print the results of these tasks we're going to want to await them so that just makes sure that we actually wait for the results before printing them because it doesn't really make sense to print results that don't exist just yet and then once again we need to run it in a main Loop but now if we were to run this it's going to start all the tasks asynchronously and as soon as we get the results back it's going to await them and return them keyword number nine break whenever you're in a loop such as a for Loop and you want to exit out of it prematurely then you should use the break keyword in this example we have a for Loop that Loops through the range of 10 and if I is equal to 5 it's going to break out of this Loop which means it's not going to continue any further with this Loop otherwise it's going to print I so by providing that condition with break you'll see that we're not going to count past four keyword number 10 class anytime you want to create a class in Python you're going to want to use the class keyword for example here I have a class called person and all this does is provide a blueprint for how our person should look like so our person should contain a name and all our person can do is work because life is miserable keyword number 11 continue suppose you have some names which are a list of typ string and that it contains Tom Bob and James and next suppose you want to Loop through these names so for name in names and say hello to everyone besides Bob so the way we're going to do this here is check if name is equal to Bob print that we do not say hello to Bob he does not deserve it and then we can add the continue keyword and what continue does here is say okay we run into this ignore the rest of the code just go to the next iteration so print hello will not be executed for Bob because continue will force the for Loop to skip to the next iteration so if we were to run this it's going to end up looking like this hello Tom we do not say hello to Bob and hello James as you can see this statement was ignored as soon as we ran into continue now before we continue with this video I just want to mention very briefly that if you are interested in developing your python skills so that you can start using them more professionally then check out IND dentle io on my website I provide plenty of Premium courses which are designed to help you achieve a much more professional level of programming much faster so if that interests you go to indent IO select a course and start learning continuing with with keyword number 12 def which can also be referred to as definition or Define usually I read it as Define because here we're defining a function name to perform the following actions in other programming languages you might have something such as Funk or even FN but in Python we use def keyword number 13 delete in Python if you ever have some sort of object that you don't want anymore fix example this name and looking at this I think I was pretty higher something to give my name an integer with the value of 10 anyway that's not the point if you have some object that you created and you want to delete you can do that using the delete keyword so here I create this random variable called name with the value of 10 and later on I delete it so if we were to run this the first time we're going to get the value back but the second time we refer to it we're going to get this name error up next next we have keyword number 14 El if or else if and L if is used as the middle layer in if else checks for example you might be checking for some weather so right now we have the weather which is rainy and the first if statement is going to check if the weather is raining but of course we might have other conditions to check for so instead of just checking if it is raining we can also check else if the weather is cloudy but in Python we refer to it as L if and we can use as many of these as we like so now we can check whether the weather is cloudy and whether the weather is sunny that was a very difficult sentence but while we're here let's also talk about keyword number 15 which is the else keyword and here else is only going to execute this code if none of these conditions are met keyword number 16 except in this example I created some dangerous code which always raises an error and then below I try to run this dangerous code which obviously will not work because it always raises a value error but in Python we can catch exceptions using the accept keyword so now we're accepting this value error as e so each time we run into this value error it's going to print yo you messed up with the value of that value error as you can see when we run this it's going to say yo you messed up bad value and while we're here we can move move on to keyword number 17 which is the finally keyword and finally runs no matter what happens in the try and accept block so even if you return a value here or you raise another exception here this block will always be executed moving on to keyword number 18 which is the four keyword and for this example I created some elements which are a list of type any and they contain these random Val values and then I'm using the four keyword to say that for each one of these elements we're going to do the following so four is used for for loops and iterations keyword number 19 from sometimes in Python you're going to want to import some very specific functionality and to do so you're going to have to use the from keyword for example if you want to import random integer from random you're going to have to type in from random import random int and just like that we're going to be able to use that functionality directly keyword number 20 global global is used to refer to objects in the global namespace for example here we have a name of Bob which we defined in the global scope now to actually make sure that we're referring to this name here we're going to have to say Global name inside our function otherwise without that we're going to create a new name called James inside here because we are not referring to the name in the global scope we're creating a whole new name in the local scope so Global just allows us to refer to the correct object in the global Scope when we are working with functions and classes and anything that has been encapsulated as you can see if we were to run this we would get James as an output because here we defined the name to be Bob and then we referred to Bob in the global scope and we changed that to James and after we called this function we could see that name was a effectively changed to James keyword number 21 if and we've already covered else and L if so I guess logically we're going backwards and the next one we have to cover is if which is used for bullan expressions and all it does is tell python to check whether this condition is true or false and if it's true it's going to execute the following block so right now we have an age of 137 and we're checking where the age is more than 100 and if it is it's going to execute wow good job mate keyword number 22 import and again we're working backwards because we've already used import a lot in this video but import is just used to import packages and modules for example we can import random and then we can print something such as random. random and that's going to give us a random float pack up next we have have keyword number 23 in and there are actually two places we can use this the first one being in a for Loop so every time you want to check that a certain name or a certain element is in an iterable you can do that using the in keyword so this will Loop through all the names and for each iteration it's going to give the current element the alas of name but we can also use n to perform membership checks for example we can check if Bob is inside the names and if Bob is inside the names we can write that we found Bob moving on to keyword number 24 is and to quickly demonstrate what it is I've created this class of type fruit it doesn't contain anything but it will be used for this example and right below I create an instance of this fruit and I'm calling it apple and then I create a second variable called other Apple which just points to the original Apple and the reason I'm doing this is that show you what is does so next I'm going to print that apple is the other apple and this is going to return to us true because both of these point to the exact same location in memory and in other words they are the exact same object otherwise if we were to create another fruit and we were to call this fruit banana you would notice that we're going to get false back because they are not the same object and it's important to note that is does not compare the value it compares the memory address and to get an idea of what it's comparing I'm going to print the ID of each one of these objects and when we run that you'll see that these two are going to return true because they share the exact same ID while banana does not so obviously it's going to return false because this ID is not equal to this ID keyword number 25 Lambda and a Lambda is what we refer to as a nameless function and here's a bad example of what it looks like and theas this is a bad example is because in general you would not assign it to a variable or at least you wouldn't do it that often but as you can see here we have a Lambda that takes X as an input or as an argument and then that uses X in that one liner and since we assigned this to P we can now use it as a function but there's nothing that will stop us from using it directly inside parentheses so we can do Lambda and then insert the argument here because this part here creates that function in in place and a better example would be if we had a function that takes a function as an argument because then we can just place in a Lambda inside here instead of creating a whole fresh function but for this example I have a function that prints the results of whatever this function does to these elements so for each element in the elements we're going to call that function on the current element and the only reason I did this is to show you that we can use a Lambda here so instead of creating a function and insert inserting that function here we can directly say that Lambda x with x * 2 because we want to multiply each one of the elements by two and the elements are just going to equal this list of 1 2 3 and four and if we were to run this we will get each number doubled and it's quite nice if we don't expect to use this functionality more than once keyword number 26 nonlocal for this example I created a function and inside the function I created an item of type string that contains the text of candle but right below that I created an inner function and once again just like with the global keyword the only way to refer to the candle or this item in the oscope is to use one of these special keywords so here we're using the non-local keyword to tell python that we're referring to this candle here and not referring to the one that we're going to create in the local scope and this is useful because if we want to do something else like create another item inside here if we were to type in item is equal to Apple this would create an apple in the local scope and it would not refer to the one from the outside but if we want to make sure that we're referring to the one from the outer scope we're going to have to use the nonlocal keyword and say non-local item and then we will easily be able to change it to Apple and whether this is a good practice or not with the global and the non-local keyword is a a whole other discussion so make sure to do some extensive research on those subjects when you have the time moving on to keyword number 27 not in this example I have some names of type list of string which are equal to Bob Tom James and Jeff and what we want to do next is check whether Bob is not in the names so that's a very simple way to negate what we did originally when we check that Bob was in the names we can do the exact opposite by adding the not keyword so if Bob is not in the names we can print that Bob is lost another use case of using not is with optionals or with the non type for example here we have an optional called selected user which is of type string or none and the reason this is an optional is because the user might have selected a user or might not have selected a user so if the selected user is not none that means means the user exists and with that we can print that you have selected the selected user otherwise it's going to print that there is no user selected and this was the proper way to check whether something is not none we are not supposed to use the comparison operators here python will not be happy with that keyword number 28 or so here we're going to have three variables one called a one called B and one called C and they're going to have these respective I values now imagine you want to check for multiple conditions such as whether C is more than a or a is equal to B by using this or keyword we're telling python that only one of these conditions have to be satisfied for this line to be executed if both of these fail this will not be executed but right now this will succeed because C is more than a 20 is more than 5 even if a is not equal to B this returns true and this returns false but all once again only checks that one of these conditions are true so obviously this is going to work otherwise once again if both of these statements or Expressions Return To Us false it's going to return false keyword number 29 pass in Python if you need to define a placeholder for a function or a class or for a for Loop you can do so by passing in pass the reason we have this is because python does not allow us to insert nothing when creating a function it doesn't Tru understand what we're doing next so here we are obliged to either pass in pass or an ellipses and I often opt in for the ellipses but it's really just a matter of preference I mean one micro argument for using ellipses is that it's literally three dots which you can type out much faster than pass but that's a really silly argument so just pick the one you prefer to use and the one that's more readable for yourself keyword number 30 raise in Python we use the raise keyword to manually raise an exception such as this one over here by using the raise keyword we can tell python that hey on this line we want to raise that exception and it will do so up next we have keyword number 31 and this is the return keyword and return is very useful for returning a value from a function because sometimes you're going to want to get a value back when you create a function for example here I have a function called get time and with this name one would probably expect to get a value back and that's exactly what we're going to do we're going to specify that we're going to return a string and then we're going to get that date and time and return it as a string using this return keyword but something you should know is that all functions by default return none even if you don't see it written anywhere python is going to do this for you but here we are returning the current date and time and when we print that we should get something like this back keyword number 32 try and we already covered accept and raise so now it's time we cover what try does and in Python we use try to run dangerous code or code that might raise an exception for example here we are trying to calculate a result and the result will depend on this calculation here here which is 1 million / 0 and obviously it's going to give us back a zero division error but we were able to run this code which will obviously fail inside the tri block so that in case it raised an exception it would not crash our program if we handled it but if we were to run this you'll see that trying to run this code will give us back the message of go home Bob you're drunk and you can insert any kind of code you want but usually you'll insert code that possibly raises an exception such as converting some user input to a float because if they were to enter the number of 10 python doesn't really know how to handle that so this would be the perfect code to insert inside a tri block moving on to keyword number 33 while and while is used for while loops and in this example I created a connection or something that simulates a connection so first imported time then I created a Boolean of is connected and I created a variable called I which contains the value of one now while we are connected I want to print that we have been connected for that amount of seconds and then sleep for 1 second and on each iteration I'm going to increment I by one because otherwise we're going to have this Loop executed forever but just to simulate that our connection eventually fails I decided to specify an if block which which checks whether I is equal to 3 and if it is it cancels the connection but now if we were to run this it's going to say connected for 1 second 2 seconds 3 seconds and then the connection will have been lost because is connected eventually evaluated to false otherwise while is also really good for creating infinite loops and you can do so by typing in while true keyword number 34 when python was first created we had to go through a lot of trouble and when I say we I mean the experts from the past because I just started using python about 4 years ago but back in the past like very far back in the past we had to go through a lot of trouble to work with context managers we would have to do something such as open a file and then we would have to make sure to close that file and in case anything went wrong we would have to use the finally block to make sure that everything was closed nicely but we live in the future and we have good functionality for that that makes it much easier to work with context managers for example imagine you have a text file that you want to open well in Python we can use the wi block to do that so with open this text file in read mode as file do the following and the benefit of using the with block here is that it will always close the file so we don't have to create any complicated try accept and finally blocks it handles it all within this block so that's a lot of free functionality and if we were to run this we will get back whatever is inside the text file that I created keyword number 35 yield in Python we use the yield keyword to create generators so if we were to create a generator such as generate numbers that takes a limit and returns to us a generator we would have to use the yield keyword to specify or to tell python that this is actually a generator and we can use this generator by doing the following so here I'm going to create some numbers of type generator and that's going to generate the numbers with a limit of 10 now each time we want to grab a number from the generator we can use the built-in next method so we can do that once twice maybe three times and for the rest of the numbers I'm just going to convert it to a list and it's important to note that generators are exhaustive which means that once you yield a value it disappears from that generator and that's why we get this crazy output because each time we retrieve a value from our generator it disappears from that generator so calling list on whatever remains will give us back whatever we haven't used just yet moving on to the soft keywords and the very soft keyword we're going to cover is the underscore and underscore has many use cases so I'm not going to be able to cover all of them here but it's just important to know that in some cases it's going to be considered a keyword and in other cases you can just use it as a variable name such as that in general you would use it as a variable name name if it is a throwaway variable and a throwaway variable is just something that you don't expect to refer to for example here we want to print hello three times using a for Loop but in this scenario we do not care about the actual value that's inside the range so it's the perfect place to Define an underscore but one place where the underscore actually becomes very useful is when you define a Dunder method in a class as you can see to define a Dunder method we need to l reading underscores and two trailing underscores and python uses those to Define Thunder methods and there are plenty of other cases for using the underscore in Python another case is literally for the case keyword and yes I worked hard to set that transition up so now we're moving on to soft keyword number 37 which is case itself and case is used in match case statements and for this example we're going back to the weather so right now it is raining and what we want to do is check what the weather variable contains in the case of rain we're going to print not again in case it's cloudy I recommend you use some Cloud cream otherwise for all the other cases it's going to print unknown weather damn you global warming shakes Fist and as you can see once again we have the underscore being used as a soft keyword but let's move on to soft keyword number 38 and soft keyword number 38 is the match keyword and here I changed our example just a bit because I want to show you the power of pattern matching in Python but in this case instead of just checking for the weather we're also going to be checking for its severity so once again we're using the match soft keyword to check for the value of weather and in the case that it is rain with a severity of less than or equal to 5 it's going to print rain with that severity now if it has the cas of rain with a severity of more than five it's going to print very hard rain with that severity and for cloudy it doesn't really matter what severity it is it's just going to print that severity but just to show you how it works right now we have rain with a severity of six so what we're going to get back is a very hard rain with a severity of six now if we were to put Cloudy with a severity of over 9,000 we will get our cloudy print message back which is cloud cream anyone with a very High severity and finally as of python 3.12 type has become a soft keyword which means we can now use it to Define custom types such as this type of int or string and with this new type we can use it with our variables to check whether it is an integer or a string but if it's anything else it's going to give us a warning back and static type Checkers are not going to be happy about this and very briefly I just want to mention that a soft keyword is something that you can both use as a variable name and as functionality that is built into python so we can use match as a variable name or we can use match in match case so we can actually match match if I understand correctly which is incredibly confusing so don't do that but we can match match and then we can use case and do whatever we want so case number is 10 and this will work just fine if if you want to make this really confusing you can even do case is equal to 10 and put Case Case as ridiculous as this might sound it works because you can use it both as a keyword and as a variable name but anyways those were the 39 keywords and soft keywords that we have in Python I really hope you guys enjoyed this video it took a lot of work I thought it was going to be maybe a 10-minute video but it ended up taking maybe an hour and a half to record so now I'm going to edit all of that and of of course I would love to hear your thoughts in the comment section down below if you have any otherwise as always thanks for watching and I'll see you in the next video
Info
Channel: Indently
Views: 130,717
Rating: undefined out of 5
Keywords: pyton, pyhton, pythn
Id: rKk8XPLysj8
Channel Id: undefined
Length: 34min 8sec (2048 seconds)
Published: Mon Apr 15 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.