Syntax are rules that dictate how programming languages should be written. Each language has its own set of syntax rules different from others. We can think of them like grammar or punctuation in spoken languages. The question mark in English (?) differs from the question mark in Greek (;).
You can deduce that when you get a syntax error, you are writing the programming language incorrectly. You may be accidentally omitting or using syntax from a different language, which is something that often occurs as developers grow their tech stack.
In this article we’ll dive into common Python SyntaxErrors, provide examples, and show you how to fix them.
Common SyntaxErrors and How to Fix Them
Dictionaries
Try to figure out why the syntax error was thrown in the below code.
student_gpas = { 'Mary': 3.0, 'Tim': 2.5 'John': 4.0 } print(f'Mary has a {student_gpas["Mary"]} GPA.') # returns SyntaxError: invalid syntax
If you say “missing comma after the key value pair of 2.5”, you are correct. Dictionaries, like objects in JavaScript, need a comma after each key value pair, with the exception of the last key value pair.
The solution to the above error is below.
student_gpas = { 'Mary': 3.0, 'Tim': 2.5, 'John': 4.0 } print(f'Mary has a {student_gpas["Mary"]} GPA.') # returns Mary has a 3.0 GPA.
Quotes
Try to figure out why the syntax error in the below code was thrown.
print('Mary's teacher stated that she has a 3.0 GPA.') # returns SyntaxError: invalid syntax
If you said something along the lines of “use of the same type of quotes within the quote”, you would be correct. There are several ways to avoid a syntax error when using quotes in Python.
1. Using double quotation marks
print("Mary's teacher stated that she has a 3.0 GPA.")
2. Using a backslash
print('Mary\'s teacher stated that she has a 3.0 GPA.')
Using New Syntax with Old Versions of Python
Make sure the syntax you are using is compatible with the version of Python you are using. For example, any Python version prior to 3.6 will not be able to recognize f-strings and will throw an invalid syntax error if you run the syntax. Developers can either update the version, or use compatible syntax for the set version.
student_gpas = { 'Mary': 3.0, 'Tim': 2.5, 'John': 4.0 } print(f'Mary has a {student_gpas["Mary"]} GPA.') # returns SyntaxError: invalid syntax on older versions of Python
Using Python Keywords Inappropriately
The best way to know if you are using a Python keyword inappropriately is to know what those keywords are and what they are used for in Python. For an in depth overview on some Python keywords, check out the articles How to Use the Python not Keyword, The Python Yield Keyword: A Guide, and Python positional argument follows keyword argument Solution.
Try to figure out why the syntax error in the below code was thrown.
continue = True print(continue) # returns SyntaxError: invalid syntax
If you answer “because we are setting a value to a keyword”, you are correct. Continue is a keyword in Python used to continue to the next iteration of a loop. A fix to the above would be to set the boolean True, to a variable name that is not a Python keyword.
on = True print(on) # returns True
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.