Understanding string and float. (Need help) Python | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Understanding string and float. (Need help) Python

Could anyone help me sorting this? If I have the following: 5 * float(“2”) I will have as result 22.0, right? In that case how can I identify if the code is using a float number or a string between “?

4th Dec 2018, 9:16 PM
Victor Biscio
Victor Biscio - avatar
3 Answers
+ 13
No the result will be 10.0 because you first transformed "2" (which is a string) into a float and then you multiplied by an integer and that gives you a float :10.0 (as long as you have a float somewhere in any operation the result will be a float unless you specify otherwise (using int() etc.)). There is an exception: 10/5 will be a float even though both 10 and 5 are int because the / (true division) will always return a float
4th Dec 2018, 9:26 PM
Uni
Uni - avatar
+ 5
You're welcome Victor Biscio I'm glad I could help ; )
9th Dec 2018, 4:37 PM
Uni
Uni - avatar
+ 1
Uni got it! thank you so much for your explanation!
9th Dec 2018, 12:02 PM
Victor Biscio
Victor Biscio - avatar