Learn Python for Investing (Part 1)

on May 31, 2020 · 6 mins read

A bit about Python

Python is a beginner friendly programming language that is extensively used in scripting. Scripting is making executable code files perform a certain task. Python is popular because of it’s beginner-friendly syntax yet it offers a powerful ecosystem for almost everything from web to machine learning. The vast libraries of Python offer everything from Data Visualisation, Machine learning to Image Processing and even building games.

Knowing Python, you can do a lot more with data than you can do in an excel or what the pre-built softwares offers.

If this is your first time writing code, I advise you to write the code as you go and come back to the tutorial if any error occurs.

Setting Up Python

There are many ways to setup Python but the most easiest, no matter what OS you are on, is to simply go to https://colab.research.google.com/. Click on New Notebook and start writing code. Executing the code is as simple as hitting Shift+Enter or navigate to Runtime and Run all.

Let’s get started

In programming, we have variables and constants. Variables are the identifiers of the data but the data can change constantly. Variables have different types like Numeric Types, Text Type, Sequence Types and Boolean Types. The types are the category of the variable.

Variables are intialized as


We have a variable x that is assigned the value 10.


is a type of String since John is a string meaning sequence of characters.


is a floating type.


is a list.

Lists are just as they are named: list of elements.

List are intialised like:


Lists can also be empty like:


List has different methods meaning different actions you can perform on them. Like reversing a list, adding more elements to the list, appending the list.

For example, on the new cell, type in:



You should see the output : []

Now go ahead and edit the code to:




You should see the output [5]

Here, append() is a method that takes one argument. What does an argument mean? Basically it’s an input to the method that the method does something with it like in our case adding the element at the end of the list.

Methods also called functions in other programming languages are very important in Python and we’ll be using many library defined methods in our cases.


Methods have the following syntax:

def method_name(args):
	do something

Methods are “called” which will be clear by the following example.

We’ll create a method where we input our name and we are greeted. So the input to the method is the name and thus the parameter is name too.

def greet(name):
  print("Hello"+" "+ name)

Type the above code in a new cell and run it. You should be shown a box to write your name, Type your name and hit enter. You’ll be greeted. IF any error occurs, check the spaces. Python very much minds the spaces so be sure, the indendation is correct.

Here, we have the method called greet, that takes in our name. We have set our_name as input meaning whatever a user inputs is set as our_name. The method is called using greet(our_name). And we get the output “Hello your_name_here”

Matplotlib for plotting data

Python is a popular language for data science due to its vast library. Libraries are imported using import keyword in Python.

We’ll start by importing our library matplotlib that we want to get introduced to in this guide.

Start a new notebook and type in the following

import matplotlib as plt

Here we’re importing the library matplotlib and naming it plt because the name it too long. We can use whatever name we like but don’t forget to use the same name in the program too.

Let’s suppose we have a dummy data for market cap of Apple Inc. for the last 7 days,


We want to plot it against


Now add,


plt.xlabel("Market Cap in Billions")

We now have set out Y-axis label of the plot to “Days” and X-axis label to “Market Cap in Billions”.

Now we’re gping to plot and to do show, add in,


We’re calling the method like we did previously with greet(). This time we haven’t defined it anywhere but that’s what the matplotlib is there for. We’ve imported the library so now we can define whatever method is defined there. Documentation is your best friend when searching for methods inside a library.

But the plot still doesn’t show, that’s because we haven’t written the code to show it, which is:


The final code is

import matplotlib.pyplot as plt
plt.xlabel("Market Cap in Billions")

And you should get the plot of market cap vs days.

In the next part, we’ll dive deeper into matplotlib and learn how to read data from CSV files and play with it.

The best way to learn is to play around with code, break things and iterate. Feel free to ask questions if you get confused.