Python Syntax Cheat Sheet

Written by Coursera • Updated on

Use this Python Syntax cheat sheet to troubleshoot errors and remember their common causes.


Reserved keywords

NoneTrueFalseandasassertbreak
classcontinuedefforfinallyexecexcept
elseelifdelfromglobalifimport
inislambdatryreturnraiseprint
passornotyieldwithwhilenonlocal

Basic rules for Python keywords

  • All keywords above except None, True, and False should be in lowercase.
  • Reserved keywords cannot be used as function names, variable names, or identifiers for other elements.

Tip: In other programming languages, true and false are also written in lowercase. But in Python, they are always capitalized.

How to check for Python keywords

  • Type >>> import keyword then >>> keyword.kwlist in interpreter to check the keyword list.
  • Use this Python 3 module to determine if a string is a keyword or a soft keyword. Here's an example:

Python keywords

Python punctuation rules

  • Indentation is crucial. Python uses it to indicate a block of code.
  • The same quote you use to begin a string literal must also be used to end it.
  • Use triple-quotes on each side of multi-line strings.
  • Use commas after dictionary elements.
  • Punctuation marks cannot be used inside identifiers.
  • The Python language is case sensitive.
  • Use square brackets to create lists.

How to tell the difference between a syntax error and an exception

There are two types of errors in Python:

  • Syntax errors: Syntax errors are raised during the parsing stage. They indicate that you have entered a character or string that the interpreter doesn’t understand.

  • Exceptions: Exceptions occur during execution. Your code may be syntactically correct but the program is encountering a problem during runtime, causing an error message to be displayed.

Troubleshooting checklist

Are my keyword arguments in the correct order?
Am I missing or misspelling a keyword?
Is my spacing and indentation consistent?
Do my quotes match?
Have I closed all parentheses and brackets?
Is my programming environment set up properly?

Don’t forget that you can stay up to date on Python releases and tips by getting involved with the Python community. Try subscribing to the free Python email newsletter or connecting with your peers on the Python programming Slack channel.


Written by Coursera • Updated on

This content has been made available for informational purposes only. Learners are advised to conduct additional research to ensure that courses and other credentials pursued meet their personal, professional, and financial goals.

Learn without limits