Movatterモバイル変換


[0]ホーム

URL:


Python Tutorial

Python range() Function



ThePython range() function returns an immutable sequence of numbers within the specified range. This function is used to iterate or loop through a particular code block till a given number of times.

Therange() function starts iterating from 0, increments by 1 by default, and stops one position before the specified end position.

This method is one of thebuilt-in functions and does not require any module to import.

Syntax

Following is the syntax of the Pythonrange() function −

range(start, stop, step)

Parameters

The Pythonrange() function accepts three parameters −

  • start − It is an optional parameter which specify the starting position. It must be an integer number from which

  • stop − This parameter represents the stop position.

  • step − It specifies the required number of increment between sequence.

Return Value

The Pythonrange() function returns a new immutable sequence of numbers .

range() Function Examples

Practice the following examples to understand the use ofrange() function in Python:

Example: Use of range() Function

If you pass a single argument to the range() function, it is considered the stop position. Therefore, the below Python code will display a sequence of numbers from 0 to one less than the specified number. With each iteration, the sequence will be incremented by 1.

print("The number in the given range:")for index in range(10):   print(index, end=" ")

When you run above program, it produces following result −

The number in the given range:0 1 2 3 4 5 6 7 8 9

Example: range() Function With Start and End Value

When you only pass two arguments to the range() function, the first one is considered the start position and the other one is the stop. In the code below, you display a sequence of numbers from 11 to 20.

print("The number in the given range:")for index in range(11, 21):   print(index, end=" ")

Following is an output of the above code −

The number in the given range:11 12 13 14 15 16 17 18 19 20

Example: Getting Returned Values Generated by range()

It is also possible to assign the value of range() function to anothervariable as demonstrated in the below example.

rangeVar = range(5, 11)for index in rangeVar:  print(index)

Output of the above code is as follows −

5678910

Example: range() Function With Start, End, and Step Values

When three arguments are passed to therange() function, it signifies the start, end, and step value respectively. In the code below, you are printing even number between 12 to 20. Notice that the output is getting incremented by value 2.

print("Even numbers between given range:")for index in range(12, 20, 2):    print(index)

Following is an output of the above code −

Even numbers between given range:12141618

Example: range() Function With Negative Step Value

To decrement the sequence of numbers, you need to pass a negative integer value for the "step" parameter as demostrated in the below example.

print("Even numbers in decreasing order:")for index in range(20, 11, -2):    print(index)

Following is an output of the above code −

Even numbers in decreasing order:2018161412
python_built_in_functions.htm
Print Page
Advertisements

[8]ページ先頭

©2009-2025 Movatter.jp