Python Functions | Real Estate Analytics
In this video, we will cover Python functions. This concept will be used extensively throughout the course, you will create three different functions that build on our previous scenarios that we worked through in the past lessons.
Ariel Herrera 0:00
In this video, we will cover Python functions. This concept will be used extensively throughout the course, you will create three different functions that build on our previous scenarios that we worked through in the past lessons. Let's jump into Lesson four notebook from our table of contents. What are functions, a function is a block of code, which only runs when it is called, you can pass data known as parameters into a function, a function can return data. As a result, we will be using functions a lot as this will help us to write code that's easy to read and repeatable. The first function we're going to create is to increase the rent percentage. If you remember from our prior lesson, we worked with a list called rent income list, where we have four units representing rental income from each one, we then tested different scenarios, if we were to increase the rent income by a certain percentage. Now we're going to build off of that by creating a function. So let's start by looping through each of the items in our list, I'm going to create a for statement, set R as the variable name, and then state in the list that we're going to go through which is rent income list. Here we print out each one of our rental values. Now what if we want to increase our percentage by 5%? How do we do this from our last lesson, we took our as the rental value, and we multiplied it by one plus the increase. So in this case, we see 1575 would be a 5% increase. And the reason why there's squiggly lines on the bottom is that Python recognizes, okay, we know what the our variable is, but it's out of order. So if we were to actually restart our runtime, and run all, we would receive an error that R is not defined, because we're doing it in the next cell. These are just things to be aware of when you're testing out certain code. And what that means when Python gives those warnings. Now that we have the formula in order to calculate rent increase, we should make a repeatable function so that we can do this each and every time without having to write out this code. So we're going to call this function, which is going to start with Def to define it, we're going to call it rent increase. And it's going to take two parameters, which is the rent value. And then the increased percentage is then followed by a colon, just like before for loops. And with our FL statements, indentation is important here. So at this line, you want to make sure that you tab so that our logic starts, let's have a way from when we define our function. Now if we see here, our variables are different. So let's make sure we copy the new names for our variables and replace it here. Now if we were to run this, we have now established our function, we can copy the function with its parameters, we have two parameters here, and we can enter into values. So let's put in 1000. And let's keep Inc, crease percentage 2.05. Now we run this cell, and nothing happens, how come it's because we didn't return anything. functions are defined based on the logic you have inside parameters up top and also what you return at the end. So let's put a return statement here. So that Python knows to return this value for us. And now when we run the cell, we could see that it is calculated for us, the increase in rent for 1000 by 5% would be 1050. Now let's apply this logic to the rest of these rental values in our list, we can copy our code that we have previously where we called our function, and we can put it into our print statement. Let's delete the cell since we won't need it anymore. And now run our cell where we're calling our function each and every time for each item in our list.
Here we could see that we have the same value each time. That's because we did not change the rental value here. So let's replace 1000 with our which is going to represent each item in our list and increase percentage. We can replace this with the value that we have set up top. Now rerun and we could see the new values congrats we did our first function together. Pretty simple functions can get more complicated but I highly suggest to keep them as short as possible and just add more functions rather than Having one super long one. Now let's move on to credit check. I'm going to rerun this code, so I can get rid of those errors that were showing up before. And the next step that we want to do is create a function that's going to check credit scores. If you remember in our if else statement lesson, we were able to create a condition that stated If an applicant's credit score was greater than the minimum credit score, then we would approve them otherwise denied. Here I am copying our logic that we set before from our previous notebook. And now I've put this underneath a function a function called credit check, it takes in three different parameters, we have the credit score of the applicant, their credit score minimum, which would allow someone to change their minimum not just to be static to 600, then we have bandwidth, which I'm setting the value upfront, it can be overridden if the user wants to do so. So let's first start off with an applicant with a good credit score. Let's copy our function up here. And now we can enter values for a parameter, our credit score could be 700. And our credit score minimum, we could state this as 650. Run our cell. And it says we haven't defined our function yet. So let's just run this cell, run the next one. And great, we have approved credit score our function so in our parameters that apply the logic and then are returned to us approved credit score because 700 is greater than 650. Now let's check this with a low credit score, we can copy our function again. And we can make this lower than 650. So let's say our applicants credit score was 600. Now we could see that that credit score would be denied. So we would deny the applicant. And one of the really cool things that we did in the last lessons was to look at bandwidth. So maybe someone's really close to the minimum, they're not going to be automatically approved. But we can ask for more references or a higher security deposit. So if we paste our function again, for our bandwidth, let's change it to 10%. And here, it's made their credit score of the applicant 620. And we run ourselves, we get returned close to approval get referrals. This shows how we can have repeatable code through a function. And it's very easy to read because it's only one line of code. And if we want to understand what's going on in this function, we just go right to the function and read the logic. The next function we're creating is to calculate monthly mortgage, the calculation is a little bit complicated. So I'm going to paste it here, we could see this function takes in three different arguments, the principal, the interest rate, and the term, then here is the calculation in order to get what the monthly mortgage will be. Let's run this cell. So Python knows what our function is. Then in the next cell, Let's uncomment this code. And for this scenario, we have a purchase price of $300,000. For possibly a single family home, we're going to put 20% down. So in total, what we're borrowing from the bank is $240,000, we're going to input this value to our mortgage payment function in order to calculate what our monthly payment will be. So copy up top your function. And now let's enter in a different values. So principal is going to equal principal. For the interest rate, let's make an arbitrary number. Let's say it's 6.25.
Then for the term, let's say this as 30 years, which is typical. Now we can do Shift answer to run our cell. And we see here that this mortgage payment looks really high. Now why is that? Well, this term is actually looking for the total term, so not the years, but 360, which would be the total months. So let's change this here to 360. And now we see that the mortgage payment is 1004 77. If we're trying to think about purchasing this property, and we're being mindful about cash flow, we want our income to be higher than our expense. That's why this function is super useful because we could run different scenarios of which purchase price would be at the right amount where we would still be cashflow positive. In this lesson we covered functions. We did three different functions, increasing rent percentage, credit check, and monthly mortgage. Let's explore list comprehensions in the following lesson. See you there why Full access the introduction to real estate Data Analytics course. Then sign up for the course in the link below. You will learn Python programming all with real estate related examples. This includes web scraping, retrieving data from sources like Zillow, realtor, Redfin, Yahoo Finance, US census and more. If you haven't already, check out the introduction video to the course on YouTube to get a full understanding of what the course has to offer. Also members of the free tech and real estate group on Facebook receive a 20% off the course. See in the next lesson
Transcribed by https://otter.ai