Going Beyond an Initial Python Script

Aimed at building a solid foundation of technical skills in Python to enable creating complex financial models solely in Python

Structuring a Complex Python Model


Notes

  • There are two layers of organization we should have in our Python models

  • The Python code itself should be well organized by using functions, which are self-contained logical units with inputs and outputs and optionally a description of what it does as well as the inputs and outputs

  • We can use Jupyter features to provide additional structure on top of this. The sub-problems or sub-models can be divided into sections using Jupyter markdown. A table of contents can be provided to easily navigate through the sections and to give an overview of the structure of the model.

  • The main inputs should be at the top of the notebook and the main outputs should be at the bottom of the notebook

Transcript

  • 00:03: hey everyone
  • 00:04: nick dear bertis here teaching you
  • 00:06: financial modeling and today we're going
  • 00:08: to be talking about
  • 00:09: structuring a complex python model
  • 00:12: as part of our lecture series on going
  • 00:14: beyond an initial python
  • 00:16: script learning python basics so that we
  • 00:19: can build out financial models
  • 00:21: using python so we've been working so
  • 00:24: far
  • 00:26: on this simple retirement model and we
  • 00:29: expanded it to
  • 00:30: a more dynamic salary retirement model
  • 00:32: in excel
  • 00:34: but so far as far as python is concerned
  • 00:37: all we've done is build out
  • 00:38: the most simplistic retirement model
  • 00:41: possible
  • 00:42: so we want to go to building the same uh
  • 00:46: dynamic salary retirement model in
  • 00:48: python as well
  • 00:50: uh but in order to do that we're gonna
  • 00:52: have to learn a little bit more of the
  • 00:55: python basics uh before we can really
  • 00:58: just
  • 00:59: jump right into that so that we don't
  • 01:02: get bogged down
  • 01:03: trying to learn everything all at once
  • 01:05: we can kind of learn the basic tools
  • 01:07: and then go and put together all those
  • 01:09: basic tools
  • 01:10: into the model that we're trying to
  • 01:12: build
  • 01:15: and then not only do we need to learn
  • 01:17: kind of you know the basic
  • 01:19: tools and and you know functions data
  • 01:22: types
  • 01:23: etc and python we also
  • 01:26: need to learn about a structure that we
  • 01:29: can use for our model
  • 01:30: uh that's going to make sense for
  • 01:32: someone that's coming to
  • 01:34: read your model that you know they can
  • 01:38: expect
  • 01:39: you know the inputs to be in a certain
  • 01:41: place outpost to be in a certain place
  • 01:44: and there would be a kind of a structure
  • 01:45: to it that is logical
  • 01:48: um so there's kind of
  • 01:52: um you know two layers to how
  • 01:55: we can structure our models
  • 01:58: in python so there's structure within
  • 02:01: basic python itself and we'll talk about
  • 02:04: how we can use
  • 02:05: functions to structure uh pieces of
  • 02:08: logic
  • 02:09: into kind of logical units and put those
  • 02:12: together
  • 02:12: to build the model but then also we're
  • 02:15: using jupiter
  • 02:17: on top of python to build out the models
  • 02:19: in this class
  • 02:20: so we can add additional structure to
  • 02:22: the model using
  • 02:24: jupiter because jupiter gives us that
  • 02:26: nicely formatted text
  • 02:28: we can basically create sections uh with
  • 02:31: that text
  • 02:32: to be able to differentiate the
  • 02:34: different parts of the model
  • 02:37: so when we build out a more complex
  • 02:40: financial model
  • 02:42: and this is you know a general kind of
  • 02:45: structure that we're looking at here
  • 02:46: it's not
  • 02:48: specific to python or excel or any other
  • 02:50: tool it's just kind of a logical
  • 02:52: structure we can use for our models so
  • 02:56: you know we've already talked about how
  • 02:58: you know a model
  • 02:59: basically just takes the inputs and
  • 03:01: ultimately converts them
  • 03:03: to outputs um and so
  • 03:06: up until now we just kind of think been
  • 03:08: thinking about the model as
  • 03:10: just this one block here without really
  • 03:12: thinking about how that model is
  • 03:14: broken down but as we dug into the
  • 03:18: uh excel dynamic salary model we
  • 03:22: found that really there were sub
  • 03:23: problems that we were solving
  • 03:26: inside the larger problem and so we
  • 03:29: formed
  • 03:29: uh you know sub models uh to deal with
  • 03:32: those individual parts of the problem
  • 03:34: and putting them all together uh made
  • 03:37: the entire
  • 03:38: model and then we can keep breaking it
  • 03:40: down even further
  • 03:41: you know within each of those sub models
  • 03:43: there were individual steps
  • 03:45: to carry out that part of the sub
  • 03:49: model so thinking about the dynamic
  • 03:51: salary model
  • 03:53: uh you know we uh ultimately our goal
  • 03:56: was to take
  • 03:58: you know things like the interest rate
  • 04:00: the savings rate
  • 04:02: uh you know the number of promotions et
  • 04:04: cetera
  • 04:05: um and convert that ultimately in a into
  • 04:08: a time until retirement
  • 04:10: and we broke that down into uh three
  • 04:13: parts
  • 04:14: uh one is to figure out the salaries
  • 04:16: over time
  • 04:17: one is to figure out the wealth over
  • 04:19: time and the last
  • 04:21: is to figure out the retirement details
  • 04:25: and so we split that down into three sub
  • 04:27: models just like is shown in this
  • 04:29: picture
  • 04:30: and then you know if we think about the
  • 04:33: um salary
  • 04:34: uh portion of the model that salary sub
  • 04:37: model
  • 04:39: we split that down into steps even
  • 04:42: further from there
  • 04:43: uh you know we calculated the cost of
  • 04:45: living factor
  • 04:46: separately we calculated the promotion
  • 04:48: factor separately
  • 04:50: and we ultimately got the salaries over
  • 04:52: time based on combining those two things
  • 04:55: so you know thinking of a model
  • 04:59: in this way is really helpful because
  • 05:01: you know when you just go
  • 05:03: and you have some problem and you need
  • 05:05: to go and build a model for it
  • 05:06: it can be very overwhelming on where do
  • 05:09: i even start on this thing
  • 05:10: there's you know so much involved here
  • 05:13: it's not clear to me how i get
  • 05:15: you know from the inputs to the ultimate
  • 05:17: solution
  • 05:18: well you just got to start breaking it
  • 05:20: down and try to
  • 05:22: work it into as many smaller sub
  • 05:24: problems as you can
  • 05:26: and then it all of a sudden becomes so
  • 05:28: much easier to solve these small
  • 05:30: sub problems like oh how do i calculate
  • 05:32: this you know cost of living
  • 05:35: adjustment factor that's a much smaller
  • 05:37: problem than this entire thing as a
  • 05:39: whole and so you can just take the small
  • 05:41: problems one at a time and that makes it
  • 05:44: easier to move forward on the model
  • 05:48: and then it's going to have a lot of
  • 05:50: benefits
  • 05:51: you know as i've mentioned people coming
  • 05:53: to read your model they can understand
  • 05:54: how it's laid out
  • 05:55: very easily and it becomes a lot easier
  • 05:58: to
  • 05:58: maintain the model going forward you
  • 06:01: know if someone
  • 06:02: needs to you know work on your
  • 06:06: uh salary portion of the model
  • 06:09: um you know now saying oh well there's
  • 06:12: gonna be
  • 06:13: uh recessions and whenever there's a
  • 06:14: recession they're going to stop doing
  • 06:16: the cost of living raises
  • 06:18: say that was our assumption uh then you
  • 06:21: know if you didn't have this structure
  • 06:22: everything was just kind of all in one
  • 06:25: then it would be not very clear where
  • 06:27: they would have to change things and if
  • 06:28: it would be
  • 06:29: you know breaking other things when they
  • 06:30: change it but if we've separated it to
  • 06:33: having the salary as its own sub model
  • 06:35: then they can just work on the salary
  • 06:37: portion of the model and not worry about
  • 06:39: how it's affecting the other parts
  • 06:41: as long as the inputs and the outputs
  • 06:43: remain in the same structure
  • 06:46: from that sub model so um
  • 06:49: a lot of benefits to using this kind of
  • 06:52: structure
  • 06:53: uh and so that's the structure we're
  • 06:55: going to follow in this class and i
  • 06:56: would recommend just in general
  • 06:58: for any financial model uh
  • 07:02: and then applying that to python we'll
  • 07:03: see uh
  • 07:05: more on the specifics as we uh
  • 07:08: you know dig into all these basics and
  • 07:10: then we actually go to
  • 07:11: implement that in the model but
  • 07:14: basically
  • 07:15: we can use functions to you to do this
  • 07:18: structure for us
  • 07:20: whereas in excel uh you know the model
  • 07:22: was the workbook
  • 07:24: right and then you know each sub model
  • 07:26: was a worksheet
  • 07:27: and each step was basically like a
  • 07:29: column and one of the tables
  • 07:33: for python it's all about functions
  • 07:36: so the entire model is going to be
  • 07:40: one single function you should be able
  • 07:41: to call a function
  • 07:43: give it your model inputs and get your
  • 07:45: model output
  • 07:46: but then each sub model is going to be
  • 07:49: its own function as well
  • 07:51: so then your main model function would
  • 07:54: basically just be calling
  • 07:56: each one of the sub model functions and
  • 07:58: then
  • 07:59: within each sub model function
  • 08:02: you can have a function for each step in
  • 08:04: the model
  • 08:05: so then each of these sub model
  • 08:08: functions
  • 08:09: is just calling each of the individual
  • 08:11: step functions to put it together
  • 08:13: and each of these individual steps
  • 08:15: functions
  • 08:16: actually have the main logic of your
  • 08:18: model and you can go to a dis
  • 08:20: additional levels of nesting if it makes
  • 08:24: sense for your problem you know you have
  • 08:26: you can break each of these steps down
  • 08:27: into even smaller sub steps
  • 08:30: and so on any number of layers is fine
  • 08:34: but this kind of functions inside
  • 08:36: functions
  • 08:38: is a nice way to structure things in
  • 08:41: python that
  • 08:42: that gets at this same kind of logical
  • 08:45: structure
  • 08:46: for the model so that's the basic idea
  • 08:49: behind structuring
  • 08:51: our python models and then you know you
  • 08:54: can also
  • 08:54: use jupiter to have you know the nice
  • 08:57: you know section headers and say you
  • 08:59: know this is the salary portion of the
  • 09:00: model and then it you know goes into all
  • 09:02: this part
  • 09:03: with the functions etc
  • 09:07: so with these ideas we'll make them a
  • 09:09: lot more concrete
  • 09:10: in the later lectures as we go to
  • 09:12: actually implement this
  • 09:13: um but that's where we'll leave it now
  • 09:15: for how to structure
  • 09:17: our python models so we'll come back
  • 09:20: next time to get started into the python
  • 09:24: basics as far as the syntax and
  • 09:26: different ways that we can work with
  • 09:28: python
  • 09:28: and we're going to be talking about
  • 09:30: conditionals in the next
  • 09:32: video so thanks for listening and see
  • 09:34: you next time

Branching Logic with Python Conditionals


Notes

  • Conditionals let you choose which logic to run based on some logical condition

  • This is just like using Excel =IF, but more flexible as we can run any arbitrary operation rather than just returning a single value

  • If/else if/else pattern becomes a lot more clear in Python as they are written as separate blocks rather than a nested =IF statement in Excel

  • Logical conditions are always evaluated first to True or False. If True, goes into if part, if False, goes into else part (if included)

  • Be careful about single = vs double ==, it is an easy mistake to make and you will get a SyntaxError applying the incorrect one

  • Elif is never strictly necessary but can help simplify the code substantially

  • The lab exercises test knowledge of conditionals but also build on our prior knowledge of for loops and show how they can be combined

