Julia Ranges Tutorial
last modified November 18, 2026
A range is an object that describes a sequence of values without storing every element in memory. It records a start, a step, and an end, and produces values on demand when iterated or indexed. Ranges are used constantly in Julia for loops, array slicing, and generating regular grids of numbers.
This tutorial covers the main range types — UnitRange,
StepRange, StepRangeLen, and LinRange — as
well as the range function, indexing, and collect. The
colon operator is introduced briefly in Julia
operators; here we go into detail. All examples were verified with Julia 1.13.4.
Unit Ranges
The simplest range is written with the colon operator and steps by one. It has
type UnitRange and includes both endpoints.
r = 1:5 println(r) println(typeof(r)) println(collect(r))
The expression 1:5 prints as 1:5, confirming that the
range is stored compactly rather than as a list. The function
collect materializes the elements into an array, showing the values
one through five.
$ julia main.jl
1:5
UnitRange{Int64}
[1, 2, 3, 4, 5]
Stepped Ranges
A three-part expression start:step:stop creates a
StepRange. The middle value is the increment between successive
elements.
r = 1:2:9 println(r) println(typeof(r)) println(collect(r))
The range runs from 1 to 9 in steps of 2, giving the odd numbers 1, 3, 5, 7, and
9. Its type is StepRange with two integer parameters: one for the
values and one for the step.
$ julia main.jl
1:2:9
StepRange{Int64, Int64}
[1, 3, 5, 7, 9]
Descending Ranges
The step may be negative, which produces a range that counts down. The same three-part syntax is used.
r = 5:-1:1 println(r) println(typeof(r)) println(collect(r))
Here the step is -1, so the values go from 5 down to 1. The type is
still a StepRange; only the sign of the step differs from the
previous example.
$ julia main.jl
5:-1:1
StepRange{Int64, Int64}
[5, 4, 3, 2, 1]
The range Function
The range function is a flexible alternative to the colon syntax. It
can build a range from a start and a length using the length keyword,
even when the resulting step is fractional.
r = range(1, 10, length=5) println(r) println(typeof(r)) println(collect(r))
Five evenly spaced values from 1 to 10 have a step of 2.25, so Julia produces a
floating-point StepRangeLen that prints as
1.0:2.25:10.0. Notice that the endpoints are shown as floats even
though the arguments were integers.
$ julia main.jl
1.0:2.25:10.0
StepRangeLen{Float64, Base.TwicePrecision{Float64}, Base.TwicePrecision{Float64}, Int64}
[1.0, 3.25, 5.5, 7.75, 10.0]
LinRange
A LinRange also describes evenly spaced values, but it stores the
two endpoints and the number of elements instead of a step. This representation
is exact at both ends.
r = LinRange(0, 1, 5) println(r) println(typeof(r)) println(collect(r))
The call asks for five values between 0 and 1, inclusive. The result prints as
LinRange{Float64}(0.0, 1.0, 5) and collects to the quarters 0.0,
0.25, 0.5, 0.75, and 1.0.
$ julia main.jl
LinRange{Float64}(0.0, 1.0, 5)
LinRange{Float64, Int64}
[0.0, 0.25, 0.5, 0.75, 1.0]
StepRangeLen
A StepRangeLen is what Julia builds for floating-point ranges with a
given step. It keeps the start, step, and length, and uses extra precision
internally to reduce rounding errors.
r = range(0.0, 1.0, step=0.25) println(r) println(typeof(r)) println(collect(r))
The step keyword fixes the increment at 0.25, producing five values
from 0.0 to 1.0. The printed type mentions TwicePrecision, the
internal mechanism that makes floating-point range indexing more accurate.
$ julia main.jl
0.0:0.25:1.0
StepRangeLen{Float64, Base.TwicePrecision{Float64}, Base.TwicePrecision{Float64}, Int64}
[0.0, 0.25, 0.5, 0.75, 1.0]
Indexing and Iteration
Ranges can be indexed like arrays, and they can be iterated in a
for loop. The functions first, last,
length, and step report their properties.
r = 1:2:9 println(r[3]) println(first(r), " ", last(r)) println(length(r), " ", step(r)) println(join(r, " "))
The third element of 1:2:9 is 5. Its first and last values are 1 and
9, it has 5 elements, and its step is 2. The last line joins the iterated values
into a single line of text.
$ julia main.jl 5 1 9 5 2 1 3 5 7 9
Membership and Empty Ranges
The in operator tests whether a value belongs to a range, and a
range whose start lies past its end is simply empty. A range also compares equal
to an array with the same elements.
println(3 in 1:5, " ", 6 in 1:5) println(length(2:1), " ", collect(2:1)) println(1:5 == collect(1:5))
The number 3 is in 1:5 while 6 is not. The range 2:1
has length zero and collects to an empty Int64 vector. The final
comparison is true: a range and an array with identical elements are equal.
$ julia main.jl true false 0 Int64[] true
Ranges as Array Indices
One of the most common uses of a range is selecting part of an array. A range of indices returns a copy of the selected elements, and a stepped range selects every nth element.
arr = [10, 20, 30, 40, 50] println(arr[2:4]) println(arr[1:2:5]) println(arr[end-1:end])
The first slice takes elements 2 through 4. The second uses a step of 2 to take
elements 1, 3, and 5. The last line uses end as a placeholder for the
final index, selecting the last two elements. Array slicing is also shown in
Julia arrays.
$ julia main.jl [20, 30, 40] [10, 30, 50] [40, 50]
Ranges in Calculations
Many functions accept any iterable, so a range can be passed directly without being collected first. Broadcasting applies an operation to each element.
println(sum(1:10)) println(collect(1:3) .+ 10) println(length(1.0:0.5:3.0))
The sum of the integers 1 through 10 is 55. Broadcasting the addition of 10 with
the dot syntax produces [11, 12, 13]. The last line counts the
elements of a floating-point stepped range, which contains 1.0, 1.5, 2.0, 2.5,
and 3.0. Broadcasting is covered in Julia
operators.
$ julia main.jl 55 [11, 12, 13] 5
Best Practices for Ranges
A few guidelines help when choosing and using ranges.
- Use
:for unit steps: it is the shortest and clearest form for consecutive integers. - Prefer
rangewhen you know the length: thelengthkeyword avoids off-by-one mistakes. - Collect only when you need an array: a range is cheaper to store and iterate.
- Watch floating-point steps: use
LinRangeorrangewithlengthwhen exact endpoints matter.
Source
Julia Arrays and Ranges documentation
Julia Colon Operator documentation
In this article we have worked with ranges in Julia: unit and stepped ranges,
descending ranges, the range function, LinRange and
StepRangeLen, indexing, iteration, membership, and array slicing.
Arrays are covered in Julia arrays, iteration in
Julia iterators, and the colon operator in
Julia operators.
Author
List all Julia tutorials.