5 Uncommon Python Features I Love

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 in this video I'm going to be sharing with you five uncommon python tricks SL features that I really love to use in Python starting with trick number one and for this example I'm going to have two iterables one which is going to be some numbers of type list of integer and the other one which is just going to be a string containing hello world now usually if you want to slice an ital you would pass in that ital and you would also use the slice notation such as colon colon minus one to reverse that ital and that will also work for the text and right now if we were to run this it would work perfectly fine it would reverse those intervals but now imagine you want to use this in many other places as programmers we don't really want to copy and paste that everywhere because maybe we will want to change this implementation later and of course changing it in each place is a bad idea so my first trick SL tip is to create a slice object and you can do that by using the builtin slice and passing in that same notation so right now we have nothing nothing minus one which means from nothing to nothing give it a minus one step and that will give it this syntax and then we can pass in rev and rev here and it will give us the exact same result except this time we can actually change the implementation in one place so we can also say minus two if that's what we want and this time it's going to reverse it with a step of two otherwise we can create another example which is the first five and I'm going to call that F5 and that will be of type slice and we will create this slice here that grabs everything from the beginning to the index of five and now if we were to replace this with F5 and this one below with F5 you'll notice that we will only get the first five elements back so slice objects can actually be very useful moving on to trick number two suppose you have two sets and that you want to perform some basic operations on these sets well python gives us the chance to actually use a lot of the built-in operators to perform the same operations that we can get with DOT notation such as difference and intersection but the first thing I want to show you is how you can combine two sets and to do so all you need to do is type in your first set and then use a pipeline with the second set and this will combine these two sets so if we were to run this we would get a set that combined these elements and of course since it can't contain duplicates four and five will not be repeated otherwise we can also subtract Elements by using the minus now if we were to subtract set B from set a you'll see that we will only get 1 two and three back because here we had four and five in set B which means means it took away four and five from set a and keep in mind that order here does matter so if we put set B minus set a we will get 8 six and seven back because four and five were the only elements found in set B that were present in set a so those were the only ones it could subtract now moving on to something more Niche if you use the ampasant this operation is going to give us back all the elements that are shared in other words the intersection of these two sets so if we were to run this you'll see that four and five are present in both of these sets otherwise we can also get the unique elements from both of these sets by using an up Arrow so now if we run this you'll see that we will get only the unique elements and this is also known as the symmetric difference trick number three suppose you have some sort of class such as a book that contains a title of type string and the amount of pages it has of type integer now what I'm going to show you how to do is how to make this class compatible with FST strings and to be more specific I'm going to show you how you can specify your very own f-string specifiers and to do this we need to use a dander method called format and it takes a format specifier of type any and what it returns to us is a string and I personally love to annotate this stuff because it gives me a clear picture of what this actually does without these annotations I personally get really confused so you're not required to add these but I highly encourage you do anyway what we're going to do inside here is use match and provide the format specifier so depending on what the user inserts as a format specifier we're going to return a string based on what they have provided so here we can say case for time and what we're going to return here is the self. pages and I actually want to return an F string so f with the self. pages divided by 60 and that will be formatted to 2f which will round it to a float with only two decimals and out of that I'm going to add an H so this will be the total time it takes to read the book and I'm just going to pretend that 60 pages per hour is a decent time but another case we're going to provide is a case for caps and all we will return here is self. tile. uper and these are just some examples you can return whatever kind of string you want don't take this literally there might not really be an advantage for returning self. title here it's just an example otherwise for every other scenario I'm going to raise a value error that we provided an unknown specifier unknown specifier for book but with that being done we can now use this comfortable in FST strings and to demonstrate it I'm going to create my if name is equal to Main Check which creates both a main entry point and inserts the if name is equal to main check for me then with that I can create two books one is going to be Harry Potter of type book and the book is going to equal very Harry Potter of 300 Pages then the second book will be python daily of type book which will equal a book with the title of python daily and this will be 20 pages and the first book I'm going to print to the console will be Harry Potter so format Harry Potter and here we can provide one of our specifiers such as caps then I'm going to duplicate this and call the second one read time or give it the text of read time because here we're going to insert time now the next time we run this you'll see that providing those format specifies we can get our object formatted in a special way directly in our FST string and again this was just for demonstration purposes because obviously you can easily just type in Upper in or actually no you can't never mind because what I meant to type is Harry potter. tile. uper and that might just be more readable to begin with but what's nice about this is we can actually insert any book object such as python daily and it will work exactly the same way the downside to this approach is that who in the world is ever going to understand which format specifiers work with your f-strings without reading some extensive documentation so I would love to hear if any of you actually use this in Python I personally love playing around with this in personal projects but if anyone else had to actually use my project I think this would be incredibly unintuitive up next we have the walrus operator and I personally love using it I I know a lot of people say it's quite confusing but I hope these examples will help you understand it better so it doesn't appear so confusing anymore and the very first thing I'm going to do is create a dictionary of users and for the key I'm using an integer and as the value just the string of the name of the user now imagine you want to create some functionality based on whether a user exists or not one approach you can take is creating a new variable called user of typ string or none and typing in users doget and passing in the key of the user that you want to get so with that we can easily check if user print user exists else print no user or boy whatever no user found and this works quite fine I mean it does exactly what we wanted to it checks if a user exists or not and if it exists it's going to say that the user exists otherwise it will say that no user was found and we can verify that by putting an ID that exists and you'll see that Mario exists but now let's save one line of Code by using the wallrus operator so here we can type in if user walrus operator is equal to users doget with the value of three then we're going to perform the same operation so just like that we can completely move that line of code and perform the exact same operation and the way the walrus operator works is that it first evaluates the expression and then assigns it to the value before performing the check so if this returns none user will have the value of none if this returns a value user will have that value so now if we run this we're going to find out that there was still no user found with the key of three but if we insert zero it's going to say that Bob exists so not only is it going going to get that value it's also going to assign it to user directly in the if statement and that was one simple example of where it can be useful but another example is in functions and classes for example here we can have a function called get info and that will take some text of type string and will return to us a dictionary then right below what we're going to return is this dictionary starting with the words because what we want to do is get the info regarding the text that we insert so first I just want to split the words and inside here I'm going to create some parentheses because if you want to use the wallers operator inside a dictionary for example you're going to have to use parentheses to not confuse python so here we can type in words wallers operator and then text. spit so we're creating this variable within the dictionary which means we can also use it on a different line for example if we want to create word count we can do that and then we can say that the word count is the length of the words so now we're using that variable directly under rather than calling text. spit twice and finally we can use it one more time below by saying character count and that will be the length of an empty string do join and we will join those words because I don't want to count the empty spaces so it was quite neat that we could just create the variable within but now we can print get info with the text of Bob and if we were to run this we' find out that there's only one word which is Bob and the word count is one with a character count of three we can also add different sentences such as hello Bob and my name is Bob and with that being done we will get all of this information back quite easily and as for the final trick that I want to show you in this video what we're going to be jumping into is a concept called currying and if you're a professional python developer you're probably going to bring up partials but I'm not going to be covering partials in this lesson I'm going to show you something that you can do quite easily which we call currying and to demonstrate how this works we're going to create a function called multiply setup and that's going to take one argument or one parameter and what this will return to us is a callable and you'll see in just a moment why we're returning a cable but within it we're going to create a function called multiply so a nested function that takes another float which we will name B and returns to us a float because what we want to do here is multiply two numbers but inside the inner function we're going to return the actual operation which is a Time B while outside of it we're going to return the multiply function so why did I do this why did I set up a circus in Python well while it might look like a joke this can actually be seen as an ideal approach for functions that you need to call with the same setup over and over for example let's create a function called double which is going to be of type cable and here we're going to provide the multiply setup and since all we want this function to do is double numbers we're going to insert two then we will duplicate this and create one that triples numbers and that will take the setup of three so what we did on both of these lines is create two cbles using our custom setup this just means that we can now refer to the variable name of double as a function and all we need to do is insert only one number instead of two so here we can type in five we can type in three and we can type in even 100 and each time we call this function it's going to double the number because we already provided that setup and essentially all this is saving us is the trouble from typing in two over and over again on the first line and once again you can also do this with partials if your functions get really complicated I recommend you look into partials but if you have some very simple setup such as the one I showed you carrying your function might just make much more sense and this also works with triple so we can just place all of these and we will be able to run all of those without having to provide the value of three each time so this is just something else I learned incredibly late when I was learning python but I think it's a really cool concept that you guys might use in the future maybe you'll never use it but it's definitely something to keep in mind or at least the theory of how it works if you really want to create some disgusting code you can even experiment this approach using Lamas but otherwise that's actually all I wanted to cover in today's video do let me know in the comment section down below if you learn something new or if you have something you want to add regarding this video but otherwise as always thanks for watching and I'll see you in the next video
Info
Channel: Indently
Views: 137,696
Rating: undefined out of 5
Keywords: pyton, pyhton, pythn
Id: sQ1Q96-Vhjk
Channel Id: undefined
Length: 15min 9sec (909 seconds)
Published: Tue Feb 27 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.