Transcript

  • 00:03: hey everyone
  • 00:04: nick dear birdis here teaching you
  • 00:06: financial modeling and today we're going
  • 00:08: to be talking about branching logic with
  • 00:10: python conditionals
  • 00:12: as part of our segment of going beyond
  • 00:14: an initial python script
  • 00:16: how we're going to learn the python
  • 00:18: basics so we can build out complex
  • 00:20: financial
  • 00:21: models so we have
  • 00:24: talked a little bit about conditionals
  • 00:27: before in the class and that was in the
  • 00:30: context of excel before
  • 00:33: and we talked about the if function in
  • 00:36: excel
  • 00:37: and how that lets us basically check
  • 00:39: some kind of logical condition
  • 00:42: and then return one value if it that
  • 00:45: condition is true and return another
  • 00:47: value if that condition is false
  • 00:50: we have the same kind of um concept
  • 00:54: in python as well uh but it's even
  • 00:57: definitely more flexible in python
  • 01:01: um and you can do a lot more with it
  • 01:05: but it's the same basic concept we're
  • 01:07: gonna be checking some kind of
  • 01:09: logical condition and then we're going
  • 01:11: to do one thing if it's true and we're
  • 01:13: going to do something else
  • 01:14: if it's false so we can see
  • 01:18: um an example here on
  • 01:21: the slide and
  • 01:24: we can see um here
  • 01:27: an if statement if 5 is equal to 6
  • 01:32: then it's going to print not true
  • 01:35: otherwise
  • 01:36: else it's going to print
  • 01:39: else claws and
  • 01:42: it's a little bit covered here in the
  • 01:46: video
  • 01:49: but
  • 01:53: you can see that it gets else clause
  • 01:56: here uh coming up
  • 02:00: and that's because this logical
  • 02:02: condition here
  • 02:03: this 5 is equal to 6 that is not true
  • 02:07: right that's false
  • 02:08: that five is equal to six and because
  • 02:11: it's false
  • 02:12: it's not going to go into the if part of
  • 02:14: the logic
  • 02:15: it's instead going to go into the else
  • 02:18: part of the logic
  • 02:19: and that's why we get else claws here
  • 02:23: so this is the basic structure and
  • 02:25: you'll notice
  • 02:26: you know just like when we did the for
  • 02:29: loop
  • 02:29: in python that it has the same kind of
  • 02:33: structure here uh that we have the if
  • 02:36: and then we have
  • 02:38: a colon and then after that colon we
  • 02:40: have a level of indentation
  • 02:43: and then under that we have the logic
  • 02:45: which actually runs under the if
  • 02:48: part of the statement and then
  • 02:51: coming over to the else it's back over
  • 02:53: at the original
  • 02:54: level of indentation and then we have
  • 02:56: another colon
  • 02:58: and then uh another level of indentation
  • 03:01: back to
  • 03:02: indentation level one um or the logic
  • 03:05: that runs under the else clause
  • 03:08: the logic which runs if the logical
  • 03:11: condition
  • 03:12: evaluates to false
  • 03:15: so just like before you know we had four
  • 03:18: you know loop variable in uh list or
  • 03:21: whatever
  • 03:22: colon indentation and then the logic
  • 03:25: which runs under the four
  • 03:26: same thing here with the if this is just
  • 03:28: generally how you have
  • 03:30: uh you know blocks of logic in python
  • 03:33: it's going to be
  • 03:34: a colon to start that block and then an
  • 03:36: end in uh
  • 03:38: for all the logic which runs inside of
  • 03:40: that block
  • 03:43: so um you know obviously just comparing
  • 03:46: five to six directly
  • 03:48: is pretty meaningless right like that's
  • 03:50: always going to evaluate to false
  • 03:53: if it's always going to evaluate to
  • 03:54: false this uh
  • 03:56: code here will never run it will always
  • 03:59: just go to the else clause
  • 04:00: so we might as well have just not even
  • 04:02: had the if and just had the else clause
  • 04:04: right
  • 04:04: just just print else clause it would
  • 04:07: have been effectively the same thing
  • 04:09: uh but where it becomes more valuable is
  • 04:13: when you're actually using variables
  • 04:15: in these conditions and those variables
  • 04:17: could be coming through with different
  • 04:19: values
  • 04:20: so uh you know here we can do
  • 04:24: uh you know if this equals that and this
  • 04:26: and that are two different variables
  • 04:28: that we've defined
  • 04:29: and for now we would find them both uh
  • 04:32: to be the same value
  • 04:34: but you know setting it up this way you
  • 04:37: know you could easily define
  • 04:39: these as other values and then that
  • 04:41: would change what the logic
  • 04:43: is going to do down here
  • 04:46: so with here we have if this equals that
  • 04:51: and for now this and that have the same
  • 04:54: value
  • 04:54: and so indeed it's going to run the
  • 04:58: logic which is under this if the yes
  • 05:00: print me um and you'll notice for this
  • 05:04: one we don't have
  • 05:05: any else clause and that's fine the else
  • 05:08: clause
  • 05:09: is optional in python so
  • 05:12: you can just do an if and then run some
  • 05:14: code if it's true
  • 05:15: and then otherwise it's just going to go
  • 05:17: on it's not
  • 05:20: it doesn't have any alternative logic to
  • 05:22: use it's just going to continue on with
  • 05:24: whatever the next code is
  • 05:28: and then here we're looking at um you
  • 05:31: know if this equals 5
  • 05:32: this was defined the woo so it is not
  • 05:34: equal to 5
  • 05:36: and so it's not going to print this last
  • 05:39: statement here
  • 05:40: um it's just going to go on and
  • 05:43: you know continue with whatever code we
  • 05:45: have left in the program
  • 05:48: so that's the basic way that you use
  • 05:51: these conditionals
  • 05:52: in python and you know of course most
  • 05:55: the time you would probably be doing
  • 05:56: something more
  • 05:57: than just printing you can have as many
  • 06:00: lines of code as you want
  • 06:02: under any one of these as long as they
  • 06:04: just all keep that
  • 06:06: level of indentation um
  • 06:10: so arbitrarily any kind of code that you
  • 06:12: want to run
  • 06:13: under whatever conditions you can do
  • 06:15: that using
  • 06:17: these conditionals
  • 06:21: and you may have noticed that here we
  • 06:24: are using
  • 06:25: two equals whereas when we had
  • 06:28: assigned variables we are using one
  • 06:31: equals
  • 06:32: so that's the difference here in python
  • 06:34: a single equals assigns
  • 06:37: two equals compares so this is saying
  • 06:41: is five equal to six and this is saying
  • 06:44: this is equal to wu so subtle difference
  • 06:48: but it's just assignment versus
  • 06:50: comparison
  • 06:51: and you want to be careful about that
  • 06:53: because if you use the wrong one
  • 06:54: you're probably going to get a syntax
  • 06:56: error and then you're going to be
  • 06:58: scratching your head
  • 06:59: why is this not working single equals
  • 07:02: to assign double equals to compare
  • 07:08: so then
  • 07:12: we now have this else condition
  • 07:15: which we talked about here um
  • 07:18: and that we can think of just like in
  • 07:20: excel how
  • 07:21: you know in excel we had if logical
  • 07:24: condition and then value if true and
  • 07:26: then value at false
  • 07:27: so the else here is like the value if
  • 07:29: false in excel
  • 07:31: it's the logic which is going to run if
  • 07:33: the condition evaluates to false
  • 07:38: and unlike excel where we're bounded by
  • 07:40: you know you just
  • 07:42: get one value or you get another value
  • 07:43: here it's running any kind of arbitrary
  • 07:46: logic
  • 07:47: um so that you know can be absolutely
  • 07:49: anything and that gives us a lot more
  • 07:51: flexibility
  • 07:52: with the way that things are going to
  • 07:55: work in our models
  • 07:58: and then one other way to use these
  • 08:00: conditionals that we didn't look at here
  • 08:02: but we will look at uh when we go to the
  • 08:05: example
  • 08:06: in the jupiter notebook uh is elif
  • 08:10: which is shorthand for elsif um
  • 08:13: so that's saying you know that's you
  • 08:15: would put that after an if
  • 08:16: then next it would be elif or elsif um
  • 08:20: and that's just saying
  • 08:21: you know if that first condition was not
  • 08:23: true
  • 08:24: but i'm giving you another condition now
  • 08:26: if that one is true
  • 08:28: then uh execute this logic and we'll
  • 08:31: make that a lot more concrete here in a
  • 08:33: moment as we go through
  • 08:35: the example in the jupiter notebook
  • 08:39: so that's where we'll um jump off to
  • 08:41: that
  • 08:42: um we're gonna go into the python basics
  • 08:45: uh jupiter notebook which has
  • 08:48: uh you know more drawn out examples on
  • 08:51: all of this
  • 08:53: um so
  • 08:56: moving over to that um and this you know
  • 09:00: is broken up into sections matching the
  • 09:02: sections of the lecture
  • 09:04: we're going to use it in all the
  • 09:05: lectures in this segment
  • 09:07: so first here we're talking about
  • 09:09: conditionals
  • 09:11: so for conditionals um
  • 09:14: you know this is a little bit of review
  • 09:16: of what we just looked at
  • 09:19: but you know basically if whatever
  • 09:21: condition
  • 09:22: evaluates to true then it's going to run
  • 09:24: the logic
  • 09:25: which is under the if statement
  • 09:28: again that indentation is important if
  • 09:31: we leave that out
  • 09:32: we're going to get an indentation error
  • 09:34: expected and indented block
  • 09:36: because we had that colon we got to have
  • 09:38: the indent coming
  • 09:40: after that
  • 09:43: and of course if the condition evaluates
  • 09:46: to false
  • 09:47: then we're not going to get whatever
  • 09:50: code
  • 09:50: under here run um and here also show
  • 09:55: uh that you can have
  • 09:58: uh you know multiple um
  • 10:03: statements under here and they'll all
  • 10:05: execute you can have as many
  • 10:07: things as you want going on under here
  • 10:12: um so what's kind of going on in the
  • 10:15: background here
  • 10:16: is it always evaluates that logical
  • 10:19: condition
  • 10:19: first and evaluates that into either
  • 10:22: true
  • 10:23: or false so we can actually pull out
  • 10:26: that condition itself and see what
  • 10:28: happens
  • 10:29: when it's evaluated uh if we look at
  • 10:32: cat uh is he is you know comparing that
  • 10:36: cat that evaluates to true whereas we if
  • 10:39: we compare it to dog then that evaluates
  • 10:41: to
  • 10:42: false um so these true or false
  • 10:46: are what we call booleans that's another
  • 10:50: data type
  • 10:51: in python and it's just it takes on
  • 10:53: values of either true or false
  • 10:55: are the only two possible values for a
  • 10:58: boolean
  • 11:01: and so basically python evaluates this
  • 11:05: and then you know as it's executing it
  • 11:07: it basically
  • 11:08: substitutes that and so now we're down
  • 11:10: to if true
  • 11:12: which it will always execute this it's
  • 11:15: gonna print got a cat or
  • 11:17: uh you know in this case it was
  • 11:18: evaluating cat
  • 11:20: is that equal to dog that evaluates to
  • 11:22: false and so it's not going to print
  • 11:24: uh got a cat
  • 11:28: um and again as i mentioned before uh
  • 11:32: single equals uh defines
  • 11:35: double equals compares so here we use
  • 11:38: the single equals
  • 11:39: and that's defining a to be equal to 5
  • 11:43: whereas double equals is comparing this
  • 11:46: is saying
  • 11:47: is a equal to 5 yes that is true
  • 11:51: is a equal to 6 no that's false because
  • 11:53: a is equal to 5
  • 11:55: which you see here
  • 11:59: and if you try to use just a single
  • 12:02: equals in the condition
  • 12:06: then it's going to give you a syntax
  • 12:08: error
  • 12:10: again a syntax error just means python
  • 12:12: doesn't understand
  • 12:13: what you're trying to do you didn't
  • 12:15: specify things in the correct way
  • 12:17: for python to understand it so because
  • 12:20: you're saying
  • 12:21: if a is equal is defined to equal five
  • 12:24: that doesn't make sense it's expecting
  • 12:26: to see a logical condition
  • 12:29: not an assignment um and so that's why
  • 12:32: it breaks here uh if you just use a
  • 12:35: single equals
  • 12:36: as soon as we put that double equals i
  • 12:39: have it right here
  • 12:40: uh then it will execute just fine
  • 12:45: so that's the basic if then we can go to
  • 12:49: using else
  • 12:50: as well so
  • 12:55: you know the else is going to run if the
  • 12:57: condition
  • 12:58: is false so
  • 13:01: here i'm defining my my variable to be
  • 13:03: abc
  • 13:05: if it's equal to nope which it's not
  • 13:07: that evaluates to false
  • 13:09: uh since it's false it's going to go
  • 13:12: into the else clause instead of
  • 13:13: into the if clause so that's why we get
  • 13:16: runs if condition is false from the
  • 13:19: output
  • 13:20: um and again pay attention to those
  • 13:23: levels of indentation
  • 13:25: it's you know if colon
  • 13:28: indentation logic that runs under the if
  • 13:31: and then
  • 13:31: back on to the last level of indentation
  • 13:35: else colon and then
  • 13:38: up into another level of indentation for
  • 13:40: the logic which runs as part of the else
  • 13:43: uh if you mess up any of these you know
  • 13:46: say you forgot that else
  • 13:47: should come back um you're going to
  • 13:50: again get a syntax
  • 13:51: error because it does not understand
  • 13:54: what you're trying to do basically this
  • 13:55: else is now
  • 13:56: coming from nowhere it doesn't
  • 13:58: understand that it's related to this
  • 13:59: if by having them on the same indication
  • 14:02: level
  • 14:03: then it is able to understand that uh
  • 14:06: this else is attached
  • 14:07: to this if
  • 14:11: um and so
  • 14:14: you know here same thing uh now looking
  • 14:18: at if it's abc which it was we defined
  • 14:21: it to be abc
  • 14:22: um so that's why we go into the if
  • 14:24: clause logic now
  • 14:26: rather than the else clause that we did
  • 14:28: back here
  • 14:32: so then this uh section here
  • 14:36: is about elif or elsif
  • 14:40: and so you don't really ever necessarily
  • 14:43: need
  • 14:44: to use e-lif it's really just
  • 14:47: a convenience thing um to make yourself
  • 14:50: not have to write as much logic
  • 14:53: and so here's the basic idea of an
  • 14:56: example where you might
  • 14:57: want to use elif
  • 15:00: and that's you know we've got uh you
  • 15:03: know
  • 15:04: whatever possible prices coming in we
  • 15:07: want to basically classify that
  • 15:09: into uh you know is it cheap is it
  • 15:13: mid-range is it expensive uh is it
  • 15:16: negative such that they're actually
  • 15:18: uh paying me rather than me paying them
  • 15:20: um
  • 15:21: so that's the idea here so um
  • 15:26: we run this uh price is a hundred and we
  • 15:29: get it's priced into the mid-range
  • 15:30: so how do we get there so it first
  • 15:33: starts if price is equal to zero
  • 15:35: well 100 or is less than zero price is a
  • 15:38: hundred so that's not
  • 15:40: less than zero that's false and so it's
  • 15:42: going to go to the next condition
  • 15:44: so now we have elif so that works kind
  • 15:47: of like an
  • 15:47: else that it builds off of the previous
  • 15:49: if but also it checks another condition
  • 15:53: so uh the price was not less than zero
  • 15:56: and so we're going to come here and then
  • 15:59: we're checking
  • 16:00: is the price less than 50. so that's
  • 16:02: basically saying that the price is now
  • 16:03: greater than zero
  • 16:05: greater than or equal to zero but less
  • 16:08: than fifty
  • 16:09: it's in that range of zero to fifty and
  • 16:12: if that were true
  • 16:13: then it would print it's pretty cheap
  • 16:15: but again a hundred is not less than
  • 16:17: fifty this evaluates to false
  • 16:20: and so it's again going to go on to the
  • 16:22: next statement
  • 16:23: um here you know else
  • 16:26: going off of this uh if and
  • 16:30: now it's saying you know else basically
  • 16:32: the price is
  • 16:33: greater than or equal to 50 because it
  • 16:35: has failed both of these conditions
  • 16:38: uh then also if
  • 16:41: that price is less than 150 then it's
  • 16:44: going to be priced in the mid-range
  • 16:47: so 100 now actually is less than 150
  • 16:52: and so that's why we're going to end up
  • 16:54: in this
  • 16:55: statement and then you'll notice that we
  • 16:57: only get that statement
  • 16:58: because now this if has evaluated to
  • 17:02: true
  • 17:03: and so the else clause is not going to
  • 17:05: execute
  • 17:07: because now we did get a condition that
  • 17:09: was true
  • 17:10: and so it's not going to go into the
  • 17:11: else the else is only
  • 17:13: if the conditions were false
  • 17:17: so if we did put the price as 200 then
  • 17:20: we are going to get up here
  • 17:21: into the else clause because here it
  • 17:23: wasn't less than zero it wasn't less
  • 17:25: than 50
  • 17:26: it wasn't less than 150 all those
  • 17:28: evaluated to false
  • 17:30: and because they have all evaluated to
  • 17:32: false we came into the else clause
  • 17:36: or it could be negative we end up here
  • 17:40: because we hit this first if condition
  • 17:42: the price is less than zero
  • 17:44: so we're here and then all of these are
  • 17:47: else based
  • 17:48: and so they're not going to run if the
  • 17:51: first
  • 17:52: if the prior condition was true um and
  • 17:55: because the first condition was true
  • 17:57: then we don't even evaluate any of these
  • 18:00: we just hit here
  • 18:01: and then it goes on after this block
  • 18:03: after that
  • 18:05: the else and the e-lift are only going
  • 18:07: to execute
  • 18:08: if the prior condition is false
  • 18:12: so the reason that i say that this is
  • 18:13: really more of a convenience thing to
  • 18:15: make your code simpler
  • 18:16: uh is that uh you know this code here
  • 18:20: does the exact same thing we can see a
  • 18:22: hundred gets into the mid-range
  • 18:24: we can see that 200 uh gets too
  • 18:26: expensive
  • 18:27: we can see that negative 10 gets us to
  • 18:31: you're going to pay me et cetera
  • 18:34: but we can see that the code is a little
  • 18:36: bit longer here so here we didn't use
  • 18:38: any else
  • 18:40: any e-lif we just did it all with plain
  • 18:43: if conditions
  • 18:44: but what we had to do here was here we
  • 18:47: could just
  • 18:48: you know knowing that it was already not
  • 18:50: less than zero
  • 18:52: we could just check if it's less than
  • 18:53: 50. but here
  • 18:55: each if is totally separate it knows
  • 18:57: nothing about
  • 18:58: any of the prior ifs and so we need to
  • 19:01: check all the conditions in each one
  • 19:04: so here now this has to be
  • 19:07: if the price is greater than zero and
  • 19:09: it's less than 50
  • 19:11: in other words it's in this zero to 50
  • 19:12: range then we're going to print that
  • 19:15: so you need to put both sides of this
  • 19:17: condition because we can't
  • 19:19: count on it's only going to get here if
  • 19:21: the prior condition failed
  • 19:23: because it's going to actually execute
  • 19:26: all of these conditions and potentially
  • 19:28: all of the
  • 19:29: statements if those evaluate to true
  • 19:34: um so that's you know kind of the basics
  • 19:36: on conditionals
  • 19:38: in python and then looking at the lab
  • 19:41: exercises so you're going to be using
  • 19:43: this same
  • 19:44: jupiter notebook for all the lab
  • 19:45: exercises in this
  • 19:47: segment and so here
  • 19:51: we have three exercises on the
  • 19:54: conditionals
  • 19:57: and these exercises
  • 20:00: you'll notice also build off of some of
  • 20:02: the material that we've already learned
  • 20:03: you're going to have to you know use a
  • 20:06: for loop for some of these exercises
  • 20:09: so you know everything in the course
  • 20:11: kind of builds on each other until
  • 20:13: ultimately we can
  • 20:14: build complex financial models
  • 20:18: so you know that concludes uh the
  • 20:20: material on conditionals
  • 20:22: and we're going to come back next time
  • 20:25: to
  • 20:25: talk about grouping objects together
  • 20:28: using
  • 20:28: lists so thank you for listening and see
  • 20:31: you next time

Grouping Objects with Python Lists


Notes

  • Lists are one of the basic container data types in Python. They hold other objects so we can work with them as a group

  • Lists hold objects one by one in order and objects can be looked up from the list using the numeric index of the object

  • It is a very common pattern to create an empty list, then go through some logic in a loop to create the object you want in the list and add it in each run of the loop

  • This numeric index is zero-based, so look up the first object by 0, the second object by 1, and so on.

  • We can pass a slice to get a group of objects out of the list as a new list or a single integer to get a single object out of the list

  • Negative numbers means count from the end of the list, -1 is last object, -2 is second to last object, and so on

  • Objects can be added to the list when it is created, but also later on using .append and .insert. Objects can be removed using .pop

  • Lab exercise 1 tests list building for loop pattern, 2 tests adding objects to lists, and 3 tests list indexing and slicing

