Python expression

I need to declare two variables in a single assignment statement a=1,b=a+1.But when I do
a,b=1,a+1 it says a is not defined,Is it possible to do so?If so how?Also how to decrement a variable inside an expression?

In Python, you cannot assign 2 variables in the same lines like this. You need to use a different line to define b.
Please elaborate on your second question…

In C ,there are post-increment and pre- operators for doing decrement in assignment statement.I need to plug this statement inside an if statement

The online judge will not allow otherwise

I want some expression like if a[j–] == c: do something in Python or if j–== c :

This would be lame… but you can do this… a=1;b=a+1 :stuck_out_tongue:

No, this is not possible. And that’s also good so. Using a result from one line already in the same line again is very confusing. Python is designed to be beautiful and not confusing. These are two logical steps, why should they be on the same line or even in the same expression? Doing such an operation has no advantage other than confuse yourself and other people. (if you know of any advantages, please tell me)

The reason why it doesn’t work, is because Python, like pretty much all other languages, evaluate first the right side of the equal sign, and then afterwards applies the assignment. So it first tries to evaluate 1, a+1 (which should generate a tuple) and fails, because he doesn’t know of the variable a yet.

I’m not quite sure what and why you want to accomplish something like that. If you really only want a=1, b=a+1, why now write a,b=1,2?

To your second question:

No, it is also not possible to decrement a variable inside an expression. Same reason as for the above problem. It is a confusing syntax (e.g. returns j-- the old or the new value?) and not pretty. Just write two lines, first decrement and in the next line do the if statement.

2 Likes