Welcome

First, a quick intro to the IPython notebook for new users of the Python for Marketing package.

The IPython Notebook was the lightbulb moment for me and Python. It made working with code much more approachable. It won't make you an instant expert, but it's helpful and forgiving for newcomers to coding.

Some of the great things about the IPython Notebook?

Inline execution of code

  • Outside of the IPython Notebook, the most common way you'll see people coding is into a text editor (like Sublime Text, the one I personally recommend).
  • Text editors are great because they give hints to when your code is wrong (known as 'syntax highlighting'), plus they have lots of plugins to make things faster.
  • However, you then need to save that file and run it somewhere.
  • The IPython Notebook solves that problem by providing the helpful sytaxt highlighting but with the ability to run code in the same place as you write it, to get a feel for whether you're on the right track.
  • Speaking from experience, this proximity to the result means that you spend less time wondering what went wrong.

Save as multiple formats - as a notebook file, as a Python script or even as HTML

  • Once you've got something working, save it as a .py file and you can run this as a standalone script. Alternatively, save the output as HTML and share with other people outside of the Notebook environment. You can even upload them to a service like the Notebook Viewer

Graphs, charts and graphics inline!

  • One of my personal favourites. In Marketing we're often visualising data and Python has some great ways to create charts and visualisations.
  • Using a Notebook, you can import the code, manipulate it into the right format and then visualise it all within the same document.

Autosaving

  • As a beginner, you'll mess up a lot. That's OK. When you create a new Notebook, you'll be creating a file on your computer, compared to some throwaway code in the terminal. It's really helpful to trace your steps backwards.
  • There are also Checkpoints, which is like a built-in revision control system that lets you step back in time.

You get the convenience of working within a text editor-style environment, but with the conveniece of inline execution of code.

A few quick simple examples


In [1]:
# Simple maths
print 2 + 2


4

In [3]:
# Assigning variables
my_variable = 2 + 2
print my_variable


4

In [4]:
# It's also forgiving. Most Python objects have a default output when you call them.
# For simple variables like integers, strings, etc - you can just type the name.
my_variable


Out[4]:
4

In [6]:
# Python has a great object type called Lists. It's a container for a list of miscellaneous items.
my_list = [1,2,3,4,"A","B","C",]
my_list


Out[6]:
[1, 2, 3, 4, 'A', 'B', 'C']

In [9]:
# You can add items to a list, using a method of that list. Use the [TAB] key to auto-complete.
# This example, '.append', will append a new item to the end of an existing list.
my_list.append(my_variable)

In [10]:
my_list


Out[10]:
[1, 2, 3, 4, 'A', 'B', 'C', 4]

In [11]:
# Loops will work just as easily here. You'll get used to working with loops.
# Here, we're saying "step through each item in my list, using the name 'single_item' to hold the current one."
for single_item in my_list:
    print single_item


1
2
3
4
A
B
C
4

Plus some more complicated examples


In [12]:
# Taken from the Seaborn site (Seaborn is a great library for visualising datasets)
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt

sns.set(style="dark")

f, axes = plt.subplots(3, 3, figsize=(9, 9), sharex=True, sharey=True)

rs = np.random.RandomState(50)

for ax, s in zip(axes.flat, np.linspace(0, 3, 10)):
    x, y = rs.randn(2, 50)
    cmap = sns.cubehelix_palette(start=s, light=1, as_cmap=True)
    sns.kdeplot(x, y, cmap=cmap, shade=True, cut=5, ax=ax)
    ax.set(xlim=(-3, 3), ylim=(-3, 3))
f.tight_layout()


---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-12-ca20878e6c7c> in <module>()
      1 # Taken from the Seaborn site (Seaborn is a great library for visualising datasets)
----> 2 import numpy as np
      3 import seaborn as sns
      4 import matplotlib.pyplot as plt
      5 

ImportError: No module named numpy

In [ ]: