#!/usr/bin/env python # coding: utf-8 # # # ### Python # # * General-purpose. # * Interpreted. # * Focuses on readability. # * Excellent for interfacing with C, C++ and Fortran code. # * Comprehesive standard library. # * Extended with a large number of third-party packages. # * Widely used in scientific programming. # This presentation will give a brief into to some key features of Python and the Scientific Python ecosystem to help those not familar with the language with the remainder of the class. This is in no way a comprehensive introduction to either topic. Excellent tutorials on Python and Scientific Python can be found online. # # We will be using IPython for this class which is a package which allows Python code to be run inside a browser. This is in no way the only way to run python, the Python/IPython shell, scripts and various IDEs can also be used but will not be coverted. # # The notebook for this materials is available if you wish to follow along on your own computer, but we will be moving fast... # ## Variables # # * ### Integers # In[ ]: a = 1 # In[ ]: a + 1 # * ### Floating point numbers # In[ ]: b = 2.1 # In[ ]: b + 1 # In[ ]: a + b # In[ ]: type(a + b) # ## Variables # # * ### Complex numbers # In[ ]: c = 1.5 + 0.5j # complex numbers # In[ ]: print c.real print c.imag # * ### Booleans # In[ ]: d = 3 > 4 # In[ ]: print d # In[ ]: type(d) # ## Variables # # * ### Strings # In[ ]: s = "Hello everyone" type(s) # In[ ]: a = "Hello " b = "World" print a + b # ## Variables can be cast from one type to another # In[ ]: a = 1 print a print type(a) # In[ ]: b = float(a) print b print type(b) # In[ ]: s = "1.23" print s print type(s) # In[ ]: f = float(s) print f print type(f) # ## Containers # * ### Lists # In[ ]: l = ['red', 'blue', 'green', 'black', 'white'] # In[ ]: len(l) # ### Indexing # In[ ]: l # In[ ]: print l[0] print l[1] print l[2] # In[ ]: print l[-1] # last element print l[-2] # In[ ]: l[0] = 'orange' print l # ### Slicing # In[ ]: print l[2:5] # In[ ]: print l[2:-1] # In[ ]: print l[1:6:2] # In[ ]: l[::-1] # ### Lists can store different type of variable in each element # In[ ]: ll = [5, 22.9, 14.8+1j, 'hello', [1,2,3]] # In[ ]: ll # In[ ]: print ll[0] print ll[1] print ll[2] print ll[3] print ll[4] # ## Containers # # * ### Dictionaries # In[ ]: d = {'name': 'Jonathan', 'id': 223984, 'location': 'USA'} # In[ ]: d.keys() # In[ ]: d.values() # In[ ]: d['name'] # In[ ]: d['id'] # In[ ]: d['id'] = 1234 # In[ ]: d['id'] # ## Containers # # * ### Tuples # In[ ]: t = ('red', 'blue', 'green') # In[ ]: t[0] # In[ ]: t[1:3] # In[ ]: t[1] = 'orange' # ## Flow control # # * ### conditional (if, else, elif) # In[ ]: a = 10 if a == 10: print "a is 10" # In[ ]: a = 10 if a > 10: print "a is larger than 10" else: print "a is less than 10... or maybe equal too" # In[ ]: a = 4 if a > 10: print "a is larger than 10" elif a < 10: print "a is less than 10" else: print "a is equal to 10" # ## Flow control # # * ### Loops # In[ ]: for i in range(10): print i # In[ ]: for color in ['red', 'blue', 'orange']: print "My favorite color is", color # ## Functions # In[ ]: def func(): print "Hello world" # In[ ]: func() # In[ ]: def func2(name): print "Hello", name # In[ ]: func2("Jonathan") # In[ ]: def times2(x): return x * 2 # In[ ]: y = times2(2) print y # In[ ]: def times_something(x, y=2): print x*y # In[ ]: times_something(3) # In[ ]: times_something(3, 3) # ## Classes # In[ ]: class Car(object): engine = 'V4' # class attribute def start(self): # class method print "Starting the car with a", self.engine, "engine" # In[ ]: mycar = Car() # In[ ]: type(mycar) # In[ ]: mycar.engine # In[ ]: mycar.start() # In[ ]: mycar.engine = 'V6' # In[ ]: mycar.engine # In[ ]: mycar.start() # ## The Scientific Python ecosystem # # * ### NumPy # # ![NumPy logo](images/numpylogo.svg) # In[ ]: import numpy as np # In[ ]: a = np.array([0, 1, 2, 3, 4, 5, 6, 7]) # In[ ]: a # In[ ]: a.shape # In[ ]: a.ndim # In[ ]: a.dtype # In[ ]: a[0::2] # In[ ]: a[a>3] # In[ ]: a * 2 + 100 # In[ ]: a.mean() # ### Arrays can be multi-dimensional # In[ ]: b = np.arange(12).reshape(3,4) # In[ ]: b.shape # In[ ]: b # In[ ]: b[1,2] # In[ ]: b[0:2, ::-1] # ## The Scientific Python ecosystem # # * ### SciPy # # ![SciPy logo](images/scipy.png) # In[ ]: import scipy # In[ ]: print scipy.__doc__ # ## The Scientific Python ecosystem # # * ### matplotlib # # ![matplotlib logo](images/matplotlib_logo.png) # In[ ]: get_ipython().run_line_magic('pylab', 'inline') # In[ ]: plot([1,2,3]) # In[ ]: a = np.random.rand(30, 30) imshow(a) colorbar()