メインコンテンツにスキップ

Python range() Function Tutorial

Learn about the Python range() function and its capabilities with the help of examples.
· 7 分 読む

AIで探求

The range() function is a very popular and widely used function in Python, especially when you are working with for loops, and sometimes also with while loops. The range() function is worth knowing and mastering because doing so will open a lot of doors: range() is used in everything from controlling the flow of a program to looping through data sets that you are using for data analysis. 

If you are just getting started in Python and would like to learn more, take DataCamp's Introduction to Data Science in Python course.

What is the range() Function in Python?

The range() function returns a sequence of numbers and is immutable, meaning its value is fixed. The range() function takes one or at most three arguments, namely the start and a stop value along with a step size.

range() was introduced in Python3. In Python2, a similar function, xrange(), was used, which had somewhat different behavior. Among other things, xrange() returned a generator object and consumed less memory, while range(), on the other hand, returns a list or sequence of numbers.

Part of the reason the range() function is useful is because it only stores the