About 48,100 results
Open links in new tab
  1. python - One line if-condition-assignment - Stack Overflow

    Oct 24, 2011 · If one line code is definitely going to happen for you, Python 3.8 introduces assignment expressions affectionately known as “the walrus operator”. someBoolValue and (num := 20) The 20 will be assigned to num if the first boolean expression is True .

  2. Assign within if statement Python - Stack Overflow

    May 6, 2015 · For Python 3.8+, PEP 572 introduces Assignment Expressions This allows assigning to variables within an expression using the notation NAME := expr . It can be used within if statements, for example:

  3. python - How to assign a variable in an IF condition, and then …

    Apr 27, 2019 · The one liner doesn't work because, in Python, assignment (fruit = isBig(y)) is a statement, not an expression. In C, C++, Perl, and countless other languages it is an expression, and you can put it in an if or a while or whatever you like, but not in Python, because the creators of Python thought that this was too easily misused (or abused) to ...

  4. What is meant by assignment is a statement in Python?

    May 22, 2020 · In the official Python documentation, the assignment = is referred to as a statement or an expression. I really don't get it what it means by assignment is a statement. How is assignment a statement?

  5. python - Assignment statement value - Stack Overflow

    Everybody knows that in Python assignments do not return a value, presumably to avoid assignments on if statements when usually just a comparison is intended: >>> if a = b: File "<st...

  6. coding style - Assignment with "or" in python - Stack Overflow

    Jan 6, 2012 · I also feel a bit unconfortable using that kind of expressions. In Learning Python 4ed it is called a "somewhat unusual behavior". Later Mark Lutz says:...it turns out to be a fairly common coding paradigm in Python: to select a nonempty object from among a fixed-size set, simply string them together in an or expression.

  7. python - Can we have assignment in a condition? - Stack Overflow

    Apr 8, 2010 · Note that in Python, unlike C, assignment cannot occur inside expressions. C programmers may grumble about this, but it avoids a common class of problems encountered in C programs: typing = in an expression when == was intended.

  8. python - What are assignment expressions (using the "walrus" or ...

    Since Python 3.8, code can use the so-called "walrus" operator (:=), documented in PEP 572, for assignment expressions. This seems like a really substantial new feature, since it allows this form of assignment within comprehensions and lambdas. What exactly are the syntax, semantics, and grammar specifications of assignment expressions?

  9. Python Multiple Assignment Statements In One Line

    Aug 22, 2015 · All target_list productions (i.e. things that look like foo =) in an assignment statement get assigned, from left to right, to the expression_list on the right end of the statement, after the expression_list gets evaluated. And of course the usual 'tuple-unpacking' assignment syntax works within this syntax, letting you do things like

  10. What does colon equal (:=) in Python mean? - Stack Overflow

    Mar 21, 2023 · The code in the question is pseudo-code; there, := represents assignment. For future visitors, though, the following might be more relevant: the next version of Python (3.8) will gain a new operator, :=, allowing assignment expressions (details, motivating examples, and discussion can be found in PEP 572, which was provisionally accepted in late June 2018).

Refresh