Python and the Jupyter Notebook for Programmers

Workshop for CDSE Days, Monday, April 9, 2018, 8:30am-12:30pm.

John Ringland, Mathematics Department

Assisted by Aly Bittner and Tara Hudson

Contents

Target audience

Experienced programmer in some other language (such as C++, Java, Matlab) with little to no experience with Python.

Q&A

Two ways to ask questions:

Python

expressive, versatile, free

Part 1

Modes of running Python code

Jupyter Notebook

Shift-Enter, Tab, Shift-Tab, Kernels, Cell types: markdown, code, Math in markdown, Export to rST

Format is JSON - easy to escape from, if ever necessary

Non-Python: ! for shell commands, % for magic commands

Jupyter Lab

Computational environment, eventual successor to Jupyter Notebook

Language basics

numbers and arithmetic

operators including +=, unfamiliar ** and //

unbounded ints, complex

assignment

multiple assignment

shallow vs deep, is

booleans

<,<=,==,!=, is

lists and tuples

range()

indexing and slicing

start:stop:stride

negative and shorthands

in

list comprehensions

strings

split, replace, zfill

dictionaries

string manipulation

Exercise 1: Trump/Clinton debates - word frequencies

Clinton in 1st debate

Trump in 1st debate

Clinton in 2nd debate

Trump in 2nd debate

functions

arguments to functions can be functions

functions can call themselves

functions can return functions

importing modules

import foo import foo.bar as blah

from foo import bar from foo import *

example: glob

dir

interacting directly with the file system

os: creating, renaming, deleting folders and files

example: art

writing text to files

Part 2: Numpy, etc.

Numpy

construction

zeros, ones, empty, linspace, eye, random.rand, random.randint

Numpy for Matlab users

indexing, slicing, broadcasting

Exercise 2: reproduce a tartan

campbell_lochawe_tartan.png blackstock_tartan.png another_tartan_rendering.png

To start with, a simple checkerboard weave like this:

weave5.jpg

Fancy indexing

Exercise: Newton's method on cube roots of 1

numpy.linalg

solving linear systems, eigendata, etc.

Graphing

matplotlib

scipy

Exercise 3: read & plot audio: black holes merge, audio file black_holes_merge.wav

Source: https://www.youtube.com/watch?v=599IgzzhhAs

special functions

bessel functions, elliptic integrals

Part 3

Part 4

Object-oriented programming

Example: geometrical optics

rainbow2.png

classes

inheritance

symbolic computation: sympy

forced damped oscillator

Other packages

  • Tensorflow
  • NLTK
  • SKLearn

interfacing with other languages

Can call and be called by C, C++, Fortran, ...

Notebooks created during workshop

Part 1

Part 2

Part 3

Part 4