X = [1, 2, 3) y = 4x + 3 a = 4 something ==== is wrong print(a) print(Q) x = 1 + 'abc' X = 1 / 0 import numpy as np np.add(1, 2, 3, 4) x = [1, 2, 3] print(x[100]) spam = "my all-time favorite" eggs = 1 / 0 print(spam) from math import sqrt def approx_pi(nterms=100): k = np.arange(nterms) return sqrt(12) * np.sum(-3.0 ** -k / (2 * k + 1)) approx_pi(100) approx_pi(1000) try: print("this block gets executed first") except: print("this block gets executed if there's an error") try: print("this block gets executed first") x = 1 / 0 # ZeroDivisionError print("we never get here") except: print("this block gets executed if there's an error") def safe_divide(a, b): try: return a / b except: print("oops, dividing by zero. Returning None.") return None print(safe_divide(15, 3)) print(safe_divide(1, 0)) safe_divide(15, 'three') def better_safe_divide(a, b): try: return a / b except ZeroDivisionError: print("oops, dividing by zero. Returning None.") return None better_safe_divide(15, 0) better_safe_divide(15, 'three') def even_better_safe_divide(a, b): try: return a / b except ZeroDivisionError: print("oops, dividing by zero. Returning None.") return None except TypeError: print("incompatible types. Returning None") return None even_better_safe_divide(15, 3) even_better_safe_divide(15, 0) even_better_safe_divide(15, 'three') import os # the "os" module has useful operating system stuff def read_file(filename): if not os.path.exists(filename): raise ValueError("'{0}' does not exist".format(filename)) f = open(filename) result = f.read() f.close() return result %%file tmp.txt this is the contents of the file read_file('tmp.txt') read_file('file.which.does.not.exist') class NonExistentFile(RuntimeError): # you can customize exception behavior by defining class methods. # we won't discuss that here. pass def read_file(filename): if not os.path.exists(filename): raise NonExistentFile(filename) f = open(filename) result = f.read() f.close() return result read_file('tmp.txt') read_file('file.which.does.not.exist') try: print("doing something") except: print("this only happens if it fails") else: print("this only happens if it succeeds") try: print("doing something") raise ValueError() except: print("this only happens if it fails") else: print("this only happens if it succeeds") try: print("do something") except: print("this only happens if it fails") else: print("this only happens if it succeeds") finally: print("this happens no matter what.") try: print("do something") raise ValueError() except: print("this only happens if it fails") else: print("this only happens if it succeeds") finally: print("this happens no matter what.") try: print("do something") except: print("this only happens if it fails") else: print("this only happens if it succeeds") print("this happens no matter what.") def divide(x, y): try: result = x / y except ZeroDivisionError: print("division by zero!") return None else: print("result is", result) return result finally: print("some sort of cleanup") divide(15, 3) divide(15, 0) def entropy(p): p = np.asarray(p) # convert p to array if necessary items = p * np.log(p) return -np.sum(items) p = np.arange(5.) p /= p.sum() entropy(p) def entropy(p): p = np.asarray(p) # convert p to array if necessary print(p) items = p * np.log(p) print(items) return -np.sum(items) entropy(p) %%file test_script.py import numpy as np def entropy(p): p = np.asarray(p) # convert p to array if necessary items = p * np.log(p) import IPython; IPython.embed() return -np.sum(items) p = np.arange(5.) p /= p.sum() entropy(p) def entropy(p): import pdb; pdb.set_trace() p = np.asarray(p) # convert p to array if necessary items = p * np.log(p) return -np.sum(items) entropy(p) %%file numbers.dat 123 456 789 def add_lines(filename): f = open(filename) lines = f.read().split() f.close() result = 0 for line in lines: result += line return result filename = 'numbers.dat' total = add_lines(filename) print(total) %debug