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.
You get the convenience of working within a text editor-style environment, but with the conveniece of inline execution of code.
In [1]:
# Simple maths
print 2 + 2
In [3]:
# Assigning variables
my_variable = 2 + 2
print my_variable
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]:
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]:
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]:
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
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()
In [ ]: