How to Use range() Function in PythonPython Basics


The range() function returns a list of numbers, starting from 0 by default, and increments by 1 (by default), and ends at a specified number.

Example

#!/usr/bin/python3
# -*- coding: UTF-8 -*-

for i in range(5):
    print(i)
print(list(range(5)))

Output:

0
1
2
3
4
[0, 1, 2, 3, 4]

Syntax

range(start, stop, step)

Parameters

Name Description
start An integer number specifying where to start. The default is 0.
stop An integer number specifying where to end.
step An integer number specifying the incrementation. The default is 1.

Return Value

It returns an iterative object.

© 2024 Learnwithgpt.org, all rights reserved. Privacy Policy | Contact Us