+ 1
How do i use if and else in one line
Like as in def on_click(x, y, button, pressed): print('{0} at {1}'.format( 'Pressed' if pressed else 'Released', (x, y))) if not pressed: # Stop listener return False
2 Answers
+ 4
if_true if condition else if_false
is equal to
if condition:
if_true
else:
if_false
You can chain them using multiple if & elses like this:
A if C1 else B if C2 else C
is equal to
if C1:
A
else:
if C2:
B
else:
C
Also else part is not executed if condition is True so this will work:
a = 5
print(a if a == 5 else b)
https://www.google.com/amp/s/www.geeksforgeeks.org/ternary-operator-in-JUMP_LINK__&&__python__&&__JUMP_LINK/amp/