Transcript

  • 00:03: hey everyone
  • 00:04: nick dierbert is here teaching you
  • 00:05: financial modeling
  • 00:07: today we're talking about grouping
  • 00:09: objects together
  • 00:10: with python lists this is part of our
  • 00:13: segment on going beyond an initial
  • 00:15: python
  • 00:16: script how to uh you know learn the
  • 00:19: python basics so that we can go
  • 00:21: and build out financial models using
  • 00:23: python
  • 00:25: so we have talked a little bit about
  • 00:28: lists
  • 00:28: in the course already but now we're
  • 00:32: going to
  • 00:32: learn a little bit more about working
  • 00:34: with them
  • 00:35: and some of the useful patterns uh using
  • 00:38: lists
  • 00:40: so um we already learned that
  • 00:44: lists are one of these uh kind of
  • 00:46: container data types
  • 00:48: in other words it's uh an object that
  • 00:50: holds
  • 00:51: other objects within it um
  • 00:54: and so here uh you know as we start to
  • 00:58: look
  • 00:58: at this example uh we can see here the
  • 01:01: inputs
  • 01:02: uh one two three we have the uh
  • 01:05: numbers one two and three stored within
  • 01:08: this list
  • 01:09: uh which we're calling inputs
  • 01:13: and so these lists allow us to group uh
  • 01:16: this one two and three together
  • 01:18: into a single variable that we can then
  • 01:21: uh you know do other things with and
  • 01:24: so a very common pattern in python
  • 01:28: and in a lot of programming languages is
  • 01:32: uh to create
  • 01:35: a list an empty list before
  • 01:38: a loop and then as you go through the
  • 01:41: loop
  • 01:41: you add items to the list to build up
  • 01:45: the list
  • 01:45: through the loop so
  • 01:49: you know here we're looking at you know
  • 01:51: basically
  • 01:52: we want to take our inputs and we want
  • 01:54: to add 10
  • 01:56: to the inputs and
  • 01:59: have the outputs be just you know each
  • 02:01: of the inputs
  • 02:02: with 10 added to them
  • 02:05: and so this pattern accomplishes that
  • 02:10: and that we have the for loop here and
  • 02:13: the for loop
  • 02:13: is going to go through each one of these
  • 02:15: inputs one at a time
  • 02:18: pulling each one out and as we go
  • 02:21: through we're going to be adding 10
  • 02:24: to whatever that input is and then
  • 02:27: we're going to be appending that value
  • 02:31: onto the outputs list so this append
  • 02:35: is a new function that we're learning
  • 02:38: about here
  • 02:39: a pin just adds an item onto the end
  • 02:42: of a list so this list was empty to
  • 02:45: start with
  • 02:46: but as we go through we're going to add
  • 02:48: each input
  • 02:50: plus 10 into the output list
  • 02:54: so then at the end of that we would have
  • 02:56: 11 12
  • 02:57: 13 in the list and then this last part
  • 03:01: here
  • 03:01: is just showing another way that you can
  • 03:04: add items into a list
  • 03:06: because append will always put the item
  • 03:08: at the end of the list
  • 03:10: insert allows you to put the item at any
  • 03:13: position
  • 03:14: in the list so here we want to insert
  • 03:19: the this a as the first value here in
  • 03:23: the list
  • 03:24: and so that's why when we take a look at
  • 03:26: what
  • 03:27: the resulting list is
  • 03:30: then we get uh a first and then
  • 03:34: 11 12 13 which was the original list we
  • 03:37: had
  • 03:38: from this part of the code
  • 03:41: and you may be wondering why it's zero
  • 03:44: to insert this a at the beginning
  • 03:48: and that gets into zero based indexing
  • 03:51: in python which we'll talk about
  • 03:53: here in a moment
  • 03:56: so uh you know one other very
  • 04:00: commonly used functionality of lists is
  • 04:03: indexing and slicing lists
  • 04:05: so that allows you to pull certain items
  • 04:09: out of the list so you can either pull a
  • 04:11: single item
  • 04:13: out of the list by its position in the
  • 04:15: list
  • 04:16: or you can pull out a whole uh you know
  • 04:19: sequence of items
  • 04:21: uh by again their their indices
  • 04:25: in the list um
  • 04:28: so here you know we're looking at an
  • 04:30: example of
  • 04:31: my list having a b c and d in it
  • 04:35: and now we're going to look at how we
  • 04:37: can index that list to pull certain
  • 04:39: items out of it
  • 04:40: and so here's where i'm coming back
  • 04:43: around to the
  • 04:44: zero based indexing in python
  • 04:47: so zero is going to get us the first
  • 04:50: item
  • 04:51: in the list here if we do my list
  • 04:53: bracket zero
  • 04:55: that gets us a if we do my list brackets
  • 04:58: one
  • 04:59: that gets us b and so that is definitely
  • 05:02: counter-intuitive
  • 05:03: um for a lot of people but this is the
  • 05:06: way that the majority of pro
  • 05:08: programming languages work that indexing
  • 05:11: is zero based
  • 05:12: zero is going to give you the first item
  • 05:14: one is going to give you the second item
  • 05:16: and so on um
  • 05:19: and we'll talk a little bit more about
  • 05:22: that
  • 05:23: when we look at the jupiter notebook
  • 05:25: example
  • 05:26: then also you can count backwards from
  • 05:30: the end of the list
  • 05:31: um so -1 any negative number is going to
  • 05:35: be counting from the end
  • 05:36: of the list so minus one is going to get
  • 05:39: you the last item in the list
  • 05:41: here that's going to be d um if you did
  • 05:44: minus two that would be the second to
  • 05:45: last
  • 05:46: item in the list so that would give you
  • 05:48: the c
  • 05:50: and then uh that's indexing and then the
  • 05:53: other part is slicing we're slicing we
  • 05:55: grab
  • 05:56: multiple things out of the list at once
  • 05:59: and it gives us a new list
  • 06:00: which has a subset of the objects which
  • 06:03: were in the original
  • 06:04: list so here
  • 06:08: the slice is defined by the colon
  • 06:11: having a colon in here and so
  • 06:14: when you slice it's uh
  • 06:18: you know the full format is um
  • 06:21: you know from this item uh up to
  • 06:24: but not including this item uh before
  • 06:28: and after
  • 06:28: the column so slicing from
  • 06:32: one to three again one
  • 06:35: is uh talking about the second item
  • 06:38: remember because of that
  • 06:39: uh zero based indexing
  • 06:42: and three is talking about the fourth
  • 06:44: item so it's
  • 06:46: from the second and up two but not
  • 06:49: including
  • 06:49: the fourth item so that means we get the
  • 06:52: second and the third item
  • 06:54: as a result
  • 06:58: but either side of this
  • 07:01: slice is optional and
  • 07:04: if you exclude it it's just going to go
  • 07:06: to the end of the list in that direction
  • 07:09: so having nothing on this side
  • 07:12: and then up until but not including the
  • 07:15: last item
  • 07:16: that's going to be every item in the
  • 07:18: list up until the last item so just
  • 07:20: chopping the last item
  • 07:22: off of this list whereas
  • 07:26: if we uh you know before the colon we
  • 07:28: specify it but after
  • 07:30: the colon we don't that means it's going
  • 07:32: to go all the way to the end of the list
  • 07:35: for the right hand side but for here
  • 07:38: we're going to start from index 1 which
  • 07:40: is the second
  • 07:41: item so this is going to be every item
  • 07:44: from the second
  • 07:44: going all the way until the end of the
  • 07:46: list so that's why we get
  • 07:48: bcd as a result of this
  • 07:52: so you can use this to grab whatever
  • 07:55: objects you want out of your list
  • 07:59: so let's go ahead and jump over to the
  • 08:01: jupyter notebook example where we'll
  • 08:02: look at these things in a little bit
  • 08:04: more
  • 08:04: detail so
  • 08:09: um you know first let's hear just look
  • 08:12: at a list um and so
  • 08:16: we define lists using these brackets uh
  • 08:19: when you just have
  • 08:20: brackets out here on an assignment uh
  • 08:23: that's saying i want to make a list
  • 08:26: and when we look at what that list is
  • 08:30: we just see brackets here again
  • 08:31: representing that that
  • 08:33: is a list and if we check the
  • 08:36: type of that then we are and
  • 08:39: going to indeed see that it is a list
  • 08:45: um and so
  • 08:48: you know we have this list and we know
  • 08:51: that it's empty
  • 08:52: right now but we can
  • 08:55: add items onto a list
  • 08:59: using append so here we're going to
  • 09:02: append
  • 09:02: five and then we're going to look at the
  • 09:04: list again
  • 09:06: and now what might be a little
  • 09:08: counter-intuitive to people
  • 09:09: is now you come back over here and you
  • 09:12: check this again
  • 09:13: now all of a sudden it's changed now it
  • 09:15: has five in it and that's because
  • 09:17: we modified the same list to add five to
  • 09:21: it we didn't
  • 09:22: uh redefine a different list uh which is
  • 09:25: going to be separate from what was up
  • 09:26: here it's the same list
  • 09:27: and so if we just run that code again
  • 09:30: then we're going to see it's actually
  • 09:31: changed and now has a five
  • 09:33: in it and then as we go and add
  • 09:37: additional items
  • 09:38: they always add on to the end so we
  • 09:40: append six and then we append seven
  • 09:42: now we have five six and seven and the
  • 09:45: list
  • 09:45: which again you know carries through to
  • 09:48: anywhere that you're using
  • 09:50: this same list
  • 09:55: um so that's also gets into another
  • 09:59: pitfall that a lot of people hit
  • 10:01: as they begin to build things out in
  • 10:04: python and jupiter
  • 10:05: is the execution order of your cells
  • 10:08: can actually change the results that
  • 10:10: you're gonna get
  • 10:11: uh as you saw before when i first went
  • 10:14: through
  • 10:15: and i ran this my list was empty and now
  • 10:18: that i come back and run it
  • 10:19: my list is 567 and so if you run your
  • 10:22: cells
  • 10:23: in different orders you get different
  • 10:25: results and so
  • 10:27: um you know definitely as you uh
  • 10:30: you know get things working in your
  • 10:32: model you want to often just
  • 10:34: restart kernel and uh run all cells
  • 10:37: that's gonna give you a new session and
  • 10:39: just start everything
  • 10:40: run everything in order and that way
  • 10:42: you'll be sure that it's gonna execute
  • 10:44: exactly like when someone else is going
  • 10:46: to be running your model
  • 10:52: so then getting
  • 10:56: into uh talking a little bit more about
  • 10:59: indexing again we have this
  • 11:01: zero based indexing zero gets you the
  • 11:04: first item
  • 11:05: one gets you the second item um etc
  • 11:09: uh you know going all on to whatever
  • 11:12: number when i get the 10th item just put
  • 11:14: in x9 just subtract one from whatever
  • 11:16: the index is and if you want to
  • 11:20: understand more why most programming
  • 11:22: languages
  • 11:23: uh set it up this way i've included a
  • 11:25: resource
  • 11:26: here where you can read a lot more about
  • 11:29: that
  • 11:29: but that's definitely outside of the
  • 11:31: scope of this class that's more of a
  • 11:33: computer science concept
  • 11:35: uh all we need to know is that it is
  • 11:38: zero based indexing
  • 11:39: we do put a zero if we want to look up
  • 11:40: the first item
  • 11:42: for our purposes we don't really need to
  • 11:44: know why but for the curious people i
  • 11:46: just added that here
  • 11:50: um so then we're jumping back into
  • 11:54: adding items to lists but looking at the
  • 11:56: insert
  • 11:57: function method instead of the
  • 12:01: append method of the list
  • 12:04: so append again always puts the item at
  • 12:06: the length at the end of the list
  • 12:08: whereas insert will add it into any
  • 12:11: position
  • 12:12: um so again we're working with this same
  • 12:15: uh
  • 12:15: my list that we had from before
  • 12:19: 567 in it right now
  • 12:22: and now we're going to insert uh
  • 12:25: at the position 0 at index 0 which would
  • 12:28: be the first item
  • 12:30: we're going to insert woo and so then
  • 12:33: when we do that and we look at the
  • 12:34: result
  • 12:35: then we're going to have first woo and
  • 12:37: then our original
  • 12:38: 567 and you can insert
  • 12:41: at any position within the list so here
  • 12:45: i want to insert at the second
  • 12:48: position or sorry index two which would
  • 12:51: be the third
  • 12:52: uh object in the list i'm gonna insert a
  • 12:55: ten
  • 12:56: and so indeed then you see the third
  • 12:58: item in the list here
  • 12:59: is indeed a ten so whatever uh
  • 13:02: index you put here it's going to put
  • 13:04: that
  • 13:06: item that object at that position
  • 13:09: in the list
  • 13:17: so then we can look at using indexing
  • 13:19: and slicing to look up items within the
  • 13:21: list
  • 13:23: so again 0 is going to give us the first
  • 13:26: item
  • 13:26: here the woo 4 should give us the fifth
  • 13:30: item
  • 13:31: in the list which indeed it does
  • 13:35: um now if you put an index
  • 13:39: that does not exist in the list then
  • 13:42: we're actually going to get an
  • 13:43: error from that so here i'm trying to
  • 13:46: look up these
  • 13:46: the thousand and one item in the list
  • 13:51: and when i do that then i get index
  • 13:53: error list index
  • 13:54: out of range so the error is telling you
  • 13:56: exactly what happened here
  • 13:58: the index that you passed on this list
  • 14:02: is out of the range of possible
  • 14:05: indexes on this list right zero
  • 14:08: should work one should work two three
  • 14:10: and four
  • 14:12: are the possible indexes here as soon as
  • 14:14: i go to
  • 14:15: five then i'm going to start getting
  • 14:18: that index error
  • 14:22: so you have to actually put an index
  • 14:24: which exists within the list
  • 14:29: or otherwise you're going to get this
  • 14:30: index error now the one you know
  • 14:33: somewhat exception to that is you can
  • 14:35: use negative numbers
  • 14:36: um to index the list as well
  • 14:40: and that's going to count from the end
  • 14:42: of the list
  • 14:43: so minus one is going to get you the
  • 14:45: last item in the list
  • 14:47: which again my list looks like that we
  • 14:50: are getting the last item here
  • 14:52: and negative two is going to give us the
  • 14:54: the next last item
  • 14:56: in the list um and
  • 15:00: this index error can come up in the
  • 15:02: other direction as well
  • 15:03: we try to look at you know a thousand
  • 15:07: uh items before the end of the list
  • 15:10: that's you know way beyond the beginning
  • 15:12: of this list so once again we're out of
  • 15:14: range
  • 15:14: and we get an index error
  • 15:20: so that's indexing about pulling a
  • 15:22: single item
  • 15:23: out of the list now we have slicing
  • 15:26: which pulls multiple
  • 15:27: objects out of this list and creates a
  • 15:30: new list
  • 15:31: from those objects so here
  • 15:35: remember again slicing is really defined
  • 15:38: by this colon
  • 15:40: and that colon means i want to get from
  • 15:44: index one up until not including
  • 15:48: index three and so that's from the
  • 15:50: second item
  • 15:51: up until but not including the fourth
  • 15:54: item
  • 15:55: so again showing what my list is here
  • 15:58: and i run this so i want to get from the
  • 16:01: second item
  • 16:02: up until we're not including the fourth
  • 16:03: item i get the second item
  • 16:06: up until but not including the fourth
  • 16:09: item so that's why we get
  • 16:11: five and ten here
  • 16:14: um and leaving either side of the colon
  • 16:17: empty again we'll just
  • 16:19: uh you know let it continue all the way
  • 16:21: to the end of the list
  • 16:24: in whatever direction so leaving the
  • 16:26: right side of the colon
  • 16:27: empty will go all the way to the end of
  • 16:29: the list leaving the left side of the
  • 16:31: colon empty we'll go all the way to the
  • 16:33: beginning of the list
  • 16:35: and so this is saying from index one
  • 16:38: until the end
  • 16:39: which means from the second item until
  • 16:42: the end
  • 16:42: and that's why we get this portion of
  • 16:45: the list
  • 16:48: and then this one says uh
  • 16:51: you know all the way from the beginning
  • 16:52: of the list up until
  • 16:55: the but not including the last item in
  • 16:58: the list
  • 16:59: and so that's why we get everything
  • 17:01: except for this last
  • 17:02: seven in the list here
  • 17:06: [Music]
  • 17:08: and then the lab exercises here
  • 17:12: would be the working with lists section
  • 17:14: of the
  • 17:15: lab exercises and so
  • 17:20: this is going to test a few different
  • 17:22: things uh
  • 17:23: the loop building pattern uh adding
  • 17:25: objects
  • 17:26: in the list and also indexing and
  • 17:30: slicing
  • 17:30: of the list as well um
  • 17:34: so that concludes our section
  • 17:37: on python lists and grouping
  • 17:40: objects together using them next time
  • 17:43: we're going to come back to
  • 17:45: instead group logic together instead
  • 17:48: using functions thanks for listening and
  • 17:51: see you next time

Grouping Logic with Python Functions


Notes

  • Functions are the logical building blocks for any Python program, including our models

  • Functions contain a piece of logic which then can be reused flexibly with different inputs

  • We have functions in Excel, e.g. =SUM, =IF, =VLOOKUP, but as an Excel user you typically do not define functions. Python has built-in functions and also it is very easy to define your own.

  • Ultimately, you should be able to run your entire model by running a single function

  • The sub-models should also be functions, and the steps within the sub-models should also be functions. This can be broken into as many layers as is necessary. Your higher-level custom functions will be calling your lower-level custom functions.

  • Not only will you have better readability and organization of your model, but also you will be able to reuse logic more often which lowers maintenance costs

  • Functions scope the variables defined within. You cannot use those variables outside the function. This lets you use more general variable names which increases readabilityDefault arguments are a great way to keep your function flexible but also easy to use

  • For these lab exercise, I have provided cells to check your work. After adding your solution and running the exercise cell, then run the check cell which will throw an AssertionError or NameError if incorrect and nothing if correct.

  • The exercises test creating functions with and without default arguments and adding docstrings to functions

