🔥Limited Offer: Get 50% OFFon AI & Full Stack Courses🔥
Back to Python Notes
Topic #88

Python Range


Python range

The built-in range() function returns an immutable sequence of numbers, commonly used for looping a specific number of times.

This set of numbers has its own data type called range.

Note: Immutable means that it cannot be modified after it is created.


Creating ranges

The range() function can be called with 1, 2, or 3 arguments, using this syntax:

<p>
<code class="pythonHigh">range(<i>start</i>, <i>stop</i>, <i>step</i>)
</code>
</p>

Call range() With One Argument

If the range function is called with only one argument, the argument represents the stop value.

The start argument is optional, and if not provided, it defaults to 0.

range(10) returns a sequence of each number from 0 to 9. (The start argument, 0 is inclusive, and the stop argument, 10 is exclusive).

Example

x = range(10)

Call range() With Two Arguments

If the range function is called with two arguments, the first argument represents the start value, and the second argument represents the stop value.

range(3, 10) returns a sequence of each number from 3 to 9:

Example

x = range(3, 10)

Call range() With Three Arguments

If the range function is called with three arguments, the third argument represents the step value.

The step value means the difference between each number in the sequence. It is optional, and if not provided, it defaults to 1.

range(3, 10, 2) returns a sequence of each number from 3 to 9, with a step of 2:

Example

x = range(3, 10, 2)

Using ranges

Ranges are often used in for loops to iterate over a sequence of numbers.

Example

for i in range(10):
  print(i)

Using List to Display Ranges

The range object is a data type that represents an immutable sequence of numbers, and it is not directly displayable.

Therefore, ranges are often converted to lists for display.

Example

print(list(range(5)))
print(list(range(1, 6)))
print(list(range(5, 20, 3)))

Slicing Ranges

Like other sequences, ranges can be sliced to extract a subsequence.

Example

r = range(10)
print(r[2])
print(r[:3])

Note: The first print statement returns the value at index 2, and the second print statement returns a new range object, from index 0 to 3.


Membership Testing

Ranges support membership testing with the in operator.

Example

r = range(0, 10, 2)
print(6 in r)
print(7 in r)

The return value is True when the number is present in the range, and False when it is not.


Length

Ranges support the len() function to get the number of elements in the range.

Example

r = range(0, 10, 2)
print(len(r))

Want to go beyond the notes?

Join CodingNow 2.0's Python course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

Python Range – FAQs

Quick answers about learning Python Range in Python.

This free note from CodingNow 2.0 explains Python Range in Python — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Python topic on CodingNow 2.0, including Python Range, is 100% free with no signup required.
With focused practice, most students grasp Python Range in 1–3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) — expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now