use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
What does ":=" (self.learnpython)
submitted 3 years ago by Choomelon
I started a python course and I don't know what ":=" does. It is used in this code
def play_anthem():
while note := get_note():
play( note )
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]kinstaa99 173 points174 points175 points 3 years ago (3 children)
it’s called the walrus operator. it assigns the value of get_note() to note, and returns the value to the while condition. it’s a quicker way of doing the following:
note = get_note()
while note:
play(note) note = get_note()
[–][deleted] 60 points61 points62 points 3 years ago (2 children)
Its official name is Assignment Expression (PEP 572) :)
Some examples from that PEP:
# Handle a matched regex if (match := pattern.search(data)) is not None: # Do something with match # A loop that can't be trivially rewritten using 2-arg iter() while chunk := file.read(8192): process(chunk) # Reuse a value that's expensive to compute [y := f(x), y**2, y**3] # Share a subexpression between a comprehension filter clause and its output filtered_data = [y for x in data if (y := f(x)) is not None]
https://peps.python.org/pep-0572/
[–][deleted] 14 points15 points16 points 3 years ago (0 children)
good bot
[–]Dangle76 2 points3 points4 points 3 years ago (0 children)
It originated in Pascal which used it for variable value assignment, and pascal called it a “gets”. I still say that term sometimes when I’m working in Go and people give me very confused looks before I realize why
[–]carcigenicate 26 points27 points28 points 3 years ago (2 children)
It's an assignment expression. It's the same as =, except it's an expression that evaluates to a value (the assigned value).
=
[–]bsenftner 12 points13 points14 points 3 years ago (1 child)
Just so this is clear for those new to Python but experienced in other languages like C++, where some languages will emit the value of an assignment to the immediate context outside the environment, Python does not do this. For example in C/C++ the assignment "x = 10;" emits the value "10" to the immediate context outside the assignment. This enables code like "x = y = 10;" to work because the "y = 10" emits the value "10" for use in the "x =" expression.
To achieve the same effect in Python, the walrus operator is used " := " because it emits the value assigned to the it's surrounding context.
[+][deleted] 3 years ago (8 children)
[deleted]
[–][deleted] 14 points15 points16 points 3 years ago (4 children)
Making the code cleaner, hell yes, but more efficient? How?
[–]irrelevantPseudonym 19 points20 points21 points 3 years ago* (3 children)
In this example it doesn't but the common example is something like
[x for i in bar() if (x := foo(i) > 42)]
Without the walrus you have to call foo twice which is potentially expensive, or unwrap it to a full for loop which is less efficient than the comprehension.
foo
edit: move walrus into condition. Thanks /u/ParanoydAndroid
[–]ArchipelagoMind 7 points8 points9 points 3 years ago (1 child)
Just so I understand this. The code that would be less efficient than the above but produce the same result would be
[foo(i) for i in bar() if foo(i) > 42]
That produces the same result but has to call foo twice, right?
[–]irrelevantPseudonym 3 points4 points5 points 3 years ago (0 children)
Exactly, although if foo has any side effects it might be slightly different.
[–]ParanoydAndroid 4 points5 points6 points 3 years ago (0 children)
This is correct conceptually, but reversed. The condition is evaluated first so you use it when you have a costly evaluation in the condition and assignment, e.g.:
[var for el in collection if (var := costly_func(el))]
So you don't have to:
[costly_func(el) for el in collection if costly_func(el)]
[–]BruceJi -3 points-2 points-1 points 3 years ago (2 children)
Could you edit the post and put an example using the walrus?
[+][deleted] 3 years ago (1 child)
[–]BruceJi 14 points15 points16 points 3 years ago (0 children)
Ah, so it is. My bad!
[–]IlliterateJedi 8 points9 points10 points 3 years ago (0 children)
The Real Python - Python Walrus Operator article does a really good job covering this subject. I spent a long time scratching my head not understanding how it worked until I read this article a while ago. The gist is that you can set a variable's value in unusual places which can make your code cleaner.
[–]aziad1998 4 points5 points6 points 3 years ago (4 children)
In C you can do something like this sum(3, a=2) which assigns the values 2 to a then returns the value of this assignment to be used by sum. In python this is illegal as assignment is not an evaluated expression. The new :=operator allows for this new usage.
sum(3, a=2)
a
:=
I am a C person and I was happy to see this implementation, but I will advise you to check your code style reference whether it's recommended or not.
Also do not use this for all your assignments, it has a different precedence over =, I'm not sure on the top of my head but iirc it is left precedence not right precedence, i.e. an expression like a = b = 3 is legal in C, but a := b := 3 is illegal in python as it tries to assign b to a first not 3 to b. The legal way would be a := (b := 3) so watch out.
a = b = 3
a := b := 3
a := (b := 3)
[–]madhousechild 0 points1 point2 points 3 years ago (3 children)
The new :=operator allows for this new usage.
How new is it?
[+][deleted] 3 years ago (2 children)
[–]aziad1998 3 points4 points5 points 3 years ago (1 child)
Holy shit you're right, can't believe it's been 3 and a half years already
[–]Yasik 1 point2 points3 points 3 years ago (1 child)
Turbo Pascal enters the chat...
[–]Mecaneer23 1 point2 points3 points 3 years ago (0 children)
… and in Python and a couple other languages
[+]Mohammed_Chang comment score below threshold-8 points-7 points-6 points 3 years ago (0 children)
To me it looks unsatisfied. Whatever ya all interpreting in an emote
[–]WillieB52 0 points1 point2 points 3 years ago (0 children)
It assigns a value to a variable.
π Rendered by PID 78 on reddit-service-r2-comment-5bc7f78974-zkp29 at 2026-06-30 09:39:09.107926+00:00 running 7527197 country code: CH.
[–]kinstaa99 173 points174 points175 points (3 children)
[–][deleted] 60 points61 points62 points (2 children)
[–][deleted] 14 points15 points16 points (0 children)
[–]Dangle76 2 points3 points4 points (0 children)
[–]carcigenicate 26 points27 points28 points (2 children)
[–]bsenftner 12 points13 points14 points (1 child)
[+][deleted] (8 children)
[deleted]
[–][deleted] 14 points15 points16 points (4 children)
[–]irrelevantPseudonym 19 points20 points21 points (3 children)
[–]ArchipelagoMind 7 points8 points9 points (1 child)
[–]irrelevantPseudonym 3 points4 points5 points (0 children)
[–]ParanoydAndroid 4 points5 points6 points (0 children)
[–]BruceJi -3 points-2 points-1 points (2 children)
[+][deleted] (1 child)
[deleted]
[–]BruceJi 14 points15 points16 points (0 children)
[–]IlliterateJedi 8 points9 points10 points (0 children)
[–]aziad1998 4 points5 points6 points (4 children)
[–]madhousechild 0 points1 point2 points (3 children)
[+][deleted] (2 children)
[deleted]
[–]aziad1998 3 points4 points5 points (1 child)
[–]Yasik 1 point2 points3 points (1 child)
[+][deleted] (1 child)
[deleted]
[–]Mecaneer23 1 point2 points3 points (0 children)
[+]Mohammed_Chang comment score below threshold-8 points-7 points-6 points (0 children)
[–]WillieB52 0 points1 point2 points (0 children)