Transcript

  • 00:04: hey everyone
  • 00:05: nick dear bertis here teaching you
  • 00:06: financial modeling and today we're going
  • 00:08: to be talking about
  • 00:10: grouping logic together with python
  • 00:12: functions
  • 00:13: this is part of our lecture segment on
  • 00:16: going beyond an
  • 00:16: initial python script and this process
  • 00:20: of learning the python basics so that we
  • 00:22: can
  • 00:22: build out complex financial models using
  • 00:25: python
  • 00:27: so we talked a little bit before
  • 00:30: about how functions are going to become
  • 00:34: the logical structure
  • 00:37: to build out our model
  • 00:40: and that we're going to use functions
  • 00:44: to uh you know define the entire model
  • 00:47: should be a function
  • 00:48: and then each sub model should it be a
  • 00:50: function
  • 00:51: and each step within the sub model
  • 00:54: should be
  • 00:54: a function and this could continue going
  • 00:57: to additional levels of nesting
  • 00:59: as necessary whatever is appropriate for
  • 01:02: the problem that you're working on
  • 01:05: so each uh you know layer of the model
  • 01:09: is going to be a function and those
  • 01:12: functions are going to call the lower
  • 01:14: level functions
  • 01:16: to ultimately build up the entire model
  • 01:19: and going to this kind of structure
  • 01:21: means that
  • 01:23: things are a lot more contained it's a
  • 01:24: lot easier to just work on one part of
  • 01:27: the model
  • 01:27: and not worry about affecting other
  • 01:29: parts
  • 01:31: and it means that you're going to be
  • 01:32: able to reuse more of your code
  • 01:36: and it's going to be easier to read your
  • 01:38: code and there's lots of other
  • 01:39: advantages here
  • 01:42: in structuring the model in this way but
  • 01:45: in order to
  • 01:46: add that kind of structure we're going
  • 01:48: to have to learn how to
  • 01:50: work with functions in python how to
  • 01:52: define them how to use them
  • 01:55: and let's go ahead and dig into that
  • 01:59: so a function is basically
  • 02:03: just a lot a logical
  • 02:08: construct where it's just a group of you
  • 02:11: know statements of code
  • 02:12: that describe some logic and that logic
  • 02:16: you know may have inputs it may have
  • 02:18: outputs
  • 02:19: and we give that function a name uh so
  • 02:23: that we can call it by that name
  • 02:26: so you know it can take uh you know what
  • 02:29: was
  • 02:30: you know maybe you know thinking about
  • 02:32: our uh dynamic
  • 02:34: salary retirement model you know this
  • 02:37: part where we have to
  • 02:38: calculate the promotion factor year over
  • 02:41: year
  • 02:42: uh there's a few things going into that
  • 02:44: you know figuring out
  • 02:45: uh is this a promotion year how many
  • 02:47: promotions have occurred
  • 02:49: up until this point etc
  • 02:52: you know all that can just you know
  • 02:54: basically get wrapped under one function
  • 02:56: you know like calculate a promotion
  • 03:00: factor could be the name of that
  • 03:01: function and then you know as someone
  • 03:04: using that function or you know as
  • 03:05: you're building your model
  • 03:07: then you just call this create
  • 03:10: promotion factor and that's going to
  • 03:12: make the factor for you
  • 03:14: running all of that you know individual
  • 03:16: lines of code
  • 03:17: that actually carry that out so then
  • 03:19: we've reduced
  • 03:20: you know calling all these individual
  • 03:22: lines of code to
  • 03:24: a single line of code of calling this
  • 03:26: function and we've also
  • 03:28: given it a nice indicative name which
  • 03:30: describes exactly what it's doing
  • 03:32: in the context of our model rather than
  • 03:34: just having individual lines of code
  • 03:38: so it can definitely help a lot with the
  • 03:40: readability and structure
  • 03:41: of your model
  • 03:44: and you know going even further than
  • 03:46: that you can also
  • 03:48: add documentation to your functions it's
  • 03:50: called a doc string
  • 03:52: and that is just
  • 03:55: a string that goes along with your
  • 03:57: function which describes what it does
  • 04:01: and that is just another way to make
  • 04:03: your model even more readable
  • 04:06: because there's a lot of explanation of
  • 04:08: how things are working all the various
  • 04:10: steps
  • 04:13: um and then i've said before you know
  • 04:16: everything is an object in python
  • 04:17: functions are
  • 04:18: objects as well um and so
  • 04:22: you know that is probably not going to
  • 04:25: uh be too important for you as far as
  • 04:28: this class
  • 04:29: um but just know that they are treated
  • 04:32: like any other object
  • 04:33: and so you could um you know do things
  • 04:36: like how
  • 04:37: you could have a list of functions um
  • 04:40: you know you can pass functions as
  • 04:42: arguments and other functions
  • 04:45: there's tons you can do here python is
  • 04:47: very flexible
  • 04:48: by treating everything as an object
  • 04:52: so here's an example function here
  • 04:56: and you'll notice that there's a couple
  • 04:58: parts going on inside this function
  • 05:01: so one thing that you may notice
  • 05:03: initially is kind of this
  • 05:05: familiar structure that we had with the
  • 05:08: for loop we had it with the if statement
  • 05:10: and the else and the elif
  • 05:12: uh that we have some kind of statement
  • 05:15: and then we have a colon
  • 05:16: and then we have indentation and then we
  • 05:18: have logic that goes under that
  • 05:21: so again this is you know the common way
  • 05:23: that
  • 05:24: uh you know logical blocks are defined
  • 05:26: in python
  • 05:27: you have a colon and then you have an
  • 05:28: indent
  • 05:30: and so it's the same here for defining a
  • 05:32: function
  • 05:34: so defining a function that's uh going
  • 05:37: to start with a def
  • 05:38: you know just like a four leaf starts
  • 05:39: with a four and if statement starts with
  • 05:41: an if
  • 05:42: defining a function starts with a def so
  • 05:44: that's the first thing we'll see here
  • 05:46: for defining the function and then after
  • 05:49: that
  • 05:50: it's the name of the function whatever
  • 05:51: we want to call it
  • 05:53: uh you know it can be anything that
  • 05:55: describes what the function
  • 05:56: is doing um but it cannot have spaces in
  • 05:59: it
  • 06:00: we use underscores instead of spaces and
  • 06:03: typically people will make these
  • 06:04: uh as well so lowercase and underscores
  • 06:09: where you would normally have spaces
  • 06:12: so that's the name of the function and
  • 06:14: then we've got to have a parenthesis
  • 06:17: and then after the parentheses the open
  • 06:19: parenthesis then we define the arguments
  • 06:22: of the function so these are the inputs
  • 06:25: this is what we're going to be able to
  • 06:26: pass into the function
  • 06:28: um we're defining that here and so i
  • 06:31: said before you know it can have inputs
  • 06:33: it can have outputs
  • 06:34: it is possible to define a function
  • 06:36: without inputs you would just have open
  • 06:38: close parenthesis here
  • 06:39: instead of having things in here but
  • 06:42: this function here does have inputs
  • 06:44: it has three inputs a b and c
  • 06:47: and uh we'll notice a and b are there
  • 06:50: uh just by themselves and then we have c
  • 06:53: equals ten
  • 06:54: and we'll talk about what this equals
  • 06:56: ten means
  • 06:57: here in a moment but for now you know
  • 07:00: we've got three arguments three inputs
  • 07:02: to this function uh they're each going
  • 07:05: to be separated by commas
  • 07:07: and you're giving the name of each
  • 07:10: input uh here and then when you're done
  • 07:14: adding all the inputs then you put the
  • 07:16: closed parenthesis
  • 07:18: and then we have the familiar colon or
  • 07:20: after the colon
  • 07:21: then we've got to have that level of
  • 07:23: indentation and then at that level of
  • 07:25: indentation
  • 07:27: then we define the logic for the
  • 07:29: function
  • 07:30: so this can be absolutely anything that
  • 07:32: happens within the function it can be
  • 07:33: running whatever lines of code you want
  • 07:35: it can be calling other functions doing
  • 07:38: whatever
  • 07:39: uh you know for loops if whatever kind
  • 07:42: of logic you want to have in there
  • 07:44: and then at the end
  • 07:48: if we want to have an output from the
  • 07:49: function which most functions do have an
  • 07:51: output
  • 07:53: then we're going to want to return the
  • 07:56: result from that function so whatever
  • 07:59: you return
  • 08:00: is going to become the output of the
  • 08:03: function
  • 08:04: so what this function is actually doing
  • 08:06: is it's adding together
  • 08:08: the three inputs um and then returning
  • 08:11: that as the output
  • 08:12: so really it's like a sum function it's
  • 08:14: just summing up these three
  • 08:16: inputs that we can give to it
  • 08:19: and then it takes that sum and it's
  • 08:22: going to return that
  • 08:23: it's going to make that the output of
  • 08:25: the function
  • 08:28: so uh you know you're always going to
  • 08:31: have
  • 08:32: kind of two parts with functions the
  • 08:34: first
  • 08:35: is defining the function and then the
  • 08:37: second
  • 08:38: is using the function so defining the
  • 08:41: function
  • 08:43: no nothing actually happens like no
  • 08:45: computation is actually done
  • 08:48: after you've defined the function it's
  • 08:50: only when you use the function
  • 08:52: that is going to run the logic which is
  • 08:54: inside of the function
  • 08:56: so you define it one time and then you
  • 08:59: can use it
  • 08:59: as many times as you want so we could
  • 09:01: keep calling my funk again and again
  • 09:04: with all sorts of different inputs and
  • 09:05: get all sorts of different outputs uh
  • 09:08: as long as we defined it one time at the
  • 09:11: beginning
  • 09:12: and you have to define it before you can
  • 09:13: use it otherwise python is not going to
  • 09:16: know
  • 09:16: what you're talking about when you say
  • 09:18: myfunk you have to
  • 09:19: first define myfunk so that it knows
  • 09:22: what you're talking about
  • 09:25: and when we call a function uh then it's
  • 09:28: the name of the function the same name
  • 09:30: that you defined
  • 09:32: and then it's an open parenthesis and
  • 09:34: then we're going to pass
  • 09:35: the arguments to the function the inputs
  • 09:38: to the function and then we're going to
  • 09:40: close parentheses
  • 09:42: and that's how you call the function and
  • 09:45: each of those inputs is going to be
  • 09:46: separated by a comma
  • 09:49: and um you'll notice that we put in 5
  • 09:52: and 6 here
  • 09:53: and we get 21 as a result so how do we
  • 09:56: get to 21
  • 09:57: when we're just putting 5 and 6 in here
  • 09:59: it's supposed to be adding up the
  • 10:00: numbers
  • 10:01: well that brings in this last part here
  • 10:03: where we had to find
  • 10:05: c equals 10 in this last part of the
  • 10:07: function
  • 10:08: so that's what's called a default
  • 10:10: argument
  • 10:12: for this input c
  • 10:15: so c is going to default to 10
  • 10:19: whenever c is not passed so
  • 10:23: this is saying that 5 becomes a 6
  • 10:25: becomes b
  • 10:27: and then c is going to take its default
  • 10:29: value of 10 because we didn't pass
  • 10:31: anything else here
  • 10:32: and so that's why we get 5 plus 6 plus
  • 10:35: 10
  • 10:36: is equal to 21. so whatever
  • 10:39: you're passing as your inputs to the
  • 10:41: function
  • 10:43: then that's going to basically come
  • 10:45: inside the function
  • 10:46: that you defined and those values that
  • 10:49: you passed are going to take on the
  • 10:51: names
  • 10:52: of the inputs that you've defined here
  • 10:54: so this 5 becomes a
  • 10:56: this 6 becomes b and c uh
  • 10:59: you know takes its default of 10 going
  • 11:02: into the function
  • 11:03: but an important thing about functions
  • 11:06: is that those names those variables are
  • 11:08: scoped
  • 11:09: to the function itself and so if you run
  • 11:13: my funk 5 6 and then after that you say
  • 11:17: you know what is a you try to print out
  • 11:19: a it's going to give you a
  • 11:21: you know name error a is not defined and
  • 11:24: that's because
  • 11:25: a is only defined within this function
  • 11:28: as soon as you leave the function it's
  • 11:31: no longer defined
  • 11:33: and so basically you call this and then
  • 11:36: you've already defined my func
  • 11:38: and so it knows python knows now that my
  • 11:40: func basically means execute this thing
  • 11:43: and so it's going to go inside this
  • 11:45: thing and then as long as it's inside
  • 11:47: this
  • 11:47: a b and c have meaning they're defined
  • 11:50: uh but as soon as it returns and it
  • 11:52: leaves this function and we come back
  • 11:54: out to here and get 21
  • 11:56: then these a b and c are no longer
  • 11:58: defined it has onset
  • 12:00: those variables so that is definitely
  • 12:04: a gotcha for people just getting started
  • 12:06: to use functions they may try and use
  • 12:08: the variables which are inside of
  • 12:10: functions
  • 12:12: whereas those say within the function
  • 12:15: but
  • 12:16: as you get more comfortable with the
  • 12:17: stuff it's definitely a
  • 12:19: feature of functions because
  • 12:22: what that allows you to do is use much
  • 12:24: more general
  • 12:25: naming for things um you know you still
  • 12:29: want to use
  • 12:30: names which are indicative of what the
  • 12:31: input actually means
  • 12:33: but it saves you from having to keep
  • 12:35: coming up with different names more and
  • 12:37: more
  • 12:37: because that name is just going to last
  • 12:39: for the function
  • 12:40: and then it's not going to be used
  • 12:42: anymore and that frees you up to again
  • 12:43: use that same name
  • 12:45: in a different function without either
  • 12:47: of them
  • 12:48: conflicting with each other
  • 12:51: so that's the basic idea with functions
  • 12:54: let's go ahead and
  • 12:55: look at some examples in the jupyter
  • 12:58: notebook
  • 13:03: so again uh function is just taking a
  • 13:06: logical process
  • 13:07: and wrapping it up into a unit
  • 13:10: um so um
  • 13:14: we've talked about how we want to
  • 13:17: structure our model
  • 13:18: and you know basically layers of
  • 13:19: functions um
  • 13:22: but let's just say you know not even
  • 13:24: thinking about the full structure of the
  • 13:25: model
  • 13:26: you're just writing out some code and
  • 13:28: then you find yourself
  • 13:30: taking the same code and then copy
  • 13:31: pasting it and changing a little thing
  • 13:33: and then copy pasting it and then
  • 13:34: changing a little thing
  • 13:36: that should definitely be an indication
  • 13:38: to you that you should be using a
  • 13:40: function
  • 13:41: for this now the function is going to
  • 13:43: save you from
  • 13:44: from just copy pasting that code
  • 13:47: having basically you know really
  • 13:48: repetitive code
  • 13:50: functions can avoid that
  • 13:54: and that's definitely really useful to
  • 13:56: have
  • 13:57: because it really helps with the
  • 13:59: maintenance of your code over time
  • 14:01: if you copy paste a whole bunch of code
  • 14:04: and then
  • 14:04: you know you've got the same basic code
  • 14:06: copy pasted 100 times
  • 14:08: and then you realize you need to change
  • 14:10: one little thing about that code
  • 14:12: you've now got to go and change it in a
  • 14:13: hundred places right
  • 14:15: whereas if you wrote one function that
  • 14:17: does that logic
  • 14:19: and then you called that function 100
  • 14:21: times in a loop or something like that
  • 14:22: so it's really just a few lines of code
  • 14:24: you just change that one function and it
  • 14:26: makes that change carry through
  • 14:28: to all the usages of the function all
  • 14:30: those hundred usages
  • 14:32: and so now it becomes way easier to
  • 14:36: make changes to the code because it's
  • 14:39: going to flow through
  • 14:40: to all the different parts of your model
  • 14:45: so this will definitely become more
  • 14:46: concrete if we look at an example
  • 14:49: of this um so here
  • 14:52: we've got an example of
  • 14:55: um and we've got you know some prices
  • 15:00: uh for three different stocks in three
  • 15:02: different lists
  • 15:03: and we want to get some summary
  • 15:05: statistics on those
  • 15:06: prices um and so
  • 15:10: you know we want to uh figure out
  • 15:13: you know what was the last price what
  • 15:15: was the average price what was the max
  • 15:17: and the minimum price
  • 15:18: out of the prices that were given so you
  • 15:20: know you might
  • 15:21: you know okay i have prices apple i go
  • 15:23: and write out this code
  • 15:25: um that the average is you know the sum
  • 15:27: over the length
  • 15:28: um we take the max we take the min
  • 15:32: um to get those and we
  • 15:36: um take the the last one in the list to
  • 15:39: get the last price
  • 15:40: and then we print that all out in a
  • 15:41: string right um
  • 15:43: and that's all well and good as long as
  • 15:45: we're just doing this
  • 15:46: one time but then we have to do the same
  • 15:50: thing with microsoft and we have to do
  • 15:52: the same thing with
  • 15:52: amazon so you might think okay we'll
  • 15:55: just take this whole thing copy paste it
  • 15:57: and let me just change aapl to msft
  • 16:00: now and you know just keep doing that
  • 16:05: and now we've got basically three blocks
  • 16:07: of
  • 16:08: pretty much the exact same code just
  • 16:09: changing out the variable names
  • 16:12: each time and so then
  • 16:16: you know we think about we want to add
  • 16:17: the standard deviation now to this
  • 16:19: okay well now we have to add that in
  • 16:22: each one of these we have to add the
  • 16:24: standard deviation three times which is
  • 16:26: going to be a big pain
  • 16:27: also this is just really repetitive to
  • 16:29: look at um
  • 16:31: and it could be easy just to you know
  • 16:34: make a mistake in just one of these
  • 16:37: and then you uh could easily miss over
  • 16:40: that because it's just in one of them
  • 16:41: and not in all of them
  • 16:43: so there's a lot of drawbacks to having
  • 16:45: this kind of approach
  • 16:47: so if you you know start to feel your
  • 16:49: code getting like this
  • 16:51: then you should think about
  • 16:52: restructuring that code
  • 16:54: and so we can restructure that using
  • 16:56: functions
  • 16:58: so now here instead we have
  • 17:01: a function price summary statistics
  • 17:04: that is going to do all this for us
  • 17:08: and so for we're going to define
  • 17:11: the function price summary statistics
  • 17:13: that's the name of the function
  • 17:15: and it's going to take prices and it's
  • 17:18: going to take the ticker symbol
  • 17:20: um and then here next you can see
  • 17:23: this is that documentation that doc
  • 17:25: string
  • 17:26: that i was mentioning which describes
  • 17:29: what the function
  • 17:30: does so for docstring you
  • 17:33: triple quotes and then you put the
  • 17:36: description of the function
  • 17:37: and then you do another triple quotes
  • 17:41: and you can have this split on as many
  • 17:43: lines as you want that's fine
  • 17:45: by doing the triple quoted string that
  • 17:47: makes it a multi-line string
  • 17:50: so that you can have this kind of format
  • 17:54: so here we're describing the function to
  • 17:58: say it takes the prices and the ticker
  • 18:00: and then it prints the summary
  • 18:01: statistics so already
  • 18:03: you know this code is starting to get a
  • 18:04: lot clearer because
  • 18:06: you know we know that this is doing
  • 18:07: price summary statistics
  • 18:09: we have a description of what it's doing
  • 18:12: um
  • 18:13: and this code is definitely less
  • 18:15: repetitive than what we were looking at
  • 18:16: before there's just less code
  • 18:18: going on here to understand so
  • 18:22: uh then we have basically the same code
  • 18:24: as you know one of these blocks from
  • 18:26: before
  • 18:27: but it's even become a little bit
  • 18:28: shorter because we didn't have to do
  • 18:30: you know prices apple prices microsoft
  • 18:33: we could use a more general prices name
  • 18:36: uh you know again this is the advantage
  • 18:38: of that scoping the names
  • 18:40: uh that this prices is only going to be
  • 18:42: defined within the function
  • 18:44: and so when we run this for apple versus
  • 18:47: running it for microsoft versus running
  • 18:49: it for amazon
  • 18:50: this prices variable is not going to
  • 18:52: conflict uh
  • 18:54: it's just going to you know when we run
  • 18:56: it for apple it's going to
  • 18:57: you know be the prices apple which is
  • 18:59: taking its place when we run it for
  • 19:01: microsoft it's going to be prices
  • 19:03: microsoft taking its place
  • 19:05: um but we can just write prices here
  • 19:08: um and it becomes a more general name
  • 19:10: and that makes this thing easier to read
  • 19:14: so then we've got all the same basic
  • 19:17: logic here
  • 19:19: just shorter names it's a little bit
  • 19:20: easier to read
  • 19:22: and definitely a lot easier to read with
  • 19:24: the name of the function as well as the
  • 19:25: description of what it does
  • 19:28: and then we want to use this function to
  • 19:31: get the summary statistics
  • 19:32: on each of these stocks so that's where
  • 19:35: uh you know i'll go ahead and uh let's
  • 19:39: just say we didn't have that at first
  • 19:41: you'll see i run that and it does
  • 19:42: nothing right we get no output here
  • 19:44: and that's because all we've done is
  • 19:46: define the function
  • 19:47: we haven't actually called the function
  • 19:50: to use it
  • 19:51: uh so that's this other part here is
  • 19:54: then
  • 19:55: actually using the function actually
  • 19:57: calling the function
  • 19:58: and so that's where we put the name of
  • 20:00: the function the same name
  • 20:02: that we defined and then we put a
  • 20:05: parenthesis
  • 20:06: and then we pass whatever arguments and
  • 20:09: then a closed parenthesis and that's how
  • 20:11: you call
  • 20:11: the function um
  • 20:15: and so um here we're first
  • 20:18: passing price is apple um
  • 20:21: and the ticker symbol aapl
  • 20:24: and so that's going to come into this
  • 20:26: function uh so let me
  • 20:28: remove these at first we're just looking
  • 20:31: at the apple part so now that's going to
  • 20:34: take
  • 20:35: prices aapl which is all these different
  • 20:37: prices
  • 20:38: and now python knows because we defined
  • 20:42: price summary statistics it knows that
  • 20:44: this means okay i'm going to come into
  • 20:46: here
  • 20:46: and now prices aapl is going to become
  • 20:49: prices
  • 20:50: so for the scope of this function as
  • 20:52: long as we're executing this
  • 20:54: now the apple prices have become the
  • 20:56: prices
  • 20:58: and the apple ticker has become the
  • 21:01: ticker and so that's why we see
  • 21:03: uh you know all these apple uh specific
  • 21:06: stuff here
  • 21:07: even though we're using a general code
  • 21:10: um
  • 21:11: and then uh you know looking at the
  • 21:14: other two
  • 21:15: you know same thing we passed the
  • 21:16: microsoft prices and microsoft and we
  • 21:18: passed the amazon prices in amazon
  • 21:21: and that's why we get uh you know the
  • 21:23: statistics
  • 21:24: specific for those particular stocks so
  • 21:27: putting it all together
  • 21:29: then we have all
  • 21:33: three summary statistics here
  • 21:36: and if you're worried about the
  • 21:39: repetition of you know typing this
  • 21:43: three times you can go a little bit
  • 21:45: further with this
  • 21:46: you know you could create an all prices
  • 21:49: which actually has
  • 21:51: uh you know the uh the apple prices
  • 21:55: and then the microsoft prices
  • 21:58: and then the amazon prices
  • 22:01: and then you can you know create the
  • 22:04: tickers
  • 22:05: list which has aapl it has uh
  • 22:09: you know msft it has amzn
  • 22:12: and then uh you know instead of this we
  • 22:15: could do
  • 22:16: uh you know four uh
  • 22:21: for price list
  • 22:24: uh sticker symbol and
  • 22:28: uh we'll learn more about this zip later
  • 22:31: that lets you loop through
  • 22:32: uh two lists at once uh you could do
  • 22:35: that
  • 22:36: and you could uh then indent this and we
  • 22:40: can put
  • 22:41: uh this price list here and we can put
  • 22:44: this uh ticker symbol
  • 22:45: here um and then we would get the same
  • 22:48: output from that um so then that becomes
  • 22:51: even a little bit more general you can
  • 22:53: continue to add more price lists and
  • 22:55: tickers in here and it's just going to
  • 22:57: continue to add the output on there so
  • 23:00: definitely that second pattern was a
  • 23:01: little bit more advanced
  • 23:03: but you'll be more comfortable with
  • 23:05: these things as we progress
  • 23:07: throughout the course um
  • 23:10: so that's kind of the motivating example
  • 23:12: on why we might want to use functions
  • 23:15: uh now let's go to uh
  • 23:18: digging a little bit more into detail of
  • 23:20: how we actually use them
  • 23:22: so again first to find the function then
  • 23:25: use the function
  • 23:26: so here we're just defining uh nothing
  • 23:29: nothing happens as far as you know any
  • 23:31: kind of output or code getting executed
  • 23:33: it's just defined
  • 23:34: now python knows that atom
  • 23:37: will take two inputs and add them
  • 23:40: together
  • 23:40: and then return that as the output um
  • 23:45: so then we add them of two and five and
  • 23:48: that will give us
  • 23:49: 7 so that makes sense it added those two
  • 23:52: numbers together
  • 23:55: and the return is important here to get
  • 23:58: that output
  • 24:00: outside of the function if we had it as
  • 24:02: just
  • 24:03: you know atom is a plus b uh
  • 24:07: then we run this you'll now see that we
  • 24:09: have no result
  • 24:10: uh coming out of the function right
  • 24:12: because we didn't return it
  • 24:14: so it's not able to come outside of the
  • 24:17: function
  • 24:18: we need to return it to actually get it
  • 24:20: as output
  • 24:21: for the function
  • 24:28: so then you know that can definitely
  • 24:31: confuse people a little bit with
  • 24:32: uh you know you bring in print so you
  • 24:35: know when you print something
  • 24:36: it's going to show it um but that
  • 24:39: doesn't mean that that value is actually
  • 24:42: coming outside of the function uh the
  • 24:44: variable
  • 24:45: is gonna stay scoped to the function if
  • 24:47: you don't return it
  • 24:48: then uh you know that value is gonna be
  • 24:50: gone
  • 24:51: even though you already printed it um so
  • 24:54: here is just a function that always
  • 24:55: returns
  • 24:56: five um but you know whatever you get
  • 25:00: whatever you pass to it it's going to
  • 25:01: add 10 to it and then it's going to
  • 25:03: print that
  • 25:04: uh but then it's ultimately going to
  • 25:05: return five
  • 25:07: so you run this and you see my value is
  • 25:10: 110
  • 25:10: but then you see five so the result that
  • 25:13: actually
  • 25:14: came out of that uh is five because we
  • 25:17: returned five
  • 25:19: and then you'll notice now if we try to
  • 25:21: look at my value
  • 25:22: even though it was already printed out
  • 25:24: here and it worked fine
  • 25:25: now we get name error my value is not
  • 25:28: defined
  • 25:29: and that's because my value is only
  • 25:32: defined within this function it's scoped
  • 25:34: to this function and so within
  • 25:38: return 5 you know anywhere inside this
  • 25:40: logic we can use
  • 25:41: myvalue and it will work just fine
  • 25:46: but then as soon as we come outside the
  • 25:48: function we can no longer use
  • 25:50: myvalue or any other variables which are
  • 25:52: defined
  • 25:53: as part of that function or the inputs
  • 25:55: to that function
  • 25:58: they're just temporary temporary
  • 25:59: variables which last only for the
  • 26:01: function
  • 26:02: body
  • 26:07: then we can add a description
  • 26:10: of what our function does and that's
  • 26:13: called a doc string
  • 26:15: um so when you you know type out your
  • 26:18: function
  • 26:20: and you know it takes some values then
  • 26:22: you get to here
  • 26:23: uh if you start typing you'll see once i
  • 26:25: type the third
  • 26:28: quotation mark it actually
  • 26:29: auto-completes the rest of that it knows
  • 26:31: i'm trying to do a triple quoted
  • 26:33: uh string and then i just hit enter a
  • 26:35: couple times
  • 26:37: and then i can go ahead and type my
  • 26:39: description
  • 26:41: so that's what we've done here
  • 26:44: is describe this function it always
  • 26:46: returns 5 but it will also print
  • 26:48: my value plus the past
  • 26:51: you know 10 plus the past value um
  • 26:55: so that now is the description of what
  • 26:57: it does
  • 26:58: um so that serves a couple purposes
  • 27:01: one is that you know you just see the
  • 27:03: function in there uh
  • 27:05: as you're just reading through the code
  • 27:07: as someone who's trying to understand
  • 27:09: the model
  • 27:10: and you have a description you can you
  • 27:12: know see in plain text
  • 27:13: what that's actually supposed to be
  • 27:14: doing without having to understand the
  • 27:16: code at a great level of detail
  • 27:20: but the other thing which you know we
  • 27:22: won't necessarily
  • 27:24: uh be getting into this course we won't
  • 27:26: at least focus on it some people may
  • 27:28: use this but in python you can define
  • 27:31: things
  • 27:32: uh you know separately you can have a
  • 27:34: separate python file where you define
  • 27:36: some functions
  • 27:37: and then you import to bring those
  • 27:39: functions into
  • 27:41: this um namespace where you can use them
  • 27:46: and so where you're defining the
  • 27:47: function where this dock string
  • 27:49: is in the code might be somewhere
  • 27:51: totally different than where you're
  • 27:52: actually working right now
  • 27:54: um but that doc string actually does
  • 27:56: stay attached
  • 27:57: to the function uh so that we can look
  • 28:00: it up
  • 28:02: so if we do the jupiter shortcut of you
  • 28:04: know the question mark
  • 28:05: after the name of the function you can
  • 28:07: see it actually shows
  • 28:09: that same doc string that we defined
  • 28:12: over here
  • 28:12: and that's going to hold true even if
  • 28:14: this is defined in some totally
  • 28:16: different spot
  • 28:17: and so it's useful that that will stay
  • 28:19: with the function
  • 28:21: and that will also come up if you do the
  • 28:23: parentheses and then shift tab
  • 28:25: you'll also see the description of what
  • 28:27: that does
  • 28:28: so i mean even if you're all just in one
  • 28:30: jupiter notebook for your model
  • 28:32: um it's still useful to have that right
  • 28:34: because your functions may be defined
  • 28:36: like way up at the top of the model and
  • 28:38: then you're using it down at the bottom
  • 28:39: and you're like wait what did this
  • 28:41: function do
  • 28:42: well just shift tab and then you've got
  • 28:44: your own description
  • 28:45: of what that function does and the
  • 28:47: arguments the inputs that it takes what
  • 28:49: it's supposed to output
  • 28:50: and so that can be really useful for uh
  • 28:53: keeping yourself organized and not
  • 28:54: having to go wait what does that do
  • 28:56: let me scroll up and read that code
  • 28:58: again i have a description of what it
  • 29:00: does so i don't need to do that
  • 29:04: and then coming to the
  • 29:07: default arguments in functions default
  • 29:10: inputs um so that's when
  • 29:13: you put equals something after
  • 29:16: the input in the definition of the
  • 29:19: function
  • 29:21: um so here this function is about
  • 29:23: getting the beginning of a string
  • 29:25: it's going to get the first however many
  • 29:27: characters
  • 29:28: of the string and
  • 29:31: we're defaulting the number of
  • 29:33: characters to five
  • 29:35: so um you know whatever
  • 29:38: we uh pass here as the string it's going
  • 29:41: to be
  • 29:42: looking for the beginning of that string
  • 29:44: and then
  • 29:46: by default it's going to grab five
  • 29:47: characters so my space
  • 29:49: st and that's why we get that as a
  • 29:52: result
  • 29:54: but we don't have to leave it at that
  • 29:56: default argument we can pass a different
  • 29:59: value for that here we want to get six
  • 30:01: characters
  • 30:03: and so now it's going to add one
  • 30:04: additional character onto that
  • 30:07: so default arguments default inputs are
  • 30:09: really useful
  • 30:10: to make your function easier to use if
  • 30:13: it's something where you can think of a
  • 30:14: reasonable default
  • 30:16: um then it's useful to put one so that
  • 30:18: you don't necessarily
  • 30:19: have to add it when you call the
  • 30:20: function but you still retain the full
  • 30:23: flexibility and that you can
  • 30:25: pass whatever you want for that argument
  • 30:28: so it's nice you know for example when
  • 30:30: you create the function which just runs
  • 30:32: your entire model
  • 30:34: that it's going to already have as
  • 30:36: defaults all the just like baseline
  • 30:38: values of all your inputs
  • 30:40: and then you can just pass the inputs
  • 30:41: that you want to change
  • 30:43: and not have to pass every input every
  • 30:45: single time
  • 30:48: so that um you know covers the basics of
  • 30:51: functions and pythons and how we can use
  • 30:53: them
  • 30:53: to group logic into units and then reuse
  • 30:57: those units uh in a larger structure
  • 31:00: and then next time we're going to come
  • 31:02: back to discuss python basic data types
  • 31:06: and you know kind of what data types are
  • 31:08: out there and how we can
  • 31:10: work with them so thanks for listening
  • 31:12: and see you next time

