Posts: 4
Threads: 2
Joined: May 2017
Hi all
python 2.7
can someone explain the logic here I am confused  (doesn't take much to confuse me
In the first example everything works OK.But the second example despite the fact that x is a variable
it doesn't work.Now the question here is this...does python have strange logic...or do I 
example 1
a = 1
print a
a +=1
print a example 2
e = 10
print e
x = e +=1
print x kind regards
al
Posts: 8,156
Threads: 160
Joined: Sep 2016
In example 2, line 3
x = e +=1 rise SyntaxError: Invalid syntax (of course).
What you expect this line to do?
Posts: 26
Threads: 0
Joined: Apr 2017
You are allowed to do:
x = e+1 but not
x = e += 1 because x = e += 1 is same as
x = e = e + 1
Posts: 8,156
Threads: 160
Joined: Sep 2016
May-03-2017, 10:39 AM
(This post was last modified: May-03-2017, 10:40 AM by buran.)
(May-03-2017, 10:34 AM)idontreallywolf Wrote: because x = e += 1 is same as
x = e = e + 1
actually, the first one is invalid syntax and rise SynatxError,
while second one is perfectly fine
e = 10
x = e = e + 1
print x
print e Output: 11
11
Posts: 26
Threads: 0
Joined: Apr 2017
(May-03-2017, 10:39 AM)buran Wrote: (May-03-2017, 10:34 AM)idontreallywolf Wrote: because x = e += 1 is same as
x = e = e + 1
actually, the first one is invalid syntax and rise SynatxError,
while second one is perfectly fine
e = 10
x = e = e + 1
print x
print e Output: 11
11
Then why would x = e += 1 be invalid if its the same as x = e = e + 1 ?
Posts: 8,156
Threads: 160
Joined: Sep 2016
May-05-2017, 06:53 AM
(This post was last modified: May-05-2017, 06:53 AM by buran.)
(May-05-2017, 06:44 AM)idontreallywolf Wrote: Then why would x = e += 1 be invalid if its the same as x = e = e + 1 ?
You are the one that repeatedly claims they are the same, not me.... :-)
x = e = e + 1 is called chained assignment and is valid python code.
Posts: 566
Threads: 10
Joined: Apr 2017
(May-05-2017, 06:53 AM)buran Wrote: (May-05-2017, 06:44 AM)idontreallywolf Wrote: Then why would x = e += 1 be invalid if its the same as x = e = e + 1 ?
You are the one that repeatedly claims they are the same, not me.... :-)
x = e = e + 1 is called chained assignment and is valid python code.
Assignment in Python - unlike in C - does not return value - that why in Python if x=3 is illegal too, and Python programmers don't have to write
if 3 == x in x = e += 1 right operand is assignment - unlike in the example above, where right operand is e + 1
Test everything in a Python shell (iPython, Azure Notebook, etc.) - Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
- Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
- You posted a claim that something you did not test works? Be prepared to eat your hat.
Posts: 8,156
Threads: 160
Joined: Sep 2016
May-05-2017, 07:22 AM
(This post was last modified: May-05-2017, 07:26 AM by buran.)
@volcano63, I don't understand what you mean that Python programmers don't have to write if 3 == x and once again x = e += 1 is invalid, raising SyntaxError.
Posts: 566
Threads: 10
Joined: Apr 2017
(May-05-2017, 07:22 AM)buran Wrote: @volcano63, I don't understand what you mean that Python programmers don't have to write if 3 == x Many C/C++ programmers acquire the habit to write if (3 == x) instead of if (x == 3) , because if you accidentally omit = , the code will still compile - but the meaning will change.
if (3 = x) is illegal assignment, failing compilation.
(May-05-2017, 07:22 AM)buran Wrote: and once again x = e += 1 is invalid, raising SyntaxError. I thought I was enhancing your point, not arguing it.
Test everything in a Python shell (iPython, Azure Notebook, etc.) - Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
- Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
- You posted a claim that something you did not test works? Be prepared to eat your hat.
Posts: 3,458
Threads: 101
Joined: Sep 2016
(May-05-2017, 01:03 PM)volcano63 Wrote: if (3 = x) is illegal assignment, failing compilation. I do this all the time, because I like when the compiler tells me I'm stupid. I believe it's commonly referred to as yoda checking, because it sounds backwards. ( https://en.wikipedia.org/wiki/Yoda_conditions)
|