Member-only story

Understanding the range() Function in Python

Allwin Raju
2 min read5 days ago

--

Photo by Hitesh Choudhary on Unsplash

The range() function in Python is used to generate a sequence of numbers. It's commonly used in loops for iteration.

Read for free: https://allwin-raju.medium.com/understanding-the-range-function-in-python-bd7f4b14d36f?sk=a1f1e6ab1c1c908b9714d354cec632bf

Syntax of range()

range(start, stop, step)
  • start (optional): The beginning of the sequence (default is 0).
  • stop (required): The upper bound (not included in the range).
  • step (optional): The increment (default is 1).

1. Using range() with One Argument (stop)

Generates numbers from 0 to stop - 1.

for i in range(5):
print(i, end=" ")

# Output: 0 1 2 3 4

2. Using range() with Two Arguments (start, stop)

Generates numbers from start to stop - 1.

for i in range(2, 6):
print(i, end=" ")

# Output: 2 3 4 5

3. Using range() with Three Arguments (start, stop, step)

The step determines the increment between numbers.

for i in range(1, 10, 2):
print(i…

--

--

No responses yet