Python as a Scientific Language A Brief Introduction

Python as a Scientific Language A Brief Introduction D. T. Welling U. of Michigan Climate and Space Play along at home: www-personal. umich. edu/~dwelling/python/

https: //xkcd. com/353/ Python is… …a multi-paradigm (procedural, object oriented, etc. ) general purpose language. …named after a Flying Circus. …relatively young: vers. 1. 0 in 1994; version 2 in 2000 (IDL->1977; Mat. Lab->1984). …extremely popular. …Open source and FREE! Current versions: 2. 7. 11 and 3. 5. 1 12/6/2020 GEM-CEDAR 2016 2

TIOBE Rankings Python: #4 Mat. Lab: #17 IDL: >50 12/6/2020 GEM-CEDAR 2016 3

Why Python? • Powerful scripting rivaling Perl, Bash, etc. • “Batteries included” (GUI, Regex, web scripting, datetime, etc. out of the box. ) • Ubiquitous across platforms IT’S FREE (Win, *nix, OSX, andand many more). I’m cheap. • Extensible with C, C++, and FORTRAN • Emphasizes clarity of source code • Natural, easy, powerful Object Oriented 12/6/2020 GEM-CEDAR 2016 4

Why Python for Science? • Numpy (array algebra), Sci. Py (common scientific functions), and Matplotlib (plotting) combine to rival IDL, Mat. Lab. • Easy to drop into C, F 90 for “heavy lifting”. • Combines scripting with. FREE numerics, visualization for IT’S and I’m cheap. complete scientific solution. 12/6/2020 GEM-CEDAR 2016 5

Resources www. python. org …/dev/peps/pep-0008/ enthought. com Dive Into Python (Mark Pilgrim) Source, documentation, other resources. Style guide (suggested coding conventions) Python think-tank; Canopy Python Distribution Open-source introduction (. net for website) Core Python Programming Excellent introduction and reference; very (Wesley Chun) thorough. GDS My Crap 12/6/2020 Google Dat Shtuff for nearly any issue www-personal. umich. edu/~dwelling/python/ GEM-CEDAR 2016 6

Getting Python *nix Already Done! Use your package manager to get additional modules. Use internal installation (WARNING: non-standard!) OS X Use package manager (Fink, Mac. Ports, Homebrew). Use Enthought Canopy Python distribution. Windows Get software from python. org and install. Use Enthought Canopy Python distribution. Scientists will want Python, Numpy, Sci. Py, Matplotlib, and IPython. 12/6/2020 GEM-CEDAR 2016 7

Interfacing with Python There are many, many ways to work with Python. • Scripting and executing from the system shell. • Command line interfaces through a Python shell, such as the default shell or IPython. • Interactive Development Environments that combine text editors with shell prompts, such as Spyder. • Jupyter Notebook, a web-based interactive session that combines mark-up and code. 12/6/2020 GEM-CEDAR 2016 8
![An Example: example_imf. py #!/usr/bin/env python • ‘’’ An example module […] ‘’’ • An Example: example_imf. py #!/usr/bin/env python • ‘’’ An example module […] ‘’’ •](http://slidetodoc.com/presentation_image_h/efde57bd160eea5e85b0649053eead65/image-9.jpg)
An Example: example_imf. py #!/usr/bin/env python • ‘’’ An example module […] ‘’’ • import numpy as np • def format_ax(ax, ylabel=None): ''’ Format an axes object […] ''' <commands> # Comments start with hashtags! 12/6/2020 • “shebang” – tells shell how to execute. “docstring” – long form comments/documentation Imports – include code from other python files Function & variable definitions (note docstrings associated with definitions) GEM-CEDAR 2016 9
![example_imf. py class Imf. Data(dict): ''’ A class for handling Imf […] ''' def example_imf. py class Imf. Data(dict): ''’ A class for handling Imf […] ''' def](http://slidetodoc.com/presentation_image_h/efde57bd160eea5e85b0649053eead65/image-10.jpg)
example_imf. py class Imf. Data(dict): ''’ A class for handling Imf […] ''' def __init__(self, filename): <commands> def calc_v(self): ''' Calculate […] ''' <commands> 12/6/2020 Class definitions: • • Class-level docstring Special method definitions: define basic object behavior • Method definitions: functions that leverage object attributes NOTE NESTED TABBING GEM-CEDAR 2016 10

example_imf. py if __name__ == '__main__’: <commands> 12/6/2020 Optional code that runs in the “__main__” namespace (when file is executed, not imported). GEM-CEDAR 2016 11
- Slides: 11