Python

A whirlwind tour

Cincinnati Children's Hospital Medical Center - Weirauch Lab

Kevin Ernst / @ernstki

Python History

  • Created by Dutch programmer Guido van Rossum while employed at CWI in the Netherlands
    • the "reference implementation" is an interpreter written in C, but Java, C#, and JIT versions also exist
    • first release around 1991 (26 years ago)
    • Perl saw its first release in 1987; 28 years ago
    • It's just about as old as the Bash shell
  • Started as a "hobby" programming project, inspired by ABC, an earlier imperative programming environment at CWI

Python History (cont'd) - the BFDL

  • Guido (pronounced gee-doh) was employed for a non-profit in the States that paid for early work on Python 1.x (interview)
  • worked at Google for about 7 years; currently employed by Dropbox
  • Holds the title in the Python community of "Benevolent Dictator for Life," which means he has the final say on design decisions, but usually defers to a community process to resolve disputes

Python 2 vs. Python 3

  • Introduces breaking changes in order to fix "bad" parts of the language
  • Remove "cruft" (multiple ways of doing the same operations) from Python 2
  • Better UTF / Unicode support
  • The print statement is now a function (requires parens)
    # Python 2.x
    print "Hello from Python 2.7.something!"
    
    # Python 3.x
    print("Hello from Python 3!")
    
    # Python 2.7.x - use 'print()' for forward compatibility
    from __future__ import print_function
    print("Hello from Python 3!")

Python 2 vs. Python 3 (cont'd)

  • The regular division operator (/) returns a float by default (use // for the old "integer division" behavior)
  • Many parts of the standard library now return "iterables" (generators) rather than lists
    • (can be more efficient because iterables are "lazy-loaded")
  • See also

Python Philosophy

  • It is often remarked that Python reads like pseudocode
  • Python is a multi-paradigm language:
    • supports OO, functional methodology, and metaprogramming (e.g., "magic methods")
  • "The Zen of Python" (try: python <<<'import this')
    • Beautiful is better than ugly
    • Explicit is better than implicit
    • Simple is better than complex
    • Complex is better than complicated
    • Readability counts

Real-world Use

Warning: not comprehensively researched—but Python is not an esoteric language by any means

Why learn Python?

  • It reads like English (or "executable pseudocode")
  • "Batteries included" - comprehensive standard library with excellent documentation
  • Relatively easy to debug because of language's deep introspection features
    • downside: no concept of "private"
    • allows IDEs & REPLs like IPython or ptpython to do auto-completion of imports, methods, & properties
  • Built-in help() function that works in the REPL
    • means standard library is entirely self-documented

Why learn Python? (cont'd)

Why learn Python 3 (or 2.7.x)?

This used to be a difficult question, but now it's easy:
CPython 2.7.x will no longer be maintained after 2020.

  • Short answer:
    • new code → Python 3
    • learn what the differences are (in case you have to port or maintain old code)
    • There is a porting tool called 2to3 installed on every computer with Python 3
  • More resources at:

Python compared to...

Programming "chrestomathy" resources

Getting started

(online tutorials / interpreters)

Getting started (cont'd)

(installation on Linux)

You already have it; type python in the shell. You might want to install the python-pip package from your package manager, though.

(installation on Windows)

The official "Python for Windows" installers are often enough (they include pip and will update your system environment variables to add Python to your %PATH% However, you may prefer Anaconda if you're using a lot of scientific libraries,
or need to use third-party libraries that require compilation.

Getting started (cont'd)

(installation on a Mac)


  # Install Python and override the system Python interpreter
  sudo port install python27
  # You can check the current setting with 'port select --list python'
  sudo port select --set python python27
  
  # For the "improved" Python REPL (interactive mode)
  sudo port install py27-ipython
  sudo port select --set ipython py27-ipython
  
  # Matplotlib and NumPy (pulls in LLVM and X dependencies--grab a coffee)
  sudo port install py27-matplotlib py27-numpy
            

Jupyter (née iPython Notebook)


  sudo port install py27-jupiter

  # Starting the notebook server (opens automatically in default browser)
  jupiter-notebook-2.7
            

Interactive "notebooks"

Jupyter / IPython notebooks are, in principle, similar to Mathematica's notebooks or Matlab's cell publishing mode

Live-coding demo

Brace yourselves...

See also

Links

SIGH, FINALLY

Thanks for your kind attention. You rock. ;)

So here's a fluffy kitten.