Syntax
Contents
Syntax#
This short tutorial is a very brief exploration of the essential features of Python syntax. It is mainly based on content from the App Tinkerstellar by Alex Staravoitau and Jack VanderPlas’s Whirlwind Tour of Python.
Syntax refers to the structure of the language (i.e., what constitutes a correctly-formed program).
Here, we’ll not focus on the semantics – the meaning of the words and symbols within the syntax.
# set the midpoint
midpoint = 5
# make two empty lists
lower = []
upper = []
# split the numbers into lower and upper
for i in range(10):
if (i < midpoint):
lower.append(i)
else:
upper.append(i)
print("lower:", lower)
print("upper:", upper)
lower: [0, 1, 2, 3, 4]
upper: [5, 6, 7, 8, 9]
Statements#
End-of-line terminates a statement in Python
midpoint = 5
This is an assignment operation, where we’ve created a variable named midpoint and assigned it the value 5.
Notice that the end of this statement is simply marked by the end of the line.
If you’d like a statement to continue to the next line, it is possible to use the “" marker to indicate this:
x = 1 + 2 + 3 + 4 +\
5 + 6 + 7 + 8
print(x)
36
It is also possible to continue expressions on the next line within parentheses:
x = (1 + 2 + 3 + 4 +
5 + 6 + 7 + 8)
print(x)
36
Most Python style guides recommend the second version of line continuation (within parentheses)
Indentation#
A block of code is a set of statements that should be treated as a unit
Code blocks are denoted by indentation (white space)
The amount of whitespace used for indenting code blocks is up to the user, as long as it is consistent throughout the script
By convention, most style guides recommend to indent code blocks by four spaces
Indented code blocks are always preceded by a colon (:) on the previous line
midpoint = 5
for i in range(10):
if i < midpoint:
lower.append(i)
else:
upper.append(i)
In the snippet below, print(x) is in the indented block, and will be executed only if x is less than 4
x=5
if x < 4:
y = x * 2
print(x)
In the snippet below, print(x) is outside the block, and will be executed regardless of the value of x
x=5
if x < 4:
y = x * 2
print(x)
5
Whitespace within lines#
White space within lines of Python code does not matter
For example, all three of these expressions are equivalent
a=1+2
a = 1 + 2
a = 1 + 2
print(a)
3
Using whitespace effectively can lead to much more readable code
b=10**-2
b = 10 ** -2
Parentheses#
Parentheses are used for grouping or calling
They can be used in the typical way to group statements or mathematical operations:
2 * (3 + 4)
14
They can also be used to indicate that a function is being called.
In the next snippet, the
print()
function is used to display the contents of a variable.The function call is indicated by a pair of opening and closing parentheses, with the arguments to the function contained within:
print('Value:', 1)
Value: 1
Some functions can be called with no arguments at all, in which case the opening and closing parentheses still must be used to indicate a function evaluation.
An example of this is the sort method of lists:
L = [4,2,3,1]
L.sort()
print(L)
[1, 2, 3, 4]
The “()” after sort indicates that the function should be executed, and is required even if no arguments are necessary.
Style guides#
The most widely used style guide in Python is known as PEP8, and can be found at https://www.python.org/dev/peps/pep-0008/
Comments#
Comments in Python are marked by #
Anything on the line following the # sign is ignored by the interpreter
# set the midpoint