Python Conditionals and For Loops | Real Estate Analytics
In this video, we will cover Python conditionals and loops. You will learn how to create if else statements, and how to iterate through a list of rental income.
Ariel Herrera 0:00
In this video, we will cover Python conditionals and loops. You will learn how to create if else statements, and how to iterate through a list of rental income. Let's jump into Lesson three from our table of contents. What are conditionals? Well, let's start first with comparison operators. Python allows you to compare two different variables to one another. And this comes pretty simple to some math that we've seen in elementary school. So we can look to see if variables are equal to one another, not equal greater than, less than, greater than or equal to, as well as less than or equal to. On the left hand side of this table is the syntax of how you perform these operators in python. Let's walk through several examples. Down below, I've set three different variables for credit score. Remember, in our previous sections, we were looking to see if an applicant would be true to pass our credit score criteria, or false where they did not pass. In this case, our minimum credit score is 600. The maximum that I believe a credit score could be is 850. Now in our case, our applicant has the most perfect credit score, which is the ideal situation at 850. We're going to uncomment the code below by highlighting everything and then clicking command forward slash. Now we can run this code to see what the output would be when we use each different operator that's available for Python, run these two cells. Now let's review our print statement. And the first scenario, we look to see the credit score for the applicant, does it equal their credit score minimum, so that's 850 equals 600. Well, that's going to be false. So we receive the boolean value false. Next credit score not equal to credit score minimums, this is going to be the inverse. And here we could see true two different values. Next, is a credit score for the applicant greater than the minimum, yes, it's 850, which is higher than 600. So that is set to true, then the reverse is it less than would be false. And the next to we look to see is a credit score applicant greater than or equal to the minimum. True, it is now is the credit score applicant less than or equal to the minimum false. Now if we change this to max, we're going to see this value change. And the reason is because we're looking at less than or equal to, in this case, the VAX score is equal to the score of the applicant. Python comparison operators are very useful and will be used throughout the class, we will likely be using them with other if else statements, let's create our first IF statement. So in the cell, we want to test if our applicants credit score will be above the minimum. In our case, our credit score for our applicant is 700. We tested it here when we were doing these print statements. But how do we actually put it into an if statement? Well, we first start with typing out if, then we start with the variable we want to compare. In this case, we'll start with credit score applicant. Next, we want to check is this value greater than or equal to the minimum credit score. So we'll we will use greater than or equal to, and then we will copy the score minimum that we set up top followed by a colon. Now what do we want to do? Once we test to see if the credit score is above the minimum, let's print yay, approved credit score. Now let's run the cell. And we could see that we did print approve credit score, we printed it because it was higher. Now if we were to take the same code and run it for the new applicant score below, let's see what happens here. Nothing we export nothing, because the credit score is below our minimum. But this isn't useful, we want to at least know that the credit score has been denied. So how do we add an extra condition? Do we add another if statement? No, we could do this all within the same if statement by using else. So on the next line, we're going to add else this is going to handle anything that is not true. Add a colon and we can print here denied credit score.
Run the cell and we could see in this scenario five now It is less than 600. So unfortunately, this tenant will not be able to move forward. Now let's walk through a real world scenario. Sometimes scores like 590 are so close to our minimum that we may still want to consider this tenant. In this scenario, the rental market typically cools down in winter months, you have an unexpected vacancy in a unit the first week of December, you are willing to review applicants who are emerging credit and close to your minimum credit score. So we are going to set a bandwidth. If the applicant has a credit score that's 5% away from our minimum, then we want to know okay, yes, they don't approve for our credit score minimum, but we should still take a deeper look, and maybe require them to have more references, or a higher security deposit. In order to calculate what this adjusted credit score minimum would be, we would need to take our credit score minimum and then subtract it by this new bandwidth. So I'm going to write out this code here. Now once we've calculated our adjusted credit score minimum, we basically took credit score minimum times the bandwidth, and we subtract this from credit score min, and now we see our adjusted score is 575. Seven D is 5%, away from 600. So now in this new scenario, we have a credit score applicant that has a credit score of 590. We want to handle this applicant when we go to more of a review. So let's copy our previous if else statement. And now we want to handle a third condition, we have one and two so far. So how do we handle a third? Well, you can also add else if statements. So if we tab back to the beginning of this line, we can enter L if. And now we can copy this part up here. But instead of testing our credit score men, we will be testing against the adjusted credit score minimum. This is our second criteria. And if the user falls into this bucket, we want to print close to approval get referrals. Now let's run the cell. And we could see this worked out perfectly. Our new variable was for an applicant that had a credit score of 590. In our first scenario, they were not higher than 600. So this did not print. In the following scenario, they were higher than our adjusted credit score. So we did print close to approval get referrals. In our previous lessons we use for loops in order to print out items from our list. For loops are used for iterating over a sequence that is either a list a tupple, a dictionary set or a string. In this example, let's imagine that we are real estate investors, we have a small portfolio, and we want to increase our rental rates. We see here that we have a list called rent and comm lists, we have four different units here. And we want to see if we increase our rental rates by a certain percentage every month. What would that look like? How much would that increase our income, we can do this with four loops. In this first cell. Let's look to see how many number of units we have. We can of course, quickly observe a Thera four. But in some cases, there may be hundreds, and we won't be able to count it as easily. So if you remember from previous lessons, we need to wrap the len function around our list. Run the cell and we can see we have four units four objects within our list. Now let's print all of our rent values within the list. We will start by writing for then we can have anything represent our variable. In previous examples I've showed you x is just a character that I highly associate with iterating through list, but in this case, we could use a variable R just for you to see that you can use any character here. So for our representing rent, in rent income list,
print rent. And what our for loop did is that for every object with enter lists, so we have 1300 14 5900 1500. Each object was labeled as our and we did something with our we prints it R so we have here the rent listed each and every time. Now if I were to just run R here and Shift Enter you could see that r is still associated With the last element in our list, which was 1500. So in theory, what a for loop does is it sets our to each of these variables. So r equals 14 5900, then 1500, and does something with it, which is what we did printing each of these rent values. But in most cases, we're not just going to be printing values, we're going to be doing something with this information. And from our scenario, we want to see if we were to increase our rent values by a certain percentage, how much more rental income would we have, we can detect this with a for loop. So I have here 1.10, which is going to represent 10%. This is going to help us to see what a 10% increase in our rental portfolio would look like. The first thing we're going to do here is set up an empty list there we're going to append to, I will call this empty list, increase rent income list. Next, we're going to create a for loop to iterate again through each of these rent values within our list. Since we did it up top, we can copy over this first line. Now the logic is going to change. First, we want to calculate what the new rent will be. So let's set a variable called New rent. In order to calculate the new rent, it's going to be rent times increase percentage. So if we just look at this for one single example, where our is 1500, and we multiply it by the increased percentage, we can see that by increasing our rent from 1500 to 10%, higher, the rent value will be 1650. And we want to do this for each and every one of these rent values. So we can now copy this piece of code that we've tested, delete the cell, paste it under new rent, and that will be our new rent value, we want to add these new objects into a new list. So let's take this list. And we're going to use dot append method at the end and place our new rent variable. And here, this is going to store our values. Whenever I do a for loop, I usually like to print out something so I could see the process as it's occurring. Some for loops can be instant. But if you're doing a lot of computational work, it can run for a long period of time. Here I'm going to print new rents.
My string is stating rent will increase from our which is going to be the value that comes directly from our list to the new rent value, which is being multiplied by this increased percentage. Let's run this cell. And we could see here, all these values have been calculated. If we increase our rent values by 10%. For our first unit, we will go from 1300 to 1430. And we have a bunch of decimals afterwards, because this multiplication turns our number into a float. In order for us to make this a little bit more clean, we can round our value or just establish it as an integer. In this case, I'm going to wrap it in int. Now let's rerun the cell, and ourselves a lot cleaner. Now, we could see here in our print statement that for the second unit 1450 would go to 5095 900 to 990 and 1500 to 1650. just by eyeballing this, we could see that we're going to make an additional couple of $100 If we increase our rent percentage by 10%. But what does that actual amount, let's put a value towards it. And the next cell, let's calculate the previous total rental income. Well, this rental income from the prior period is in the rent income list. So let's copy this variable, and we can wrap it in some. Now we could see that our total rent income previously was 5001 50. Let's do the same logic for our new list which is increase rent income list. We still have that issue because it is a float value. So let's change this into an integer. Now we can see that our previous total rent income 5001 50 If we make a 10% increase will be 5665. And let's see what the total increase amount actually is. We'll subtract these two values from one another. Here we can see the total difference between these two lists is 515. So in total, our cash flow will increase by $515. Now another way to calculate this 5665 is by taking the total sum of our rent income list, and multiplying it by the increased percentage. Here we can see that 5665 matches 5665. Up here. In one case, we apply the logic to each individual rent value. In the second case, we summed up the entire rent list, and multiplied it by that percentage. Now let's shift over to do while loops. Let's say in a scenario, we want to increase our rental rates in total to $7,000. However, we don't know what that percentage value will be, let's test different percent increases. To see when we would hit our quota to save time, once we hit our target value or a quota, we want to stop the code. Therefore, we will use a while loop to do so a while loop is constructed similar to an if else statement, where you start with while and then you test two different conditions. So in this case, we have three values that up top I, which is going to start with a percentage. So in this case, multiplying to see 10% increase in rent, then our rent income has to have a start value. So we're going to set this to total rent income is zero, and our target value is $7,000. In total rent income. So while our total rent income is less than our target value, let's do something. And once it hits our target value, stop the code. So first, we want to calculate rent increase, we're going to set a new variable called increase underscore percentage. This is adding one to the value of i. So if we saw beforehand, when we were looking at increased percentage, we did 1.10, which was an faster way for us to get what that percent, the actual total income would be after applying the 10% increase. So we're adding that one here. And we can make a print statement to show how much we're increasing rent by.
Now let's calculate total rent income. So as we saw previously, we could just sum the rent income and multiply it by increased percentage. So let's copy that code, bring it down and set this to a new value called total rent income, which is that value, that's going to change from zero to the new sum. And we're going to continue this calculation until we hit our $7,000. So the part that changes here is that instead of the static 1.1 value, this is going to change each and every time with this new variable called increased percentage. Let's print this value. And next we want AI to continuously change eyes representing the percent increase. So in this case, we're going to tell Python, for AI every single time in this loop, we want to add point one, so it's going to go from point one, which is a starting to point to point 3.4. Let's run this cell here. And we could see that each scenario, if we increase the rent by 10%, we already know this value, we did it beforehand. 20% 6001 80, still not our target of 7000 then increased rent by 30% 6006 95. Now will we increase by 40%. Now we hit our target value. So Python recognizes from our statement up here once that target value is hit, then stop running the code. This saves us time down the line instead of testing each and every value. In this lesson we covered conditionals if else statements and loops for each and do while statements in the course we will not be touching do while statements, so don't worry if it was a little bit confusing. We will be really focusing on if else and for each. We've also had sneak peek from prior videos of creating functions. In the next lesson, we're going to touch on functions even more. And if you need more help to understand loops, definitely check out the video below for an in depth view. Alright, see in the next lesson, want full access to the introduction to real estate Data Analytics course. Then sign up for the course in the link below. You will learn Python programming or 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 seeing the next lesson.
Transcribed by https://otter.ai