What does the colon (: =) mean in Python?

What does the operand := mean, more specifically for Python?

Can someone explain how to read this piece of code?

 node := root, cost = 0 frontier := priority queue containing node only explored := empty set 
+23
source share
4 answers

What you found is pseudo code

http://en.wikipedia.org/wiki/Pseudocode

Pseudo - code is an unofficial high-level description of the current principle of a computer program or other algorithm.

The := operator := actually an assignment operator. In python, this is just an = operator.

To translate this pseudo code into Python, you need to know the references to data structures and a little more implementation of the algorithm.

Some notes about psuedocode

:= - assignment operator or = in python

= - equality operator or == in python

Please note that there are certain types of pseudocode and your mileage may vary:

Pascal Style PseudoCode

 procedure fizzbuzz For i := 1 to 100 do set print_number to true; If i is divisible by 3 then print "Fizz"; set print_number to false; If i is divisible by 5 then print "Buzz"; set print_number to false; If print_number, print i; print a newline; end 

C-style pseudo-code

 void function fizzbuzz For (i = 1; i <= 100; i++) { set print_number to true; If i is divisible by 3 print "Fizz"; set print_number to false; If i is divisible by 5 print "Buzz"; set print_number to false; If print_number, print i; print a newline; } 

Note the differences in the use of parentheses and the assignment operator.

+11
source

PEP572 proposed support for the := operator in Python to allow variable assignment in expressions.

This syntax will be available in Python 3.8.

+39
source

The code in the question is pseudo code; there := represents the destination.

For future visitors, however, the following may be appropriate: the next version of Python (3.8) will receive a new operator,: := , which allows us to express assignments (details, examples of motivation and discussion) in PEP. 572 , which was provisionally adopted at the end of June 2018).

With this new operator, you can write something like this:

 if (m := re.search(pat, s)): print m.span() else if (m := re.search(pat2, s): … while len(bytes := x.read()) > 0: … do something with 'bytes' [stripped for l in lines if len(stripped := l.strip()) > 0] 

instead of them:

 m = re.search(pat, s) if m: print m.span() else: m = re.search(pat2, s) if m: … while True: bytes = x.read() if len(bytes) <= 0: return … do something with 'bytes' [l for l in (l.stripped() for l in lines) if len(l) > 0] 
+25
source

Happy 3.8 October 14 release

There is a new syntax: =, which assigns values ​​to variables as part of a larger expression. He is affectionately called the "walrus operator" because of its resemblance to the walrus's eyes and fangs.

In this example, an assignment expression helps to avoid double calling len ():

if (n: = len (a))> 10: print (f "The list is too long ({n} items, expected & lt; = 10)")

https://docs.python.org/3/whatsnew/3.8.html

0
source

Source: https://habr.com/ru/post/975729/


All Articles