Bogota
Bogota

Reputation: 391

Inline Initialization of Variable

I have to set the value of exp_valu if the value of val1 is 1. I tried to this using the below code, but somehow it is failing. Where am I making mistake?

val1=1
val2=0

exp_valu = if val1: 1 else: 2
print(exp_valu)

Upvotes: 0

Views: 280

Answers (1)

Leo Arad
Leo Arad

Reputation: 4472

You can try
exp_valu= 1 if val1 == 1 else 2
In line if statmet not excpecting to have: in it.
The one line if statment look like

value if condition else vale

Upvotes: 1

Related Questions