Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,940 --> 00:00:01,520
Welcome back.
2
00:00:01,780 --> 00:00:09,020
Let's talk about the walrus operator, and this one is a fun one because, well, it looks like a walrus.
3
00:00:09,070 --> 00:00:15,670
It looks like this sea looks like, well, Google walrus, if you don't know what that is, it's an
4
00:00:15,670 --> 00:00:16,180
animal.
5
00:00:16,180 --> 00:00:17,380
And it kind of looks like that.
6
00:00:17,560 --> 00:00:18,250
Just Google this.
7
00:00:18,610 --> 00:00:19,030
All right.
8
00:00:19,060 --> 00:00:20,090
We're getting distracted.
9
00:00:20,560 --> 00:00:23,770
So what is this new operator we're going to learn about?
10
00:00:24,460 --> 00:00:30,970
The reason I want to teach you this is because I also want to teach you how to learn about new features
11
00:00:30,970 --> 00:00:33,190
in Python, for example.
12
00:00:33,220 --> 00:00:35,020
This is a new feature.
13
00:00:35,710 --> 00:00:43,750
If I go to the Python documentation here, usually what Python does with each new version, they create
14
00:00:43,750 --> 00:00:46,260
a what's new in Python document.
15
00:00:46,780 --> 00:00:49,200
So you might see what's new in Python three point nine.
16
00:00:49,300 --> 00:00:51,060
What's new in Python, three point ten.
17
00:00:51,610 --> 00:00:57,670
And it gives you a summary of some of the new features added to the language, because, remember,
18
00:00:57,850 --> 00:01:00,270
a language is constantly evolving.
19
00:01:01,030 --> 00:01:06,490
So let's say you want to learn about new three point eight features, because let's say the Reppel you're
20
00:01:06,490 --> 00:01:08,720
on now has three point eight.
21
00:01:08,740 --> 00:01:15,310
So it's able to understand the new features like the Walrus operator that we weren't able to write before
22
00:01:15,310 --> 00:01:15,970
this version.
23
00:01:16,780 --> 00:01:21,160
So I would go into the documents and see what's new in Python three point eight.
24
00:01:21,550 --> 00:01:25,630
And we see one of the first things is the assignment expression.
25
00:01:26,640 --> 00:01:28,710
Called the Walrus operator.
26
00:01:30,100 --> 00:01:34,480
So what I would usually do is just read about the summary.
27
00:01:35,600 --> 00:01:40,550
Of what this does and you can learn about all the new features added in this version.
28
00:01:41,680 --> 00:01:47,320
Now, let's just focus on the Walrus operator, because it is useful and also kind of fun to learn about
29
00:01:47,710 --> 00:01:54,120
what this new Syntex does is it assigns value to variables as part of a larger expression.
30
00:01:54,160 --> 00:02:00,880
So usually you use it in an expression when something is being evaluated, like in an if statement,
31
00:02:01,000 --> 00:02:03,230
maybe a while statement.
32
00:02:03,970 --> 00:02:07,120
So let's play around with this and see what we can do.
33
00:02:08,380 --> 00:02:11,980
So let's say we have a string and we'll call this string.
34
00:02:13,780 --> 00:02:14,980
Hello.
35
00:02:17,870 --> 00:02:18,830
Now in here.
36
00:02:19,910 --> 00:02:23,570
Let's say I want to do a conditional statement, I'm going to say if.
37
00:02:24,490 --> 00:02:26,590
Length of a.
38
00:02:27,730 --> 00:02:29,710
Is greater than 10.
39
00:02:30,970 --> 00:02:38,260
I want you to print a string that simply says too long.
40
00:02:39,910 --> 00:02:42,550
And we'll say lenth a.
41
00:02:44,930 --> 00:02:45,620
Elements.
42
00:02:47,300 --> 00:02:49,730
Well, make sure I do that properly.
43
00:02:51,300 --> 00:02:52,830
Now, if I click run here.
44
00:02:54,910 --> 00:02:59,930
I get to log 14 elements and why is that?
45
00:02:59,950 --> 00:03:04,320
Well, because each character here we have 14 elements because it's greater than 10.
46
00:03:04,780 --> 00:03:08,400
This conditional block runs and we print out too long.
47
00:03:08,980 --> 00:03:11,600
We have a length of 14 elements.
48
00:03:12,430 --> 00:03:14,800
So where does the walrus operator come in?
49
00:03:15,460 --> 00:03:22,980
Well, we're writing code and we're repeating ourselves here because we're calculating a length twice.
50
00:03:23,410 --> 00:03:29,320
We're using this as a variable and doing some sort of a calculation multiple times because we want to
51
00:03:29,320 --> 00:03:30,700
use it here and here.
52
00:03:32,010 --> 00:03:38,040
But with a walrus operator, I can do exactly that, remember, if I go to the documentation it says
53
00:03:38,190 --> 00:03:42,690
assigns values to variables as part of a larger expression.
54
00:03:43,730 --> 00:03:52,760
So what I can do here is I can create another variable like NP and that says, well, Woolworth's operator
55
00:03:52,760 --> 00:03:53,960
So Colin.
56
00:03:55,070 --> 00:04:01,400
And equals little walrus there and this end is going to equal length.
57
00:04:02,060 --> 00:04:04,340
So I'm just going to wrap this in brackets.
58
00:04:05,000 --> 00:04:14,480
So we're evaluating this part of the expression and we're assigning the variable NP, whatever the outcome
59
00:04:14,480 --> 00:04:16,070
of length A is.
60
00:04:19,440 --> 00:04:24,250
So now NT is something that has the value of a.
61
00:04:24,690 --> 00:04:26,040
So I can replace this.
62
00:04:28,740 --> 00:04:29,910
And if I run this now.
63
00:04:31,240 --> 00:04:32,320
Again, same thing.
64
00:04:33,640 --> 00:04:36,280
So, again, what we can do is we can assign.
65
00:04:38,760 --> 00:04:42,510
Values two variables as part of a larger expression.
66
00:04:43,860 --> 00:04:50,400
For example, if I did and equals lenth a well, this isn't going to run, it's invalid syntax, but
67
00:04:50,400 --> 00:04:53,270
because of the walrus operator we can do this now.
68
00:04:53,580 --> 00:04:55,550
Let's do one more just to make sure we have it.
69
00:04:56,010 --> 00:05:04,530
So let's say in here we're going to do a while loop while Loop is going to say and walvis operator.
70
00:05:06,350 --> 00:05:07,700
Lenth a.
71
00:05:08,880 --> 00:05:12,240
Again, we're going to do the same thing if it's less than this time, let's do one.
72
00:05:13,170 --> 00:05:23,250
And while this is true, I want to print the value of RN and we're going to reassign the value of the
73
00:05:23,250 --> 00:05:26,490
variable A to have one less letter.
74
00:05:26,850 --> 00:05:31,700
So I can just do that with the minus one short form here.
75
00:05:32,220 --> 00:05:40,710
So we're going to remove the last letter every time we loop through here so that the a variable, the
76
00:05:40,710 --> 00:05:44,100
hello becomes smaller and smaller and smaller until.
77
00:05:46,130 --> 00:05:50,520
It equals to one, and once it equals to one, the while loop is going to stop.
78
00:05:50,840 --> 00:05:52,550
So let's just run this.
79
00:05:54,950 --> 00:06:01,070
All right, we're getting true, true, true, and we made a bit of an error here because remember the
80
00:06:01,070 --> 00:06:04,400
parentheses, we want to evaluate this first.
81
00:06:04,970 --> 00:06:06,140
So if I run this again.
82
00:06:07,560 --> 00:06:09,490
Fourteen, thirteen, twelve, eleven, ten.
83
00:06:09,540 --> 00:06:15,330
This is the length of A that's being calculated every time and at the very end if I print.
84
00:06:19,420 --> 00:06:20,170
And run it.
85
00:06:21,090 --> 00:06:23,070
You see that we have each remaining.
86
00:06:24,520 --> 00:06:30,850
There you have it, the walrus operator, you might not see it very often, it's essentially a way for
87
00:06:30,850 --> 00:06:38,710
us to minimize doing calculations that are similar, let's say, inside of an if statement or a wild
88
00:06:38,710 --> 00:06:44,250
statement where we want to do something based on a condition and then calculate that value again.
89
00:06:45,040 --> 00:06:52,560
But this also hopefully teaches you how to look a new Python versions and learn about new features.
90
00:06:52,930 --> 00:06:56,200
There's only a handful of features that get added to the language.
91
00:06:56,410 --> 00:06:58,900
Most of the important ones have already been added.
92
00:06:59,140 --> 00:07:06,070
But it's also good to keep an eye out on things as the language evolves so that whenever you see something
93
00:07:06,070 --> 00:07:12,280
like a walrus operator and the code base, you now understand what it does and that it's part of the
94
00:07:12,280 --> 00:07:14,920
language I'll see in the next one by.
8868
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.