2 + 24
A Jupyter notebook is a document that mixes code and text. It’s organized into cells: code cells run Python, and Markdown cells let you write formatted text to explain what the code is doing. That combination — code and explanation in one place — is what makes notebooks useful for data analysis.
The goal of this course isn’t to turn you into a Python programmer, but to give you enough Python to do serious financial analysis. The tools covered here are what’s actually needed for the work, not an exhaustive tour of the language.
As in any programming language, we can do simple mathematical computations in Python. In Jupyter, these computations can be done interactively in the notebook.
For example, if you want to compute \(2 + 2\) simply type 2 + 2 and press Ctrl + Enter in a code cell.
Note that if you just press Enter you will just have an additional line in your code cell. Code cells can have several lines of code.
By pressing Ctrl + Enter we execute the code written in the cell. If you press instead Shift + Enter you will execute the code in that cell and mode to a new cell.
Other mathematical operations work as usual. For example, say we want to compute \((2 + 3 \times 2) / 8.\) We then type:
The only operation that might be unusual in Python compared to other programming languages is exponentiation. If we want to compute \(7^{3}\) in Python we type:
Thus, exponentiation is performed by using double multiplication signs. Therefore, if we wanted to compute \((5.8 \times 8.7 - 48.25)^{3.2 + 2.4}\) we would type:
One of the most useful features of any programming language is the fact that we can store our computations in variables. In mathematics, for example we say \(x = 3\) or \(y = 4\). In Python, we would say:
The previous cell stored the value 3 in the variable x and the value 4 in y. We can recall the values of these variables as follows:
display() is a Jupyter function that prints the contents of a variable. A simpler alternative: placing a variable name on the last line of a cell outputs it automatically, without calling any function. We will see later that variables can contain other things besides numbers, like strings, tables full of data and graphs, among others.
We can also perform operations on variables. For example, we could compute \(x + y\) or \(x \times y\)
Note that we can reassign variables:
And we can also increase their values:
Note that x = x + 3 is not a mathematical statement — it’s an instruction: take the current value of x, add 3 to it, and store the result back in x. In Python, = always means assignment, not equality.
To check whether two things are equal, Python uses a double equal sign ==:
This is always False for any value of x, since no number equals itself plus 3. The distinction between = (assign) and == (compare) is one of the most common sources of confusion when starting out.
For computers there is a difference between 2 and 2.0. The first number is an integer whereas the second is a float. An integer allows us to count elements whereas a float allows us to measure quantities. In Python we do not have to declare the type of a variable, since the data type is set when you assign a value to a variable:
In the previous examples, the function type returns the type of the variable. Strings are another useful data type in Python.
Note that both variables c and d are strings. In Python we can use both single and double quotes to denote strings. I use single quotes for symbol-like strings such as tickers (e.g. 'MSFT') and double quotes for phrases used, for example, in graph titles.
Double quotes are also useful if you need to define a string that contains a single quote such as
Note that in Python, variable names:
A list is a built-in Python data structure for storing an ordered collection of items. Lists can hold numbers, strings, or a mix of types.
Items are accessed by their position, starting at 0. Negative indices count from the end.
You can add new items to a list with append.
Unlike numeric variables, lists can mix types — numbers, strings, or anything else.
A dictionary stores data as key–value pairs. Keys are typically strings; values can be any type. Dictionaries let you look up data by name rather than by position.
Retrieve a value by its key.
You’ll see lists and dictionaries throughout the course. They also serve as the building blocks for Pandas DataFrames — the main tool for tabular financial data — which we introduce in a later notebook.
Problem 1 Use Python to compute \(4^{5} - 7 \times (6^{2} - 3^{7})\).
Problem 2 Create a variable \(a = 2.3\), add 8.7 to \(a\) and display its value.
Note that in Python there is a shortcut to add a number to a variable:
When you writea += 8.7 you effectively add 8.7 to the variable a and change its value accordingly.
Problem 3 Define the following variables:
See what happens if you add them together, i.e. if you do a + b + c.
We can concatenate strings by using the + sign.
Problem 4 Create a list containing the tickers 'AAPL', 'MSFT', 'GOOG', and 'AMZN'. Append 'NVDA' to the list and print the result.
Problem 5 Create a dictionary called stock with the keys 'ticker', 'price', and 'shares' for a position of 50 shares of AAPL at $195.50. Retrieve the total value of the position from the dictionary.