Python Basic Data Types


Notes

  • Everything in Python is an object and every object has a type. The type defines the data structure and functions for that object

  • A class defines a type (see next section)

  • Strings are used to represent text and have a lot of convenience functions attached to them.

  • We can make a “format” or “f” string by putting f in front of it. This allows us to place variables in the string and apply formatting to them.

  • Most of the time you don’t need to care whether a number is an integer (int) or floating point number (float). Probably the only place we will care about it in this class is looping a number of times requires an integer.

  • range allows us to loop a certain number of times

  • tuple: think list that you cannot change after creating. Better to use these when you know the objects you want in the group at the outset and you know they will not change.

  • Dictionaries associate one object (value) with another object (key) and allow you to look up the value by the key

  • Because everything in Python is an object, you can have any arbitrary nesting of objects. Lists of lists of strings, dictionaries where keys are integers and values are lists of strings, and so on, whatever is appropriate to solve your problem

  • The lab exercises test working with tuples, dicts, and nested data structures

Transcript

  • 00:03: hey everyone
  • 00:04: nick dear brotis here teaching you
  • 00:05: financial modeling and today
  • 00:07: we're going to talk about basic data
  • 00:09: types in python
  • 00:11: as part of our segment on going beyond
  • 00:13: an initial python script
  • 00:15: how we can learn the python basic tools
  • 00:17: and skills so that we can go
  • 00:19: and build financial models using python
  • 00:22: so we've talked a little bit about data
  • 00:25: types
  • 00:26: kind of informally so far
  • 00:30: we've talked about a few different data
  • 00:32: types already
  • 00:34: we have learned about strings we've
  • 00:36: learned about numbers
  • 00:38: lists and booleans so far
  • 00:41: [Music]
  • 00:42: and everything in python
  • 00:45: is an object and every object has
  • 00:48: a type um so this type defines
  • 00:53: uh the data structure or
  • 00:56: whatever object this is as well as
  • 00:59: any functions uh which are attached to
  • 01:02: that
  • 01:03: object and when a function is attached
  • 01:06: to an object
  • 01:06: in python we call that a method but
  • 01:08: still you know basically
  • 01:10: just a function attached to an object
  • 01:14: um so in order to
  • 01:18: define how type
  • 01:21: uh you know what what functions are on
  • 01:23: the type what is the data structure for
  • 01:25: the type
  • 01:26: in order to define that we use a class
  • 01:29: and that's going to be
  • 01:30: the next video is focusing on classes
  • 01:33: and and how
  • 01:34: we can define custom types in python
  • 01:38: but for the purposes of this video we're
  • 01:41: just going to focus on
  • 01:42: the basic data types that are built into
  • 01:44: python and how we can work with them
  • 01:49: so um you know we already know about
  • 01:52: strings we've already used strings
  • 01:54: you know they represent text characters
  • 01:58: [Music]
  • 01:59: and now let's learn a little bit more
  • 02:02: about things that we can
  • 02:03: do with those strings so
  • 02:07: you might have noticed if you do some
  • 02:09: calculation
  • 02:10: uh you end up with a number there might
  • 02:12: be a lot of decimals
  • 02:13: on that number um or the idea that
  • 02:17: you know you can't just type a
  • 02:18: percentage into python right like you
  • 02:21: have to put it as a decimal
  • 02:23: but then you want to have that formatted
  • 02:25: as a percentage when you display it
  • 02:28: and all sorts of other formats
  • 02:34: these formatted uh strings are going to
  • 02:37: be useful to help us out with that
  • 02:39: um and also you know it can be including
  • 02:43: whatever your result is as part of a
  • 02:45: sentence
  • 02:47: you know mary has five years till
  • 02:50: retirement
  • 02:51: versus just putting the number five out
  • 02:53: there
  • 02:54: so the formatted strings also help with
  • 02:58: that
  • 02:59: and so these formatted or f strings
  • 03:02: is the same way that you define a string
  • 03:05: but then you just put this
  • 03:06: f on the beginning of it and that makes
  • 03:09: it into an s string or a format string
  • 03:11: and that
  • 03:12: gives it this extra functionality that
  • 03:15: we can
  • 03:16: just drop other variables or expressions
  • 03:19: into the string
  • 03:20: and also uh tell it how that we want
  • 03:22: those
  • 03:23: formatted in the ultimate output
  • 03:26: so here's a little example of that so
  • 03:29: first doing a short calculation here
  • 03:32: just doing five divided by six
  • 03:34: and then if you just print that you just
  • 03:36: show that result
  • 03:38: uh we're looking at point eight three
  • 03:39: three three three three
  • 03:41: uh you know repeating um and so
  • 03:44: obviously the output of that
  • 03:45: does not look great right we probably
  • 03:48: want to round that
  • 03:49: if we're going to display that to the
  • 03:51: consumer of our model
  • 03:55: and then also you know what is this
  • 03:56: number what is the context for that
  • 03:58: number
  • 03:59: it's definitely really helpful to
  • 04:01: include that in a sentence
  • 04:02: when we display it or some other kind of
  • 04:06: way to tell the person who's reading
  • 04:09: your model
  • 04:10: what that number actually means and here
  • 04:13: you know
  • 04:14: we're just saying my number is because
  • 04:16: you know it's just an example
  • 04:17: calculation but
  • 04:18: in reality you would want to actually
  • 04:20: describe what that number is
  • 04:23: um so you see the result of
  • 04:26: using this f string is ultimately we get
  • 04:29: this kind of
  • 04:31: output my number is 0.83 and now it does
  • 04:34: not have
  • 04:35: all these decimals on it we can see it
  • 04:37: only has two
  • 04:39: decimal places so we can break down how
  • 04:42: we did this
  • 04:43: so you'll notice uh with this f string
  • 04:46: uh
  • 04:46: you see we have we have that f and then
  • 04:49: the quotes so that's how we know it's a
  • 04:50: formatted or an f string and that means
  • 04:52: that
  • 04:53: we can do this it's not just a normal
  • 04:55: plane string
  • 04:58: um and then we have my number is you
  • 05:00: know just like we're going to see in the
  • 05:01: output
  • 05:02: and then here comes the special part so
  • 05:04: here we have
  • 05:06: um the curly braces
  • 05:09: and within the curly braces that's where
  • 05:12: we can do
  • 05:13: this variable substitution or expression
  • 05:15: substitution
  • 05:17: uh in order to drop our numbers into the
  • 05:20: middle of a string
  • 05:21: so whenever you do the curly braces
  • 05:23: that's going to say
  • 05:25: here is where i want to put whatever
  • 05:27: variable or expression
  • 05:28: you know you could even do simple math
  • 05:30: inside of here or whatever
  • 05:33: uh call a function or whatever you want
  • 05:35: to do valid but
  • 05:36: as long as it's valid python for the
  • 05:38: most part it should work in here
  • 05:40: um and then inside those curly braces is
  • 05:44: where we put
  • 05:45: you know what we actually want to put
  • 05:47: into the string
  • 05:48: and you'll notice you know here as well
  • 05:50: as you know jupiter and
  • 05:52: any other editor is going to help you
  • 05:54: out here
  • 05:55: and you notice that uh this changed
  • 05:58: color versus the rest of the string
  • 06:00: right the rest of the string is red
  • 06:02: signifying that it's just a string but
  • 06:05: then this part
  • 06:06: went black signifying that it's now
  • 06:09: looking at the variable mynum and not
  • 06:12: just a string
  • 06:13: mynum so we're going to take this value
  • 06:17: and we're putting it here in this point
  • 06:19: in the string so
  • 06:22: you know if without this extra part we
  • 06:23: would get my number is
  • 06:25: zero point eight three three three three
  • 06:26: through three uh and so on
  • 06:28: if you just did curly braces my num
  • 06:31: curly braces
  • 06:32: that's without doing any kind of
  • 06:33: formatting to it just put it in there
  • 06:36: exactly like it would print normally uh
  • 06:38: but then we also want to apply
  • 06:40: formatting to it
  • 06:41: so to apply formatting to it then we
  • 06:44: have this
  • 06:45: colon and then after the colon we
  • 06:48: specify
  • 06:48: a format code um which says
  • 06:52: you know how do we want to format this
  • 06:54: number uh so what this one
  • 06:56: here means is we want to have a decimal
  • 07:00: format we want to have two decimal
  • 07:02: places and we want that to be a fixed
  • 07:04: number of decimal places uh so even if
  • 07:08: you put like five
  • 07:09: just plain five in here it would come as
  • 07:11: five point zero zero
  • 07:13: because we have two fixed decimal places
  • 07:17: [Music]
  • 07:18: and there's a lot of different codes
  • 07:21: that you have there you know it goes
  • 07:22: beyond just
  • 07:23: basic numbers you can also have things
  • 07:26: like formatting a date
  • 07:29: or other things um and
  • 07:32: i have included in the resources a
  • 07:34: reference that you can look at
  • 07:36: uh for all the possible format codes but
  • 07:38: for the most part
  • 07:39: uh you know we're gonna use like this
  • 07:41: fixed uh
  • 07:42: you know just number of decimals or
  • 07:44: we're going to format it as a percentage
  • 07:46: those are going to be the main
  • 07:48: uh ways that we're going to use these as
  • 07:50: far as this class
  • 07:54: and when we jump into the jupiter
  • 07:56: example we'll look at a few other
  • 07:58: things that we can do with strings as
  • 07:59: well
  • 08:02: then jumping back over to numbers in
  • 08:04: python so we've already talked about
  • 08:06: numbers in python we've already used
  • 08:08: numbers in python
  • 08:10: um but you know i've been saying that
  • 08:12: numbers are type
  • 08:14: in python but that's actually a bit of a
  • 08:16: simplification
  • 08:17: uh there are actually two main types of
  • 08:20: numbers
  • 08:21: in python a floating point number and an
  • 08:24: integer
  • 08:25: or float and end would be these two
  • 08:29: main data types for numbers
  • 08:33: and the vast majority of the time you
  • 08:35: don't have to care
  • 08:37: whether it's a floating point number or
  • 08:39: an integer
  • 08:40: it really does not matter most the time
  • 08:42: and that's why i've
  • 08:43: you know just kind of simplified it so
  • 08:45: far
  • 08:47: but occasionally it does come up and so
  • 08:50: it's important to
  • 08:51: know about it still um and so the
  • 08:54: difference here
  • 08:55: is that you know an integer you know
  • 08:57: just like going back to
  • 08:58: you know plain old number theory an
  • 09:00: integer is you know a whole number
  • 09:03: uh you know one two three negative one
  • 09:05: two three
  • 09:06: zero uh it doesn't have any decimals
  • 09:10: whereas a floating point number does
  • 09:12: have decimals
  • 09:15: so even if it's zero
  • 09:18: even if all the decimals are zero if it
  • 09:21: has decimals
  • 09:22: it becomes a floating point number
  • 09:26: so that's why if you look at a couple
  • 09:29: examples here
  • 09:30: so 3.5 that's a floating point number a
  • 09:34: float
  • 09:35: 3.0 you might think that's an integer
  • 09:38: but actually it's a float as well
  • 09:40: because we did point zero
  • 09:42: uh even though you know that's three
  • 09:44: it's the exact same value as the integer
  • 09:46: because we did that point zero that
  • 09:48: makes it actually change the data type
  • 09:50: to a floating point number rather than
  • 09:53: the integer
  • 09:54: it's only if we just put the integer
  • 09:57: part of it just a 3
  • 09:58: then it becomes an integer
  • 10:01: so uh and you know you can compare
  • 10:04: across
  • 10:05: uh floats and then just fine you know if
  • 10:07: we do 3.0
  • 10:09: is that equal to three that'll return
  • 10:11: true those are definitely equal
  • 10:14: and yet they have different data types
  • 10:17: um and so again most the time this
  • 10:20: doesn't matter
  • 10:21: but one instance where we are going to
  • 10:24: run in that it does matter
  • 10:26: is if you want to loop a certain number
  • 10:29: of times
  • 10:30: and we'll look at that in the jupiter
  • 10:32: example how we do that say we just want
  • 10:34: to loop five times
  • 10:36: uh you cannot give it a float you can
  • 10:38: only give it
  • 10:39: an integer uh because you know looping
  • 10:42: 5.6 times
  • 10:44: doesn't mean anything right like what do
  • 10:46: you do for that point six
  • 10:48: loop um and so python has restricted it
  • 10:51: so that it only will take an integer
  • 10:52: because those would be the
  • 10:54: sensical values for that um so if you
  • 10:57: try to pass it
  • 10:58: afloat then you'll get an error
  • 11:01: about that and we'll see an example of
  • 11:03: that in the jupiter notebook
  • 11:08: and then we'll just introduce a couple
  • 11:10: additional built-in types
  • 11:13: and these are both like container types
  • 11:15: they hold other
  • 11:16: objects so one here is a tuple
  • 11:20: and a tuple you can think of as it's
  • 11:23: basically a list
  • 11:24: uh you know it holds items they're
  • 11:26: ordered you can look them up by index
  • 11:29: but the one main difference between a
  • 11:31: tuple and a list
  • 11:32: is that uh you know with the list you
  • 11:35: can create it with ever
  • 11:36: with whatever items and then you can add
  • 11:38: items you can remove items
  • 11:40: uh whereas the tuple cannot be changed
  • 11:43: after
  • 11:43: it's created whatever you have in there
  • 11:45: when you create it
  • 11:46: is going to be what's always in that
  • 11:48: tuple you cannot append
  • 11:50: you cannot insert you cannot remove
  • 11:53: and so it's always what you started with
  • 11:57: um and the difference in specifying it
  • 12:00: is that you're going to use parentheses
  • 12:02: instead of square brackets um so this
  • 12:05: here would be a tuple of a and b
  • 12:11: and you know generally you're just going
  • 12:13: to want to use tuples
  • 12:14: if it's some instance where you don't
  • 12:17: need to change the items
  • 12:19: and if it is some instance where you are
  • 12:21: going to need to change it
  • 12:22: as you go through your code then you
  • 12:25: want to use a list
  • 12:26: instead so you know not a not a huge
  • 12:30: difference
  • 12:31: but it's definitely a nice little extra
  • 12:34: protection that if you're
  • 12:36: expecting that this um you know it
  • 12:39: shouldn't change
  • 12:40: at all uh if you use a list for that you
  • 12:43: could accidentally append to it or
  • 12:45: remove from it and that could mess up
  • 12:47: all your other code
  • 12:48: so it's just a little bit safer to use
  • 12:50: tuples and they are more
  • 12:52: performant as well
  • 12:55: then the other data type that we're
  • 12:57: introducing here
  • 12:59: is a dictionary or dict
  • 13:02: and so that is a mapping of values to
  • 13:05: other
  • 13:06: values so you know you could
  • 13:10: map a to one and then you can go back to
  • 13:13: this dictionary and look up
  • 13:14: what's the value for a and you're going
  • 13:16: to get one
  • 13:18: is the basic idea behind them um
  • 13:22: and we're not going to use them
  • 13:23: immediately other than just you know
  • 13:25: introducing them and playing around with
  • 13:26: them understanding how to use them
  • 13:28: um but we will definitely use them as we
  • 13:32: proceed throughout the course
  • 13:33: so i wanted to introduce them now since
  • 13:35: they are a very fundamental data type
  • 13:37: in python
  • 13:40: so with that we can jump over to the
  • 13:43: jupiter example
  • 13:48: so uh first going back to strings
  • 13:51: here um so again uh
  • 13:55: you know you define it with quotes then
  • 13:57: you're going to get a string
  • 13:58: right so we have that string
  • 14:02: abc uh and we have the type of that
  • 14:04: being
  • 14:05: a string um and
  • 14:09: one thing which we haven't mentioned
  • 14:11: before is you do have an option
  • 14:13: of whether you want to use single quotes
  • 14:16: as i have been using
  • 14:18: or you can use double quotes for that as
  • 14:21: well
  • 14:22: it does the same thing and so it doesn't
  • 14:26: really matter
  • 14:27: which one you use now you don't want to
  • 14:30: mix them you don't want to do a single
  • 14:32: quote and then a double quote
  • 14:34: that is going to give you a syntax error
  • 14:36: so watch for that
  • 14:37: you know typically just pick one style
  • 14:39: and and stick with it
  • 14:41: it really doesn't matter too much the
  • 14:43: only time that
  • 14:44: i ever make a conscious decision of
  • 14:47: which one i'm going to use
  • 14:48: is if i want to use the other
  • 14:54: mark inside the string right so here i
  • 14:57: wanted to use
  • 14:58: a single uh quote inside the string and
  • 15:01: so i use double quotes on the outside
  • 15:03: if i use single quotes on the outside
  • 15:05: for that
  • 15:06: then i mean you can see already that
  • 15:08: it's messed up just looking at the
  • 15:10: coloring here
  • 15:11: and again we're going to get a syntax
  • 15:13: error because it interprets the string
  • 15:14: ending here
  • 15:16: and then from here it starts to
  • 15:17: interpret these as just variables and
  • 15:19: and yeah it can't uh this is not valid
  • 15:22: python at this point
  • 15:24: um you can work with that by putting a
  • 15:26: backslash there
  • 15:27: that escapes the uh
  • 15:30: the quote and then we're back to uh what
  • 15:34: we have before
  • 15:35: um but i think it's you know if you know
  • 15:37: you're going to use a single quote
  • 15:39: then just put double quotes on the
  • 15:41: outside that's a little bit clearer
  • 15:44: and then if you know you're going to
  • 15:46: have to use double quotes
  • 15:48: then again you would want single quotes
  • 15:51: on the outside to
  • 15:53: make sure that you don't have any issues
  • 15:55: there
  • 15:57: but beyond that it really does not
  • 15:59: matter just pick one
  • 16:01: and stick with it consistently make sure
  • 16:03: that you have the same
  • 16:04: character on both sides of the string
  • 16:06: for it to be valid
  • 16:11: so there's a lot that we can do with
  • 16:13: strings
  • 16:16: and so if you want to look at
  • 16:19: you know all the things that you can do
  • 16:22: um
  • 16:22: then you just can you know do dot
  • 16:26: after the string and then hit tab and
  • 16:29: then
  • 16:29: jupiter is gonna pop up all the possible
  • 16:32: things
  • 16:33: that you can do here um so there's tons
  • 16:35: here
  • 16:36: and we're not going to use the vast
  • 16:38: majority of it in the course
  • 16:40: but it's all there you know basically if
  • 16:43: you think
  • 16:44: uh how do i capitalize a string
  • 16:48: well you can you know just just scroll
  • 16:50: through these and look for what
  • 16:52: you know makes sense oh a dot
  • 16:53: capitalized yeah that sounds right
  • 16:56: um now i can see that it got capitalized
  • 16:58: at the beginning
  • 17:00: um but what if uh you know
  • 17:04: i don't know i want to get it to all
  • 17:07: uppercase and i don't know
  • 17:09: uh you know again i could look through
  • 17:10: the methods and try and pick it out
  • 17:12: um but definitely you know google is
  • 17:15: your friend
  • 17:16: uh python string uh
  • 17:19: to all uppercase are all caps let's do
  • 17:22: that
  • 17:22: um so then um
  • 17:26: we can see some results coming up oh
  • 17:28: string dot
  • 17:29: upper maybe that's uh what we need
  • 17:32: oh string dot upper okay that's what we
  • 17:34: can do
  • 17:35: um so then we put upper there instead
  • 17:39: and we can see that does indeed work
  • 17:43: so definitely google is your friend for
  • 17:46: finding out what you can do in python
  • 17:47: there are so many resources out there
  • 17:50: there are millions and millions of
  • 17:51: people out there who work with python
  • 17:54: and so it's really easy to find help uh
  • 17:57: for you know how do i do this particular
  • 17:59: thing just google it
  • 18:01: you're most likely going to find some
  • 18:03: kind of answer
  • 18:05: um and then don't really worry about
  • 18:07: this code this was just so that
  • 18:09: in the notebook i could have printed out
  • 18:11: all these different methods that we have
  • 18:13: on the string
  • 18:17: and then you know here my a
  • 18:22: is all lowercase so you know is lower i
  • 18:24: can check if it's all lowercase so you
  • 18:26: know these are just more examples of
  • 18:27: things you can do with the strings
  • 18:29: but there's so many things here and you
  • 18:32: can just google for whenever you need to
  • 18:33: do anything
  • 18:34: in particular
  • 18:38: so moving on to numbers um
  • 18:42: so again it's not just numbers we have
  • 18:46: floats and ants in python so 3.5 is a
  • 18:49: float
  • 18:50: 3.0 is a float 3 is an n
  • 18:54: even though 3 is equal to 3.0
  • 18:57: we can see that the types themselves are
  • 19:00: not equal
  • 19:01: even though the values are equal
  • 19:05: so again it doesn't really matter most
  • 19:07: of the time whether you have floats or
  • 19:10: integers you know you can go and you can
  • 19:12: do math
  • 19:13: uh you know with with one being a float
  • 19:16: and one being the integer
  • 19:17: it doesn't matter um
  • 19:20: where it does matter is when we want to
  • 19:22: loop a certain number of times
  • 19:24: um and there are other places as well
  • 19:26: but you know probably
  • 19:28: they're we're not really going to hit
  • 19:29: them in the course this is the one that
  • 19:31: i know is going to come up in the course
  • 19:34: um so here is a basic way that we can do
  • 19:38: a loop
  • 19:38: for a number of times this is the
  • 19:40: built-in function range
  • 19:42: you give a range a number
  • 19:46: and it's basically like making a list
  • 19:50: in the background of all the numbers
  • 19:54: leading up to that point
  • 19:56: um not exactly it's not actually a list
  • 19:59: it's a generator but we don't need to
  • 20:01: worry about that we can think of it as a
  • 20:03: list
  • 20:04: uh and effectively for our purposes it
  • 20:06: works uh
  • 20:07: like creating a list with um here it
  • 20:10: would be
  • 20:11: zero one two three four um so again that
  • 20:15: um zero based indexing um
  • 20:18: it's gonna start from zero on the first
  • 20:20: one and it's going to reach
  • 20:22: ultimately the fifth number which is
  • 20:24: four
  • 20:26: um so then if we do want to get you know
  • 20:29: starting from
  • 20:30: one instead you can either you know just
  • 20:34: add one
  • 20:35: to the number you know here is also
  • 20:37: showing that we can put an actual
  • 20:39: expression
  • 20:40: inside one of these f strings it doesn't
  • 20:42: just have to be a variable
  • 20:43: we also don't need to have the format
  • 20:45: part we can have just
  • 20:47: the uh the value itself here the value
  • 20:50: didn't need formatting and so
  • 20:51: we left it off um
  • 20:54: and so you can do it that way
  • 20:58: or you can
  • 21:02: instead you know start the range from
  • 21:04: one and then go to
  • 21:06: one higher than where you actually want
  • 21:08: to go that will also
  • 21:09: uh get you the one based indexing there
  • 21:14: um but anyway so that's you know how you
  • 21:17: can run a loop for a certain number of
  • 21:18: times
  • 21:20: but you know let's say that we
  • 21:23: um you know we had uh
  • 21:28: let's see you know num loops um
  • 21:31: equals 10 divided by two um and so okay
  • 21:34: yeah that should be five
  • 21:36: loops right um and then we put that
  • 21:39: there
  • 21:40: um we're gonna get this float object
  • 21:42: cannot be interpreted as an integer
  • 21:45: because this num num loops
  • 21:49: came as a float any result of division
  • 21:52: is going to be a float
  • 21:54: and so this is not going to work you
  • 21:57: need to pass an integer here
  • 21:58: even though uh you know the value of
  • 22:02: that
  • 22:03: is five and we check
  • 22:06: is it equal to five yeah why doesn't
  • 22:08: this work right but it's because it's
  • 22:10: not
  • 22:10: an integer so
  • 22:13: there is a very easy way to fix this
  • 22:16: if you take this you know we have this
  • 22:22: num loops which is a float but if we uh
  • 22:26: take the integer of num loops
  • 22:31: then that will get us back to having an
  • 22:33: integer
  • 22:34: so we just wrap it in int and then we're
  • 22:37: going to have
  • 22:38: an integer come out of that you do need
  • 22:41: to be careful with that because that
  • 22:43: will kind of do the floor
  • 22:44: if you do n of 5.6 that's gonna give you
  • 22:47: five right you are losing information
  • 22:49: there
  • 22:50: so you want to make sure to only do that
  • 22:51: if you know that it's a whole number
  • 22:54: anyway or you don't care about this part
  • 22:56: of the number
  • 22:57: um but it is a solution for this because
  • 23:00: we know this is going to be a whole
  • 23:01: number
  • 23:02: it's just coming as a float we just put
  • 23:05: this
  • 23:05: in an n as well and now all of a sudden
  • 23:09: this works just fine
  • 23:13: so that's uh the additional material on
  • 23:16: numbers and now let's look at
  • 23:18: tuples so again tuples you know work a
  • 23:21: lot like lists
  • 23:24: you can index them you can loop through
  • 23:26: them just like lists
  • 23:29: but you just can't add or remove items
  • 23:32: so here i'm going to define
  • 23:34: my tube as one two three uh so we have
  • 23:38: these three numbers into this tuple
  • 23:42: um that's one way you can create a tuple
  • 23:45: another way
  • 23:45: is you know if you already had a list
  • 23:47: you can convert that list into a tuple
  • 23:50: so here i have a list abc and now i'm
  • 23:53: doing
  • 23:54: tuple of my list so here another
  • 23:57: built-in function
  • 23:58: tuple um
  • 24:01: and that's converting that list into a
  • 24:03: tuple now
  • 24:05: now that list still exists you already
  • 24:08: defined it it's still there
  • 24:09: but you just now also have this tuple
  • 24:14: separately which you're not going to be
  • 24:16: able to add or remove items
  • 24:19: and then you know the same thing works
  • 24:21: for converting from tuples into lists
  • 24:23: uh if you wrap you know whatever in list
  • 24:26: then it's going to convert it to a list
  • 24:28: so here we went from a list to a tuple
  • 24:31: and back
  • 24:32: to a list you know really just for sake
  • 24:34: of example here
  • 24:35: you wouldn't normally just be going back
  • 24:36: and forth for no reason
  • 24:38: but um just showing that you can easily
  • 24:41: convert back and forth between lists and
  • 24:44: tuples
  • 24:46: um now we know that um
  • 24:50: you know we define lists like uh we can
  • 24:52: do a
  • 24:53: equals you know five six seven or
  • 24:56: whatever
  • 24:56: and then that's you know gonna give us
  • 24:58: five six seven also
  • 25:00: uh if you just have that five in there
  • 25:03: then that also
  • 25:04: works just fine uh the type of that
  • 25:07: is still going to be a list but you do
  • 25:10: need to be careful with tuples
  • 25:12: uh when you create a tuple um you know
  • 25:15: also
  • 25:16: if you have multiple items you don't
  • 25:18: really have to think about it it'll work
  • 25:19: fine
  • 25:20: the issue is creating a tuple with a
  • 25:22: single item in it
  • 25:24: uh because if uh you want to create a
  • 25:27: tuple with a single item you
  • 25:29: do have to put a trailing comma on it
  • 25:32: so that python knows that it's a tuple
  • 25:35: so this here is going to create a tuple
  • 25:38: um whereas this here is not going to
  • 25:42: create a tuple
  • 25:43: even though you think you know i put the
  • 25:44: parentheses it should be a tuple
  • 25:46: uh if there's only a single item you do
  • 25:49: need that trailing comma
  • 25:51: so you can see actually it's not coming
  • 25:53: as the tuple of a anymore it's coming as
  • 25:55: just a
  • 25:56: and it's coming as just a string and
  • 25:59: that's because
  • 26:00: you know tuples are not the only meaning
  • 26:02: of parentheses in python
  • 26:04: parentheses and python can also be used
  • 26:07: to group things
  • 26:08: for you know like order of operations
  • 26:10: purposes
  • 26:11: um so here we basically made a group
  • 26:13: which has just a in it
  • 26:15: which just then again evaluates to a um
  • 26:19: instead of uh what we intended to do
  • 26:21: which was make a tuple
  • 26:23: there you do need the comma to say that
  • 26:25: it's tuple
  • 26:28: um but you know just like lists you can
  • 26:31: index them
  • 26:34: so my second tuple again was abc
  • 26:38: um so you know zero is going to give us
  • 26:40: the first item
  • 26:41: up until a negative one gives us
  • 26:43: everyone with the last item so the
  • 26:44: indexing
  • 26:46: all works exactly the same as it does
  • 26:48: for lists
  • 26:49: um you can also uh loop through them
  • 26:53: just like um just like this
  • 26:57: um so that works just fine
  • 27:00: um the one thing that you can't do
  • 27:03: is you can't go and try uh and append to
  • 27:07: it
  • 27:07: um so let's try and append 10 oh wait
  • 27:12: super object has no attribute append
  • 27:14: right because you can't add
  • 27:16: anything to an existing tuple
  • 27:21: so that's tuples and then the other
  • 27:24: totally new data type we're looking at
  • 27:26: here
  • 27:28: is uh dictionary so dictionaries i mean
  • 27:31: it's going to be you know dict is
  • 27:32: is how it's you know talked about in
  • 27:34: python
  • 27:36: so that's a mapping of items to other
  • 27:39: items objects
  • 27:40: other objects and so you know here's an
  • 27:43: example
  • 27:44: and you can define the dictionary with
  • 27:47: these curly braces
  • 27:49: so what this is saying is we have a
  • 27:52: dictionary
  • 27:53: and we have uh the key a
  • 27:57: mapped to the value one and we have the
  • 27:59: key b
  • 28:01: mapped to the value two so it's whatever
  • 28:04: you know two things you wanna associate
  • 28:06: with each other you put the key first so
  • 28:09: the key
  • 28:09: is what we're gonna be able to look
  • 28:11: things up by
  • 28:13: and then you put a colon and then you
  • 28:16: put
  • 28:16: what you want the value or what is the
  • 28:19: result
  • 28:20: of looking up by the key so
  • 28:24: let's make that a little bit more
  • 28:25: concrete so i've got my dictionary here
  • 28:31: a is associated with one b is associated
  • 28:33: with two
  • 28:34: that means we can go and we can look up
  • 28:37: the value
  • 28:38: for a and here you know we say give me
  • 28:40: the value
  • 28:41: for a out of my dictionary and we get 1
  • 28:44: because 1
  • 28:45: was associated with a similarly we can
  • 28:49: look up b
  • 28:50: and it's going to give us the value 2
  • 28:54: because that was the value associated
  • 28:56: with b
  • 28:58: and if you try to look up a key which is
  • 29:00: not there
  • 29:02: then you're going to get a key error
  • 29:04: because it cannot find that key
  • 29:07: in the dictionary
  • 29:10: and also you cannot look up by the value
  • 29:13: you cannot look up one and expect
  • 29:15: to get a uh you know there is there are
  • 29:18: definitely ways to reverse the
  • 29:19: dictionary and
  • 29:20: and make it look up the opposite way but
  • 29:22: you know just basic usage
  • 29:24: you look up by the keys and you get the
  • 29:26: values as a result look up by
  • 29:28: a and b get one and two not the other
  • 29:32: way around
  • 29:34: um so we'll definitely do a fair amount
  • 29:38: more with dictionaries as we go through
  • 29:39: the course
  • 29:41: but i just wanted to kind of introduce
  • 29:42: them very quickly here
  • 29:44: just so you're familiar with the idea of
  • 29:47: them and we'll build on that more
  • 29:49: later
  • 29:53: uh so the last uh topic that i want to
  • 29:55: talk about within
  • 29:56: the basic data types is this concept
  • 30:00: that um again everything in python is an
  • 30:03: object
  • 30:03: and so everything is kind of treated in
  • 30:06: the same way and that
  • 30:08: allows you to have any kind of nesting
  • 30:11: of data types that you want you can have
  • 30:13: very
  • 30:15: you know complex nested data structures
  • 30:18: just based out of these basic data
  • 30:21: so for an example a very common
  • 30:25: one of these types of data structures is
  • 30:27: to have a list
  • 30:28: of dictionaries so then
  • 30:31: you know each dictionary is representing
  • 30:33: like some kind of entity
  • 30:35: here each dictionary is representing
  • 30:39: a person and we have multiple people
  • 30:43: so each dictionary is one person
  • 30:46: and then we have multiple people in the
  • 30:49: inputs list um and then within the
  • 30:53: dictionary we have
  • 30:54: you know some different characteristics
  • 30:56: of this person
  • 30:58: so uh name and weight are the two
  • 31:00: characteristics that we're tracking here
  • 31:03: um and so for john we have his name is
  • 31:06: john of course
  • 31:07: and then that his weight is 180 and for
  • 31:09: the other one the name is sarah and the
  • 31:10: weights 140.
  • 31:12: um so then you can do something like
  • 31:16: uh you know loop through this inputs
  • 31:19: list so that
  • 31:20: this input variable that we're getting
  • 31:22: here is the entire dictionary itself
  • 31:25: and then we want to pull the name out of
  • 31:27: the dictionary and the weight
  • 31:29: out of the dictionary to display details
  • 31:31: about each of these people
  • 31:34: so then you know this is each of these
  • 31:36: people introducing
  • 31:37: themselves for whatever reason they
  • 31:39: introduce themselves by saying their
  • 31:41: weight
  • 31:42: but uh you know the basic idea here is
  • 31:46: you can work with the any kind of nested
  • 31:48: data structure
  • 31:50: and uh you can take this to any level
  • 31:53: that you want
  • 31:53: so here's a more complicated example
  • 31:57: um so again each it's a list of
  • 32:00: dictionaries where each dictionary
  • 32:02: represents a person uh but now also
  • 32:05: each person has friends associated with
  • 32:08: them and so then friends
  • 32:10: is itself a list of
  • 32:14: dictionaries which again represent more
  • 32:16: people
  • 32:18: and then for each of those people it
  • 32:20: says the activities that they have
  • 32:22: shared
  • 32:22: with the original uh outer person here
  • 32:27: so definitely a more complicated data
  • 32:30: structure to work with here
  • 32:32: but you know the idea that you can
  • 32:33: continue to nest these things as many
  • 32:35: times as you want for whatever
  • 32:37: data structure makes sense for the
  • 32:39: particular problem that you're working
  • 32:41: on
  • 32:43: um so here's you know
  • 32:46: for the point that we're at uh in our
  • 32:48: python learning this is kind of a lot
  • 32:49: going on here
  • 32:51: um i don't expect you to understand uh
  • 32:54: this whole
  • 32:55: block of code right now if you if you
  • 32:57: take the time and really go through it
  • 32:58: um you can definitely
  • 33:00: figure it out um but mainly here i'm
  • 33:02: just trying to highlight
  • 33:04: uh that you can work with these nested
  • 33:06: data structures um
  • 33:08: and so here i'm ultimately uh printing
  • 33:10: you know for each person
  • 33:12: you know who who they hang out with and
  • 33:14: what they do together
  • 33:16: um and so you know sarah here has two
  • 33:18: friends and we see
  • 33:19: two statements for sarah um and then i
  • 33:23: made it also
  • 33:24: uh you know if it's not the first person
  • 33:26: that it's gonna say they also
  • 33:28: hang out with um so
  • 33:32: yeah you can you know have any kind of
  • 33:35: arbitrary logic that works with any kind
  • 33:37: of data structure
  • 33:39: things are very flexible in that sense
  • 33:41: in python
  • 33:43: so that finishes up the material for the
  • 33:47: basic data types
  • 33:48: in python next time we're going to talk
  • 33:51: about creating our own
  • 33:52: custom data types by using python
  • 33:56: classes so thanks for listening and see
  • 33:59: you next time

