I'm new in Python and I have a question. Is this possible to assign if-else statement into a variable in the same line? Thanks.
Asked
Active
Viewed 53 times
0
-
Yes, Python has conditional expressions, see e.g. https://stackoverflow.com/q/394809/3001761. – jonrsharpe Mar 16 '22 at 20:40
2 Answers
0
You can try Python's conditional expression :
a = 1
even = 'true' if a%2==0 else 'false'
You'll find the official documentation for conditional expression here.
TisaneFruitRouge
- 3
- 3
-1
In python,lambda function.
this is example how you can use it.
max = lambda a, b : a if(a > b) else b
print(max(1, 2))
print(max(10, 2))
#output:
#2
#10
QuantumX
- 118
- 9