Python is a dynamic object-oriented programming language with extensive standard libraries. These notes summarize the Python 3.1 tutorial.
define func(n,str="default"):
define func(formal1, formal2, *arguments **keywords)
vec = [2, 4, 6] [[x, x**2] for x in vec if x > 2] [[4, 16], [6, 36]] [x+y for x in vec for y in vec2 if x > 2]
open('/tmp/workfile', 'w')
while a<10: pass
for x in list: pass for x in range(1,5): pass
if a›10: pass elif a›5: pass else:
import fib from fib import fib1, fib2
try: result = x / y except ZeroDivisionError: print("division by zero!") except Exception as inst: print(type(inst)) # the exception instance print(inst.args) # arguments stored in .args print(inst) # also prints .args else: print("result is", result) finally: print("executing finally clause")
raise Exception('mine')
or just raise
in an exception handler.with
can be used to automatically call the cleanup actions of an object e.g. with open("myfile.txt") as f: for line in f: print(line)