Coerced data types - what's the logic? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Coerced data types - what's the logic?

So in the lesson it shows how if you mix one float with integers in a list then convert it to a numpy array, all values will be coerced as floats. I tried to see if the order of the float matters but it doesn't: if there is a float anywhere in the list, all become floats. Then I tried to make a list with boolean type: [1==1, 2==3] and if you try to mix with integer [1==1, 2==3, 9] then the dtype is coerced to all integer (the array becomes [1 0 9] clearly 1 for true and 0 for false). If you add a float, all become floats. So: int + bool = int int + float = float float + float = float Two questions: 1. Why the list can handle a bool like 1==2 but cannot handle FALSE or true? 2. Why the integers coerced into floats look like 1. 2. and not 1.0 2.0?

8th Oct 2020, 7:17 AM
Nassim Abed
Nassim Abed - avatar
1 Answer
+ 1
Int + book = int cause the values True and False also represents 1 and 0 respectively. So the operation is carried out on there integer value Int + float = float, cause an int is literally just a float without decimal 17(int) & 17.0(float). When similar operations occur an int is easily flex to fit into the float data type I hope I answer it to the best you understand 😊
10th Oct 2020, 2:04 AM
Eedriz_
Eedriz_ - avatar