Why Python and Some Basic Concepts

Python is a higher level programming language, easy for novice learner to get a hang of in a short time period. It’s versatile, efficient and powerful.

Versatile refers not only to its seamless incorporation of multiple applications but also its ubiquitously used in many established products such as Facebook, trading software etc. This language is highly speedy in certain executions except for iterative loops, upon which, there is even a jargon -pythonic – becoming a popular word. Powerful can be attributed to its open-source nature, that there are so many packages developed to accomplish all sorts of aims. For example,

NumPy: has a lot of the core functionality for scientific computing. Under the hood is calling C-compiled code, so is much faster than the same functions written in Python. Not the most user-friendly.

SciPy: similar to NumPy but has more means for sampling from distributions, calculating test statistics…etc.

MatPlotLib: The main plotting framework, giving you magic power do craft fancy graphs.

Seaborn: import it after MatPlotLib and it will make your plots a lot prettier by default. Also has its own functionality, but I find the coolest stuff runs too slow.

Pandas: mostly a thin wrapper around NumPy/SciPy to make more user-friendly. Ideal for interacting with tables of data, which they call a DataFrame. Also has wrappers around plotting functionality to enable quick plotting while avoiding complications of MPL. I use Pandas more than anything for manipulating data.

Scikit-learn: Has a lot of supervised and unsupervised machine learning algorithms. Also has many metrics for doing the model selection and a nice preprocessing library for doing things like Principal Component Analysis or encoding categorical variables.

Zipline (Quantopian) is capable of back-testing trading algorithms, including accounting for things like slippage, as well as calculating various risk metrics.

Django: for web development.

Nltk: natural language processing.

To grasp Python, we need to know two fundamental concepts – object and class.

Class is a set encompassing a group of objects sharing same attributes and methods. Objects subdue to Class, objects can be methods and variables. The class makes it easy to inherit between parent-child related classes. For example, a dog class can inherit from an animal class, meaning methods and attributes defined in the animal class can be directly used by dog class. Another example, car class inherits from Vehicle class.

class Vehicle:


car = Vehicle()

print(car) # <__main__.Vehicle instance at 0x7fb1de6c2638>


In Python, __init__() is often used in the beginning to construct an initialization function.

tesla_model_s = Vehicle(4, ‘electric’, 5, 250)

class Vehicle:

def __init__(self, number_of_wheels, type_of_tank, seating_capacity, maximum_velocity):

self.number_of_wheels = number_of_wheels

self.type_of_tank = type_of_tank

self.seating_capacity = seating_capacity

self.maximum_velocity = maximum_velocity

def number_of_wheels(self):

return self.number_of_wheels

def set_number_of_wheels(self, number):

self.number_of_wheels = number

Above example contains two methods, number_of_wheels and set_number_of_wheels, or getter & setter respectively. A method is differentiated from normal functions in that it contains self as its first parameter. It’s also worth noting that in Python, @property (decorators) is used to define getter & setter:


def number_of_wheels(self):

return self.number_of_wheels


def number_of_wheels(self, number):

self.number_of_wheels = number





Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.