10 Ways You Can Use "_" In Python (Do you know ALL of them?)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video I'm going to be showing you 10 different ways you can use the underscore in Python now the first way requires us to open up the python console and what we can do here is create a variable or we can actually do a calculation so we can do 10 plus 10 and it's going to return to us 20. now what the underscore does in the python console is let us use the last result so that says 20 we can say 20 times underscore and it's going to use the last result and you can do this as much as you want so you can say underscore divided by 5 and we're going to get or times 5 and we're going to get 2 000 while this might not be such a popular case since a lot of us do not use the python console it is still worth mentioning a second way to use the underscore is to make your numbers much more readable so for example pretend you have some large number and that's going to be of type integer and it's going to be something such as a billion at this point I have no idea how many zeros I actually used and whether I actually created a billion so in Python you can actually use the underscore to format the number and when you actually print the number for example if we type in print number it's going to exclude the underscores they're just there to help you visually understand the number you are writing thirdly we can use it for unimportant values such as if we have a value of 10 we can assign it to the underscore because it is unimportant and this isn't really a use case for it it's just to show you that we should in theory never use this underscore if we're assigning 10 to it and I guess a better use case would be for underscore in range three so each time we Loop through the range we're assigning a value to underscore but since we don't really care about it we're just going to use the underscore here so now we can say print hello and if everything goes well it should print hello to the console three times it is important to mention though that the underscore does work as a variable name so if we print that it's still going to print the numbers but usually you use this as a convention if you do not want to use a value the fourth example of doing this would be with a try and accept block here we can type in result equals one divided by zero and obviously this is going to give us a0 division error so accept zero division error as underscore because we do not care about using the error message or the error in general and here you can handle it we can say handled and now we are a pro python developer I'm just kidding of course but if you run this it's going to run just fine and we have this placeholder here that does absolutely nothing because we do not care about it one of the more useful places to use an underscore is with unpacking tuples so for example here we have a sample Tuple and that's going to equal a tuple of one two three and four and now let's pretend we want to unpack this Tuple so we just want to get one and three back so here we can type in a underscore B underscore and that's going to equal the sample Tuple so that allows us to actually just retrieve the values of A and B of course the underscore is going to be assigned default because that's the last assigned variable but it's going to keep our code much cleaner because we're not assigning random variables to random names that we have to keep track of later so if we run that we'll get three and again if we print the underscore we should get four back but next we have something a bit more interesting and let's pretend we have five elements here and this is combining the underscore with an asterisk so here we can say a b and in front of the underscore we're going to add this asterisk which is going to unpack all the values between A and B which means we're only going to get back 1 and five so that's a pretty neat tip if you just want to get the last two values and if we don't print it of course we'll see nothing so he will type in print A and B and in the console we'll get one and five back and if you're curious if you print the underscore now you're going to get a list of the elements inside the middle section for use case number seven we're going to import from uuid uuid4 to generate some uuids and in this example we're going to create a class called user and it's going to have an initializer and in this initializer we're going to do something a bit special we're going to type in self dot underscore ID and that's going to equal a uuid4 so why did we add an underscore in front of ID well the idea here is that we want ID to be protected which means it should only be used in the current class and in subclasses if anything and in Python this is merely a convention because it does not prevent anyone from using it outside the class or in subclasses but it's still used as a convention so here we can type in Dev get ID which should also only be used in this class or in subclasses and that's going to return self dot underscore ID if we create an instance of this class we say user is equal to user and we type in user and use dot notation you'll notice that we're not going to get any suggestions from our code editor even if we want to get the ID it's not going to show up and most code editors will hide it from you because we've defined it to be protected but it is very important to mention that if we type in get ID like that and we actually print this it's not going to prevent us from using that if you actually want to prevent usage of it outside of the class we're going to have to create a private function or attribute and to do this you use the double underscore and this is how you define a private attribute or a private function in Python by using two underscores in front of an attribute name or in front of a function name and if we try to use it now it's not going to let us use it because python performs something called name manga link so instead of giving us back underscore underscore get ID it does some funky magic and gives us something else back so as you can see if we try to get this ID using this private method it's going to say that this does not exist and to keep it short name mangling just renames this function to something else so it makes it harder for us to access it it doesn't make it impossible there are still ways to get this but it just becomes a lot harder and just counter-intuitive but moving on to use case number nine and this has to do with using reserved names pretend you want to name a variable if for whatever reason you just want to name it if it's impossible because it is reserved by python so something you can do is add an underscore after the keyword that you want to use as a variable name and it works with anything you can say class is class or whatever you want it can also be string is string and we were able to use those keywords now because we added a slight modification the final way I'm going to show you that you can use underscores is with Dunder methods and dunda methods are just these special methods with double underscores on each side that have inbuilt functionality which can be overwritten and the best way to describe this is to show you so first we're going to create an initializer and right there immediately we are using a Dunder method that initializes the class and he will type in self dot ID is equal to uuid 4 and I guess I have to import that again so we'll do that import uuid4 and this time we're going to add some special functionality using Dunder methods because right now if we type in fruit is equal to fruit and we print this fruit we get this ugly mess back so what we can do is Define a Dunder method for the string representation and here we're going to return the formatted string of fruit and the uuid so self dot ID now the next time we run this we're going to get something much more readable regarding the fruit that we have and under methods definitely deserve another video but you can do so much with them so if you type in for example def integer you can also tell the program what to do to this class if you want to turn it into an integer and in this case we're just going to return the number of four so now we can create some very funky code we can say print the value or the sum of 10 plus the integer of fruit so we can now convert this fruit type into an integer and this is wrong it has to be the double underscore or the dunder integer so with that done the method you're now telling the program okay this is what happens when you want to turn it into an integer and when you run it it is not happy because I did not close this part here or I removed it for some reason but now if you run it again you're going to get 14 because 10 plus the integer of fruit returns 14 since the integer converts through to 2 4 every time we use the integer method but anyways those were 10 different ways you could use the underscore and I mean I thought it was kind of silly to mention that you can use it in variable names for example VA to to separate words you can use the underscore of course but that's common knowledge so in reality these were 11 ways you could use it but I really hope you enjoyed this video do let me know in the comment section whether you knew all of this or if you learned something new but with all that being said as always thanks for watching and I'll see you in the next video
Info
Channel: Indently
Views: 22,559
Rating: undefined out of 5
Keywords: pyton, pyhton, pythn
Id: 7GupibhOTdY
Channel Id: undefined
Length: 9min 55sec (595 seconds)
Published: Thu May 11 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.