Each statement is generally a single line (statements can be stacked on a line with semicolons but this is unusual).
Standard 'inline' comments (text for humans) start # # This is an inline comment.
'Block' comments across multiple lines start """ and end """, or ''' and end '''
"""
This is a block comment.
"""
You can have as many blank lines as you like to structure code.
Compound statements
In many languages, lines have some kind of end of line marker (e.g. a semi-colon) and code is divided into control flow sections by braces/curly brackets:
if (a < 10) {
alert(a);
}
alert("done");
But this allows errors (a missing bracket or semicolon can change the meaning), and requires coders to indent code inside the sections to make it clear where the control flow is.
In Python, lines are generally one statement per line and are grouped in blocks by indenting and dedenting.
You can use spaces or tabs, and as many as you like of either, but be consistent. We recommend using 4 spaces.
In theory, this means formatting and syntax are one. In practice, the interpreter basically looks at whether indenting is more or less than previous lines, this works in some versions of Python:
if a < 10 :
print (a) # Mix of tabs / spaces
print ("done")
But it isn't good practice. Align blocks at the same flow control:
if a < 10 :
print (a)
print ("done")
Overall, this causes less bugs, but you need to make sure your indenting is correct or the control flow changes.
This:
if a < 10 :
print (a)
print ("done")
Is completely different to this:
if a < 10 :
print (a)
print ("done")
Delimiters
Newlines and indenting/dedenting at the start of a line are special kinds of delimiter-ish things, as are '"#\
Others (some of which also act as operators):
( ) [ ] { }
, : . ; @ = ->
+= -= *= /= //= %= @=
&= |= ^= >>= <<= **=
Enclosing delimiters
Python especially uses three style of enclosing delimiters. These are what the Python documentation calls them:
{} braces # Sometimes called curly brackets elsewhere. [] brackets # Sometimes called square brackets elsewhere. () parentheses # Sometimes called curved brackets elsewhere.
Line breaks
Lines can be joined together into a single line with a backslash, \ provided this isn't part of a piece of non-comment text or followed by a comment.
Lines inside {} () [] can also be split (and commented); though non-comment text needs quotemarks around each line.
Triple quoted text may be split across lines, but the formatting (spaces; linebreaks) will be kept in the text when used (i.e. if it's not a comment).
print \
("""
Daisy, Daisy,
Give me your answer, do...
""")
Keywords
The following are reserved for special uses and can't be used for anything else:
False class finally is return
None continue for lambda try
True def from nonlocal while
and del global not with
as elif if or yield
assert else import pass
break except in raise
Import
Import adds libraries of code (modules in Python) so we can use them.
Usually at the top of the file we add:
import module_name
For example:
import builtins
From then on we can use code inside the library. We'll come back to imports when we look at the standard library.
Identifiers
These are labels stuck on things.
The main use of identifiers is in constructing variables.
But they are also used for naming, for example, procedures, or as Python calls them functions.
Builtins
As well as keywords, there are some functions (like print()) built into the standard distribution. These are in a module called builtins.
The shell loads this up automatically, but hides it with the name __builtins__ .
This double-underscore notation is used in Python to hide things so they aren't accidentally altered. It is called 'dunder' notation
It's easier to deal with if you import it normally. To see what's inside, enter:
>>> import builtins
>>> dir(builtins)
dir() is one of the builtin functions. Used with a module name it tells you what functions and variables are available inside the module. Used on its own, it tells you what identifiers are currently being used.