One-Liner If-Else Statements
In Python, one-liner if-else statements are known as ternary operators or conditional operators. Generally, a conditional operator follows this pattern for your convenience: true_expression if condition else false_expression
Python: 3.0    Anurag Rana    04 Jul 2021   0
Login to upvote
1
2
3
4
5
6
7
age = 30
age_group = "Adult" if age > 18 else "Child"

print(age_group)

# output
# Adult
Copy | Download as: .py file   PNG
Share on: FB   Twitter   LinkedIn   WA
©2021 - code108labs   Contributors   Hosted on DigitalOcean   Partner website: pythoncircle.com