Objective:

To understand the importance of Python syntax

Outcomes:

  • All: To recognise incorrect Python syntax
  • Most: To correct Python syntax
  • Some: To recognise different types of error

All programming languages are very particular about syntax such as spelling and punctuation. In Python, for example:

  1. The print() command must be followed by a pair of round brackets, in which is placed the information you want to display.
  2. If you are specifying a string (of characters), it must be surrounded by a pair of ” quote symbols, eg. print(“like this”)

Exercises


Edit the Python program above, as follows:

  1. Remove the first quote before “Hello World” and re-run the program. Note that the output window on the right correctly identifies: SyntaxError: invalid syntax
  2. Replace the first quote, and remove the second quote after “Hello World”. The program now reads print("Hello World). Re-run the program. Python reports: SyntaxError: EOL while scanning string literal. This means there is a syntax error (ie no closing quote), and that Python has reached the End Of File (EOF) before it could find the closing quote, while it was scanning the literal string “Hello World”.
  3. Replace the second quote, and remove the round brackets. The program now reads print "Hello World". Re-run the program. Python reports: SyntaxError: Missing parentheses in call to ‘print’; it has identified the syntax error, that the round brackets (parentheses) are missing, when it tried to execute (or call) the print command.

Syntax Keywords

  • brackets: punctuation symbols, that may be (round), {curly} or [square]
  • EOF: End of File
  • parentheses: round brackets
  • quote symbol: the " character, not to be confused with smart typographic quote: . Often the “double quote marks” are distinguished from the ‘single quote marks’, and the latter is sometimes just called an apostrophe mark
  • syntax: the format of the code required by a computer language, so that it is understood