Welcome back.
Let's talk about the Walrus.
OPERATOR And this one is a fun one because, well, it looks like a walrus.
It looks like this sea looks like.
Well, Google Walrus, if you don't know what that is, it's an animal.
And it kind of looks like that.
Just Google this.
All right.
We're getting distracted.
So what is this new operator we're going to learn about?
The reason I want to teach you this is because I also want to teach you how to learn about new features
in Python.
For example, this is a new feature.
If I go to the Python documentation here, usually what Python does with each new version, they create
a what's new in Python document.
So you might see what's new in Python 3.9.
What's new in Python 3.10?
And it gives you a summary of some of the new features added to the language.
Because remember, a language is constantly evolving.
So let's say you want to learn about new 3.8 features because let's say the reptile you're on now has
3.8.
So it's able to understand the new features like the walrus operator that we weren't able to write before
this version.
So I would go into the documents and see what's new in Python 3.8.
And we see one of the first things is the assignment expression.
Called the walrus operator.
So what I would usually do is just read about the summary.
Of what this does, and you can learn about all the new features added in this version.
Now let's just focus on the walrus operator because it is useful and also kind of fun to learn about
what this new syntax does is it assigns value to variables as part of a larger expression.
So usually you use it in an expression when something is being evaluated like in an if statement may
be a while statement.
So let's play around with this and see what we can do.
So let's say we have a string and we'll call this string.
Hello.
Now in here.
Let's say I want to do a conditional statement.
I'm going to say if length of a.
Is greater than ten.
I want you to print a string.
That simply says too long.
And we'll say length a.
Elements.
Well, make sure I do that properly.
Now if I click run here.
I get too long.
14 elements.
And why is that?
Well, because each character here we have 14 elements.
Because it's greater than ten.
This conditional block runs and we print out too long.
We have length of 14 elements.
So where does the walrus operator come in?
Well, we're writing code and we're repeating ourselves here because we're calculating a length twice.
We're using this a variable and doing some sort of a calculation multiple times because we want to use
it here and here.
But with the walrus operator, I can do exactly that.
Remember, if I go to the documentation, it says assigns values to variables as part of a larger expression.
So what I can do here is I can create another variable like RN and that says well Woolworths operator
so Colin.
And equals little Woolworths there.
And this end is going to equal length a.
So I'm just going to wrap this in brackets.
So we're evaluating this part of the expression and we're assigning the variable.
Whatever the outcome of length A is.
So now NW is something that has the value of a.
So I can replace this.
With NW.
And if I run this now.
Again.
Same thing.
So again, what we can do is we can assign.
Values to variables as part of a larger expression.
For example, if I did an equals length A well, this isn't going to run.
It's invalid syntax.
But because of the walrus operator, we can do this now.
Let's do one more just to make sure we have it.
So let's say in here we're going to do a wild loop while loop is going to say NW while we're so operator.
Length a.
Again, we're going to do the same thing.
If it's less than this time, let's do one.
And while this is true, I want to print the value of NN and we're going to re assign the value of a,
the variable A to have one less letter.
So I can just do that with the minus one short form here.
So we're going to remove the last letter every time we loop through here so that the a variable the
hello becomes smaller and smaller and smaller until.
It equals to one, and once it equals to one, the while loop is going to stop.
So let's just run this.
All right, we're getting true, true, true.
And we made a bit of an error here because remember the parentheses.
We want to evaluate this first.
So if I run this again.
14, 13, 12, 11, ten.
This is the length of a that's being calculated every time.
And at the very end, if I print.
A.
And run it.
You see that we have h remaining.
There you have it, the walrus operator.
You might not see it a very often.
It's essentially a way for us to minimize doing calculations that are similar, let's say, inside of
an if statement or a wall statement where we want to do something based on a condition and then calculate
that value again.
But this also hopefully teaches you how to look and you Python versions and learn about new features.
There's only a handful of features that get added to the language.
Most of the important ones have already been added, but it's also good to keep an eye out on things
as the language evolves, so that whenever you see something like a walrus operator and the code base,
you now understand what it does and that it's part of the language.
I'll see in the next one.
Buh bye.