if (n=2+3)==5:
print(n)
I executed the above code in python but it was showing an error in "if condition"`. I want to know whether there is a way to achieve writing the initilization statement in "if condition".
if (n=2+3)==5:
print(n)
I executed the above code in python but it was showing an error in "if condition"`. I want to know whether there is a way to achieve writing the initilization statement in "if condition".
In python,we can use walrus operator ( := ) but it was introduced in python 3.8 , so it works only in python 3.8 or latter versions.
if (n:=2+3)==5: print(n)
This works fine and gives output as you expected.
Basically what you can place after the 'if' keyword is an expression that either evaluates to true or false. This expression is made up of comparison and logical operators. Ther is no scope for using an assignment operator here. Even if you are thinking of doing that you are following a bad programming practice. Good programming practices are always appreciated so avoid bad ones.Thanks!