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