Last updated: Apr 8, 2024
Reading time·4 min
The Python "SyntaxError: invalid syntax" is often caused when we use a single equals sign instead of double equals in an if statement.
To solve the error, use double equals ==
if comparing values and make sure
the line of the if
statement ends with a colon.
Here is an example of how the error occurs.
name = 'Bobby Hadz' # ⛔️ SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='? if name = 'Bobby Hadz': print('success')
The error is caused because we used a single equals sign instead of double equals.
If comparing values, make sure to use double equals.
name = 'Bobby Hadz' # ✅ Using double equals when comparing if name == 'Bobby Hadz': # 👇️ This runs print('success')
Always use double equals (==) when comparing values and single equals (=) when assigning a value to a variable.
# ✅ Assignment single (=) equals name = 'bobby hadz' # ✅ Comparison double (==) equals if name == 'bobby hadz': print('success')
if
statement ends with a colonMake sure the line of the if
statement ends with a colon :
.
name = 'bobby hadz' if name == 'bobby hadz': # 👈️ Must end with colon print('success')
If you forget the colon at the end of the line, the error occurs.
Another common cause of the error is forgetting to indent your code properly.
name = 'bobby hadz' if name == 'bobby hadz': print('one') # ⛔️ Badly indented code print('two')
Use a tab to indent your code consistently in the entire if
block.
name = 'bobby hadz' if name == 'bobby hadz': print('one') # ✅ Proper indentation print('two')
The code in the if
statement should be consistently indented.
Never mix tabs and spaces when indenting code in Python because the interpreter often has issues with lines indented using both tabs and spaces.
You can either use tabs when indenting code or spaces, but never mix the two.
If you need to leave a code block empty before you get to implement it, use a
pass
statement.
my_str = '100' if int(my_str) == 100: print('success') else: pass
The pass statement does nothing and is used when a statement is required syntactically but the program requires no action.
class Employee: pass
Using a pass
statement is necessary because leaving the else
block empty
would cause a SyntaxError
.
my_str = '100' if int(my_str) == 100: print('success') else: # ⛔️ Error
if
statement might be causing the errorIf the error is not resolved, look at the code that's right above the if
statement.
Make sure you haven't forgotten to close a parenthesis )
, a square bracket ]
or a curly brace }
.
my_str = '100' # ⛔️ SyntaxError: invalid syntax if int(my_str == 100: print('success')
The code sample above has a missing closing parenthesis, which causes the error.
Make sure all your quotes ""
, parentheses ()
, curly braces {}
and square
brackets []
are closed.
my_str = '100' # ✅ Has a closing parenthesis now if int(my_str) == 100: print('success')
else
statement should also end with a colonIf you have an if/else
statement, make sure the else
statement also ends
with a colon and its code is indented consistently.
name = 'Alice' if name == 'Alice': # 👇️ This runs print('success') else: print('failure')
If you have an if
or else
statement that you haven't yet implemented, use a
pass
statement.
name = 'Alice' if name == 'Alice': print('success') else: pass
The pass statement does nothing and is used when a statement is required syntactically but the program requires no action.
You might get a SyntaxError
exception if you have spelling errors.
Python is case-sensitive, so make sure to use lowercase keywords like if
and
else
instead of If
or Else
.
my_str = '100' # ✅ Keywords are case-sensitive if int(my_str) == 100: print('success') else: print('condition not met')
I've also written an article on how to check for multiple conditions in an if statement.
You can learn more about the related topics by checking out the following tutorials: