Last modified: Mar 30, 2026 By Alexander Williams

Python Range with Float: Generate Sequences

Python's range() function is a fundamental tool. It generates sequences of integers. Developers often ask if it can handle floats. The short answer is no. This article explains why and shows you the best alternatives.

Understanding the Python Range Function

The range() function creates an immutable sequence of numbers. It is commonly used in for loops. You can read more about its core behavior in our Python Range Function Guide.

It accepts one to three integer arguments: start, stop, and step. The sequence includes the start value but excludes the stop value. For a deeper dive into this behavior, see our article on Is Python Range Inclusive.


# Basic range() examples
print(list(range(5)))
print(list(range(2, 8)))
print(list(range(0, 10, 2)))
    

[0, 1, 2, 3, 4]
[2, 3, 4, 5, 6, 7]
[0, 2, 4, 6, 8]
    

Why Range() Does Not Support Floats

If you try to use a float with range(), you will get a TypeError. This is a deliberate design choice in Python.

The function is optimized for integer sequences and loop control. Using floats would introduce complexity and potential for floating-point rounding errors.

Floating-point arithmetic is not perfectly precise. This could lead to unexpected sequence lengths or missing values.


# This will cause an error
try:
    for i in range(0.0, 5.0, 0.5):
        print(i)
except TypeError as e:
    print(f"Error: {e}")
    

Error: 'float' object cannot be interpreted as an integer
    

Method 1: Using NumPy's arange Function

The most powerful alternative is NumPy's arange() function. It works similarly to range() but supports floating-point steps. You must install the NumPy library first.


import numpy as np

# Generate a sequence from 0.0 to 2.0 in steps of 0.5
float_sequence = np.arange(0.0, 2.1, 0.5)
print(float_sequence)
print(type(float_sequence))
    

[0.  0.5 1.  1.5 2. ]
<class 'numpy.ndarray'>
    

Note the stop value. To include 2.0, we used 2.1 as the stop argument. This avoids issues with floating-point precision. It's a key difference from the standard Python Range Inclusive pattern.

Method 2: Using a List Comprehension

You can create a float sequence without external libraries. Use a list comprehension with a calculated step. This method gives you a standard Python list.


start = 1.0
stop = 3.0
step = 0.25

# Calculate number of steps and generate list
num_steps = int((stop - start) / step)
float_list = [start + i * step for i in range(num_steps)]

print(float_list)
    

[1.0, 1.25, 1.5, 1.75, 2.0, 2.25, 2.5, 2.75]
    

This approach is simple and built-in. However, you must manage the stop value logic yourself to avoid off-by-one errors.

Method 3: Using a Custom Generator Function

For reusable and memory-efficient code, write a custom generator. It yields values on the fly, similar to range(). This is ideal for large sequences.


def float_range(start, stop, step):
    """A generator that yields a sequence of floats."""
    current = start
    while current < stop:  # Uses less-than to exclude stop
        yield round(current, 10)  # Optional rounding for clarity
        current += step

# Use the custom generator
for num in float_range(0, 1, 0.1):
    print(f"{num:.1f}", end=" ")
    

0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9
    

This method gives you full control. You can modify it to be inclusive of the stop value if needed.

Choosing the Right Method

Your choice depends on your project's needs.

Use NumPy's arange for scientific computing or when you need array operations. It is fast and handles edge cases well.

Use a list comprehension for simple scripts where you don't want extra dependencies. It's quick to write and understand.

Use a custom generator for large datasets or when you need specific behavior. It is memory efficient and customizable.

For more on generating sequences, explore our guide on Python Range Float.

Conclusion

Python's built-in range() function does not support float arguments. This is a design feature to ensure predictability and performance with integers.

To create sequences with floats, you have excellent options. Use NumPy for heavy-duty work. Use list comprehensions for simplicity. Or build a custom generator for control.

Understanding this limitation is key to writing robust Python code. By using the right tool for floating-point sequences, you avoid errors and write clearer, more efficient programs.