+ 3

Question

Why it is integer? For dividing it turns float number

22nd Nov 2021, 1:15 PM
Ксения Краснова
3 odpowiedzi
+ 4
Thats how division works in python. Always returns a float
22nd Nov 2021, 1:29 PM
Slick
Slick - avatar
+ 2
Ксения Краснова 5 / 2 = 2.5 float 5 // 2 = 2 integer
22nd Nov 2021, 1:32 PM
Paul K Sadler
Paul K Sadler - avatar
+ 2
Python simply lets you declare a variable without any need of telling the data type. If you declare a variable like x = 10 then it is an integer, as the compiler itself understand. But if you work upon it using division, x=x/4 The answer is 2.5 and the compiler itself converts it to the required type that is float, unlike other languages like C++ where it rounds it off to make it an integer. So, that should not be a problem. If you want to get an integer as a result, you can convert it explicitly. 🙂A smiley for you.
24th Nov 2021, 10:40 AM
Anuj Kumar Patel
Anuj Kumar Patel - avatar