Getting to know IPython notebook.

Jev Kuznetsov (jev@tradingwithpython.com).

This notebook is a part of Trading With Python course.

What is an IPython notebook

The IPython Notebook is a web-based interactive computational environment where you can combine code execution, text, mathematics, plots and rich media into a single document

It was inspired by math software like Mathematica and Maple. IPython makes working with interactive code easy. You can now do (financial) research in the same manner as you would write a document, by combining written ideas, code and output to a single document.

We will be using the notebook for the interactive research work. A conventional editor or an IDE will be used for writing python modules.

A typical workflow is: Prototype in notebook -> Create module in IDE -> Reuse functions in notebook

Getting around

The notebook consists of 'cells', each cell is a short section of code or text.

The text can be just simple raw text.

Or rich formatted text using the Markdown language.

You can interact with a cell by using the menu above or use keyboar shortcuts:

  • Double click on a cell to edit it.
  • Ctrl-Enter to run the code and advance to the next cell
  • Shift-Enter to run the cell code.

To get hint about keyboard shortcuts, press Ctrl-m h

Before we begin

There are some specific IPython system commands which are called cell magics. A cell magic normally starts with a % sign


In [1]:
# text after'#' is a comment, it is not executed
# let's get current location
%pwd


Out[1]:
u'D:\\My Dropbox\\Consulting\\TradingWithPython2\\notebooks'

another handy command is ls for listing files in the current directory


In [2]:
ls


 Volume in drive D is Data
 Volume Serial Number is FEFE-8A7E

 Directory of D:\My Dropbox\Consulting\TradingWithPython2\notebooks

08-Sep-13  02:12 PM    <DIR>          .
08-Sep-13  02:12 PM    <DIR>          ..
08-Sep-13  07:14 PM            10,778 twp_01_IPython_Notebook.ipynb
08-Sep-13  07:20 PM           234,833 twp_02_Leveraged_etfs.ipynb
14-Apr-13  10:11 PM            10,536 twp_03_Working_with_modules.ipynb
15-Apr-13  10:32 PM           274,035 twp_04_Day_seasonality.ipynb
25-Apr-13  11:36 PM            67,123 twp_05_Reading_csv_files.ipynb
26-Apr-13  12:20 AM           171,195 twp_06_Building_a_stock_database.ipynb
29-Apr-13  08:39 PM            90,780 twp_07_Sharpe_and_drawdown.ipynb
29-Apr-13  09:05 PM           301,692 twp_08_moving_averages_strat.ipynb
28-Apr-13  06:32 PM           198,551 twp_09_permanent_portfolio.ipynb
29-Apr-13  09:07 PM           208,163 twp_10_xlp_strategy.ipynb
02-May-13  11:54 PM         4,384,498 twp_11_spread_trading_strategy.ipynb
              11 File(s)      5,952,184 bytes
               2 Dir(s)  176,208,478,208 bytes free

Overview of python basics

let's take a look at some Python basics that you should know by now. If you encounter something you are unfamiliar with, please take a look at learnpython.org

You should also take a look at these chapters of the Python for Data Analysys (PDA) book

  • Chapter 3: IPython: An Interactive Computing and Development Environment
  • Appendix: Python Language Essentials

In [3]:
#printing is easy:
print "Hello world!"


Hello world!

In [4]:
# simple variables
a = 3
b = 'some text'
c = 3.0

print a, b, c


3 some text 3.0

In [5]:
#lists
L = [1,2,3]
print L

L = range(20)
print L


[1, 2, 3]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]

to get help for range function just type range?


In [6]:
#what is range
range?

You can remove the help popup by dragging it down or double-clicking on the division line.


In [7]:
#dictionaries
d = {'one':1,'two':2,'label':5}
print type(d)
print "Value at 'two' is ", d['two']


<type 'dict'>
Value at 'two' is  2

In [8]:
#loops
primes = [2,3,5,7]
for prime in primes:
    print prime


2
3
5
7

In [9]:
#functions
def my_function(n):
    print "number:", n
    
my_function(20)


number: 20

In [10]:
#classes
class MyClass:
      variable = "blah"

      def function(self):
           print "This is a message inside the class."
            
c = MyClass()           
c.function()


This is a message inside the class.

In [11]:
1/0


---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call last)
<ipython-input-11-05c9758a9c21> in <module>()
----> 1 1/0

ZeroDivisionError: integer division or modulo by zero

This is basically a very short overview of python programming language, which is not very special except for the clear syntax. In the next session we'll take a look at what makes Python so powerful for scientific and financial applications: numpy.