madcrazydrumma
madcrazydrumma

Reputation: 1897

Do floating points take over integers in a result in Python?

So I have a Python function that sums values from a list:

def inc(list):
    if len(list) > 1:
        return list[0] + inc(list[1:])
    elif len(list) == 1:
        return list[0]
    else:
        print("Empty List!")

If i apply this function to the list [1.0, 2, 3.0, 4] it returns 10.0. Why does it return a floating point decimal instead of an integer?

Upvotes: 0

Views: 27

Answers (1)

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 799044

Python coerces operands to a common type in order to operate on them. In the case of int and float, both are coerced to float and hence that is the type of the result.

Upvotes: 1

Related Questions