+ 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

17th Jul 2019, 6:06 PM
Siddhesh.k Jain
Siddhesh.k Jain - avatar
1 Respuesta
+ 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/
17th Jul 2019, 8:13 PM
Mert Yazıcı
Mert Yazıcı - avatar