float() turn my number into a float?
float() is a built-in function, so Python comes by default with this ability to convert to float. To use it, we simply write a number or string inside of the parentheses, like this:
You’ll get errors if you try to use it any other way. It cannot be used as below:
10.float() # invalid
float() = my_integer # invalid
To see a list of all built-in functions available in Python 2, take a look at this documentation page!
9 posts were split to a new topic: Cucmber logic
Ah. Okay, thank you. That explains it.
Can i write float like this
Variable name = float(100.)/6 if not can u guys explain me how should i. correct this
100. is a float, converting that to float would be super-unnecessary
Nobody but you can correct that, because only you know what you meant by it.
Can i write float like this
float_cucumbers_per_person = cucumbers ./num_people
if not can you guys please explain that for me? thanks
Can you? Yes. Would you?, not likely, unless it is in the command line. Python 3 has made this question moot since all evaluations result in a float unless explicitly stated otherwise. No fudging or casting required. Now it’s the other way around for integers.
As I understand it, floor division is the same in both versions. This will need testing…
1 // 1 => 1
1.0 // 1 => 1.0
1 // 1.0 => 1.0
i tried to use it with a variable like float(cucumbers) and tried to print it, the value inside the cucumber was also a whole number only, this i supposed will turn my data entered in it to as float, but thats not what it did, it returned me just the whole number only.
so can the float function only be used with the numerals and not with the variables,
#this worked fine, but not this one
Seems odd behavior. Cannot reproduce in Python 3.
>>> a = 1
SyntaxError: invalid syntax
Above we see that the function works fine with either number or variable (assuming a number is at the other end). What we also see is that the shorthand syntax only works on numbers, and not on variables. Just tossing this in.
float(n) doesn’t turn n or the value it refers to, into a float
it creates a float
the only thing float at all does with its argument is to look at it, you get the result as the return value
variables have nothing to do with it, because values are not aware of what is or isn’t referring to them
and values can’t in some way restructure themselves to be something else. If you have a list then that will forever remain a list
if you want to change what a variable refers to, use assignment:
a = float(a)
This gives me a correct float answer (that being 16.6666666667) when i print:
float_cucumbers_per_person = float(cucumbers)/num_people
This also gives me the correct float answer:
float_cucumbers_per_person = cucumbers/float(num_people)
Why does putting both variables in the float function like this give an incorrect float answer (16.0)? :
float_cucumbers_per_person = float(cucumbers/num_people)
What exactly is happening so i can understand it better?
if the division involves two integers, python will floor (round down) the result. Casting to float after the rounding down will not give the desired output (giving rounding down has already occurred), which is why you cast one of the two numbers to float, so the division involves a float and the rounding down doesn’t occur
In python 3 there is only one type of division. Every time it gives float value.
Please be careful with your phrasing. “normal” division (
/) will always give a float value, but python3 also has floor division (
//) which will give an integer. So there are multiple types of division.
You had to revive the topic for this question?
its very google-able question have you tried to use google and read some documentation?