Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Full lesson preview

Iterate With Range

Generate sequences of integers using range parameters and loops.

Python practice10 minLoops & IterationBeginnerLast updated March 15, 2026

Problem statement

Create a function iterate_with_range(start, stop, step=1) that returns a list of integers produced by iterating from start to stop using step, similar to Python's built-in range behavior. If step is zero, return an empty list instead of raising an exception. Use a loop to build the resulting list.

Task

Implement a function that returns the list of integers produced by Python's range(start, stop, step), handling a zero step gracefully.

Examples

Positive step

Input

iterate_with_range(0, 5)

Output

[0, 1, 2, 3, 4]

Numbers from 0 up to (but not including) 5 with step 1.

Input format

A function call iterate_with_range(start, stop, step) where start and stop are integers and step is an integer (optional, defaults to 1).

Output format

Return a list of integers generated by stepping from start to stop using step. If step is 0, return an empty list.

Constraints

-10**9 <= start, stop <= 10**9, step is any integer. The resulting list length will be reasonable for typical inputs in exercises.

Samples

Sample 1

Input

iterate_with_range(5, 0, -2)

Output

[5, 3, 1]

Start at 5, step -2, stop before reaching 0.