Python Jumpstart by Building 10 Apps Transcripts
Chapter: App 9: Real Estate Analysis App
Lecture: Mining data with loops
0:00 All right, now you've seen all about lambdas, and you've seen how easy the are to write to sort these data,
0:08 and that allowed us to solve two problems really easily, if we had a sorted set of purchases, sorted form low to high by price,
0:16 we can say well the most expensive one is the last one, and the least expensive one is the first one. But let's ask some more interesting questions,
0:24 what is the average house price, it turns out that there is a statistics module here, and on there we have a mean method
0:31 so we'd like to use this but we need to import that at the top, thank you PyCharm,
0:36 and we have to give it- well what does it say- data, that's kind of cool, it even has the same name so of course we should give it data,
0:44 let's see how that works. Hm, can't convert type purchase to numerator denominator.
0:50 Hm, ok so it looks like we need to give it an iterable set of numbers, not an iterable set of purchases which we just made up,
0:58 so how do we get that, let's just go like this, here we'll say prices, and we'll create that to be a list
1:06 and we'll say for purchase, let's say pur in data: prices this is really painful, right,
1:13 so we are going to go prices and we'll say append and what do we want to append, we want to append the purchase.price.
1:19 And then we can get the average of prices so we'll say ave_price = this and let's just print,
1:25 the average home price is this is in USD and let's put a comma separator, and a format, ave_price, let's try that.
1:36 Awesome, the average price is some number around 234,000 and let's go and round that off, there we go,
1:43 234,144 and actually that is probably truncating, but close enough for what we are after here, this is ok but this is not so nice, right,
1:54 we created a list and we did a loop to grab the data and maybe we only want to, maybe there is certain times we want to do a test,
2:02 so it turns out that there is actually a better way to do this, let's go and answer other questions while using this looping construct
2:09 and then we can do something better Ha!, it looks like I left a pass here, no harm no fell, so it's going to look very similar to this,
2:19 so here we are going to say give me the prices, and I want to go to there and grab each purchase and append the price,
2:26 but in this case, we want only the case where it's a purchase of a two bedroom home so we'll say if pur.beds==2: then we'll do this,
2:34 and this will be the average price of 2 bedroom, there we go, so let's run this again, awesome, so the average price of all homes is 234,000 dollars
2:44 but the average price of a 2 bedroom home is a 165,000 dollars. Maybe we also want to answer the questions like well,
2:51 how many baths does it have on average, and things like that, hm, so this is going to be a little bit harder.
2:58 Well, it turns out this is like programming for all the programming languages almost all of them anyway, C, C++, Java,
3:08 it does loop around here, write this procedural code, we can actually improve upon this in 2 ways,
3:13 we can improve by using what are called list comprehensions, which let us write this much more concisely, and then we can actually improve upon them
3:21 using something called generator expressions, we are going to get to both of those,
3:26 first, let's look at the challenge looking around and trying to run this code on both Python 3 and Python 2.