Python Reference
Basic Operations
- Use
variable = valueto assign a value to a variable. - Use
print first, second, thirdto display values. - Python counts from 0, not from 1.
#starts a comment.- Statements in a block must be indented (usually by four spaces).
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.
Control Flow
Create a
forloop to process elements in a collection one at a time:for variable in collection: ...body...Create a conditional using
if,elif, andelse:if condition_1: ...body... elif condition_2: ...body... else: ...body...- Use
==to test for equality. X and Yis only true if both X and Y are true.X or Yis true if either X or Y, or both, are true.Use
assert condition, messageto check that something is true when the program is running.
Functions
def name(...params...)defines a new function.def name(param=default)specifies a default value for a parameter.- Call a function using
name(...values...).
Libraries
- Import a library into a program using
import libraryname. - The
syslibrary 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.
Arrays
import numpyto load the NumPy library.array.shapegives the shape of an array.array[x, y]selects a single element from an array.low:highspecifies a slice including elements fromlowtohigh-1.array.mean(),array.max(), andarray.min()calculate simple statistics.array.mean(axis=0)calculates statistics across the specified axis.