variable = value
to assign a value to a variable.print first, second, third
to display values.#
starts a comment.help(thing)
displays help.len(thing)
produces the length of a collection.[value1, value2, value3, ...]
creates a list.list_name[i]
selects the i'th value from a list.Create a for
loop to process elements in a collection one at a time:
for variable in collection:
...body...
Create a conditional using if
, elif
, and else
:
if condition_1:
...body...
elif condition_2:
...body...
else:
...body...
==
to test for equality.X and Y
is only true if both X and Y are true.X or Y
is true if either X or Y, or both, are true.assert condition, message
to check that something is true when the program is running.def name(...params...)
defines a new function.def name(param=default)
specifies a default value for a parameter.name(...values...)
.import libraryname
.sys
library contains:
sys.argv
: the command-line arguments a program was run with.sys.stdin
, sys.stdout
: standard input and output.glob.glob(pattern)
returns a list of files whose names match a pattern.import numpy
to load the NumPy library.array.shape
gives the shape of an array.array[x, y]
selects a single element from an array.low:high
specifies a slice including elements from low
to high-1
.array.mean()
, array.max()
, and array.min()
calculate simple statistics.array.mean(axis=0)
calculates statistics across the specified axis.