0
Python compound statements
How would I compress this so I only have anotherVar one time? someVar = anotherVar if (cond1 >= 0) else anotherVar Thanks
2 Respostas
+ 3
This way, you will get <anotherVar> assigned to <someVar> whether or not <cond1> >= 0 as I understand it. Can you elaborate more on what it is you're trying to do?
+ 2
The pattern of the ternary operator is:
A if condition else B
I'm not sure how you want to make it shorter.
One situation where you can is if the first value turns out False.
x = input() or 'default'
In this case, either the user enters anything, then x will be that, or they just press enter, then the input string is zero length and False, and in that case, x will be default.
Maybe you can describe your specific situation?