Creating Python Data Types with Classes


Notes

  • We’ve talked about strings, floats, ints, lists, tuples, and dicts as basic data types. It is possible to define your own custom data types as well using classes

  • I will not be expecting you to construct your own data types in this class. This section is mainly so you can understand how Python works in general. We will start using custom data types defined in external packages so it is good to have an understanding of this and how to use custom objects.

  • The difference between objects of the same data type (same class definition) is the data contained within. They will all have the same functions attached to them (called methods when it is functions attached to objects)

  • Feel free to study the car example in more detail to get a greater understanding of how to define classes, but we will not spend time on this

  • Dataclasses are simplified versions of classes meant to group data together. We will use them to organize our model inputs.

  • The lab exercises test working with externally-defined custom data types and creating and using dataclasses for our model inputs.

Transcript

  • 00:03: hey everyone
  • 00:04: nick dearborn is here teaching you
  • 00:06: financial modeling and today we're going
  • 00:08: to talk about
  • 00:09: creating data types in python with
  • 00:12: classes so this is part of our segment
  • 00:16: on going beyond an initial python script
  • 00:19: the tools and skills that we need to get
  • 00:23: to
  • 00:23: building financial models in python
  • 00:27: so we have already talked about
  • 00:30: data types and the basic data types that
  • 00:33: are built
  • 00:34: into python such as strings uh you know
  • 00:37: floats
  • 00:38: in tuples dicks booleans
  • 00:42: etc but i've also mentioned
  • 00:45: that we can also have custom data types
  • 00:48: in python
  • 00:50: and that is where uh you know the
  • 00:53: flexibility
  • 00:54: of python really comes in
  • 00:57: and that you can do basically anything
  • 01:01: with python and you can build all these
  • 01:03: sorts of extensions to python and
  • 01:05: people have you know put out 250 000
  • 01:08: different packages
  • 01:09: out there which extend python
  • 01:12: functionality
  • 01:13: and most of that is by way of defining
  • 01:16: these classes to create
  • 01:18: new data types that you can use when
  • 01:21: you're working with python
  • 01:24: so um
  • 01:27: the class is the definition
  • 01:31: of the data type itself
  • 01:34: um so that's definitely a little bit
  • 01:36: abstract
  • 01:38: but let's break down what the data type
  • 01:41: actually means and it will become a
  • 01:42: little bit more concrete
  • 01:44: um so a data type and python
  • 01:48: is just uh a way to describe
  • 01:53: the data which should be stored
  • 01:56: within the object and the functions
  • 02:00: which are attached to the object and we
  • 02:03: call those methods
  • 02:04: when they're functions attached to an
  • 02:06: object
  • 02:09: and so we define the class and that
  • 02:12: defines the data type
  • 02:14: and then you can create objects of that
  • 02:18: class and so from a single
  • 02:22: class definition a single data type you
  • 02:24: can have as many objects
  • 02:26: as you want of that type
  • 02:30: and so for each one of those objects
  • 02:33: they could potentially have different
  • 02:35: data that you're passing in
  • 02:37: or that particular object but they're
  • 02:40: going to have
  • 02:40: the same functions attached to them the
  • 02:43: same ones which
  • 02:44: are defined within the class and
  • 02:47: attached to the data type
  • 02:51: and while the data itself may be
  • 02:53: different the structure
  • 02:55: of the data is going to be the same
  • 02:58: because that is also
  • 02:59: defined within the class
  • 03:03: so again you know everything in python
  • 03:06: is an object except for just you know
  • 03:08: the names of things
  • 03:10: those are just references back to those
  • 03:12: objects
  • 03:13: um and so we've already seen these
  • 03:17: strings floats it's list tuples
  • 03:20: they're just types of objects and there
  • 03:23: are other types other built-in types
  • 03:25: that we didn't talk about because
  • 03:27: they're not going to be
  • 03:28: you know very useful for the things that
  • 03:30: we're doing or at least uh
  • 03:32: you know at the level of python that
  • 03:34: we're going to explore in this class
  • 03:36: there's definitely a few that
  • 03:38: would be useful but are just more
  • 03:39: advanced to use
  • 03:42: but you know we're not limited to just
  • 03:45: using those built-in types right there
  • 03:47: are these custom types
  • 03:50: you can you know import a package which
  • 03:52: is going to have its own custom types
  • 03:54: and you can just
  • 03:55: use those and benefit from the
  • 03:56: functionality they built out
  • 03:58: or you can define your own class and
  • 04:01: associated data type
  • 04:03: and use it for your own model or program
  • 04:08: so again the class defines the type
  • 04:12: of the object um
  • 04:15: and then we can create as many objects
  • 04:17: as we want
  • 04:18: of that type
  • 04:22: so um
  • 04:25: the class definition is going to say
  • 04:28: what type of data we need to pass in
  • 04:30: to create an object of that data type
  • 04:35: and then we also on that class define
  • 04:38: the functions or methods
  • 04:41: which should go along with that type
  • 04:44: of object and then every object we
  • 04:47: create
  • 04:47: of that type is gonna have all those
  • 04:50: functions
  • 04:51: so it can be a nice way to uh organize
  • 04:54: your code so that it can
  • 04:55: easily be reused
  • 04:59: um so to make this a little more
  • 05:02: concrete
  • 05:03: you know thinking about the list that's
  • 05:05: the data type we're already familiar
  • 05:06: with we've already worked with
  • 05:09: so in the list we might have one list
  • 05:11: that has one two three
  • 05:13: another list that has abc right we
  • 05:15: constructed them both in the same way
  • 05:18: they're both expecting to receive this
  • 05:20: sequence of different objects
  • 05:22: and so uh you know the class definition
  • 05:25: for lists
  • 05:26: that's in you know python's source code
  • 05:29: um
  • 05:29: you know says that we should pass it
  • 05:31: some kind of sequence of items
  • 05:32: and that's going to become you know just
  • 05:34: an ordered
  • 05:36: uh structure in the list
  • 05:40: uh within every single list that we work
  • 05:42: with we can do it
  • 05:43: a dot append and that will add into the
  • 05:46: list
  • 05:46: right if that's an item in the list so
  • 05:49: no matter if it's the one two three list
  • 05:51: or if it's the abc list
  • 05:53: either one we can append on it just
  • 05:55: anything that's a list
  • 05:56: we can do an append on it and that's
  • 05:59: because that append
  • 06:00: is defined within the class which
  • 06:03: defines the list
  • 06:04: data type
  • 06:08: so then looking at that you know with
  • 06:10: that same kind of visualization that we
  • 06:12: looked at before
  • 06:13: where we have one class it defines the
  • 06:17: structure of the data and it defines the
  • 06:19: functions and then we can create objects
  • 06:21: of that
  • 06:22: so here list is the class
  • 06:25: and so for the list you know we have the
  • 06:28: items
  • 06:29: and the lists you know just this ordered
  • 06:31: sequence of items
  • 06:32: and then it has defined the methods
  • 06:35: append
  • 06:36: insert pop etc all the other uh list
  • 06:39: methods that we work with
  • 06:41: and so whether we create you know a list
  • 06:44: of
  • 06:44: a and b the strings or we create a list
  • 06:48: of one and two they're both gonna have
  • 06:51: append insert all the functions uh
  • 06:54: but the actual items the data is inside
  • 06:57: uh is gonna be different this one has a
  • 06:59: and b this one has one and two uh but
  • 07:02: they are in the same
  • 07:03: structure they are you know a sequence
  • 07:05: and ordered sequence
  • 07:06: of items
  • 07:09: um so this concept extends
  • 07:12: to creating your own custom data types
  • 07:16: so uh you know let's use a car as an
  • 07:19: example because that's you know it's a
  • 07:20: very concrete thing that everybody knows
  • 07:23: about
  • 07:23: um so we can make a class uh for a car
  • 07:27: and make a dab type for a car
  • 07:29: um and so you know for a car
  • 07:32: you think about kind of the main
  • 07:33: characteristics uh
  • 07:35: you know right at the highest level the
  • 07:37: make and the model are like the most
  • 07:39: important characteristics of the car
  • 07:41: right um
  • 07:42: and so you know certainly there there
  • 07:44: could be other characteristics
  • 07:46: uh other data which we have within the
  • 07:48: car
  • 07:49: um data type but let's just keep it
  • 07:51: simple and say you know just make and
  • 07:53: model
  • 07:54: are the two pieces of data which we're
  • 07:56: gonna have
  • 07:57: in the car and then with a car you can
  • 08:00: drive the car
  • 08:01: uh you know you can fill it up uh add
  • 08:03: gas to it
  • 08:04: etc um and
  • 08:08: so uh we can create then you know two
  • 08:11: different cars here
  • 08:12: we can create a honda civic bypassing it
  • 08:14: honda and then civic
  • 08:15: um and so the make is going to be honda
  • 08:18: the model is going to be civic
  • 08:19: and that you can drive the honda civic
  • 08:21: you can fill up the honda civic
  • 08:23: or we could create the toyota corolla
  • 08:26: and for that the data is going to be
  • 08:28: different though it will be in the same
  • 08:29: structure it is going to have a make it
  • 08:31: is going to have a model
  • 08:32: it's just going to be different values
  • 08:33: for each of those
  • 08:35: and we are going to be able to drive it
  • 08:37: we are going to be able to fill it up
  • 08:38: etc so again the functions
  • 08:42: come universally to all the objects and
  • 08:44: the data structure
  • 08:45: is going to come but it's going to be
  • 08:47: different actual values
  • 08:49: in the data depending on how we created
  • 08:51: the object
  • 08:55: so um let's just say we have this
  • 08:59: car class defined elsewhere
  • 09:03: so we're going to import that car
  • 09:06: class and so this can be as simple as
  • 09:11: just you know a python file
  • 09:13: in the same directory as your notebook
  • 09:16: then you would be able to import like
  • 09:18: this or it could be from some
  • 09:20: external package which you've installed
  • 09:22: then you can also
  • 09:23: import like that if this was the name of
  • 09:25: the package
  • 09:26: um but you know the main idea is we're
  • 09:30: just we're getting the car
  • 09:32: uh class even though we didn't define it
  • 09:34: you know
  • 09:35: in this particular uh session directly
  • 09:39: so then we can create the car and
  • 09:44: calling a class to create an object
  • 09:47: of that data type looks just like
  • 09:49: calling a function
  • 09:51: you put the name of the class and then
  • 09:53: open parentheses
  • 09:54: and then you pass whatever inputs and
  • 09:57: then close parentheses and that's going
  • 09:58: to make
  • 09:59: the object so if we print that out
  • 10:03: then we can see that it prints as a car
  • 10:07: and then if we check the type we can see
  • 10:09: that the type is
  • 10:10: a car type inside the
  • 10:13: car example module
  • 10:16: and then if we check the make of this
  • 10:19: honda civic we get honda
  • 10:21: and if we call the drive method uh
  • 10:24: then we're gonna see that it's driving
  • 10:26: away um
  • 10:28: so again you know calling any function
  • 10:30: we gotta do open close parenthesis to
  • 10:32: actually call
  • 10:34: that function in this case a method
  • 10:39: so you know it's generally a little bit
  • 10:41: more advanced
  • 10:42: to construct your own classes your own
  • 10:46: custom data types
  • 10:47: that's not where we're going to be
  • 10:48: focusing in this course
  • 10:51: i would definitely encourage you to
  • 10:52: learn about that it's very useful
  • 10:55: you know i certainly apply it all the
  • 10:57: time
  • 10:59: but it's just you know one of those
  • 11:01: things where if this was a two-semester
  • 11:03: course i would be happy to spend time on
  • 11:05: this
  • 11:05: but we have very limited time we got to
  • 11:07: make sure to
  • 11:08: get all the financial modeling material
  • 11:10: in and so we can't spend a bunch of time
  • 11:12: on
  • 11:13: you know more generally useful
  • 11:15: programming concepts
  • 11:17: so definitely i would encourage you to
  • 11:19: learn about that on your own
  • 11:22: you will definitely find it useful going
  • 11:24: forward
  • 11:25: but for the purposes of this course you
  • 11:27: don't need to know how to do that
  • 11:29: um now why do we just cover this
  • 11:33: if we're not going to go and create our
  • 11:35: own custom classes
  • 11:37: so couple reasons one is just to give a
  • 11:40: better understanding of the general
  • 11:42: workings of python you know again
  • 11:44: everything
  • 11:45: in python is an object every object has
  • 11:48: a type
  • 11:48: we can define those types using classes
  • 11:53: and then you know when you're just
  • 11:55: calling functions or methods
  • 11:57: um you know sometimes we've had like
  • 12:00: you know when it's a list we do dot
  • 12:02: append and so it's you know
  • 12:03: attached to the object we then do a dot
  • 12:07: and then we call a function versus if we
  • 12:09: just define a function
  • 12:11: then you're just calling that open close
  • 12:14: parenthesis there's not a dot or any
  • 12:16: variable before that
  • 12:18: so hopefully this makes a little more
  • 12:19: sense now
  • 12:21: that when we see this kind of format is
  • 12:23: because
  • 12:24: uh whatever the data type of the object
  • 12:27: the class has defined this function as a
  • 12:30: method
  • 12:31: of the data type and so you can do dot
  • 12:35: and then the function on anything
  • 12:38: of that data type versus a plane
  • 12:41: function
  • 12:42: is not attached to any data type and you
  • 12:45: just call it
  • 12:46: uh by its name without any dot or
  • 12:48: variable name before that
  • 12:52: and then the um
  • 12:55: you know one other reason which i didn't
  • 12:57: put on here is then that
  • 12:58: uh you know all these uh libraries which
  • 13:01: are going to bring in
  • 13:02: and work with they have defined their
  • 13:04: own
  • 13:05: custom classes and so just getting a
  • 13:07: little bit more visibility
  • 13:09: into you know when you just import car
  • 13:12: and you use that like what is going on
  • 13:13: where did that come from what is this
  • 13:15: car thing
  • 13:16: it's a class and that defines the data
  • 13:19: type that we're going to use
  • 13:20: so you know giving more visibility into
  • 13:23: how that works
  • 13:24: um but then also there is a
  • 13:27: simplified version of classes called
  • 13:31: data classes
  • 13:32: and we are going to directly create
  • 13:34: those
  • 13:35: so those we're going to use to organize
  • 13:38: the inputs
  • 13:39: for our model um
  • 13:43: but we're not going to go to building
  • 13:45: you know full-on just general classes in
  • 13:48: this course you can certainly feel free
  • 13:50: to use them
  • 13:51: in your projects if you you know figure
  • 13:53: out everything on your own
  • 13:55: i'm happy to answer questions on them
  • 13:57: but it's just not something we're going
  • 13:58: to
  • 13:59: focus on in the course um
  • 14:03: but data classes we will so data classes
  • 14:06: uh is they're a lot simpler to define
  • 14:09: than a class um and typically they're
  • 14:12: just used to kind of group data together
  • 14:14: which is exactly
  • 14:15: what we're trying to do here
  • 14:19: so for data classes here's kind of the
  • 14:22: setup
  • 14:23: to define a data class
  • 14:27: so here um we do need to import it
  • 14:31: it is built into python but it's uh
  • 14:34: you know there are about 30 or 40
  • 14:36: modules
  • 14:37: or packages which are built into python
  • 14:39: but you still need to import them
  • 14:41: they're not just you know they're
  • 14:43: waiting for you to use it you still need
  • 14:45: to import it even though it comes with
  • 14:46: every python
  • 14:48: this is one of those instances data
  • 14:49: classes is built in
  • 14:51: but we still got to import it so we do
  • 14:54: this import so we get the data class
  • 14:56: and then um this is what's called a
  • 14:58: decorator
  • 14:59: you don't really need to worry about
  • 15:00: that that's definitely a more advanced
  • 15:02: uh concept actually you know like
  • 15:05: creating decorators and understanding
  • 15:07: how they work in detail uh we're not
  • 15:09: gonna go there
  • 15:10: in this class that's definitely
  • 15:12: something you can learn about on your
  • 15:13: own
  • 15:14: just know that you have to put this at
  • 15:16: data class at sign data class
  • 15:18: above the class statement and then it's
  • 15:21: class
  • 15:22: the keyword class and then whatever you
  • 15:24: want the name
  • 15:25: of the class to be and then a colon and
  • 15:28: then just like we've seen with
  • 15:30: you know if statements we've seen it
  • 15:31: with for loops we've seen it with
  • 15:33: defining functions
  • 15:34: we do this colon and then after that we
  • 15:37: have indentation and then everything
  • 15:39: indented
  • 15:40: is going to be part of this class
  • 15:42: definition
  • 15:44: so for that the data class
  • 15:47: uh what we do is we define all the
  • 15:50: pieces of data
  • 15:51: which are going to be stored within this
  • 15:53: class so we're defining that data
  • 15:55: structure
  • 15:56: of the class and so here
  • 15:59: i'm going to say that these model inputs
  • 16:03: should have interest rates as one of the
  • 16:06: pieces of data in here
  • 16:08: and that should be a tuple and that's
  • 16:10: going to default to five six and seven
  • 16:12: percent
  • 16:14: and then uh we should have a payment
  • 16:17: as well that's going to be a floating
  • 16:20: point number
  • 16:21: um because it could have decimals
  • 16:23: potentially
  • 16:24: and we're going to set that equal to a
  • 16:27: thousand by default
  • 16:29: um so then we can create
  • 16:33: an instance of the inputs so again it's
  • 16:36: just like the functions you know first
  • 16:38: you have to define the function and then
  • 16:40: you have to use the function
  • 16:42: same concept with classes first you have
  • 16:45: to define the class
  • 16:47: then you can use the class so
  • 16:50: here we're just setting up the
  • 16:52: definition uh
  • 16:53: you know this we're setting up this part
  • 16:56: we haven't actually created the objects
  • 16:58: out of it yet
  • 17:00: so to create the objects then it looks
  • 17:03: again just like calling a function
  • 17:05: you put the name of the class and then
  • 17:07: open parentheses
  • 17:09: and then you pass whatever arguments and
  • 17:11: then you close
  • 17:12: the parentheses so here
  • 17:16: we're passing that the payment uh should
  • 17:18: be equal to
  • 17:20: 2000 um and we're not passing the
  • 17:23: interest rates
  • 17:23: so those are gonna take the default
  • 17:25: value of five six
  • 17:27: and seven percent so then when we print
  • 17:29: out
  • 17:30: the inputs then uh we're going to see
  • 17:33: that it's an instance of
  • 17:35: model inputs um we have the interest
  • 17:37: rates five six and seven percent
  • 17:39: and we have a two thousand payment here
  • 17:42: and the type of that
  • 17:43: is model inputs and we can grab those
  • 17:47: individual things
  • 17:48: out of the inputs we can do inputs dot
  • 17:50: interest rates
  • 17:51: that gives us five six seven percent and
  • 17:53: we can do inputs.payment and that will
  • 17:55: give us 2 000.
  • 17:57: so that's the basic idea of how to
  • 18:00: create and work with
  • 18:01: data classes so why
  • 18:04: why would we want to use data classes
  • 18:06: when are we going to use data classes
  • 18:10: so again it's just a convenient way to
  • 18:12: group data together you can definitely
  • 18:14: use them for a lot more than that
  • 18:16: but that's the most common usage and
  • 18:18: what we're going to be focusing on in
  • 18:20: this course we just have
  • 18:22: you know all these different inputs we
  • 18:23: just want to package up those inputs
  • 18:25: into a single place
  • 18:29: and you know the reason that we end up
  • 18:31: going to this is because financial
  • 18:33: models
  • 18:33: uh typically have a lot of different
  • 18:35: inputs you know we might have easily
  • 18:38: you know 15 20 30 different inputs that
  • 18:41: we need to pass
  • 18:42: in and uh you know what you'll find when
  • 18:46: we go with this structure of having you
  • 18:48: know functions within functions
  • 18:50: and passing the data you know back and
  • 18:52: forth between all the functions
  • 18:54: if you keep everything as just
  • 18:55: individual variables
  • 18:57: all of a sudden you know every function
  • 18:58: is passing like 10 inputs to each other
  • 19:01: function
  • 19:02: and it just becomes very tedious to be
  • 19:04: passing
  • 19:05: all these individual variables around
  • 19:07: everywhere so by grouping it all into
  • 19:10: a single data class we can just pass
  • 19:12: this data class
  • 19:14: around we can pass the instance uh of
  • 19:16: the data class around
  • 19:18: as one single variable and then we have
  • 19:20: access to all the inputs in every
  • 19:22: function
  • 19:22: very easily just by passing this data
  • 19:26: so it's mainly a convenience thing it's
  • 19:28: also going to make your code shorter
  • 19:30: and that it groups all your inputs
  • 19:32: together
  • 19:36: um
  • 19:38: the other nice advantage of this was um
  • 19:42: again all this data is together and you
  • 19:45: can just
  • 19:45: you know whatever the name of your
  • 19:47: instance is for the data class
  • 19:49: you type that and then you do dot and
  • 19:50: then you hit tab and you're gonna see
  • 19:53: all your inputs right you can auto
  • 19:54: complete all your different inputs
  • 19:56: you have just your inputs there you
  • 19:58: don't have all your other intermediate
  • 19:59: variables and things like that
  • 20:01: um so it's it's nice and convenient to
  • 20:05: work with it as well even within
  • 20:08: the functions
  • 20:13: so let's go ahead and look at the
  • 20:14: jupiter example for this
  • 20:19: so um again every data type
  • 20:23: has an associated class um so even
  • 20:26: the built-in data types they also have
  • 20:29: classes um and
  • 20:34: so um those built-in data types often
  • 20:38: have
  • 20:38: also more convenient ways to create them
  • 20:42: but you can create them just like a
  • 20:44: normal class as well
  • 20:46: um so you know for strings we've looked
  • 20:48: at creating them like this right like
  • 20:49: singular double quotes
  • 20:51: automatically is going to go to a string
  • 20:53: that's a little just
  • 20:54: you know shortcut python has thrown in
  • 20:56: there for us to make our lives easier
  • 20:57: because we use strings all the time
  • 21:00: um but uh you know scr
  • 21:04: is actually the class uh for
  • 21:07: string the name of the class for string
  • 21:09: i mean you can see that when you check
  • 21:10: the type it's str
  • 21:12: um and so you can also create strings
  • 21:16: by you know just like we looked at with
  • 21:19: car and with model inputs you put the
  • 21:21: name of the class
  • 21:22: and then parentheses and the data you
  • 21:24: want to pass and then close parentheses
  • 21:27: that's going to work exactly the same
  • 21:28: way so this would be
  • 21:30: you know kind of just the standard way
  • 21:32: of using any class
  • 21:33: here we're using it for the string but
  • 21:36: of course you know the built-in data
  • 21:37: types have the more convenient ways to
  • 21:39: construct them as well
  • 21:45: uh but then moving to custom classes um
  • 21:50: so you know here we're gonna look at
  • 21:52: working with this custom car
  • 21:54: class and if you're curious uh you know
  • 21:56: what this code looks like to create this
  • 21:58: class
  • 21:59: uh we have it here in this car
  • 22:01: example.py
  • 22:02: um so it's all this stuff here
  • 22:06: and you know this stuff is probably not
  • 22:08: gonna make a lot of sense
  • 22:09: uh just looking at it um there's you
  • 22:12: know a fair number of things in here
  • 22:14: which
  • 22:14: uh takes some deeper knowledge of python
  • 22:16: to understand what's going on
  • 22:18: um so don't really worry about it uh you
  • 22:21: know if you have extra time
  • 22:23: i would recommend reading out more on
  • 22:24: classes and learning about how this
  • 22:26: works
  • 22:27: but it's not going to be anything that
  • 22:29: is covered in the course
  • 22:32: so we don't really need to worry about
  • 22:33: how it's actually defined
  • 22:35: we just want to work with it right
  • 22:38: so um here i'm doing from car example
  • 22:42: import car and this worked because
  • 22:45: i have car example defined in the same
  • 22:48: folder
  • 22:49: as my notebook so if you want to run
  • 22:52: this on your own make sure you download
  • 22:54: this car example and that it's in the
  • 22:56: same
  • 22:57: folder as your notebook in order for
  • 22:59: this to run otherwise you're going to
  • 23:00: get an import error
  • 23:01: it won't be able to find car example
  • 23:04: and you won't be able to run this part
  • 23:07: of the code
  • 23:09: so um here we're going to make a
  • 23:13: subaru forester that's going to be my
  • 23:14: car
  • 23:16: and we can see it's coming through as a
  • 23:18: car with the make
  • 23:19: as subaru and the model as forester
  • 23:23: and so now you know we have these
  • 23:26: methods which are attached
  • 23:27: on the data type the class of the car
  • 23:31: and if we hit dot and then tab we can
  • 23:34: see what those are
  • 23:36: and jupiter even helps us out a little
  • 23:38: bit here
  • 23:39: um with telling us you know what is this
  • 23:43: thing these two are functions right
  • 23:45: drive and fill up whereas the gas
  • 23:47: percentage the make and the model those
  • 23:50: are data and stored within the class
  • 23:53: um so checking you know make i'm going
  • 23:55: to get subaru
  • 23:57: or you know running drive i'm going to
  • 23:59: see that it's driving away
  • 24:01: and that the gas is now down to 40
  • 24:05: percent
  • 24:06: so if i you know keep driving the car
  • 24:08: here i'm going to drive it five times
  • 24:10: um we can see that it drives and it
  • 24:13: takes 10 of the gas to drive and then it
  • 24:15: drives and takes 10
  • 24:17: more and so on until we hit zero percent
  • 24:21: um and now we are out of gas
  • 24:24: um so now when we
  • 24:27: um call drive we are just going to be
  • 24:30: out of gas no matter
  • 24:32: how many times that we drive um
  • 24:35: so that's the basic you know idea here
  • 24:39: and then we can
  • 24:40: uh you know fill up the car now the gas
  • 24:42: is at 100
  • 24:43: and we can start you know driving again
  • 24:46: until we get down to
  • 24:47: zero percent um then it's out of gas
  • 24:51: and then we can fill it up etc so you
  • 24:53: can give whatever kind of functionality
  • 24:55: you want to your objects
  • 24:56: and uh you know we're going to show or
  • 24:59: we're going to use
  • 25:01: these custom data types defined in other
  • 25:03: libraries
  • 25:04: uh that help us do a lot of interesting
  • 25:06: things uh
  • 25:07: without really understanding you know
  • 25:09: how that was defined
  • 25:11: in the background we just need to know
  • 25:13: you know how can we
  • 25:14: figure out the functions on this um how
  • 25:17: can we
  • 25:18: uh figure out the data associated with
  • 25:20: this and you know just doing the dot tab
  • 25:23: is going to get you a lot of the way
  • 25:25: there and then you know the other main
  • 25:27: part of it
  • 25:28: is you can also just google about
  • 25:30: whatever
  • 25:31: um you know custom data type that you're
  • 25:34: working with you know here
  • 25:35: i defined this car so you're not gonna
  • 25:37: find that on google
  • 25:39: uh but uh you know for any of these
  • 25:41: libraries
  • 25:42: packages that we're going to work with
  • 25:44: you'll be able to google
  • 25:45: all about it
  • 25:49: so then that brings us to data classes
  • 25:52: where data classes are
  • 25:54: the simplified class so you know we had
  • 25:56: a lot of stuff
  • 25:57: going on here in this class um
  • 26:00: but for the data class i mean bam we got
  • 26:04: four lines
  • 26:05: and it's done right it's a lot simpler
  • 26:07: to define that
  • 26:08: um so we got to have this data class
  • 26:11: import
  • 26:12: and then we've got to do this at sign
  • 26:14: data class
  • 26:15: this decorator um you know again you
  • 26:18: don't have to worry about what a
  • 26:19: decorator is
  • 26:21: that's a more advanced python thing but
  • 26:23: you just know that you put it here
  • 26:24: for a data class and then the very next
  • 26:27: line you want to have as the class
  • 26:29: statement
  • 26:30: and then you have the keyword class and
  • 26:32: then the name
  • 26:33: of the class you want to create and then
  • 26:35: a colon and then the indent and then
  • 26:37: this is where you start defining
  • 26:39: your variables uh that are going to be
  • 26:41: attached to
  • 26:42: the class um so again this is just
  • 26:45: like the um
  • 26:49: example in the slides we have a payment
  • 26:51: defaults to a thousand and we have
  • 26:52: interest rates defaults to five
  • 26:54: six and seven percent
  • 26:59: um so then you know again
  • 27:02: this is just the definition part right
  • 27:04: we haven't actually uh
  • 27:06: created any concrete object we haven't
  • 27:10: you know we don't have this data really
  • 27:11: anywhere yet
  • 27:13: uh we need to create an instance of the
  • 27:16: class uh to get that so here we're doing
  • 27:19: data
  • 27:20: data equals model inputs open close
  • 27:22: parentheses
  • 27:24: so again you know just like functions to
  • 27:26: could to create an instance of the class
  • 27:28: you put the class name
  • 27:29: and then open close parenthesis and you
  • 27:31: can pass any arguments
  • 27:32: in here um so you know i could pass 2
  • 27:35: 000 and that would come
  • 27:37: for the payment um you know 2 000
  • 27:40: then i could do you know 0.1.2.3 if i
  • 27:44: wanted to change the interest rates as
  • 27:46: well
  • 27:47: also uh if you want to
  • 27:50: change you know them not in order then
  • 27:53: you can specify them by name
  • 27:56: now it's taking the default payment of
  • 27:57: thousand and changing the interest rates
  • 28:00: um
  • 28:02: but you can also just do it with nothing
  • 28:04: to get the defaults for everything
  • 28:07: and then once you have that um well we
  • 28:10: can just do data dot
  • 28:11: and look at it well we've got interest
  • 28:13: rates and payments
  • 28:14: are the two pieces of information we
  • 28:17: have associated with that data class
  • 28:20: so we can you know look up either of
  • 28:22: those individually
  • 28:23: and get that value back
  • 28:27: so here's showing also that you can
  • 28:29: override those defaults
  • 28:32: and get that information as well
  • 28:36: so then to show the motivation why we
  • 28:39: want to use these things
  • 28:42: rather than just passing around the
  • 28:43: payment interest rates themselves
  • 28:45: uh so here uh we have a function
  • 28:50: which is going to determine
  • 28:53: uh you know if you have an interest rate
  • 28:56: and you have a payment
  • 28:57: what would be the principal that uh
  • 29:00: you know got to that percentage interest
  • 29:03: rate equating that payment
  • 29:05: um and then uh you know does that for
  • 29:08: each one of the interest rates
  • 29:10: so we can call that um and
  • 29:14: we uh call that and we get
  • 29:18: the results we would expect you know for
  • 29:20: three percent uh
  • 29:22: to get to a thousand payment that would
  • 29:23: have had to have been thirty three
  • 29:25: 000 principal and if it's a four percent
  • 29:28: interest rate to get to a thousand
  • 29:30: dollars it would have had to have been
  • 29:31: 25 000
  • 29:34: um so but you did notice
  • 29:38: um that we had to pass the payment and
  • 29:41: rates
  • 29:41: separately here um where as
  • 29:45: if we create a data class um
  • 29:48: here we're creating the data an instance
  • 29:51: of model inputs
  • 29:52: and then here we pass only the data and
  • 29:55: in here then we do data.interest rates
  • 29:57: data.payment
  • 29:58: um to get the same result um so the key
  • 30:02: difference here being
  • 30:04: two things one is that we passed only a
  • 30:07: single
  • 30:08: argument here and the other is you know
  • 30:10: while you're writing this
  • 30:11: you can go you know what were my
  • 30:13: variables data.oh it's interest rates
  • 30:16: and then use that what was the other one
  • 30:18: oh it's data.payment
  • 30:20: um so
  • 30:23: um you know not a big difference when we
  • 30:25: only have two inputs
  • 30:27: but you can imagine if you had 10
  • 30:29: different inputs into
  • 30:30: you know the function that runs your
  • 30:32: whole model um
  • 30:34: then all of a sudden we're passing a
  • 30:35: whole bunch of things here and it gets
  • 30:37: pretty tedious just typing out all those
  • 30:39: separate variables each time
  • 30:41: it might be that then your main model
  • 30:43: function which takes these ten inputs
  • 30:45: then passes
  • 30:45: five of them to the first sub model and
  • 30:47: then you know seven of them to the
  • 30:49: second
  • 30:49: sub model and you're just passing a lot
  • 30:51: of things uh
  • 30:53: in a lot of places and it becomes
  • 30:54: tedious whereas here you just pass the
  • 30:56: data
  • 30:57: everywhere a single variable going
  • 30:59: everywhere
  • 31:00: um and then you can just use whatever
  • 31:02: you need out of that data so quite a bit
  • 31:06: more convenient to use
  • 31:09: so that's the basic idea with data
  • 31:12: classes and also
  • 31:14: you know we looked at using other custom
  • 31:17: classes as well
  • 31:19: um and just the whole idea of what a
  • 31:20: class is what uh
  • 31:22: how it's relates to a data type defines
  • 31:24: the data type
  • 31:26: so um next time we're going to come back
  • 31:29: and cover handling errors in python
  • 31:33: and that will be uh wrapping up our
  • 31:36: segment
  • 31:36: on the basics in python that will
  • 31:39: prepare us to actually go and build out
  • 31:41: a full-on financial model
  • 31:42: in python so thanks for listening and
  • 31:45: see you next time

