Chris
Chris

Reputation: 807

Python list types

This is quite a weird/silly question so if anyone as a better title name, please request an edit.

I've created a function that returns the product of numbers in a list. When testing this function, I tried lists that that included intergers and reals(floats), such as items = [1,2.0,3,4.0]

Function:

items = [1.0, 2, 3.0, 10, "a"]

def mult1(items):
    total = 1
    for i in items:
            total = total * i
    return total

print mult1(items)

This function above works with items being [1.0, 2, 3.0, 10] and the output of that particular list being 60.0.

I've studied Standard ML where you can only generally have a list of a particular type (I'm sure there's code out there that somehow makes my statement irrelevant), so intrigued to find out what would happen if I entered a string as a list item, I did, and got an error (I expected to get an error because you can't multiply a string with a number).

However, the error I received has confused me:

TypeError: can't multiply sequence by non-int of type 'float'

This error has confused me. I understand that a string cannot be multiplied, but does this also suggest that all of the items in the list are considered a float despite some looking like integers?

I thought that the error should suggest it cannot compute a type that is not a float or not an integer, but to me (probably wrong) it seems like it's suggesting that it can't multiply the list by a a type that is not an integer that is also not of type float?

To me this sounds like each element has two types, integer and float.

Upvotes: 2

Views: 265

Answers (3)

Padraic Cunningham
Padraic Cunningham

Reputation: 180502

You can multiply a string but only by an int, you see the error as you are trying to multiply a string by a float:

In [7]: "foo" * 2
Out[7]: 'foofoo'

In [8]: "foo" * 2.0
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-8-3128e6ce951c> in <module>()
----> 1 "foo" * 2.

TypeError: can't multiply sequence by non-int of type 'float'

In your particular case you are trying to multiply a from your list by a float:

 [1.0, 2, 3.0, 10, "a"] 
                    ^
                   error

Total starts out an int but when you multiply an int by a float you get a float so total = total * 1.0 -> 1.0:

In [9]: 1 * 1.0
Out[9]: 1.0

Upvotes: 2

R Nar
R Nar

Reputation: 5515

This error occurs because you can actually multiply some non-number types by ints, but not by floats. So if your product function looks something like:

def product(l):
    prod = l[0]
    for x in l[1:]:
        prod *= x
    return prod

and your list looks like this : l = ['this string',3,2.0]

then your iterations will show the following:

>>> def product(l):
    prod = l[0]
    for x in l[1:]:
        prod *= x
        print(prod)
    return prod

>>> l = ['this string',3,2.0]
>>> product(l)
this stringthis stringthis string
Traceback (most recent call last):
  File "<pyshell#69>", line 1, in <module>
    product(l)
  File "<pyshell#67>", line 4, in product
    prod *= x
TypeError: can't multiply sequence by non-int of type 'float'

note that the first iteration is allowed because str*int is a valid expression (so is list*int and tup*int etc) and so prod is still valid but is a STRING, not an int or float. then when you DO try to do a str*float expression, the TypeError will occur.

Upvotes: 2

tom
tom

Reputation: 19163

You're getting this error because you are (perhaps unintentionally) multiplying a sequence (list, str, tuple) by a float.

>>> a = [1, 2]
>>> a * 2
[1, 2, 1, 2]
>>> a * 2.0
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can't multiply sequence by non-int of type 'float'
>>>

Upvotes: 3

Related Questions