Learning Scientific Programming with Python (2nd edition)
E4.1: Assignment vs. the comparison operator
A common syntax error experienced by beginner Python programmers is in using the assignment operator = instead of the equality operator == in a conditional expression:
>>> if a = 5:
File "<stdin>", line 1
if a = 5:
^
SyntaxError: invalid syntax
This assignment a = 5 does not return a value, it simply assigns the integer object 5 to the variable name a, and so there is nothing corresponding to True or False that the if statement can use: hence the SyntaxError. This contrasts with the C language in which an assignment returns the value of the variable being assigned (and so the statement a = 5 evaluates to true). This behaviour is the source of many hard-to-find bugs and security vulnerabilities and its omission from the Python language is by design.