in c/c++, I can "assign a value to a variable" in if-statement as follows:
int a;
if ( (a = foo()) > 0)
{
printf("%d\n", a);
}
Is there any equivalent statement for python?
Not really. In Python, an assignment is a statement, whereas in C it is an expression. The condition in the if statement must be an expression in both languages.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With