What does the 'walrus operator' (:=), added in Python 3.8, do?
-
A
Assigns a value inside an expression
Correct answer
-
B
Compares two values for identity
-
C
Swaps the values of two variables
-
D
Declares a constant that cannot change
Correct answer
A. Assigns a value inside an expression
20 views
Explanation
Assignment expressions allow code such as while (line := f.readline()): or if (n := len(data)) > 10:, which avoids calling a function twice.
Reference
Asked in: NTS, Python Developer Interviews
Doubts & discussion
Not sure why the answer is A? Ask here and get an explanation.
Ask a doubt about this MCQ