Python Principles

Syntax Errors

Syntax means the arrangement of letters and symbols in code.

So if you get a syntax error, it usually means you've misplaced a symbol or letter somewhere in your code.

Python will usually tell you which line and where in the line the problem is. Pay very careful attention to the error message. There will be a ^ that points at the precise error point, such as in this example:

print(x)x
        ^

It is completely normal for beginners to make syntax errors all the time. The only thing that helps is practice.

You might want to compare your code with code from the reference sheet to check that your syntax follows the format Python expects.

You can also remove lines of code one at a time until you no longer get a syntax error to pinpoint where the issue is. Simplify or rewrite the offending line until you get rid of the error.

Common causes

  • forgetting to put quotes around a string
  • forgetting a colon : when using if, else, for, while, or def.
  • using = instead of == in a comparison
  • using return and = in the same line
  • mismatched number of open and close parentheses
  • misspelling a keyword or function name
  • inconsistent indentation
  • using a keyword as a variable name
  • using illegal characters (like spaces) in variable names
  • wrong capitalization: remember that Python is case-sensitive

Further reading

"My Code Isn't Working" cheat sheet

Getting Unstuck blog post

Think Python Debugging wiki page

Improve your Python skills fast

The fastest way to learn programming is with lots of practice. Learn a programming concept, then write code to test your understanding and make it stick. Try our online interactive Python course today—it's free!

Learn more about the course

Want to get better at Python quickly? Try our interactive lessons today! Memberships are 100% FREE this week only!