COURSE INTRODUCTION
And Python Relative to Other Tools
COURSE INTRODUCTION
MATT BRIGIDA
Associate Professor of Finance (SUNY Polytechnic Institute) & Financial Education Advisor, Milken Institute Center for Financial Markets
Curriculum
- First Quarter: Understanding Python and its ecosystem, and comparing it with other tools. Running Python in Jupyter Notebooks
- Second Quarter: Spreadsheet data and corporate finance tasks. No Trading bots!
- Third Quarter: Data from the web via API and investments tasks.
- Fourth Quarter: Next steps: Collaboration, library management, and other ways to run python.
Python
Note, in this course we will use Python 3. Python 2 is an earlier version of Python that is not completely compatible with Python 3. Python 2 is still around as legacy code, but by the end of 2020 it will no longer receive security updates.
Python Relative to Excel
Python and Excel have many similarities.
- In Python we give values names, and operate on the names. In Excel we simply reference the cell with the value.
- Operators are overloaded in both, for example:
"fin" + "ance"
returns
finance
and,
-
3 + 2
returns
5
in both Excel and Python.
- Both can connect to external data sources.
Python Relative to Excel
There are some important differences:
- Excel is a visual tool---by default you see the data and you choose region to operate on.
- Python doesn't show the data you create, and you select data by its name.
- So for example in Excel you assign to a location and reference a cell location in a calculation:
[A1] = B2 + 1 # assigns the value of B2 + 1 to cell location A1
However in Python we assign to, and reference, values instead of locations:
y = x + 1 # assigns the value of x + 1 to variable y
The latter method of assigning to values is more efficient for writing and reading, but also has some important benefits that we won't cover in this course. For example, we can make certain variables available to functions, however also disallow a function access to other variables. This means the 'user' can't access certain things we don't want changed.
- Python can be extended by creating new data types and methods.
- In Python it is much easier to automate tasks (scripting).
- Python can handle much larger data sets and more varied and intensive calculations.
But VB...
Much of what you can do in Python you can also do by extending Excel's functionality via the old Visual Basic/VB languages. Python is much more widely used than Visual Basic, and this is meaningful when you are looking for help. See the number of questions for each language here.
Python Relative to R
- R is focused on statistics and data analysis.
- Python is a general purpose language, but additional libraries add functionality similar to R.
- For many niche statistical methods (such as on panel data for example) R is better.
- Python is increasingly better for AI/ML.
- Either will work very well for our purposes.
Python Relative to C/C++/Java
- Compiled languages run faster, though take much longer to write than Python code.
- They also lack the interactivity of Python, R, or Excel.
- Python is referred to as a high-level language, whereas C is low-level. The lower the level the closer you are to the machine.
Python Relative to Javascript
- Javascript is the language of the interactive web.
- Python calculations are often fed into Javascript to be visualized.
- This presentation is in HTML with Javascript for interaction.
Summary: Why Use Python
- Python is a high-level, easy to use, general purpose programming language.
- For many of your tasks, Python will be easier to use than a spreadsheet.
- Because it is simple text, it is easier to share Python than a spreadsheet.
- Python is similar to R, but general purpose and less focused on mathematics and statistics.
- C/C++/Javascript are not appropriate for spreadsheet like tasks.
Exercise
What will the following do in Python? How can you fix it?
"Week " + 1
Answer
We'll introduce the Jupyter notebooks we will use to evaluate Python code in the next session. So here is the answer to the first exercise:
>>> "Week " + 1
### TypeError: can only concatenate str (not "int") to str
So this throws an error which says we can't concatenate a string and an integer (int).
Answer
We can fix this with:
>>> "Week " + str(1)
### Week 1
We simply converted the integer to a string with the str()
function.