How to condense if/else into one line in Python?

753    Asked by AbdulRehman in Python , Asked on Mar 2, 2021
Answered by Abdul Rehman

To condense if/else into one line in Python an example of Python's way of doing "ternary" expressions:-

i = 5 if a > 7 else 0

Translates into:-

if a > 7:

i = 5

else:

i = 0



Your Answer

Interviews

Parent Categories