Is THIS Python's MOST Underrated Operator? (Walrus Operator)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
The Walrus operator what is it and how can we use it in Python well recently I found myself using it quite a lot when creating projects so I thought I would share how it works with you guys for those of you who don't know what the walrus operator is it's this symbol here that kind of looks like a walrus it's supposed to be two eyes and the tasks or at least that's why they call it the walrus operator because it resembles the face of a walrus with a little imagination of course but this walrus operator was introduced in Python 3.8 and what it does is assign values to variables as part of a larger expression although that just sounds like a lot of legal jargon so let's simplify this and actually create some examples so you can use it very soon in your own projects so to demonstrate it I'm going to create a function called analyze text and it's going to return towards a lot of nice details regarding whatever text we decide to insert so we'll have some details of type dictionary because we want to return a dictionary of cool information and the first information we want to return is how many words are located in the text and immediately we're going to create some parentheses for this key and we're going to type in Words which is a value we did not create yet but then we can type in Walrus operator text Dot split and then right below it we're going to type in amount so we can get the amount of words and and we can type in length of words and as you can see in this very simple line of code or in this dictionary we were able to assign a variable inside it while executing an expression and getting the value back from it the benefit of doing this is that we don't have to do words equals text Dot split and use a whole other line to actually do that I mean of course it would look a bit cleaner maybe inside the dictionary but what we did with the walrus operator was clean enough to understand that and we were able to use it just inside here that's a perfectly fine example of where you can use the walrus operator so I'm going to remove that variable out there and we will continue in here we can also say characters which is going to be length of empty string dot join and we want to join the words and reversed is going to be the words reversed then we can return the details so in this example we managed to save one line of code but of course you don't have to limit it to only one field you can also do it with another one and another one so instead of having four different let's say variables out here we can have VAR VAR 2 VAR 3. instead of having all of these out here you can just leave them inside the dictionary and use them for the next field if you know you're going to use them closely after now when we actually print analyze text and we say hello world we're going to get a lot of good information back the words are hello and world we have two words and 12 characters and reverse we get World hello so that's one place I really enjoy using the walrus operator next suppose you have some user input of type string and that's going to equal hello world now something else you can do is use it with if statements and I use it here a lot as well you can type in if text walrus operator length of user input is more than five then we can use that value we can print okay the text is good so text followed by a thumbs up because thumbs up means good else we will print the text and say thumbs down like this and this parentheses should only be inside here but now pay attention to what's going to happen when we run the program we're going to get 13 characters returned with a thumbs up because here it was able to evaluate this expression it first got the length of the user input then it assigned it to text and then it checked that text was more than five before trying to execute the if statement so we did that all in one line of code so we didn't have to type in text equals length of user input and then check it down here we were able to do it immediately inside the if statement and personally I use it a lot to check if a function returns a value and not so for example we have a function here that says get value and we're just going to return none by default functions return none but we're going to explicitly do so here and what I would do in this example is check if VA walrus operator get value then I would use that variable so I would have a way of assigning whatever values inside get value to this variable else we can type in print no value and just like that we were able to check whether this actually gave us back a value and not and assign it to the variable if it did otherwise we could execute something else and that's one of my favorite ways to use it just checking if something is empty or not otherwise if you say let's say value it will return the value and assign it to variable in one line so once again we don't have to type in VAR get value and then check if VA you are able to do all of that on one line and that's how you could use the walrus operator in a nutshell quite effective very simple to use and it can easily help you clean up your code Base by reducing the lines of code and if there are other ways you tend to use the walrus operator please share it in the comment section down below but with all that being said as always thanks for watching and I'll see you in the next video
Info
Channel: Indently
Views: 33,147
Rating: undefined out of 5
Keywords: pyton, pyhton, pythn
Id: MEMDi9mTCiU
Channel Id: undefined
Length: 5min 44sec (344 seconds)
Published: Sun Jun 11 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.