Financial Programming with Python
FAQs
What’s the relevance of Python?
Python is the primary language that we will be using in this course. It’s a semantic language which makes it easier to read and understand, and the reliance on indentation makes the organized structure quicker to grasp. With this language, we’ll be able to dive into a slew of libraries that will make solving complex data problems more simple.

What’s up with this crazy indentation?
With Python, indentation is more than just organization and readability. Python’s functionality actually depends on proper indentation!
In this snippet, we’re using indentation to tell our code where our for loops begin and end. In Python, indenting creates blocks of code that work together. Similarly, indenting backwards tells the program when to end a loop.
for x in range(10):
print(x)
for x in range(20, 30):
print(x)
The code you will write in Python will eventually be seen by someone else. Focusing on organization and readability is important because you want colleagues to be able to read your code. If your code is poorly organized it will be difficult to read later on.
How do list comprehensions work?
In a list comprehension, you are writing a for loop in a concise format that outputs a list object. It can be confusing because the variable is repeated twice. What the code is saying is: for each item in my old list, add that item to my new list.
For example:
Input:
old_list = range(10)
new_list = [item for item in old_list]
print(new_list)
Output:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
How are variables assigned with a for loop?
Typically a for loop is iterating over an object and extracting each of the next smallest objects. The variable is established after the word `For`. If your variable is `letter` and your object is `name` then Python finds the next smallest item in the `name` object you are iterating over and assigns `letter` to each of them.
Input:
name = 'Elliot'
for letter in name:
print(letter)
Ouput:
E
l
l
i
o
t
In the example of a list of strings, the next smallest items are the individual strings:
Input:
name_list = ['Elliot' ,'Darlene', 'Angela', 'Shayla']
for name in name_list:
print(name)
Output:
Elliot
Darlene
Angela
Shayla
How does slicing work?
Slice notation allows you to extract certain information from a list. The syntax is `a[start:stop:step]`, with `step` being an optional component. If a start number is omitted, it is assumed to be zero. If a stop number is omitted, it is assumed to be one more than the index number of the last item in the data. In the following examples omitting the stop number is the equivalent of using the number 4, because there are only 4 items (0, 1, 2, 3) in the list.
Given the following list: name_list = ['Elliot' ,'Darlene', 'Angela', 'Shayla']
Input: name_list[:] OR name_list[0:4]
Both have the same output:
['Elliot' ,'Darlene', 'Angela', 'Shayla']
Input: name_list[0:2]
Output is first two names:
['Elliot' ,'Darlene']
Input: name_list[2:4]
Output is last two names:
['Angela', 'Shayla']
Input: name_list[0:4:2] OR name_list[:4:2] OR name_list[::2]
All three have the same output – every other name, starting with the first name:
['Elliot', 'Angela']
For more information check out this Stack Overflow post.
How do you access values in a nested dictionary?
Think of a dictionary like an assembled puzzle. To get one of the pieces out of the puzzle, you have to deconstruct the puzzle by breaking apart the larger chunks and then the smaller chunks. If you have a list of two dictionaries, you must first extract the dictionary you want, then you can extract the value you want.
In the following list of dictionaries lets access the 2nd pilot of the Galactica:
battlestars =
[{'Ship': 'Pegasus',
'Commander': 'Admiral Helena Cain',
'Pilots': ['Whiplash', 'Thumper']},
{'Ship': 'Galactica',
'Commander': 'Admiral William Adama',
'Pilots': ['Starbuck', 'Apollo', 'Helo', 'Athena']}]
Our list contains two dictionaries, with the Galactica being in the 2nd one.
So we first call our list object (battlestars), then the index position of our 2nd dictionary – 1.
battlestars[1]
Which gives us:
{'Ship': 'Galactica',
'Commander': 'Admiral William Adama',
'Pilots': ['Starbuck', 'Apollo', 'Helo', 'Athena']}
Now that we have the dictionary we want, we can call the key we want.
We need to get information on pilots, so we call the ‘Pilots’ key:
battlestars[1]['Pilots']
Which gives us:
['Starbuck', 'Apollo', 'Helo', 'Athena']
Now we have extracted a list of pilots. We just need to use the index position of the Pilot we want to finish up. We want the 2nd pilot in the list, so we call position 1:
battlestars[1]['Pilots'][1]
Which gives us:
'Apollo'
How can tabular data be accessed to facilitate exploration in Python?
Tabular data is data in the form of a table with rows, columns and values. Spreadsheets and CSV files are a very well-known forms of tabular data. This format makes it simple to find values, but how do we manipulate those values using Python? The data must be ‘read into’ a program.
Python has a CSV module with a reader() function that allows for the ‘reading in’ or ‘parsing’ of csv tabular data into your python code. The data is stored in a variable that can be interated over using a for loop, thereby extracting the data within.
To open our sample battlestar.csv file, and then designate each column as a variable we would do the following:
alias = []
model_number = []
with open('auditingprojects/battlestar.csv', 'r') as file:
csvfile = csv.reader(file, delimiter=',')
for row in csvfile:
alias.append(row[0])
model_number.append(row[1])
When we say for row in file, we are literally storing a row in the spreadsheet in the variable row during each loop:
To get the columns we use indexing:
In this way, the alias variable now holds row[0] – the first column and all its row values. And the model number variable now holds row[1] – the second column and all its row values.
Main guide
Helpful Links
- Python – Beginner
- Python Scripting
- Python f-strings
- Python Data Structures
- Python CSV Module
- Git/GitHub
- Visual Git Guide
- Python 3’s f-Strings