Last modified: Mar 30, 2026 By Alexander Williams
Python Range Float: How to Generate Sequences
Python's range() function is a fundamental tool. It creates sequences of integers. Programmers use it for loops and list generation. But a common question arises. Can you use range() with floating-point numbers?
The short answer is no. The range() function only works with integers. Trying to use a float will cause a TypeError. This article explains why and shows you the best alternatives.
Why Python's Range Function Rejects Floats
The range() function is designed for efficiency. It generates integer sequences on the fly. This is perfect for controlling loops a specific number of times. Using integers avoids precision problems common with floats.
Floating-point arithmetic can be imprecise. This is due to how computers represent decimal numbers. An infinite loop could occur if range() tried to iterate with floats. The stop condition might never be met due to rounding errors.
Python prevents this by design. It raises a clear TypeError if you pass a float. This forces you to think about your sequence's precision from the start.
# This will cause an error
for i in range(0.0, 5.0, 0.5):
print(i)
Traceback (most recent call last):
File "", line 1, in
TypeError: 'float' object cannot be interpreted as an integer
Understanding the Range Function's Limits
To understand this limit, you must know how range() works. It takes start, stop, and step arguments. All three must be integers. The function yields numbers from start up to, but not including, stop.
For a deep dive into its exclusive stop behavior, see our guide on Is Python Range Inclusive? Stop Value Explained.
This integer-only rule is non-negotiable. It is a core part of the function's contract. This ensures predictable and fast performance in loops.
Method 1: Use a List Comprehension
The simplest alternative is a list comprehension. You can use the integer range() to control iterations. Then, calculate the float value inside the loop. This gives you full control over the step size.
# Generate a sequence from 0.0 to 2.0 in steps of 0.2
step = 0.2
num_steps = int((2.0 - 0.0) / step) + 1 # Calculate number of items
float_sequence = [0.0 + i * step for i in range(num_steps)]
print(float_sequence)
[0.0, 0.2, 0.4, 0.6000000000000001, 0.8, 1.0, 1.2, 1.4, 1.5999999999999999, 1.8, 2.0]
Notice the slight floating-point representation errors (e.g., 0.6000000000000001). This is why range() avoids floats. For more on controlling the sequence bounds, read Python Range Inclusive: How to Include the Stop Value.
Method 2: Use NumPy's arange Function
For scientific computing, NumPy is the standard. Its numpy.arange() function works like range() but supports floats. It is fast and efficient for large sequences.
You must first install NumPy using pip install numpy.
import numpy as np
# Generate float sequence with numpy.arange
float_seq = np.arange(0.0, 2.1, 0.2) # Note: stop is 2.1 to include 2.0
print(float_seq)
[0. 0.2 0.4 0.6 0.8 1. 1.2 1.4 1.6 1.8 2. ]
Be cautious with the stop value. Due to floating-point precision, the last value might be excluded unexpectedly. It's often safer to use numpy.linspace().
Method 3: Use NumPy's linspace Function
The numpy.linspace() function is often better than arange() for floats. You specify the start, stop, and the number of samples you want. It evenly spaces the numbers, ensuring the stop value is included.
import numpy as np
# Generate 11 evenly spaced numbers from 0.0 to 2.0
float_seq = np.linspace(0.0, 2.0, 11)
print(float_seq)
[0. 0.2 0.4 0.6 0.8 1. 1.2 1.4 1.6 1.8 2. ]
This method avoids the stop-value ambiguity. It is the most reliable way to get a precise float sequence.
Choosing the Right Tool for the Job
Your choice depends on your needs. For simple, small sequences in pure Python, a list comprehension is fine. For data science or large numerical arrays, use NumPy.
Remember the core principle: range() is for integers. For a complete reference on its proper use, check our Python Range Function Guide: Syntax & Examples.
If you need inclusive floating-point ranges, always lean on numpy.linspace(). It provides the most predictable results.
Conclusion
You cannot use floats directly in Python's range() function. This is a deliberate design choice. It ensures performance and prevents subtle bugs from floating-point errors.
You have several effective alternatives. Use a list comprehension for basic tasks. Use NumPy's arange() or linspace() for advanced numerical work. The linspace() function is generally the safest bet for float sequences.
Understanding this limitation is key to writing robust Python code. You now know how to generate any float sequence you need.