0
Can anyone explain me please
print ( float ("10"*5 ) ) output 1010101010.0 print ( float ("10")*5 ) output 50.0 why?
6 Answers
0
you are always welcome
+ 3
float("10"*5) is doing the operation, then turning it into a float. basically your telling it to print "10" five times. 1010101010 and then turn it into a float. 1010101010.0
float("10") is turning 10 into a float. Then it is multiplying it by 5.
The difference is when "10" is being converted into a float
+ 3
The first code outputs 10, which is a character string, five times and converts it to float.
On the other hand, the second one converts character string 10 to float and multiplies it by five
+ 1
ok so i believe what you should consider here is your operator precedence, your () operator is of a higher precedence, its just like your math were you have a question as such 2 * (3 + 5), you evaluate the operands in the brackets first before considering others so this is the flow,
your first line of code >> print ( float ("10"*5 ) ) =>output 1010101010.0, would resolve the bracket first by multiplying the string "10" 5 times then thereafter calling the float method on it so you have float("1010101010") ->>1010101010.0
as for the second line of code the the string "10" in the brackets if first converted to a float before multiplying with 5 float"10" ->> 10.0 * 5 ->> 50.0
i hope this help and don't mind my long answers i was just trying to break it down better
0
thank you for replying
0
thank you
john emma



