from IPython.display import Image Image(filename='confused-cat.jpg') Image(filename='big_data.png') a = 23 print("The answer is " + str(a)) b = a + len("confused kitty is confused") print("The answer is now " + str(b)) a = "Goooooooooooooollllll" print("The answer is now " + a) len("This is a string") len(23) x = { "foo": [1,2,3], "bar": "star" } x["foo"] len(x) x += 1 2 + "2" int("2.3") inval = raw_input("Think of a number: ") try: ival = int(inval) print("Yay: an integer {}".format(ival)) except ValueError: try: fval = float(inval) print("Oh, a real-valued number {}".format(fval)) except: print("Errrr: {}".format(inval)) raise ValueError("I did not expect to be sent '{}'".format(inval)) def convert_value(inval): """Convert an input value (a string) into an integer or floating point value, returning the converted value. For other input a ValueError is raised.""" try: val = int(inval) print("Yay: an integer {}".format(val)) except ValueError: try: val = float(inval) print("Oh, a real-valued number {}".format(val)) except: print("Errrr: {}".format(inval)) raise ValueError("I did not expect to be sent '{}'".format(inval)) return val convert_value("23") convert_value("23.0") convert_value("23i") import numpy as np x = np.arange(1, 10, 2) print(x) type(x) dir(x) x.mean() m = x.mean() print("The mean is {0}".format(m)) print(" or {:.3f}".format(m)) print(" or {:13.6e}".format(m)) print(" or even %g" % (m,)) # Oh, don't forget that a # character starts a comment, print("Unless # it is part of a string") print('and that strings start and end with either a single or double quote') # this is back to being a comment """Although you can also make really long "comments" using the triple-quote, as shown here. This is normally used for providing in-line documentation to your code, but can be useful to quickly comment out a region of recalcitrant code, since it creates a string which is then ignored by the program. """ # Let's do something import matplotlib.pyplot as plt x = np.linspace(0, 10, 45) # a one-dimensional array of values y = np.sin(x) * np.exp(-x/5) # this calculation is done on each element of the array plt.plot(x, y) plt.show() %matplotlib inline plt.plot(x, y) plt.plot(x, y, 'r--') plt.xlabel("x") plt.ylabel(r"$sin(x) * e^{-x/5}$") # use LaTeX; to avoid potential confusion with \ characters, use r"..."! help(np.arange) np.*lin*? help(convert_value) np.mean np.pi np.mean() np.pi() help(np.mean) a = np.mean print(x[0:5]) print(np.mean(x)) print(a(x)) help(range) range(1, 10, 2) range = 23.0 range(1, 10, 2) help(range) # Simple scalar types a = 23 # integer a = -2.3e-43 # floating-point a = 4.2+2.3j # complex a = "A string" # strings a = True # booleans, False is also acceptable a = None # None is used to indicate "does not exist"/"invalid"/"undefined"/"missing" # Containers a = [1,2,3,"foo",[1,2]] # list (elements do not need to have the same type) a = { "foo": 1, "bar": { "foobar": True }} # dictionary a = (True, 23.4, "false") # tuples (like lists but are immutable) # Access elements using [...] syntax a = [1,2,3]; print(a[1]); # the first element of a list is numbered 0 a = { "foo": 1 }; print(a["foo"]) a = (True, 23.4, "false"); print(a[0]) # again, first element is numbered 0 print("-----") # We can change elements in a list a = [1,2,3] print(a) a[1] = 4 print(a) # but not in tuples a = (1, 2, 3) print(a) a[1] = 4 # np.arange is like Python's range a = np.arange(12) print(a) # The resize method works "in place" (i.e. changes the object it is called on) a.resize(3,4) print(a) # The reshape method creates a copy of the input array b = np.arange(10, 34, 2) c = b.reshape(3,4) print(b) print(c) # By default many routines will "ignore" the dimensions of the data print("b.sum = {}".format(b.sum())) print("c.sum = {}".format(c.sum())) # Some will work as "reducers" - i.e. here we sum along # one axis, so converting a 2D array into a 1D one. print("c.sum[axis=0] = {}".format(c.sum(axis=0))) print("c.sum[axis=1] = {}".format(c.sum(axis=1)))