0

Why python give result in float?

31st Dec 2017, 3:05 AM
kusum
2 Answers
+ 4
If you mean in 3.x vs 2.x ... https://www.python.org/dev/peps/pep-0238/#motivation I'm still reading, but the first thing that pops out at me is 'because surprise integers'. Python is *dynamically* typed so (in 2.x) a calculation you expect to remain in floating point could suddenly truncate to integer floor division if any part converted to an integer. Basically I think they're enforcing 'true division' in 3.x instead of flipping suddenly to the status quo popular when 2.x was born (Java/C more rigidly by static type). edit: You might want to read that link; the other reasons are also compelling.
31st Dec 2017, 4:21 AM
Kirk Schafer
Kirk Schafer - avatar
+ 2
It gives a float result as default. If you want to get an int, use a double back slash "//". print(10 / 5) # 2.0 print(10 // 5) # 2
31st Dec 2017, 4:23 AM
Boris Batinkov
Boris Batinkov - avatar