Concept

Common Syntax Errors in Python

  • Missing comma after dictionary entry
  • Confusing assignment operator (=) and comparison operator (==)
  • Using Python keyword where unintended*
  • Missing parentheses, brackets, or quotes**
  • Adding extra parentheses or brackets
  • Missing a colon at the end of a for, while, or if statement
  • Improper indentation

*Note: The keyword library can be used to check if something is a Python keyword. To demonstrate,

import keyword as kw kw.iskeyword('class') # returns True

**Note: To initialize a string containing the quote character (e.g. English contractions), you can escape the quote symbol using a backslash:

message = 'can\'t' print(message) # prints "can't"

0

1

Updated 2021-06-29

Tags

Data Science