5 Really Cool Python Functions

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 five really cool python functions that come with python that was a redundant statement but that's what we're going to be doing and these are just functions that I really enjoyed using over the years so I hope you learned something new today so starting with the first very cool function we're going to be exploring the exec function which comes built into Python and this is probably the most powerful function you have in Python and that's because with exec we can actually execute any string as code so here we can insert a snippet such as print let's say hello and that's my fault for using single quotation marks inside single quotation marks so hello Bob exclamation mark and exec or execute will take care of executing this snippet of code and if we were to run this program you'll see in the console that we will get hello Bob what makes this really cool is that you can actually import a text file or you can download something from the internet in the form of text or as a string and you can execute it later using the exx function so here we have some text and it contains all of this random code and all we need to do to execute it is use this exec function so we're going to execute this text and it's important that you indent it properly other otherwise it's not going to be able to read the text file or what I meant to say is that you need to write it the same way you would write your regular scripts cuz all it's doing is executing python code so indentation does matter and if we were to run that it's going to execute that entire string so for example what I did in this string is print hello Bob and then I created a range and I iterated through that range printing each one of those iterations and then I used some other print statements so that gave us the output of I am code being executed from a string and it's very important to stress that you should never use xec with files or text that you do not trust because if you were to use this on some sort of website or online project people might be able to retrieve sensitive information from your computer or they might even be able to execute random commands on your computer and you do not want that but really there's just no limit to what you can do with this function if you want to take some user input and you want to execute the code that the user inputs you can do that you can type in something such as print hello Bob because I'm extra creative and if we were to tap on enter it will execute that snippet of code so something really cool you could do with this and that's some and this is something I've always wanted to do but never got around to actually doing is creating some sort of image recognition program where you can practically let's say you know write code by hand and it's going to recognize your handwriting and then once it pass passes that to text you can just insert it into something such as exec and just like that you will have a handwriting to code converter which would be really cool just imagine writing code in a diary or something and that might not really be a really good real world example but that's something I've always wanted to do moving on to function number two and for this function we're going to import Funk tools or actually from Funk tools we're going to import the partial and partial is just a very easy way of currying our functions if you ever have some arguments that you want to reuse over and over I recommend you use this partial so let me show you how it works by creating a sample function and bear with me I don't always create the best examples but this should get the point across so here we have a function called specifications and this could be specifications for a car or for a product and each time you use this method or this function you're going to have to specify a caller a name and an amount but in some cases if you're using a function a lot providing the same arguments over and over can become quite redundant and quite annoying sometimes you're just going to want to set up a function so that you can use it with a single argument and instead of creating a whole new function it can be much better just to Curry it or to create a partial from it for example suppose you want to call this function and here we want to call it with something such as red Bob that doesn't have to be in capital letters and let's say 10 times or the amount will be set to 10 and imagine the next time you want to do this you want to do it five times and the next time maybe 20 times the fact that we have to repeat this is quite annoying especially if we know that we're always going to specify the color to be red and the name to be Bob so what we're going to do instead is create some partial functions and to get started I'm going to create something called Cola and name SPC X and that's going to be of type partial and here we can actually partial that function by first inserting the function so here we have specifications without the parenthesis and then we need to pass in the arguments and the keyword arguments so to get started with this function we're always going to want to specify the cola and the name but the amount is always going to be fixed so here we can type in amount equals 10 and now with this being done we can actually call our col and name specs and all we need to do is pass in red and Bob and the amount will be provided from now on WE practically created a new function that always provides the amount and if we were to run this you'll see that we will get these specifications with the color being set to Red the name set to Bob and the amount set to 10 and it doesn't matter what we call with this function we can also change it to Blue and we can say this is a cat and the amount will always be set to 10 otherwise we can can do something else such as amount specs and when it change it name to something better such as specify amount which makes it more clear that we want to specify the amount I'm going to remove this and this is going to be of type partial which is going to equal a partial function with our specifications and what we need to specify are the first two arguments because here we only want to specify the amount so for example blue and Bob and this will fill the first two arguments so you just have to provide it the way you see it here we have the specifications so the first argument is color blue and the second argument is name so we put in Bob if you ever want to put this in a random order you're going to have to specify them as keyword arguments such as amount equals 10 but in this case we want to specify the amount so now we can do that we can say specify amount we can pass in 10 we can pass in five we can pass in 10,000 and it will call function over and over with the exact same setup but with our different amounts so we don't have to provide this boilerplate code for the first two arguments no I do not want the updates I wonder how it calculates that I would be interested in updating my computer or my my software at 855 it's such a random hour this AI is getting out of hand and I'm going to show you one more example so you really get an idea on how this works so here we're going to type in and specify name because all we want to do is specify the name here and this one's a bit more tricky because what we want to provide is the name which is perfectly centered between color and amount and it's actually not as tricky as it's going to look but it can be tricky so specify name of type partial is going to equal this partial over here and we need to pass in those specifications as always we're going to pass in the color of green and then we need to provide the keyword argument since we're skipping one so amount is going to equal 10 and this means that we're providing the first argument and the last argument which just leaves us with this name which we should provide so now we can type in specify name and it can be Bob it can be Amanda and it can also be Bob to and the next time we run this the name will change each time but the rest of the arguments will stay the same so once again this example might not have been the best but it should show you that you can use functions over and over again without having to supply the same arguments or copy and paste these same arguments over and over so theoretically you can specify any function using this approach up next we have function number three and I'm actually going to be showing you two functions here because these go hand in hand or it's good to know about both of them to say the very least so here I'm going to import from it tools permutations and and combinations with replacement and both of these functions allow us to see all of the possible combinations for a given set of elements and to explain what that means I'm going to get started by creating some permutations and that's going to be called perms of type permutations which will be of type string and here we can just call permutations and inside here we need to pass in an iterable and this is going to contain a b and c so we have three elements here and and as soon as we print this what you should notice is that we're going to get a did python break here I have I have no idea what's going on here if I just copy my previous code so what you guys need to know is that I'm incredibly confused because I literally copied my example which I created off screen and here it works just fine as long as apparent L we encapsulate it in a function so I was actually quite confused why permutations of type string was not working as a type cuz if we remove that you hover over perms you get this type inference which shows us that this is some permutations of type string but apparently the second we take that out it's not that happy so what we're going to do is actually follow my favorite practice which is creating an if name is equal to Main Check and creating a main entry point and just putting it inside there so that when we run it we should get all the possible combinations of a b and c and this has no repeating combinations so all of these combinations are unique and to explain better what that actually means I'm going to remove this print Tuple statement and I'm going to paste in a for Loop so for a b and c in this Tuple of permutations and this is exhaustive which means once we convert it to something more physical such as a list or a tupal this gets emptied out just like with a generator but here we're grabbing each one of the elements in this combination so that when we print it we will get an output such as this one which makes it a lot easier to understand what's going on so we have three elements that we are creating permutations of a b and c and this ends up being the first combination then we have ACB B A BCA cab and CBA none of these are repeating so they are all unique the permutations gives us back unique combinations otherwise we also have combinations with Replacements which will give us back nonunique combinations but it will give us back all the combinations which might be something you actually want so for the next example I'm just going to remove all of this and I'm going to create a type called combinations with Replacements and this is using the new type syntax that we have in Python 3.12 so I called it string uncore combinations because this type is incredibly long but with that we can type in combinations or coms of type string combinations and that's going to equal combination with replacement and we're going to do the same thing as we did with permutations so we're going to pass in a b and c and then we need to define the length of the combinations so here we can specify any number we can put one although one wouldn't really make much sense because of course that would just print one character out for example if we print Tuple of the Combs we'll just get a B and C but if we were to pass in two we'll get combinations of two as you can see a a a b a c BB BC CC otherwise we can do three and I'm going to do the same thing as I did with the permutations and loop through it so we're going to grab a B and C in this Tuple of combinations and when we print that out you will see that we will get all the possible combinations and I guess what I said earlier about permutations wasn't perfectly accurate because here all of these are also unique but what I meant to say is that here we can have repeating characters or repeating elements while with permutations we do not but still these are two very good functions you should know about if you ever want to create loads of combinations in Python moving on to function number four and function number four is actually going to be three functions and we're going to import these from the random module so import Choice choices and Sample and the reason I'm covering this as one function is because these are all very closely related and very often those who don't know python end up writing these by hand but to demonstrate what these are I'm going to create a list of names which is going to be of type string and it's going to contain Bob George Anna and Sophia and the first thing we want to do is pick a winner cuz we're going to pretend that this is a raffle and that all of these contestants put their name into this raffle box so here we can type in winner of type string is going to equal the choice from this iterable and what that should do is pick a winner each time we run the script so if we were to run it Sophia won this time otherwise we run it again and you'll see that Anna won this time or Bob won this time so choice is just a very good function to know about for choosing a random element from a non-empty sequence but what if we want to pick multiple winners multiple unique winners from our raffle well to do so we're going to create another variable called winners which will be a list of type string and we're going to use the sample function and here we just need to pass in the names and the amount that we want to pick from so here here we're specifying that we want to get two unique elements back at random and if we were to run this you'll see that each time that we run this we're going to get two unique elements back and this is good for Raffles because you don't want sopia to win two times in a row because that just wouldn't be fair or wouldn't make sense in terms of the raffle unless you want her to get both the first prize and the second prize but in a lot of cases you will probably want to get unique names back if you are hosting a raffle otherwise if you don't care about them being unique you can just do something such as creating a variable called random names of list of type string and use the choices function which allows you to pass in an iterable and here you can also specify how many elements you want to get back but these will not be guaranteed to be unique and very quickly I want to specify that this doesn't guarantee that they are unique either if you have two Bobs inside here there's a chance you might get both of them back for best results you will probably convert that to a set or do some sort of operation that of course checks that there's no repeating values if you truly want it to be unique but what I meant is that this can draw the same Bob twice so if we were to print random names and we were to run this here we get George and Sophia and look at the chance we got Bob and Bob back when we used choices so these are some very good functions to know about if you want to create a raffle or if you want to pick some random elements from an iterable moving on to the final function of the day and this is one I find quite useful when I don't want to waste my time trying to find out where a file path is so to demonstrate what I'm talking about I'm going to import from TK Inta or T kinta please let me know in the comments section down below how you pronounce this I still have no idea after programming in Python all these is but I'm just going to say TK inter for now so from TK inter I'm going to import the file dialogue as FD and what's really cool about this is that we can create a variable called path of type string and now we can type in FD do ask open file name and we can give that a title such as select a file and then we can print the path and each time we run our script it's going to open up this file dialogue this open file dialogue and that just makes it very easy to search for a file that you might want to use in your project for example maybe we want to open up this MP3 file or we want to actually grab the path of it so that we can open it in our script later if we were to tap on that and tap on open you'll see that in our script it's going to give us back the file path which means we will have an accurate representation of where that file actually is and that just makes it very quick to find a file that you want that you might want to use in your program so I really enjoy this function and you can even get more wild with this by creating an initial directory or providing an initial file so for this example I always want to open this in the downloads so here we can pass in the file path to the downloads which means that every time we run this it's always going to open in the downloads which is quite cool now I can tap on rubed ducky and in our project we will have the path to that rubber ducky object and finally since this is a very simple function I'm also going to show you that you can specify some file types and to do so we need to provide a tuple of file types so inside here we're going to pass in the first file type which will be a PDF and here we're going to use the star syntax to say star.pdf which means any file name which ends in.pdf then we'll add a comma and right below that we'll add an MP3 file so MP3 and here we can type in star. MP3 and it looks like I nested these terribly so I'm going to remove that and put that there cuz that's what it should have looked like so now the next time we open this it's only going to highlight the files that we can actually open such as PDF files or MP3 files but something such as a pi file or an object file is not going to be highlighted and actually we can't even click on them so this time we can tap on this Swedish PDF file and we should get the path back to that PDF file but those were the five really cool functions that I wanted to share with all of you today do let me know in the comments section down below whether you have any functions that you'd like to share or that you find really cool or even if you just want to say hello Bob in the comment section down below go for it but otherwise with all that being said as always thanks for watching and I'll see you in the next video
Info
Channel: Indently
Views: 56,296
Rating: undefined out of 5
Keywords: pyton, pyhton, pythn
Id: CnbgMnUCsUM
Channel Id: undefined
Length: 19min 57sec (1197 seconds)
Published: Thu May 23 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.