Brief documentation of common elements of Python

Comments:

 # anything after a hash is a comment.

Special Values:

True, False # boolean values


"....." or '.....' or '''.....'''' # strings (the last may include newlines)
anything surrounded by _ and _ is a System defined name place = value # assignment place, place, place = value, value, value # multiple assignment Operators: + - * / * // % # arithmetic * is power, # // and % are integer division and remainder > < >= <= == != # comparison is # object equality and, or, not # logical # Note, 0, empty string/list/tuple are all treated as False # almost everying else is treated as True Built-in functions min(), max() # take a sequence or tuple, or multiple arguments abs(), round() float() int() # convert a string to a number range(i, j) # returns a sequence of integers from i to j-1 random() randint(a, b) choice(sequence) # requires: from random import * Defining Functions: def name(param, param, ....) : statements return value # returns the value
Conditionals:

if condition : statements [ elif condition : statements ] [ else : statements ]

Loops:

while condition : statements

for i in sequence : # ranges are particularly useful sequences statements

break # get out of the for or while loop continue # jump immediately to the next iteration

Sequences: Strings, Lists, Ranges, Tuples:

names[i] # access i'th element of names len(names) # number of items in sequence n in names # true if names contains n (or a value equal to n) (substring for strings) names[i:j] # the subsequence of names from i to j-1 names.index(n) # index of first occurrence of n in names names1 + names2 # concatenate

Strings only: str.capitalize() str.lower() str.upper() str.startswith(substr) str.isnumeric str.split(" ") # return list of words in string

Ranges range(start, stop) : # sequence from start to stop-1 range(stop) : # sequence from 0 to stop-1 range(start, stop, step) : # sequence from start by step up to stop

Lists only: names = [] # empty list names = ["john", "james", "jeremy", "justin"] del names[i] # remove the ith element names1.append(names2) names.extend(newname) names.insert(i, newname)

Input/Output;

print( val, val, ...) # print out values. # Special args: sep=' ' and end='\n' for separator and end input("prompt") # ask for user input and return as a string

Graphical Output: from tkinter import * # make everything in the tkinter module available. global window global canvas window = Tk() canvas = Canvas(window, width=450, height=300, bg = 'white') canvas.pack() canvas.delete(ALL) canvas.create_rectangle(left, top, right, bottom, fill="orange", outline="black") canvas.create_oval(left, top, right, bottom, fill="red") canvas.update()

Event Input Button(window, text="Move", command=doMove).pack()

Files open("file name", mode='r' ) # open a file, mode is 'r' for reading, 'w' # for writing new 'a' for appending file.write(string) # write a string to a file print(values file=file) # write out values to a file, file.readline() # read one line from file as a string file.read(n) # read n characters from file as a string

Exceptions