Python 3, an illustrated tour Transcripts
0:00 In this video we're going to talk about integer division.
0:03 This came out in Python 3 pep 238 introduced it.
0:06 In Python 2 when you divided a number by another number
0:11 if you divided floats, you'd get back a float as a result,
0:14 but for integers, you would get back an integer as a result.
0:17 And this is called floor division.
0:19 Guido writes in his Python history blog about how this was a mistake
0:23 and Python 3 attempted to rectify this
0:27 by changing the behavior of the division operator.
0:30 So in Python 3 slash does what's called true division
0:35 there's a __truediv__
0:38 and double slash does floor division __floordiv__
0:41 So if I divide 2 by 3 in Python 3, I get point .6666,
0:46 and if I say 2//3 then I get that floor division and I get back 0 as a result.
0:55 Note that floor division also works on floats
0:58 if I say 2.0 divided by 3.1, I get .66,
1:01 but if I do floor division on those numbers, I get 0.0
1:05 and because everything is an object in Python,
1:07 if we want to we can go through the steps
1:09 to show that you can invoke __truediv__ on an integer and __floordiv__ as well.
1:16 Now again, you typically don't do this, we typically don't call the dunder methods
1:20 but they're there because in Python everything is an object
1:22 and you can call the object methods if you want to.
1:25 Quick summary, in Python 3 when you divide two numbers,
1:29 you will get a float as a result.
1:32 If you don't want a float then use a double divide
1:35 to do what's called floor division.