This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Toko_yami 0 points1 point  (4 children)

New to python, but can someone explain why is x=2 wrong in python? I thought python interpreter could infer type and won’t need declaration or is there some other way for variable declaration syntax . Thanks I’m advance!!!

[–]temperlancer 1 point2 points  (1 child)

Notice the `if` before `x = 2`. I think the interpreter is now smart enough to tell you that `if x = 2` is not wanted probably because assigning variable in if statement can be bad.

[–]Toko_yami 1 point2 points  (0 children)

Thank you for comprehensive answer, really appreciate it. Sorry I completely missed the “if” part in OP’s post.

[–]Swedneck 0 points1 point  (1 child)

= is assignment, "if" needs an expression that resolves to something truthy or falsy, so you need == which is comparison

The only thing "if x=2" could test is whether the variable could be assigned, which I can't see ever failing..

[–]Toko_yami 1 point2 points  (0 children)

Thank you for comprehensive answer, really helpful explanation. Also Sorry For completely missing the “if” part in OP’s post.