Introduction to Python programming for Communication

These IPython notebooks are part of a workshop at the Annenberg School for Communication at University of Pennsylvania beginning in February 2016. The goal of the workshops is to be a collaborative environment for researchers in Communication to learn and practice basic Python programming skills for use in their research.

Topics covered will include:

  • Basic Python programming
  • Handling and processing textual data
  • Collecting data and web scraping
  • Accessing APIs (e.g. NYTimes, Twitter)
  • Content analysis using dictionary and NLP based approaches

Installing Python

The Anaconda distribution of Python 3 is recommended:

https://www.continuum.io/downloads

Select the Python 3 installer for your operating system and run through the installation steps.

Working with Python

We will work with Python in a number of different ways, including:

  • IPython notebooks - a web based system for producing code, images and text to easily and quickly produce documents to share your work and results. This document IS an IPython notebook!
  • Python scripts - the next step is to create script files in a text editor that can run anywhere Python is installed
  • Spyder IDE - The Anaconda Python distributiom includes a fully featured IDE (Integrated Development Environment) that supports multiple files and a comprehensive help and documentation system. IDEs can be helpful when you move on to develop complex programs.

Installing a text editor

There are numerous text editors across different OSes, e.g. Notepad, Notepad++, TextMate, TextWrangler, nano, emacs, vim, that you may already be familar with. Many of these are Python aware (i.e., will do some kind of highlighting of syntax and may even highlight errors).

If you don't have a particular favorite or would like to try a newish one we recommend using Atom.

Download at:

https://atom.io/

and follow installation for your operating system.

Starting an IPython notebook

  • You can start an IPython notebook from the Anaconda Launcher which will added to your applications/programs list when you installed the Anaconda distribution.
    1. Start up the Launcher
    2. Click on 'Launch' for the ipython-notebook

This will open up a terminal/console and start an IPython notebook running.

It will also open up a new tab in your default browser. This is the HOME location of the notebook server.

The notebook server will start running in your home directory (/Users/[USERNAME] on a mac and C:/Users/[USERNAME])

  • Create a new folder for the Python workshop here, e.g. 'python_workshop'.
    • You can do that outside of the notebook server as you usually would (e.g. Windows Explorer or Finder)
    • or you can select New > Folder from the top right hand option in the home page:
    • this will create 'Untitled Folder' that you can then select and rename:
  • Select this folder and create a new notebook:

Using an IPython notebook


In [ ]: