Python 3, an Illustrated Tour Transcripts
Lecture: Walk-through: Number
Login or purchase this course to watch this video and the rest of the course contents.
0:00 In this video we're going to look at num test, open it up in your editor and validate that you can run it. If you don't run it from your editor,
0:09 run it from the command line, it should work as well. We see that there is one failure., it's a name error on line 8,
0:14 so I think we're good to go on my machine. Let's look at this first test here, there are 102 floors in the Empire State Building,
0:21 if you have walked up a seventh of them, how many whole floors have you walked up, store the result in floors.
40:01 So the idea here is to say we want to make the distinction between true division and floor division,
0:40 because we're walking up floors that should be a hint that we should use floor division here which gives us this division or whole numbers,
0:49 So, 102 and we've walked up a seventh of those. So in Python 3 we use the double slash to get the floor division. Let's run it and see if it works,
1:05 and I have a typo, I did 107 instead 102, I'm trying again. Okay, so that one looked like it worked.
1:13 So that's the whole number of floors that we walked up. What percentage of floors have you climbed,
1:19 store the result as a string with one decimal of precision in the variable per so what percentage have we climbed?
1:28 I'm going to put it in an fstring here feature Python 3 so need to put curly braces around here. So we have climbed 7 out of 102.
1:37 So 7 divided by 102 will give us the number of floors. Let's run this and see what it says. Okay. So this is what I got here
1:54 per do the reverse of it does it equal to that, no, it says it's not. So what's happening here? I am getting a number that's not formatted correctly.
2:08 So it wants me to format it as a percent. So in order to do that, I need to put a colon here and this says I want one decimal, so I put .1
2:17 and then I put percent there to format it as a percent. Okay, in that case, it looks like it worked. It should be 6.9% doing a little trickery here.
2:27 so you don't just cheat and type in 6.9% Okay, I have (2^64)-1 satoshis, can I divide them wholly by 3.
2:36 How many would each person get store the result in coins? So again, this is floor division, if we want to do whole division. So coins = satoshis//3
2:50 and if we multiply that by 3, if it's integer or a floor division, it should get us back to where we started from.
2:58 So let's see here satoshis divided by 3. It looks like that is indeed the case you can divide them by 3.
3:09 The US population is around 326 million, some number after that how many whole coins would eat US citizen get, store the result in US coins.
3:25 So US coins is equal to and there's a hint, use underscore to make the population easier to read so I'm going to say 326_979_681
3:40 so there's our total population and we are going to divide our coins. So whole coins would each person get, we need to divide satoshis.
3:58 Satoshis divided by that, that should give us the whole coins and let's run that. Okay, that looked like it worked.
4:11 So again, when we do the double slash that's what we call floor division, that gives us a whole integer number.
4:18 Okay, I have .5 pumpkin pies and 1.5 apple pies. I want to use Python to round the number of each pie store the result in pumpkin and apple,
4:28 so pumpkin = round (.5) and apple = round (1.5) So you might think well, I do, I was taught when you round
4:45 if it's .5 you go up to the next number, but apparently that's not always the case generally. Python 3 actually doesn't do that.
4:55 It does what's called banker’s rounding, where it rounds to the nearest even number here. So this one should round to 2 and this one should round to 0
5:07 and that's why the sum of those is 2. Okay, hopefully that gave you a little feel for floor division in Python 3.
5:17 And again, this is the behavior just a single slash in Python 2 and the ability to put underscores in number literals
5:26 just to help you read them better. These are good as commas, placeholders are also good in her and binary literals as well.
5:36 And also, we learned a little bit about round, how round does what's called banker’s rounding
5:41 one of the benefits of that is that it eliminates the bias towards rounding high, they should even out if you have randomly distributed numbers,
5:49 hence that's why bakers like to use banker’s rounding.