 Welcome back everyone in this video. We're gonna talk about the walrus operator the what that's right the walrus operator It's funny name and it gets it from the syntax. It's colon equal And if you look at it like if you like cock your head sideways, it kind of looks like a walrus It's got the little eyes and the tusk and everything. That's where it gets its name This is a little bit confusing and this might hurt your brain a little bit And to make it even more complex you have to have the right version This isn't available in python until python 3.8 or higher But what the walrus operator allows us to do is assign a variable from an Expression meaning we're gonna take some chunk of code and turn the end result of that code Into a variable without having to do a whole lot of complex stuff In case you're wondering about versioning remember if you pull up a terminal And there's different ways of doing this you can see down in the corner. I'm using python 3.6.9 There are different versions installed. So for example if I type python that's 2.7 By type python 3 my system's at 3.6.9 So that's not gonna work. I would have to do 3.8 I have 3.86 just use whatever you have if you don't have 3.8 or higher installed This will not work and you'll have to go out to python's website and download and install and configure Python 3.8 or higher follow the instructions for your operating system Google is your best friend if you have a problem or you can visit me in the voidromes facebook group There's details at the end of this video For our purposes here we're just gonna go down here And we're going to select python 3 and then select the correct workspace Which should be python 3.8 or higher And suddenly everything starts popping up and saying do you want to install this do you want to install that I'm not gonna do any of that for this video. I'm gonna apologize if it keeps popping up though Let's dive in let's look at some common issues and I put the parentheses here for a reason Because that's gonna be your biggest issue when working with the walrus operator For example if I say why and then walrus I'm just gonna say walrus Len of hello What do you think we're doing here? Well let's try and print why out I get no graphical error in the IDE because well there's really no pylenter installed I can install kite or something else like that but I just wanted to switch versions and see this thing work And it's saying syntax error invalid syntax What do you mean invalid syntax? I'm using the walrus operator and I've got the right version of python Hmm this is what I'm talking about let's just grab this guy right here And let's just put that same thing inside of parentheses here And I'm gonna put some notes at the end here Just for anybody who downloads the code so they know what's going on And let's comment this bad boy out put that at the end there didn't I there we go And let's rerun this now suddenly we get the output Len 5 it's valid but according to pythons website it's not recommended why because it's just confusing to look at What is going on here it's almost like we're calling a function remember In programming and in math anything inside of the parentheses is pretty much done first So what we're really telling python is as you're reading this stop what you're doing Brunch all this stuff between these parentheses and then replace all that parentheses with the value which is 5 So it's just going to really make a variable called 5 seems confusing And it is 9 times out of 10 That's going to be your biggest issue is just you forget to put something in the parentheses So let's go ahead and look at another real world example here Let's say people equals and we're going to make a list of people So me my wife and family dog big old stinky dumb dog He's probably down there wanting a treat right now but he's not getting one because I'm up here making a video for dog So I'm going to say if and walrus Len and we're going to just get the length of that variable right there I don't want to just say less than or equal to 3 Then go ahead and print it out we're going to print out n Little confusing but what's going on here I'm saying n is walrus or equal to this expression The length of people which in this case should be 3 because I have three elements We're saying if it's less than or equal to 3 go ahead and print it out When we run this we have been betrayed it actually says 2 What come on why is it true why because we forgot the parentheses Let's just take the same thing and let's just go ahead and wrap it in parentheses here Rerun it and sure enough we get 3 So 9 times out of 10 if you have a problem with the walrus operator it's because you're missing your parentheses I am not a fan of introducing new concepts without some sort of example so let's make this super short But super confusing because the walrus operator is confusing Alright so I'm going to say lines equals and we're just going to make a list And we're going to use a function Well can add with a maximum of 5 that's the default Which is going to determine whether or not we can add to that list And we're going to say Louisville Lines I don't like doing this because what we're saying is now we can modify this But I want to make sure python knows we want this list we're not creating some new variable That gets a little bit cumbersome not a fan doing that When in doubt you should actually send it as a parameter slash argument Let's work with the walrus operator let's unleash our inner walrus I'm going to say allowed equals And notice how allowed's not defined so what we're doing is we're letting the walrus operator define this variable So if you're worried you're going to get an undefined error don't worry it's not going to happen But whenever you see walrus think the walrus needs to open his mouth you should see these parentheses If you don't you're going to have a bad time And say this is going to be the count and again walrus operator so we need some sort of parentheses But we're going to use a function len we're just going to get the length of those lines Now we're going to say we want the max and that looks really crazy and confusing but what are we doing here We're creating one two variables and we're saying allowed is going to be this code expression here Oops my mouse ever wants to cooperate this code expression here and count is going to be this code expression here Whenever you see these parentheses you are working with some sort of expression or scope but usually both Let's go ahead and print and we're going to say f you can enter and we want the max as the count more We want to tell the user in real time how many more they can actually enter We're going to drop back down and say return and we're going to just return whether or not this was even allowed That looks super confusing but the whole point of the walrus operator is we've reduced code We now have a variable that got excited expression we didn't have to say something like allowed equals something something something And then put the allowed in the if it just automatically does all of it for us To further make this confusing let's do something that I see people doing all the time I'm going to say while and then a function is going to return a bull and we're just going to loop until that function says we can't do it anymore Say lines go ahead and append that and we're going to make another variable called L and we're going to get the input from the user Whoa that's crazy all right so we are making yet a third variable here and let's go ahead and just print out that we're done Okay highlight the walrus operating seat just in our little example here we are creating one two three variables Whenever you see walrus think immediately to the left is what we're creating immediately to the right whatever's in the walrus's mouth is what we're using to create it Kind of crazy so if allowed and the count less than max then we're going to go ahead and get the max minus the count blah blah blah They get super confusing we're going to return a bull we're going to use that bull and then we're going to get this value from the input all in one line Really really reduces our code but it also makes it a little bit harder to read so I'm going to say we're just going to enter some stuff One we can enter four more three more two more one more and boom you entered and it gives us our nice neat little list Major takeaway here walrus operator is super convenient but can be super super confusing whenever you see the walrus operator think immediately under the tusk You need the mouth which are these parentheses and then whatever's in the parentheses is going to get assigned to the variable Because you're really taking an expression and putting it into a variable You also need python 3.8 or higher or you're going to get a whole lot of crazy errors because earlier versions of python have no idea what you're talking about That in a nutshell is the walrus operator I hope you enjoyed this video you can find the source code out on github.com If you need additional help myself and thousands of other developers are hanging out in the void realms Facebook group This is a large group with lots of developers and we talk about everything technology related not just the technology that you just watched And if you want official training I do develop courses out on udemy.com This is official classroom style training If you go out there and the course you're looking for is just simply not there drop me a note I'm either working on it or I will actually develop it I will put a link down below for all three of those and as always help me help you smash that like and subscribe button The more popular these videos become the more I'll create and publish out on YouTube Thank you for watching