Basic Syntax - jude-lindale/Wiki GitHub Wiki
Basic Syntax
The cleanliness of Python's syntax has led some to call it "executable pseudocode"
Syntax refers to the structure of the language (i.e., what constitutes a correctly-formed program). For the time being, we'll not focus on the semantics – the meaning of the words and symbols within the syntax – but will return to this at a later point.
Syntax Example Script
# 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)
#
Comments are marked by The scrips starts with the comment # set the midpoint
In python comments are marked with the pound sign (#
). Anything on the line following the pound sign is ignored by the interpreter. This means that one can have standalone comments, like the example above, as well as inline comments like the example below.
x += 2 # shorthand for x = x + 2
End-of-Line Terminates a Statment
The following line in the example script is midpoint = 5 This is an assignment operation, where we've created a variable named midpoint and assigned it the value 5
To continue a statement to the next line use \
.
x = 1 + 2 + 3 + 4 +\
5 + 6 + 7 + 8
Alterniavley one can put the expression within parentheses to continue a statement.
x = (1 + 2 + 3 + 4 +
5 + 6 + 7 + 8)
Semicolon Can Optionally Terminate a Statement
it can be useful to put multiple statements on a single line.
lower = []; upper = []
however this is generally discouraged.
Indentation
Blocks of code are denoted by indentation.
for i in range(100):
# indentation indicates code block
total += i
Indented blocks always come after a colon (:
) on the previous line.
The use of indentation helps to enforce the uniform, readable style that many find appealing in Python code.
>>> if x < 4: >>> if x < 4:
... y = x * 2 ... y = x * 2
... print(x) ... print(x)
In the snippet on the left, print(x) is in the indented block, and will be executed only if x is less than 4. In the snippet on the right print(x) is outside the block, and will be executed regardless of the value of x!
Parentheses Are for Grouping or Calling
Parentheses can be used to group statements or mathematical operations.
2 * (3 + 4)
Thye can also be used to indicate that a function is being called such as print()
. the print()
function is used to display the contents of a variable (see the sidebar). The function call is indicated by a pair of opening and closing parentheses, with the arguments to the function contained within.
print('first value:', 1)
first value: 1
Some functions can be called with no arguments, 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.