Startup IPy notebook for Intro to Data Analysis Course

Text Using Markdown

If you double click on this cell, you will see the text change so that all of the formatting is removed. This allows you to edit this block of text. This block of text is written using Markdown, which is a way to format text using headers, links, italics, and many other options. Hit shift + enter or shift + return to show the formatted text again. This is called "running" the cell, and you can also do it using the run button in the toolbar.

Code cells

One great advantage of IPython notebooks is that you can show your Python code alongside the results, add comments to the code, or even add blocks of text using Markdown. These notebooks allow you to collaborate with others and share your work. The following cell is a code cell.


In [1]:
# Hit shift + enter or use the run button to run this cell and see the results

print 'hello world'


hello world

In [2]:
# The last line of every code cell will be displayed by default, 
# even if you don't print it. Run this cell to see how this works.

2 + 2 # The result of this line will not be displayed
3 + 3 # The result of this line will be displayed, because it is the last line of the cell


Out[2]:
6

Nicely formatted results

IPython notebooks allow you to display nicely formatted results, such as plots and tables, directly in the notebook. You'll learn how to use the following libraries later on in this course, but for now here's a preview of what IPython notebook can do.


In [3]:
# If you run this cell, you should see the values displayed as a table.

# Pandas is a software library for data manipulation and analysis. You'll learn to use it later in this course.
import pandas as pd

df = pd.DataFrame({'a': [2, 4, 6, 8], 'b': [1, 3, 5, 7]})
df


Out[3]:
a b
0 2 1
1 4 3
2 6 5
3 8 7

In [4]:
# If you run this cell, you should see a scatter plot of the function y = x^2

%pylab inline
import matplotlib.pyplot as plt

xs = range(-30, 31)
ys = [x ** 2 for x in xs]

plt.scatter(xs, ys)


Populating the interactive namespace from numpy and matplotlib
/usr/local/lib/python2.7/dist-packages/matplotlib/font_manager.py:273: UserWarning: Matplotlib is building the font cache using fc-list. This may take a moment.
  warnings.warn('Matplotlib is building the font cache using fc-list. This may take a moment.')
Out[4]:
<matplotlib.collections.PathCollection at 0x7f7a24616c10>

Creating cells

To create a new code cell, click "Insert > Insert Cell [Above or Below]". A code cell will automatically be created.

To create a new markdown cell, first follow the process above to create a code cell, then change the type from "Code" to "Markdown" using the dropdown next to the run, stop, and restart buttons.

Re-running cells

If you find a bug in your code, you can always update the cell and re-run it. However, any cells that come afterward won't be automatically updated. Try it out below. First run each of the three cells. The first two don't have any output, but you will be able to tell they've run because a number will appear next to them, for example, "In [5]". The third cell should output the message "Intro to Data Analysis is awesome!"


In [8]:
class_name = "Nishanth Koganti"

In [10]:
message = class_name + " is awesome!"

In [11]:
message


Out[11]:
'Nishanth Koganti is awesome!'

Once you've run all three cells, try modifying the first one to set class_name to your name, rather than "Intro to Data Analysis", so you can print that you are awesome. Then rerun the first and third cells without rerunning the second.

You should have seen that the third cell still printed "Intro to Data Analysis is awesome!" That's because you didn't rerun the second cell, so even though the class_name variable was updated, the message variable was not. Now try rerunning the second cell, and then the third.

You should have seen the output change to "your name is awesome!" Often, after changing a cell, you'll want to rerun all the cells below it. You can do that quickly by clicking "Cell > Run All Below".