Ternary operator
The ternary operator is shorthand for if else
Syntax:
expression1 if condition1 else expression2
Traditional way
Python3
age = 18 eligible = False if age > = 18 : eligible = True else : eligible = False |
One Liner
Python3
age = 18 eligible = True if age > = 18 else False |
If the condition in the if statement is true, the first expression is returned otherwise expression in the else part is returned.
10 Useful Python One Liners That Developers Must Know
Python is known for its easy-to-code and intuitive syntax. And one-liners are like the cherry on the cake which makes Python a more beautiful and beloved programming language. There are numerous features in Python Language and one of its most loved features is its one-liners. Writing Code in Python is already easy compared to other programming languages and using the one-liners makes it more easier and cool. Now let’s see what are these Python one-liners.
Contact Us