Python compound statements | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Python compound statements

How would I compress this so I only have anotherVar one time? someVar = anotherVar if (cond1 >= 0) else anotherVar Thanks

22nd Oct 2019, 8:44 AM
Branam
Branam - avatar
2 Answers
+ 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?
22nd Oct 2019, 9:13 AM
Ipang
+ 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?
22nd Oct 2019, 9:15 AM
HonFu
HonFu - avatar