Why does float('5') work ... | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Why does float('5') work ...

... but not int('5.0')?

11th Aug 2018, 4:32 PM
HonFu
HonFu - avatar
8 Answers
+ 4
'5' => convert string to float.True because you can convert string to float. '3.3' => convert int to float.False because you need to convert string to int to float.try this. print(int(float('3.3'))) the reason behind it that you can convert small data to big data but only it is possible when your numerical value is string.like '3'=3.0 but when you go big data to small data it give you error because you can't compress it. but when your numerical value is not in strings type then it is possible.
11th Aug 2018, 5:00 PM
Maninder $ingh
Maninder $ingh - avatar
+ 3
This principle is know as type conversion. Type conversions are of two types. 1. Type promotion When a lower size datatype gets converted into a higher size datatype. This happens automatically, since there will be no loss of precision, i.e. data. Adding zeroes after decimal does not make a difference to an integer. So this happens automatically. Thus float(5) works because int gets converted into a decimal number (float) without loss of precision. 2.Type coercion This means converting a higher datatype to a lower datatype. It does not happen automatically add it involves possible loss of precision. For example, while trying to convert a decimal number into integer, you need to remove all digits after decimal, which means precision loss. Thus it won't happen, i.e. int('5.0') won't work. It needs to be done specially, the process being called as Type Casting.
11th Aug 2018, 4:41 PM
Vedant Bang
Vedant Bang - avatar
+ 3
Okay, but why can I do int(5.5) then, losing precision? (Probably this is one of the things that's not confusing for people who know less abstract languages ...)
11th Aug 2018, 4:46 PM
HonFu
HonFu - avatar
+ 2
I don't know what's happening in the background, but the statements I presented definitely work out like that in Python.
11th Aug 2018, 4:58 PM
HonFu
HonFu - avatar
+ 2
What is the underlying mechanism? Why do strings make it a problem but it works between numbers? (And wouldn't you expect languages like Python to calmly do the needed two steps in the background?) Okay, it seems like my noob mind has to meditate over this a while longer. ;-)
11th Aug 2018, 5:05 PM
HonFu
HonFu - avatar
+ 1
You sure it's happening like that?
11th Aug 2018, 4:51 PM
Vedant Bang
Vedant Bang - avatar
+ 1
Oh right, stringy problems
11th Aug 2018, 5:02 PM
Vedant Bang
Vedant Bang - avatar
+ 1
when you convert a string into an int, it tries to take an integer value from said string. since 5.0 isn't an integer, it will return an error (python doesn't automatically convert it to an int) but converting a string into a float with a float value inside the string obviously works, as well as rounding a float to the nearest integer. you'd expect a language like python to auto-convert it to a float first but i believe it's something to do with just floating points in general the reason float('5') works is because 5 can also be a float
11th Aug 2018, 8:39 PM
hinanawi
hinanawi - avatar