implicit convertion in python | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

implicit convertion in python

Why have to convert the integer to a float when 8/2? I suppose it might be easier for computer to operate in integer than float.

17th Feb 2019, 10:28 AM
apprentice
apprentice - avatar
3 Answers
+ 3
It just is so. True division "/" (almost) always gives the result as a double floating number. But for whole number division you can use: Floor division "//", which gives it's result (almost) always as a whole number (Can be either float and integer depending on the operands) I added "almost" because in case of ZeroDivisionError nothing is returned.
17th Feb 2019, 10:51 AM
Seb TheS
Seb TheS - avatar
+ 4
The / division always gives float value, regardless if the result is integer or not. You can use also 8//2 that is the floor division operator, if both operands are integers then the result will be also guaranteed integer in Python3
17th Feb 2019, 10:36 AM
Tibor Santa
Tibor Santa - avatar
+ 1
Thankx. It helps me a lot.
18th Feb 2019, 8:55 AM
apprentice
apprentice - avatar