Why Doesn't This Give the Remainder?

Why doesn’t

float_cucumbers_per_person = float(cucumbers/num_people)
print float_cucumbers_per_person

give me the remainder of cucumbers / num_people ?

Whole code:

cucumbers = 100
num_people = 6
whole_cucumbers_per_person = cucumbers / num_people
print whole_cucumbers_per_person

float_cucumbers_per_person = float(cucumbers/num_people)
print float_cucumbers_per_person

1 Like

the remainder is calculated using the modulo operator (%), do you mean it doesn’t give decimal value?

that is because dividing two integers will give an integer as result, casting to float after the division means the rounding down has already occurred. there is FAQ about this

It just seemed like poor design that the command didn’t give a decimal value, but I guess I just don’t understand the grand scheme of how python works yet. Thanks for the reply.

this division behaviour was changed in python3. In python3, dividing two integers gives a float.

the only advantage i found of python2 his division:

a_list = ['a', 'b', 'c']
middle_value = a_list[ len(a_list) / 2 ] 

to get the middle value from the list using index. If this division would give a float (1.5), python would throw an error given indexes of list have to be integers

to overcome this problem in python3, you could use floor division (//), which will floor (round down) to the nearest integer

each programming language has its quirks.