The Walrus Operator (:=), introduced in Python 3.8, allows you to assign values to variables as part of expressions. This makes your code cleaner, more concise, and often more efficient, especially in loops and conditional statements.
What is the Walrus Operator?
Article: https://www.pybeginners.com/article/walrus-operator-in-python-with-practical-examples/
📚 Check out more learning resources:
📝 Blogs: https://www.pybeginners.com/
đź’Ľ LinkedIn: https://www.linkedin.com/newsletters/pybeginners-7267881715929415681/
đź“– Free eBooks & PDFs:
📌 New videos every week — covering Python basics, hands-on exercises, projects, and career tips!
👉 Subscribe, join the PyBeginners family, and let's make Python simple together 💻💬
#LearnPython #PyBeginners #PythonForBeginners #PythonMadeEasy
Show More Show Less View Video Transcript
0:00
Hello guys, welcome back to the channel
0:01
Jean here. So today we are going to
0:02
learn about war operator in Python. So
0:05
wars operator it was introduced in
0:07
Python 3.8 and this operator it allow us
0:11
to assign values to a variable as a part
0:13
of an expression. So this will make your
0:16
code more cleaner and more concise and
0:19
often more efficient. Let's say that
0:21
okay because especially in loops and the
0:23
conditional statement but in this
0:26
tutorial we are going to see this word
0:29
operator then we are going also to do
0:31
some example okay to see some example so
0:33
that we can see when we can use this
0:34
word operator and in which case and when
0:37
we don't need it also we are going to
0:40
see in this same video. So if you new to
0:43
this video please subscribe to the
0:44
channel give a like to this video and
0:46
let's go.
0:48
So what is war operator? See the wars
0:51
operator is nicknamed the wars operator.
0:54
It was named because it looks like a
0:57
war. So something like this. Okay. Dot
0:59
and equals. So this is the words
1:01
operator that we are talking about. So
1:03
this operator it allow us to assign and
1:06
evaluate in a single expression
1:09
something that wasn't possible before
1:11
the standard Python syntax. Okay. It
1:13
means that before the Python 3 do 8.
1:18
Okay, those version below it wasn't
1:20
possible to use this warus operator. But
1:23
now above Python 3.8 we are able to make
1:27
use of this world operator. So how we
1:29
can use it is see before the words
1:31
operate we used to do things like this.
1:32
Let me give you some example before this
1:35
is before. So before words operator
1:38
we used to do things like this. Okay for
1:40
example if you want to print
1:43
how many items is present in one list.
1:45
So we used to do things like this. For
1:46
example, let me say n it can be equal uh
1:49
let's say length of my list my list
1:53
something like this let me open here oh
1:56
sorry let me open and close here so just
1:59
let me create a list called my list I'm
2:02
coming here before so I'll create a list
2:05
called my list then I'm just going to do
2:07
something like this then I'll just put
2:09
some things here inside of this list let
2:11
me see two three okay and uh I can mix
2:16
also let's say which is my name juo and
2:20
come here I can do also at least nine
2:22
okay present so let's say that I have
2:24
this list and I want to see how many
2:26
items are in this list so before the
2:30
words operate we used do something like
2:31
this then we can do something like if if
2:34
what if n greater than five if n greater
2:38
than five dot don't we do something like
2:40
this print
2:42
open and close so let's use f string for
2:44
that Then we can say the list
2:48
the list as and we can open. Then let's
2:51
give an
2:55
items items this way and see this is how
2:59
we used to do things before the walls
3:01
operate. So now if we save this and if
3:03
we link this same code uh we can see the
3:06
result here. Okay the list
3:09
the list has eight items. Okay just let
3:11
me give let's say for this one save and
3:14
run again. The list has six items. But
3:17
this is how we used to do things before
3:19
the wars operator. But now after war
3:22
operators, we can say something like
3:23
this. Okay. After
3:28
after
3:30
so after world operator we can do this
3:32
same thing just like this. See uh okay
3:35
just let me copy this line. Okay. I just
3:38
give a little space and I'll just put it
3:41
here. So now after words operate we can
3:44
do this same thing just by doing
3:45
something like this C if open and close.
3:49
Now here in one single line I can create
3:52
this same statement that I just did
3:54
here. Okay, which can be something like
3:56
this. n dot if n dot let's use the words
4:01
operator length length of what length of
4:04
my list this way length of my list then
4:07
I'm just come here come out then say
4:09
greater than five as you can see in one
4:12
single statement we wrote almost all of
4:14
these things that we just did here okay
4:17
instead of like separating the things so
4:18
greater than five dot we can just do
4:20
something like this what what we can do
4:22
we can say print what I'm going to print
4:24
as always f string do this
4:28
uh do this then I can say the list
4:32
as
4:34
what as n items as n item so this is how
4:40
we do the things now after the war
4:43
operator but before we used to do like
4:45
this first we need to create any
4:46
variable then we can store the length of
4:48
the list then we can come here and
4:50
create the condition but after that with
4:52
words operate we can do this same thing
4:54
in one single line for example we create
4:56
the n variable. Okay. Then we will be
5:00
assigning the length of this uh list
5:02
into this variable. Then after that we
5:05
can comparate it with five and if it is
5:08
true yes uh sorry not true and if the
5:12
condition succeed then it will just
5:15
print if not then it will just keep
5:16
ignore it. So this is the difference by
5:18
using words operate and by not using
5:21
word operators. There are a lot of
5:23
common case that we can make use for
5:25
worries operate. So one of these is
5:27
reducing redundancy in loops. The second
5:30
one can be reading files line by line
5:32
even also like counting elements in a
5:35
list and also avoiding repeated
5:37
functions called in your code. Okay. So
5:40
we can make use of words operator for
5:42
that. So let's see an example like for
5:44
for example reducing redundancy in a
5:46
loops. Okay. So this can be something
5:48
like this. I'm just going to delete all
5:50
of these things.
5:53
Let me comment here for example. Okay,
5:56
reducing the redundancy and we can say
5:58
something like this. For example, uh
6:00
before the word wars operate, we used to
6:02
do something like this. Let me say
6:03
before. So this is before the wars
6:06
operate. So before the words operate, we
6:08
we used to do things like line equal
6:10
input.
6:12
Then uh we can open and close and let's
6:14
say type
6:17
type something type something. Okay,
6:21
then we will do something like this.
6:23
file
6:25
give space line equal uh different
6:29
different equal and let's say something
6:31
like exit for if you want to exit the
6:34
thing exit dot and we could have do
6:37
something like this print open then
6:40
let's say fring you
6:44
type it what did I type you type it like
6:48
this and just give line
6:52
good then We can say line will be equal
6:56
input.
6:58
And again we can say like type also f a
7:02
string
7:05
type
7:08
something again. Yeah. Boom. So this is
7:11
how we used to do before the world
7:13
operate. So if I save this and let me
7:15
run this.
7:16
It say type something. Let me type
7:18
something. Let's say hi. What it said?
7:21
You type hi again. type something let's
7:23
say again hola
7:26
what he said he said you type it hola
7:28
now if I give like for examp
7:31
it it will boom ex is it the code as you
7:33
will see so this is how we use the
7:35
things before the words operator but now
7:36
with words operator we can just do this
7:39
like in one single line it's uh which
7:42
can be something like this so I just
7:44
keep this code same code here so I'm
7:46
just going to give like a new example
7:48
let's say now after
7:51
this is Afterwards operator after words
7:53
operator we can do something like this
7:54
while
7:56
open and close let's say line
8:00
uh let's now call our words operator
8:03
line let's say input
8:06
open and close do like this and we can
8:09
say type
8:12
type what type something perfect and
8:16
okay let me give dot and give it the
8:18
space now we can just see different.
8:22
Now let's create the condition. Okay.
8:24
Different of what? Different of exit.
8:27
Exit.
8:28
Okay. Oh, sorry. We can just come here
8:30
dot and do enter. And after that we can
8:33
just say this. Okay. Print.
8:37
Print what? Print
8:40
line.
8:42
No, not line. Now let's say print. And
8:47
let's use f string again.
8:51
in u type
8:55
you type dot then we can just call the
8:58
line itself line. So this is the
9:01
difference by using uh by not using
9:04
words operator. This is by using words
9:07
operator. So as you can see the lines of
9:09
code is kind of decreased. Okay. It's
9:11
very short, very small and is better
9:13
than this one. Okay. But there are
9:16
people who can think now I think this is
9:17
more easy. Okay. even to read the code
9:19
than this one. So it's upon you if you
9:20
want to use wireless operator or not. So
9:23
it's is not like composition that you
9:25
must use wireless operator but just make
9:28
a consideration okay of using it. It's
9:31
really very good things very good skills
9:33
to use it. Let me save now. Let's run
9:35
the code. If I run the code type
9:37
something let me say uh
9:40
again type something. Hi,
9:44
hola.
9:47
And let's say exit.
9:54
Oh, exit.
9:56
As it exit this way,
10:02
exit. Boom. It closed the code. So, this
10:04
is the difference by using words
10:06
operate. So, see, there's a lot of cool
10:08
things that you can do by using words
10:09
operate and more stuff that you can do.
10:11
So that's why I will keep a link in the
10:13
description of this same video so that
10:15
you can go there and you can see okay so
10:17
there is but there are some things that
10:19
you need to do okay while using wireless
10:23
operate okay things that you need to
10:25
watch out for for them readability of
10:28
the code okay use wisely because
10:30
overusing words operate it can make your
10:32
code hard to read okay even if it is
10:35
good but it can really make your code
10:37
hard to read and also the compatibility
10:39
okay so it works only in Python 3.8 and
10:43
above. Okay. So if there is a version
10:47
like if version below this, it will not
10:50
work at all. And there is also
10:52
limitation. It cannot be used with
10:54
lambda operation operators uh
10:56
expressions by using words operate in
10:59
combination with lambda operation
11:00
expression. You can make use of it.
11:04
And when you should use words operate,
11:06
you can use word operate if you want to
11:08
avoid repeating functions call or
11:10
expression even. So you can use it okay
11:13
when you need to assign and and evaluate
11:16
a value at the same time. So it can save
11:18
you the lines of the of codes and you
11:21
also when you want to compact and
11:25
expressive code without hurting your
11:27
readability of the code. So you can make
11:29
use of it. See the word operators is
11:32
very powerful addition to the Python
11:34
that helps you to write a cleaner and
11:36
more efficient code but you must know
11:39
when to use it properly. Okay. So don't
11:41
just jump and make use of words operate
11:43
as you wish but you need to considerate
11:46
when is the proper time to make use of
11:48
it. So we will stay here. My name is if
11:50
you like the video please give a like to
11:52
the video and see you in the next video.
11:53
Okay. Bye.

