Python 3, an Illustrated Tour Transcripts
Chapter: Numbers
Lecture: Division

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 talk about integer division. This came out in Python 3 PEP 238 introduced it.
0:07 In Python 2 when you divided a number by another number if you divided floats, you'd get back a float as a result,
0:15 but for integers, you would get back an integer as a result. And this is called floor division.
0:20 Guido writes in his Python history blog about how this was a mistake and Python 3 attempted to rectify this
0:28 by changing the behavior of the division operator. So in Python 3 slash does what's called true division there's a __truediv__
0:39 and double slash does floor division __floordiv__ So if I divide 2 by 3 in Python 3, I get point .6666,
0:47 and if I say 2//3 then I get that floor division and I get back 0 as a result. Note that floor division also works on floats
0:59 if I say 2.0 divided by 3.1, I get .66, but if I do floor division on those numbers, I get 0.0 and because everything is an object in Python,
1:08 if we want to we can go through the steps to show that you can invoke __truediv__ on an integer and __floordiv__ as well.
1:17 Now again, you typically don't do this, we typically don't call the dunder methods but they're there because in Python everything is an object
1:23 and you can call the object methods if you want to. Quick summary, in Python 3 when you divide two numbers, you will get a float as a result.
1:33 If you don't want a float then use a double divide to do what's called floor division.


Talk Python's Mastodon Michael Kennedy's Mastodon