There are two types of numbers in Python:
- Integers – positive and negative whole numbers, e.g., -6, 0, and 9,
- Floats – positive and negative decimal numbers, for example, -2.3, 0.0, and 3.142
Python language allows a user to multiply an integer with a string, list, or tuple to generate a repeated sequence; for example,
1 2 3 4 5 6 7 8 9 |
# Make string with str1 repeated seven times str1 = "Tomasz"*7 print(str1) # Creates a long list made of 5 copies of the original list. lst1 = [1, "Decker", 3.4]*5 print(lst1) # Creates a tuple with four copies of the original tuple. t1 = (2, None, [0, 1, 2])*4 print(t1) |
Output:
TomaszTomaszTomaszTomaszTomaszTomaszTomasz [1, 'Decker', 3.4, 1, 'Decker', 3.4, 1, 'Decker', 3.4, 1, 'Decker', 3.4, 1, 'Decker', 3.4] (2, None, [0, 1, 2], 2, None, [0, 1, 2], 2, None, [0, 1, 2], 2, None, [0, 1, 2])
But what happens if we multiply any of the sequences above with a float? That is where the “Multiply Sequence by Float” Error comes in.
What causes of “Multiply Sequence by Float” Error and How to Solve it
The “Multiply Sequence by Float” Error is a TypeError raised when we try multiplying a sequence (e.g., string, list, tuple) with float. For example,
1 |
"Tomasz"*3.2 # Multiplying a string and a float |
Output:
TypeError: can't multiply sequence by non-int of type 'float'
In the case above, Python throws a TypeError exception – an exception thrown when we try to operate on datatypes that are not compatible with that specific operation.
The error message explains the error better – we cannot multiply a sequence (in this case, string) with a float (an integer was expected).
Several cases can lead to “TypeError: can’t multiply sequence by non-int of type ‘float'”. Let’s discuss some of them.
Case 1: Multiplying a String With a Float
An example falling into this category is already given above. Here is another example.
1 |
print("5.6"*3.2) # Multiplying a numeric string and a float |
Output:
TypeError: can't multiply sequence by non-int of type 'float'
A reminder: a string is any character or group of characters enclosed in double or single quotes. “5.6“ is a string.
Solution: Convert string to float or integer
If you have a numerical string like in “5.6”*3.2, convert the string into float and multiply the two numbers, that is,
1 2 |
com1 = float("5.6")*3.2 # or int("5.6") to convert the string to an integer print(com1) |
Output:
17.919999999999998
Another scenario, in this case, is when we want to repeat a sequence like in “Tomasz”*3.2. In that case, we need to convert the multiplication factor into an int, that is,
1 2 |
str2 = "Tomasz"*int(3.2) # the float 3.2 is converted into the value 3 (an integer) print(str2) |
Output:
TomaszTomaszTomasz
Case 2: Attempt to Multiply a List or Tuple With a Float
This fits a situation where you want to multiply every list element with a float. For example, you might do [2, 4, 5]*3.5 expecting to get [ 7. 14. 17.5], instead, you will get this
1 2 |
lst1 = [2, 4, 5] print(lst1*3.5) |
Output:
TypeError: can't multiply sequence by non-int of type 'float'
Solution: Use a list comprehension or NumPy package
If you want to multiply each list element or type with a given number, you can use list comprehension, as shown below.
1 2 3 |
lst1 = [2, 4, 5] lst2 = [item*3.5 for item in lst1] print(lst2) |
Output:
[7.0, 14.0, 17.5]
Alternatively, you can use the NumPy package. This is especially useful if you have a huge list. The package supports vectorization, which may speed up your computation in such a case.
1 2 3 4 5 |
import numpy as np lst1 = [2, 4, 5] arr1 = np.array(lst1)*3.5 print(arr1) |
Output:
[ 7. 14. 17.5]
Note: You may need to install NumPy, in this case, using pip with the command:
pip install numpy
Case 3a: Multiplying Elements of Two lists or Tuples
This does not lead to the exact error we discuss in this article, but a variant. In this case, a user wants to multiply elements at corresponding positions in two lists or tuples.
For example, you may want [2, 3, 4]*[2, -2, 3] to yield [4, -6, 12] but, instead, you will get something like this
1 2 |
lst3 = [2, 3, 4]*[2, -2, 3] print(lst3) |
Output:
TypeError: can't multiply sequence by non-int of type 'list'
Solution: Using list comprehension, map() function, or NumPy
There are three options here. The first one is to use list comprehension, as follows.
1 2 3 4 5 6 |
lst1 = [2, 3, 4] lst2 = [2, -2, 3] # zip() pairs the values from the two lists, eg zip([1, 2], [3, 4]) becomes # [(1,3), (2,4) result = [item1*item2 for item1, item2 in zip(lst1, lst2)] print(result) |
Output:
[4, -6, 12]
The zip() function allows us to pair the corresponding values from the two lists, and list comprehension iterates through each of the pairs multiplying them.
The second solution, in this case, is to use map() with the lambda function. That is,
1 2 3 4 5 6 |
lst1 = [2, 3, 4] lst2 = [2, -2, 3] # lambda takes two arguments item1 and item2 and returns the product of the two # map(function, iterable) applies the lambda function to the values from the iterable result = list(map(lambda item1, item2: item1*item2, lst1, lst2)) print(result) |
Output:
[4, -6, 12]
Lastly, you can use NumPy. It makes things a lot easier, as seen in the example below.
1 2 3 4 5 6 |
import numpy as np lst1 = [2, 3, 4] lst2 = [2, -2, 3] # Convert the two lists into NumPy arrays and multiply them with the "*" operator. result = np.array([2, 3, 4])*np.array([2, -2, 3]) print(result) |
Output:
[ 4 -6 12]
Case 3b: Multiplying a List of Numerical Strings with a Float
This is a slight twist of Case 3a. In this case, we have a list of numerical strings, e.g. [“10.2”, “7.6”, “-3.4”], and we want to multiply each value by a float, say 2.1.
As expected, [“10.2”, “7.6”, “-3.4”]*2.1 will yield an error.
1 2 |
lst4 = ["10.2", "7.6", "-3.4"] * 2.1 print(lst4) |
Output:
TypeError: can't multiply sequence by non-int of type 'float'
Solution: We need to convert each value of the list into float or int
We can conveniently convert each list item into a number using the map() function introduced in Case 3a. That can be done as follows.
1 2 3 4 |
lst4 = ["10.2", "7.6", "-3.4"] # map(float, lst4) will take each element of lst4 and convert it to a float. lst4 = list(map(float, lst4)) # you can use "int" instead of "float to convert to integers. print(lst4) |
Output:
[10.2, 7.6, -3.4]
Case 4: Using the input() Function
The input() function in Python 3 and raw_input() function in Python convert every input into a string, even if you insert a number in the prompt. For example,
1 2 3 4 |
salary = input("Enter the salary: ") print(salary) # Check the data type print(type(salary)) |
Output:
Enter the salary: 2719 2719 <class 'str'>
When I executed the code above, I was issued a prompt, and I entered the integer 2719 and hit Enter, but input() converted the input value into a string.
The problem will come when we insert an input value and try to multiply it with a float directly. For example, in the code snippet below, I want to take the input salary and compute an increment (11% of the salary).
1 2 3 4 5 |
salary = input("Enter the salary: ") # At this point, salary is a string, # We are trying to multiply a string with a float, 0.11. increment = salary*0.11 print(increment) |
Output:
TypeError: can't multiply sequence by non-int of type 'float'
Solution: Convert the input value into a float or int
That can be done using float(), int(), or eval() functions, as shown below.
1 2 3 4 5 |
#Convert User input to an integer or float salary = float(input("Enter the salary: ")) # or eval(input("Enter the salary: ")) or int(input("Enter the salary: ")) increment = salary*0.11 print(increment) |
Output:
Enter the salary: 2719 299.09
Note: Use int(input(<prompt>)) only when you are sure that the inputs are integers; otherwise, you will get an error.
Conclusion
This article discussed the causes and solutions to the “TypeError: can’t multiply sequence by non-int of type ‘float'” Error in Python. After going through the guide, you should be able to solve the error and its variants whenever it occurs. The variants of this error include:
- TypeError: can’t multiply sequence by non-int of type ‘float’
- TypeError: can’t multiply sequence by non-int of type ‘string’
- TypeError: can’t multiply sequence by non-int of type ‘list’ (See Case 3a)
- TypeError: can’t multiply sequence by non-int of type tuple (See Case 3a)