Afrikaans
Akan
Albanian
Amharic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranรฎ)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
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 three point nine.
What's new in Python, three point ten.
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 three point eight features, because let's say the Reppel you're
on now has three point eight.
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 three point eight.
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 Syntex 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,
maybe 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 10.
I want you to print a string that simply says too long.
And we'll say lenth a.
Elements.
Well, make sure I do that properly.
Now, if I click run here.
I get to log 14 elements and why is that?
Well, because each character here we have 14 elements because it's greater than 10.
This conditional block runs and we print out too long.
We have a 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 as a variable and doing some sort of a calculation multiple times because we want to
use it here and here.
But with a 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 NP and that says, well, Woolworth's operator
So Colin.
And equals little walrus there and this end is going to equal length.
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 NP, whatever the outcome
of length A is.
So now NT is something that has the value of a.
So I can replace this.
And if I run this now.
Again, same thing.
So, again, what we can do is we can assign.
Values two variables as part of a larger expression.
For example, if I did and equals lenth 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 while loop while Loop is going to say and walvis operator.
Lenth 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 RN and we're going to reassign the value of 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.
Fourteen, thirteen, twelve, eleven, ten.
This is the length of A that's being calculated every time and at the very end if I print.
And run it.
You see that we have each remaining.
There you have it, the walrus operator, you might not see it 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 wild
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 a new 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 by.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.