Handling Errors in Python


Notes

  • Error handling is generally a more advanced pattern and so we won’t put a lot of focus on it in the class. But sometimes it is the best solution to a problem.

  • Combining lists of different lengths is one possible application of error handling, but there are many others

Transcript

  • 00:03: hey everyone
  • 00:04: nick dear burtis here teaching you
  • 00:05: financial modeling today we're going to
  • 00:07: be talking about
  • 00:08: error handling in python which will
  • 00:11: conclude our segment on
  • 00:13: going beyond an initial python script
  • 00:16: the tools and skills that we need in
  • 00:18: python to go
  • 00:19: and build out financial models so
  • 00:23: this is going to be a quick video we're
  • 00:25: just going to be
  • 00:27: quickly reviewing error handling uh
  • 00:30: most people probably are not even going
  • 00:32: to need this it's just something to be
  • 00:34: familiar with in python because you may
  • 00:37: need it
  • 00:38: at certain points
  • 00:40: [Music]
  • 00:41: you know definitely it comes up a lot
  • 00:43: more in general programming than in
  • 00:44: building financial models but there are
  • 00:46: still cases where you
  • 00:48: may find it useful uh for modeling
  • 00:51: and then you know as you go outside the
  • 00:53: class and expand
  • 00:54: into other areas in python you'll find
  • 00:56: it useful as well
  • 00:59: so you know at this point if you've been
  • 01:01: following along and you've been working
  • 01:03: on
  • 01:04: all the labs and you've been uh you know
  • 01:07: building out the the models and examples
  • 01:10: along with me
  • 01:11: then i'm sure you've seen errors coming
  • 01:13: up i've also shown examples of errors
  • 01:15: you know syntax error
  • 01:17: annotation error name error uh
  • 01:21: you know index error there's so many
  • 01:24: different kinds of errors
  • 01:27: and when you hit an error in python code
  • 01:31: it stops the execution of the code
  • 01:34: wherever you're at as soon as it throws
  • 01:36: the error it's just done it's just
  • 01:38: it's not going to execute anything after
  • 01:40: that
  • 01:43: and sometimes you're going to be going
  • 01:46: through your code and you're going to
  • 01:47: expect
  • 01:48: to get an error uh that this error
  • 01:52: is actually something that
  • 01:56: is inherent to the way that the model
  • 01:57: works uh
  • 01:59: it could be you know something like you
  • 02:01: could set it up that
  • 02:03: you know you're supposed to put you know
  • 02:05: a number of years as an input into the
  • 02:06: model
  • 02:07: and you want to add some validation such
  • 02:09: that if a user
  • 02:11: uh you know gets less than or it puts in
  • 02:14: less than
  • 02:15: uh zero for the number of years then
  • 02:19: you know it could be you raising an
  • 02:20: error or catching the the error that
  • 02:22: comes out of your program and
  • 02:24: and then you know printing that out to
  • 02:25: the user hey make sure you pass a number
  • 02:27: greater than uh zero for
  • 02:31: the number of years or you know it could
  • 02:34: be
  • 02:34: something that comes up just from the
  • 02:36: logic of your model where you do
  • 02:38: actually expect to see an error
  • 02:39: in some or all of the cases um and you
  • 02:42: want to do something in response to that
  • 02:44: error instead of just having the code
  • 02:47: stop
  • 02:48: at that point uh
  • 02:51: so here's the basic structure for error
  • 02:54: handling
  • 02:55: um so um
  • 02:58: the structure is what's called a try and
  • 03:01: accept
  • 03:02: clause um and
  • 03:05: basically you try to do something and
  • 03:07: then if there's an error
  • 03:09: then you're gonna go into the accept
  • 03:12: um so here you know we've got
  • 03:15: a list defined that has two items a and
  • 03:17: b
  • 03:18: and then we're trying to uh
  • 03:21: look up the 11th item from the list of
  • 03:25: course the list only has
  • 03:26: two items and so there is no 11th item
  • 03:29: and so as we saw you know previously
  • 03:31: when covering lists
  • 03:33: uh that we're gonna get an index error
  • 03:35: in that case
  • 03:36: right um and so
  • 03:40: uh you know you run this code and
  • 03:43: it's just going to print out caught the
  • 03:45: error
  • 03:46: it's not going to fail it's not going to
  • 03:49: show you any index error it's just going
  • 03:52: to print
  • 03:52: caught the error and that's because we
  • 03:55: tried
  • 03:56: to look up the 11th item in the list and
  • 03:59: then
  • 03:59: this triggered the index error
  • 04:03: but then we have this accept clause and
  • 04:05: so this accept clause
  • 04:07: says you know whatever exception you put
  • 04:09: here
  • 04:10: it's going to look for that exception
  • 04:12: coming up within the try clause
  • 04:15: and if it does then we're all of a
  • 04:17: sudden going to be
  • 04:18: in this part of the logic
  • 04:20: [Music]
  • 04:22: so it's a totally different way to
  • 04:23: handle uh the flow
  • 04:25: in your python program versus you know
  • 04:27: like using conditionals or something
  • 04:29: like that to control flow
  • 04:31: um it's you know if you get an error
  • 04:34: we're going somewhere else in the code
  • 04:36: um so
  • 04:40: there's a lot of different possible
  • 04:42: applications of this and we'll look at
  • 04:43: an example here
  • 04:44: in the moment um and then you'll notice
  • 04:48: it's just like
  • 04:49: uh you know every other kind of logical
  • 04:51: block that we've defined in
  • 04:53: python you know be it for loops be it
  • 04:56: if statements uh function definitions
  • 04:59: class definitions uh now also try and
  • 05:03: accept
  • 05:03: again it's you put uh the
  • 05:07: you know the control statement and then
  • 05:10: a colon and then on the next line you
  • 05:13: end in
  • 05:14: and then whatever is indented is going
  • 05:16: to be running
  • 05:17: as part of that block that logical block
  • 05:21: so the try block as this code in it the
  • 05:23: accept block
  • 05:25: has this code in it because they are
  • 05:27: indented
  • 05:28: and the try and the accept have to be on
  • 05:31: the same indentation level
  • 05:33: just like when we looked at if and else
  • 05:35: right like if and else are like part of
  • 05:37: the same
  • 05:38: um construct here and so then the same
  • 05:42: thing with try and accept
  • 05:43: um they go together and so they should
  • 05:46: be on the same indentation
  • 05:47: level so then um
  • 05:52: you know just thinking about an example
  • 05:53: here uh so let's say
  • 05:55: you know you've got um you know two
  • 05:58: different annuities
  • 05:59: uh they both pay a hundred dollars a
  • 06:01: year for five years
  • 06:02: uh but one you're getting in the year
  • 06:04: zero and one you're getting in year
  • 06:06: three
  • 06:07: so we can define this annuity as just a
  • 06:10: list of a hundred five times
  • 06:12: and here's a nice little shortcut that
  • 06:14: will let you define
  • 06:15: something repetitive like that this will
  • 06:17: expand out to being
  • 06:19: a list with a hundred in it five times
  • 06:22: um and then we want to figure out
  • 06:26: what are the cash flows overall you know
  • 06:28: going up to 15 years
  • 06:31: or you know however many years
  • 06:36: so here's some code which is able to
  • 06:40: get to that result using error handling
  • 06:44: so here looking at 10 years
  • 06:47: and we've got these two different
  • 06:48: annuities um
  • 06:50: this second annuity is going to start in
  • 06:52: year four uh the first
  • 06:54: annuity or year three or year zero
  • 06:57: whatever you want to call the first year
  • 06:58: zero or one it's gonna start three years
  • 07:00: later for the second annuity
  • 07:03: um and we want to ultimately get the
  • 07:06: cash flows at the end right so
  • 07:08: three periods of getting just 100 and
  • 07:11: then two periods where they're both
  • 07:12: paying we get 200
  • 07:13: and then three more periods are just a
  • 07:15: hundred and then zero for the remaining
  • 07:17: periods is the result that we would
  • 07:18: expect
  • 07:19: of putting these two things together um
  • 07:22: so one way you might think to put them
  • 07:24: together is
  • 07:24: okay let me loop through the number of
  • 07:26: years and then
  • 07:28: let me go through the annuities and add
  • 07:31: up
  • 07:31: you know looking up that year's value
  • 07:34: from
  • 07:34: the annuity
  • 07:38: but the
  • 07:41: issue there is then that
  • 07:46: the annuities uh
  • 07:49: don't have equal links here this one is
  • 07:52: only five
  • 07:53: long this is eight long and so
  • 07:57: as you get uh to year six
  • 08:00: all of a sudden is trying to look up the
  • 08:02: sixth year in this annuity which has
  • 08:03: only five items
  • 08:05: and that's going to give you an index
  • 08:07: error um
  • 08:08: so then if we have this try accept
  • 08:10: handling in here
  • 08:12: uh if we get the index error well we're
  • 08:14: just not going to do anything
  • 08:16: that's what pass means is just uh you
  • 08:18: know go on ahead
  • 08:20: uh i don't want to do anything here
  • 08:23: because in that case you know we don't
  • 08:25: need to add anything the annuity is done
  • 08:27: so we just want it to keep going without
  • 08:30: throwing an error
  • 08:32: and so then when we do that then we
  • 08:34: ultimately get the output
  • 08:36: that we would expect of these two
  • 08:38: annuities
  • 08:39: combined so that's a quick overview
  • 08:44: on error handling in python
  • 08:47: definitely uh you know it's not going to
  • 08:50: be super common in financial modeling to
  • 08:52: hit this until you get to you know
  • 08:54: definitely more complex
  • 08:56: uh models but a good pattern to know
  • 08:59: regardless and if you're unsure about
  • 09:02: whether you should be applying it or you
  • 09:04: have questions about applying it just
  • 09:06: feel free to to ask me about that
  • 09:09: and i can give more contacts there
  • 09:12: so that wraps up our segment on
  • 09:15: going beyond an initial python script
  • 09:18: where now we have the basic python
  • 09:22: knowledge that we need
  • 09:23: such that we can go and build out the
  • 09:25: dynamic salary retirement model
  • 09:27: fully in python for our next major
  • 09:30: segment so thanks for listening and see
  • 09:33: you next time