3 ms·
Yes. But that is easy to explain. "It always rounds down" Of the top of my head come the range/enumerate/zip returns. They are really useful and needed to teac
by Vaskivo 10y ago
Yes. But that is easy to explain. "It always rounds down"
Of the top of my head come the range/enumerate/zip returns. They are really useful and needed to teach "pythonic Python". But it's hard to explain that it doesn't return a list. "It returns an iterator, that is lazy, and you need to call 'list()' on it to use it as a list..." You get my point.
You fall to a "just do like I told you. You'll get it later." that plagues a lot of other languages when being taught to newbies.
But I never said that Python 3 is less "newbie-friendly" than Python 2...
- cgriswald 10y ago> Yes. But that is easy to explain. "It always rounds down" The explanation is easy, but it isn't simple. With respect, "It always rounds down" is a terrible explanation. It explains the apparent behavior of the / operated in Python 2, but it does not really explain why. A better explanation is that the Python is both dynamically- and strongly-typed and that / does integer division on integers and "true" division on floats. For the newbie who understands these concepts, this is a far more useful explanation and metaphor; and for the newbie who doesn't, he has to learn somewhere, and frankly the / operator is not a bad example.
- webmaven 10y ago> / does integer division on integers and "true" division on floats. Floats don't really get true division either: https://docs.python.org/3/tutorial/floatingpoint.html https://docs.python.org/3/tutorial/floatingpoint.html And as it happens, I once explained a related problem with round() to Zed Shaw on Twitter: https://twitter.com/zedshaw/status/777780480737357824 https://twitter.com/zedshaw/status/777780480737357824
- amyjess 10y agoThis is one reason why I go out of my way to avoid floats and instead use Decimal whenever I need to support non-integral numbers. (and from playing around with Perl 6, I love how decimal literals are of type Rat instead of Num; I wish Python decided to do something similar in Python 3) Edit in response to Zed's question, in 3.6 I always just do: print(f'{foo:.2f}') Works with Decimals and floats.
- webmaven 10y agoYeah, I'm pretty sure he'll turn up his nose at that solution as well. For purposes of teaching the language to a beginner, he wants literals being rounded or divided to "just work" in a way that is unsurprising to those beginners, and there isn't any way to achieve that unless the default type of a number with a decimal point is Decimal rather than float (which isn't at all likely to happen). The closest we are likely to get to that ideal is by introducing a Decimal literal like 0d2.125, riffing off the Hex and Octal literals (eg. 0xDEADB33F, 0o76510). P.S., unless I am mistaken, you're just truncating the float, not actually rounding it
- deleted 10y ago[deleted]
- kstrauser 10y agoSlight correction: it always rounds toward zero. -3/2 == -2 in Python 2.
- willhslade 10y agoThat's away from zero.
- kstrauser 10y agoI am s-m-r-t smart! Sigh. Yes, it's rounding down. I feel a little vindicated that at least three people saw that and said, "oh, he's right. Have my upvote!"
- Sean1708 10y agoDon't get me wrong, I wasn't having a go at you. It's just that I learnt 3 when I already had quite a handle on 2, and I'm not very good at putting myself in the shoes of a beginner.
- Vaskivo 10y agoNo problem. I didn't think you were. And I understand that it is hard for some to jump back to a newbie perspective. Some people look for performance in a programming language. Others for a thriving ecosystem. I look for usability and elegance. So I tend to look at programming languages that way.