Thursday, January 16, 2025
HomeProgrammingPython range() function

Python range() function

The range() function in Python generates a sequence of numbers and is commonly used in loops. It takes up to three arguments:

1. range(stop): Generates numbers from 0 up to, but not including, stop.

range(5) # Output: 0, 1, 2, 3, 4

See also  Implementation of Stack in JavaScript

2. range(start, stop): Generates numbers from start to stop (exclusive).

range(2, 6) # Output: 2, 3, 4, 5

3. range(start, stop, step): Generates numbers from start to stop, incrementing by step.

range(1, 10, 2) # Output: 1, 3, 5, 7, 9

See also  How can I find the index for a given item in a list in Python?

range() produces an immutable sequence, which can be converted to a list if needed. It is memory efficient, as it generates numbers on demand.

RELATED ARTICLES
0 0 votes
Article Rating

Leave a Reply

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
- Advertisment -

Most Popular

Recent Comments

0
Would love your thoughts, please comment.x
()
x