# float() argument must be a string or a real number, not X

Last updated: Apr 8, 2024
5 min

## #float() argument must be a string or a real number, not list

The Python "TypeError: float() argument must be a string or a real number, not 'list'" occurs when we pass a list to the `float()` class.

To solve the error, access a specific item in the list and pass the item to the `float()` class, e.g. `float(my_list[0])`.

Here is an example of how the error occurs.

main.py
```Copied!```my_list = ['1.1', '2.2', '3.3']

# โ๏ธ TypeError: float() argument must be a string or a real number, not 'list'
result = float(my_list)
``````

We passed an entire list to the float() class which caused the error.

## #Access the list at a specific index to solve the error

One way to solve the error is to access the list at a specific index and pass the item to the `float()` class.

main.py
```Copied!```my_list = ['1.1', '2.2', '3.3']

result = float(my_list[0])

print(result)  # ๐๏ธ 1.1
``````

We accessed the list at index `0` and used the `float()` class to convert the value to a floating-point number.

Indices are `0` based, so the first item in the list has an index of `0`, and the last has an index of `-1`.

## #Converting all values in a list to floating-point numbers

If you meant to convert all items in the list to floating-point numbers, use a list comprehension.

main.py
```Copied!```my_list = ['1.1', '2.2', '3.3']

new_list = [float(x) for x in my_list]

print(new_list) # ๐๏ธ [1.1, 2.2, 3.3]
``````

List comprehensions are used to perform some operation for every element or select a subset of elements that meet a condition.

We pass each string in the list to the `float()` class to convert each item to an integer.

Alternatively, you can use the `map()` function.

main.py
```Copied!```my_list = ['1.1', '2.2', '3.3']

new_list = list(map(float, my_list))

print(new_list)  # ๐๏ธ [1.1, 2.2, 3.3]
``````

The map() function takes a function and an iterable as arguments and calls the function with each item of the iterable.

We passed the `float()` class to `map()`, so the class gets passed each value in the list as an argument and converts it to a floating-point number.

The last step is to convert the map object to a list.

## #float() argument must be string or real number, not NoneType

The Python "TypeError: float() argument must be a string or a real number, not 'NoneType'" occurs when we pass a `None` value to the `float()` class.

To solve the error, correct the assignment or provide a fallback value.

Here is an example of how the error occurs.

main.py
```Copied!```example = None

# โ๏ธ TypeError: float() argument must be a string or a real number, not 'NoneType'
result = float(example)
``````

We are passing a `None` value to the `float()` class which causes the error.

## #Common sources of None in Python

The most common sources of `None` values are:

1. Having a function that doesn't return anything (returns `None` implicitly).
2. Explicitly setting a variable to `None`.
3. Assigning a variable to the result of calling a built-in function that doesn't return anything.
4. Having a function that only returns a value if a certain condition is met.

## #Providing a default value if the variable is None

One way to solve the error is to provide a fallback value, e.g. `0` if the variable stores `None`.

main.py
```Copied!```example = None

result = float(example or 0)

print(result)  # ๐๏ธ 0.0
``````

The expression checks if the example variable stores a falsy value, in which case `0` is returned.

## #Functions that don't return a value return None

Functions that don't explicitly return a value return `None`.

main.py
```Copied!```# ๐๏ธ this function returns None
def get_str():
print('3.14')

# โ๏ธ TypeError: float() argument must be a string or a real number, not 'NoneType'
result = float(get_str())
``````

You can use a return statement to return a value from a function.

main.py
```Copied!```def get_str():
return '3.14'

result = float(get_str())

print(result)  # ๐๏ธ 3.14
``````

## #Checking whether a variable doesn't store a None value

Use an `if` statement if you need to check whether a variable doesn't store a `None` value before passing it to the `float()` class.

main.py
```Copied!```example = None

if example is not None:
result = float(example)
print(result)
else:
# ๐๏ธ this runs
print('variable stores a None value')
``````

Alternatively, you can reassign the variable to a fallback value.

main.py
```Copied!```example = None

if example is None:
example = 0

result = float(example)

print(result)  # ๐๏ธ 0.0
``````

If the variable is equal to `None`, the `if` block runs and sets it to `0`.

## #A function that only returns a value if a condition is met

Another common cause of the error is having a function that returns a value only if a condition is met.

main.py
```Copied!```def get_num(a):
if a > 15:
return a

my_num = get_num(10.5)
print(my_num)  # ๐๏ธ None
``````

The `if` block in the `get_num` function is only run if the passed-in number is greater than `15`.

In all other cases, the function doesn't return anything and ends up implicitly returning `None`.

To solve the error, you either have to check if the function didn't return `None` or return a default value if the condition is not met.

main.py
```Copied!```def get_num(a):
if a > 15:
return a

return 0  # ๐๏ธ Return a fallback if condition not met

my_num = get_num(10.5)
print(my_num)  # ๐๏ธ 0
``````

Now the function is guaranteed to return a value regardless of whether the condition is met.

## #float() argument must be string or real number, not method

The Python "TypeError: float() argument must be a string or a real number, not 'method'" occurs when we pass a method to the `float()` class.

To solve the error, make sure to call the method with parentheses, e.g. `my_method()`.

Here is an example of how the error occurs.

main.py
```Copied!```class MyClass():
def get_str(self):
return '3.14'

m = MyClass()

# โ๏ธ TypeError: float() argument must be a string or a real number, not 'method'
result = float(m.get_str) # ๐๏ธ Forgot to call the method
``````
We forgot to call the method with parentheses, e.g. `m.get_str()`, so our code actually tries to convert a method to a floating-point number.

## #Call the method to solve the error

To solve the error, make sure to call the method.

main.py
```Copied!```class MyClass():
def get_str(self):
return '3.14'

e = MyClass()

# โ Call the method() with parentheses
result = float(e.get_str())

print(result)  # ๐๏ธ 3.14
``````

We used parentheses to invoke the method, so now our code converts the return value of the method to a float.

If your method takes arguments, make sure to provide them when calling it, e.g. `my_obj.my_method(10, 20)`.

## #Checking what type the variable stores

If you aren't sure what type of object a variable stores, use the `type()` class.

main.py
```Copied!```class MyClass():
def get_str(self):
return '3.14'

e = MyClass()

print(type(e.get_str))  # ๐๏ธ <class 'method'>
print(callable(e.get_str))  # ๐๏ธ True

my_float = 3.14

print(type(my_float)) # ๐๏ธ <class 'float'>
print(isinstance(my_float, float)) # ๐๏ธ True
``````

The type class returns the type of an object.

The isinstance() function returns `True` if the passed-in object is an instance or a subclass of the passed-in class.

The callable function takes an object as an argument and returns `True` if the object appears callable, otherwise, `False` is returned.

If the `callable()` function returns `True`, it is still possible that calling the object fails, however, if it returns `False`, calling the object will never succeed.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
You can use the search field on my Home Page to filter through all of my articles.