Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

TensorFlow · Function

tf.range

Creates a one-dimensional Tensor containing a numeric sequence from start toward, but excluding, limit. The step is delta, and TensorFlow infers the result dtype unless dtype is specified.

How it works

tf.range builds a one-dimensional Tensor whose values begin at start and advance by delta until reaching, but excluding, limit. With the two-argument form, start is interpreted as the exclusive limit and the sequence begins at zero; TensorFlow infers the element type unless dtype is supplied.

Syntax

tf.range(limit, delta=1, dtype=None, name='range')
tf.range(start, limit, delta=1, dtype=None, name='range')

Parameters

NameTypeDescription
startTensor or compatible scalarA scalar 0-D Tensor. It is the first sequence value when limit is provided; otherwise it is treated as the exclusive limit and start becomes 0.
limitTensor or compatible scalar, optionalA scalar 0-D Tensor representing the exclusive upper limit. If omitted, the value supplied as start is used as the limit.
deltaTensor or compatible scalarA scalar 0-D Tensor specifying how much to add to start for each element. Defaults to 1.
dtypetf.DType, optionalThe type of the resulting Tensor's elements.
namestring, optionalThe operation name. Defaults to "range".

Returns

A one-dimensional Tensor with element type dtype.

Key points

  • The function constructs a numeric sequence as a Tensor.
  • When limit is supplied, the sequence starts at zero unless start is explicitly given.
  • delta controls the fixed increment between neighboring values.
  • If delta is omitted, consecutive values differ by one.
  • The result is a rank-one Tensor whose elements use the selected dtype.
  • Its interval-and-step behavior corresponds closely to NumPy's arange operation.

Common mistakes

  • The limit value is not included in the result.
  • Mixed or unintended input types can produce an unexpected tensor dtype; pass dtype when the element type must be controlled.
  • Omitting limit changes the meaning of start: it becomes the exclusive upper bound, not the first value.

Example: Create an integer sequence

import tensorflow as tf

values = tf.range(3, 18, 3)
print(values.numpy())  # [ 3  6  9 12 15]

Creates an integer tensor containing values from 3 through 15 in steps of 3.

When to use it

Create index sequences

Generate integer indices for tensor slicing, lookup tables, or iteration-like graph logic.

Construct stepped values

Build regularly spaced numeric values when the endpoint should not be included.

Control numeric element type

Create floating-point sequences by supplying floating-point inputs or an explicit dtype.

Interview questions

QWhat does tf.range produce and how are its bounds interpreted?

tf.range returns a rank-1 Tensor containing a numeric sequence. It starts at start, increments by delta, and stops before limit; the element type is inferred unless dtype is provided.

Follow-up: How does the one-argument form work? The supplied argument is treated as limit, start defaults to zero, and the limit remains exclusive.

QWhat are the supported call forms?

The signature supports either tf.range(limit, delta=1, dtype=None, name='range') or tf.range(start, limit, delta=1, dtype=None, name='range'). In the one-argument form, start defaults to zero.

Follow-up: What does a negative delta imply? The sequence advances downward, so the bounds and step must be oriented consistently for values to be produced.

QWhat are the parameter and return constraints?

start, limit, and delta are scalar, zero-dimensional Tensor-like inputs. limit is the exclusive upper bound, delta defaults to one, and name defaults to 'range'.

Follow-up: What shape does the result have? It is always a one-dimensional Tensor, with element type dtype.

  • tf.linspace
  • tf.range
  • tf.ragged.range
  • tf.compat.v1.range

Source: tf.range | TensorFlow v2.16.1. Checked 2026-10-15.