step
.. step(r)
Get the step size of a :obj:`Range` object.
Examples
In the Julia programming language, the function step(r)
Get the step size of a Range
object.
julia> r = 1:2:10;
julia> step(r)
2
This function returns the step size of the Range
object r
. It is particularly useful when working with ranges that have a non-default step size.
Common examples of its use:
-
Check the step size of a range:
julia> range1 = 0:0.5:2.5; julia> step(range1) 0.5
This example returns the step size of the range
range1
. -
Use the step size in a loop:
julia> for i in 1:step(range1):10 println(i) end
In this example, the step size of
range1
is used in a loop to iterate over the range with a custom step size. - Calculate the length of a range using the step size:
julia> range2 = 10:-1:1; julia> length(range2) / step(range2) 10.0
Here, the length of
range2
is divided by the step size to obtain the number of steps taken within the range.
Common mistake example:
julia> r = 10:-2:1;
julia> step(r)
ERROR: MethodError: no method matching step(::StepRangeLen{Int64,Base.TwicePrecision{Int64},Base.TwicePrecision{Int64}})
In this example, the step
function is applied to a range that has a non-integer step size. The step
function expects an integer step size for the range.
See Also
User Contributed Notes
Add a Note
The format of note supported is markdown, use triple backtick to start and end a code block.