Jev Kuznetsov (jev@tradingwithpython.com).
This notebook is a part of Trading With Python course.
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
Or rich formatted text using the Markdown language.
You can interact with a cell by using the menu above or use keyboar shortcuts:
To get hint about keyboard shortcuts, press Ctrl-m h
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]:
another handy command is ls for listing files in the current directory
In [2]:
ls
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
In [3]:
#printing is easy:
print "Hello world!"
In [4]:
# simple variables
a = 3
b = 'some text'
c = 3.0
print a, b, c
In [5]:
#lists
L = [1,2,3]
print L
L = range(20)
print L
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']
In [8]:
#loops
primes = [2,3,5,7]
for prime in primes:
print prime
In [9]:
#functions
def my_function(n):
print "number:", n
my_function(20)
In [10]:
#classes
class MyClass:
variable = "blah"
def function(self):
print "This is a message inside the class."
c = MyClass()
c.function()
In [11]:
1/0
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.