Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Range Function in Python | Loops in Python
Introduction to Python
course content

Course Content

Introduction to Python

Introduction to Python

1. First Acquaintance with Python
2. Variables and Types in Python
3. Conditional Statements in Python
4. Other Data Types in Python
5. Loops in Python
6. Functions in Python

book
Range Function in Python

To iterate over indices instead of elements, the range() function is essential. It generates a sequence of numbers and returns a range object. This function accepts one, two, or three numeric arguments. With a single argument n, range(n) produces integers from 0 up to, but not including, n.

For example, range(5) generates the numbers 0 to 4.

123
# Range with one argument for i in range(5): print(i, end = ' ')
copy

When range() is given two arguments, n and m, it generates integers starting from n up to, but not including, m.

For example, range(5, 10) produces the numbers 5 through 9.

123
# Range with two arguments for i in range(5, 10): print(i, end = ' ')
copy

When range() receives three arguments n, m, and s it generates numbers starting from n, up to but not including m, incrementing by s each time.

For example, range(10, 30, 5) produces the sequence 10, 15, 20, 25.

123
# Range with three arguments for i in range(10, 30, 5): print(i, end = ' ')
copy
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 5. Chapter 5
We're sorry to hear that something went wrong. What happened?
some-alt