I heard this behavior is because python's integers are immutable.
++x or x++ will redefine 5 to 6, which the interpreter forbids ...
x = 55
x ++5
x = 55
++ x6
x = 55
y = (x += 1, x - 1)5
x = 55
y = (x += 1, x)6
On Thu, 07 Nov 2024 12:55:53 +0530, Annada Behera wrote:
I heard this behavior is because python's integers are immutable.
Nothing to do with that.
++x or x++ will redefine 5 to 6, which the interpreter forbids ...
One of those is actually syntactically valid.
It just won’t do what you expect it to do.
Well you can use your Browser, since
JavaScript understand post and pre increment:
So we have x ++ equals in Python:
x + = 1
x - 1
But I don't know how to combine an
assignment and an expression into one
expession. In JavaScript one can use
the comma:
x = 55
y = (x += 1, x - 1)5
x = 55
y = (x += 1, x)6
But in Python the comma would create a tuple.
6x = 5
y = (x := x + 1); x
(6, 6)x, y
(6, 6)x = 5 # define
x += 1 # increment
y = x # alias
x, y
5x = 5
(x := x + 1) - 1
6x
--- Synchronet 3.20a-Linux NewsLink 1.1146x = 5
y = (x := x + 1); x
(6, 6)... irrational drivel removed ..x, y
On 8/11/24 14:40, Mild Shock via Python-list wrote:
Well you can use your Browser, since
JavaScript understand post and pre increment:
Question: are we talking Python or JavaScript?
So we have x ++ equals in Python:
Trying to find a word-for-word translation serves as badly in computer- programming languages as it does in human spoken-languages. Learn how to adapt and embrace the differences...
x + = 1
x - 1
The above probably only 'works' (the way you expect) in the REPL.
But I don't know how to combine an
assignment and an expression into one
expession. In JavaScript one can use
Again!
"Everything should be made as simple as possible, but no simpler."
Check out "The Zen of Python" and PEP-0008 for Python idioms.
the comma:
x = 55
y = (x += 1, x - 1)5
x = 55
y = (x += 1, x)6
But in Python the comma would create a tuple.
Exactly, just as driving on the left side of the road will be fine in
some countries but cause a crash in others. Learn the local rules FIRST!
The 'walrus operator' could be applied:
6x = 5
y = (x := x + 1); x
(6, 6)x, y
However, if such were submitted for Code Review, unhappiness would result.
Was the question re-phrased to: how to ... in Python, we'd end-up with something more like this:
(6, 6)x = 5 # define
x += 1 # increment
y = x # alias
x, y
(6, 6)x = 5
y = x = x + 1
x, y
Sysop: | DaiTengu |
---|---|
Location: | Appleton, WI |
Users: | 994 |
Nodes: | 10 (0 / 10) |
Uptime: | 97:12:29 |
Calls: | 13,016 |
Calls today: | 2 |
Files: | 186,574 |
Messages: | 3,282,095 |