Language structure
A language has words, grammar and meaning
Lexis: the words. Syntax: the order. Semantics: the meaning.
Every language, human or programming, has three layers. Lexis is its vocabulary: the words and symbols that exist. A dollar sign is not one of Python's. Syntax is its grammar: the order the words must come in. A plus with nothing after it breaks that. Semantics is meaning. Adding text to a number is spelt correctly and well formed, but it does not mean anything.
Language structure · Going deeper
Four kinds of error
SyntaxError before running. The rest are yours to find.
Running code
A compiler translates first. An interpreter, as it runs.
Compiler: all at once. Interpreter: statement by statement.
A computer cannot run source code directly. There are two ways to get it translated. A compiler translates the whole program once, into a machine code file. That file then runs on its own, but only on the platform it was made for. An interpreter reads the source and carries it out as it goes. Python works this way, so you need Python installed to run a Python program.
Running code · Going deeper
What Python does with your file
Check all, then run line by line.
Check yourself
What happens when this runs?
Program structure
Keywords are reserved. Every other name is yours.
Keyword, identifier, expression, statement.
Take one line apart. if is a keyword: a reserved word with a fixed job. count is an identifier: a name the programmer chose. The greater-than sign is an operator. Three is a literal: a value written out. The colon says a block follows. Count greater than three is an expression: something that works out to a value. And the whole line, with its block, is a statement: an instruction that does something.
Program structure · Going deeper
The 35 keywords
True, False, None have capitals. print is not one.
Indentation
Indentation is the block
A colon opens a block. Four spaces put a line in it.
Most languages mark a block with brackets. Python uses the indentation itself. Here the second print is not indented, so it is outside the loop. It runs once, at the end. Indent it by four spaces, and it is inside the loop. Now it runs every time round. Same lines, different program.
Indentation · Going deeper
Indentation errors
IndentationError: nothing runs.
Check yourself
What happens when this runs?
Comments
A comment is for the reader. Python skips it.
# to the end of the line. Not inside quotes.
A hash sign starts a comment, and Python ignores everything from there to the end of the line. Look at what is left of these five lines. The first line is all comment, and the note after two hundred and fifty is dropped: only the assignment runs. The third line is a statement that has been switched off. The fourth has a hash inside quotes, which is just text. So this prints two hundred and fifty, then hash one seller.
Comments · Going deeper
Using comments
Explain, note, disable.
Check yourself
What does this print?
Lesson complete
You finished “How Python reads and runs a program”
Answer the questions along the way to see your score here.