Python I: Intro

Expectations

  • Previous programming experience recommended. Focus is on using Python as a tool, not on teaching programming.
  • Will learn language while learning [hopefully] useful tools.
  • Python is not the solution to all your problems. (I.e., this seminar is informational rather than evangelical in nature. Draw your own conclusions.)
  • Python is not a magical gift box; it is a programming language. At least some work will be required to reach your goals.

Python Interest Group at MSU?

  • People would meet to help each other install Python and various Python packages.
  • People would talk about how they use Python at work.
  • People would share their experiences with various pieces of Python software.
  • Any interest?

Why Python?

  • Popularity. You will likely encounter it. Maybe you already did and maybe that is why you are here?

  • Compared to many other scripting languages, it has a fairly simple syntax which encourages the writing of readable code and may be easier to learn.

  • Compared to other scripting languages, it has one of the most full-featured sets of tools for scientific computing: NumPy, SciPy, pandas, and matplotlib.

  • Comparisons and Contrasts

    • Matlab/Octave

      • vs NumPy, SciPy, matplotlib, and scikits
    • R

      • Can embed in IPython
      • vs pandas, StatsModels, and matplotlib
    • Julia

      • Can embed in IPython
    • Perl, Ruby, Scala, Clojure, Haskell, OCaml, etc...

    • Javascript

  • Consistency

Interpreters

  • Python

    • Python 2 and Python 3
    • CPython, IronPython, Jython, and Stackless Python
  • IPython

    • IPython Terminal
    • IPython QtConsole
    • IPython Notebook
  • Interpreters on the Web

  • Running Programs

    • Pitfall Warning: Explicit print vs implicit print.
  • Compiling Programs

    • No explicit compilation. Performed on-the-fly from source into Python VM bytecode. (Note presence of .pyc and .pyo files.)
    • PyPy and Nuitka
    • Pyrex and Cython
    • Numba

Everything is an Object

  • Almost everything is an object.

    • Don’t worry too much about what an object is. Consider it to be a some kind of value which has some associated attributes.
    • Attributes, themselves, are generally objects.
    • Objects are created from types. Types themselves are objects.
    • Don’t worry about object-oriented programming, if you’re not familiar with it. Existing types are flexible enough that you usually won’t have to create your own. (But, it is easy to do so if you have the need!)
  • Information about an object and its attributes can be found.

Built-in Types

  • object
  • type
  • module
  • NoneType
  • bool
  • function (named and anonymous)
  • int, long
  • float
  • complex
  • str, unicode, bytearray
  • tuple
  • list
  • set
  • dict

Variables

  • Important: Types are associated with values rather than variable names.

  • Variable names are references to values.

    • References to values are created by assignment with = statement.
    • References are likewise changed with = statement.
    • References are deleted with del statement.
    • Examples.
  • Pitfall Warning: Multiple named references to same sequence or mapping.

    • Example.
    • How to make a copy of a sequence? Several ways - more on that later.
  • Multiple assignment can be performed using commas as separators.

  • Multiple values can be swapped without explicit intermediate variables.

    • Exercise: Try it!

Operators

  • +, -, *, /, //, %, **

    • Exercises:

      • What happens if you add strings?
      • What happens if you multiply a string, tuple, or list by an integer?
    • Notes on integer division vs true division.

    • Examples of string interpolation.

    • Examples of the assigning variants of these operators.

  • ==, !=, <, <=, >=, >

  • is, is not, in, not in

    • Notes on precedence and alternative keyword orders.
  • not, and, or

    • Notes on “zeroish” vs “non-zeroish” values.
    • Notes on short-circuiting evaluation.
  • ~, &, |, ^, <<, >>

    • Examples of the assigning variants of these operators.

Working with Objects

  • Objects are instances of types.

    • Instances can be created by calling types or factory functions.
    • Examples.
  • dir()

  • hasattr(), getattr(), setattr()

  • Dot notation (.) is used to access attributes.

    • Exercise: Try to add an attribute to an instance of object.
  • The class statement defines a new type.

    • Inheritance. Old-style and new-style classes.
    • Example of simple class.
    • Exercise: Define a new class. Create an instance of it. Then, try to add a custom attribute to it. If successful, then try accessing that attribute.
  • Note on special methods with double underscores.

Working with Strings

Working with Tuples

  • Creation of tuples.
  • Length, indexing, and slicing like strings.
  • Pitfall Warning: Syntactic sugar for 1-element tuple.
  • Note on multiple assignment and tuples.

Working with Lists

  • Creation of lists.

  • Length, indexing, and slicing like strings.

  • list.append() and list.insert()

    • Exercise: Insert an item at the front of a list.
  • list.extend()

  • Item removal.

    • Use del with an index or slice.
    • list.pop()
    • list.remove()
  • list.count()

  • list.reverse() and reversed()

  • list.sort() and sorted()

Working with Sets

Working with Dictionaries

  • Creation of dictionaries.

    • From a list of key-value pairs.

    • dict.fromkeys()

    • Dictionary comprehensions.

    • Examples.

    • Exercise: Create a dictionary, using a list of letters as keys and a list of numbers as values.

  • Indexing by key, but no slicing.

  • Value retrieval by indexing vs dict.get().

  • Manipulating sequences as dictionary values: dict.setdefault()

  • Testing for a key with the in operator.

  • Lists of keys, values, and key-value pairs.

    • Views vs iterators.
  • frozendict

Flow Control and Modularity

  • pass

  • def - yield - return

    • Docstrings.
    • Functions can return multiple values.
    • Arbitrary numbers of arguments.
    • Keyword arguments.
    • Examples.
  • if - elif - else

    • Examples.
  • for .. in - continue - break - else

    • Really? An else clause with a loop? Yes.
    • Examples.
  • while - continue - break - else

    • Examples.
  • try - except - else - finally

    • The exception hierarchy.
    • Examples.
  • with

    • Examples later.

Functional Programming

Working with Files

Miscellany

Standard Library

Namespaces, Scopes, and Modules

Back to the Future

  • __future__

    • division
    • print_function
    • absolute_import

Python Sundries

Human-Readable Data

Math and Statistics

Gathering Data

  • csv

    • Can handle other separators besides commas.
    • Can ignore header lines.
    • Examples.
  • urllib, urllib2

    • FTP and HTTP retrieval of data.
    • Can scrape web pages for data. Use in conjunction with something like BeautifulSoup. For example, see this Stack Overflow question.
    • Examples.

Data Persistence

  • pickle

    • pickle.dump()
    • pickle.load()
    • Exercise: Create a dictionary and set. Dump them to a file. Load them from the file.

Raking Data

Files, Directories, and Subprocesses