range function in python | range function in python | detailed explanation of range() function | usage of range(len()) in Python


range() is a built-in function in Python that generates a sequence of integers. Specifically, it is defined as follows:

range([start], [stop], [step])

start, stop, and step represent the start value, end value, and step size of the sequence, respectively. start and step are optional parameters and default to 0 and 1 if not specified.

1. Range() passes different parameters

1. When passing a parameter

When only one parameter is passed in, it means to generate a sequence of integers starting from 0 and smaller than the parameter. For example:

>>> range(5)
#range(0, 5)

In this example, range(5) means to generate a sequence of integers starting from 0 and less than 5. It should be noted that the range() function actually returns a range type object, not a list, but it can be used like a list in most cases.

2. When passing two parameters

When two parameters are passed in, it means to generate a sequence of integers starting from start and less than stop. For example:

>>> range(2, 5)
range(2, 5)

In this example, range(2, 5) means to generate a sequence of integers starting from 2 and less than 5.

3. When passing three parameters

When three parameters are passed in, it means to generate an integer sequence starting from start, increasing step each time, and less than stop. For example:

>>> range(0, 10, 2)
range(0, 10, 2)

range(0, 10, 2) means to generate an integer sequence starting from 0 and increasing by 2 each time, less than 10.

It should be noted that when step is a positive number, stop must be greater than start, otherwise no number will be generated. For example:

>>> range(5, 2)
range(5, 2)

In this example, no numbers can be generated due to start=5, stop=2, step=1.

Also, when step is negative, start must be greater than stop to generate a sequence. For example:

>>> range(5, 2, -1)
range(5, 2, -1)

In start=5, stop=2, step=-1, so the generated sequence is 5, 4, 3.

In practical applications, the range() function has many common usages. Next, various uses of the range() function will be introduced in detail, including:

  1. Use range() to build a for loop
  2. Usage of range(len()) when traversing a list
  3. Use range() to generate a fixed-length arithmetic sequence
  4. Use range() to generate a sequence of integers in reverse order

2. Use range() to build a for loop

One of the most common uses of the range() function is to build iterators for for loops. For example, the following code demonstrates how to use the range() function to build a loop from 0 to 4:

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

Output result:

0
1
2
3
4

range(5) returns a sequence of integers containing 5 elements, and each element is used in the for loop to assign a value to the variable i and output.

It should be noted that when using the range() function, the value range of the loop variable i is from 0 to n-1. Therefore, if you need to cycle from 1, you can add one:

for i in range(1, 6):
    print(i)
1
2
3
4
5

3. Use range(len()) when traversing the list

When working with lists, it is sometimes necessary to iterate over the indices and values ​​of the list at the same time. At this time, you can use the range(len()) function to generate an integer sequence with the same length as the list, and use the elements of the sequence as the index of the list in the loop. For example, the following code demonstrates how to iterate over a list and output its index and value:

fruit_list = ['apple', 'banana', 'orange']
for i in range(len(fruit_list)):
    print('Index:', i, 'Value:', fruit_list[i])

Output result:

Index: 0 Value: apple
Index: 1 Value: banana
Index: 2 Value: orange

In this example, range(len(fruit_list)) returns an integer sequence [0, 1, 2] containing 3 elements. When looping, use the element i of the sequence as the index of fruit_list, and access the list through fruit_list[i] element.

3.1 Traverse the list directly using the for loop

Note that while it is possible to manipulate lists by iterating over their indices and values, iterators are generally preferred in Python. For example, a for loop can be used to directly iterate over each element of a list:

for fruit in fruit_list:
    print(fruit)

The output is as follows

apple
banana
orange

4. Use range() to generate a fixed-length arithmetic sequence

In addition to using the range() function in the for loop, it can also be used to generate a fixed-length arithmetic sequence. Specifically, this can be achieved in Python using list comprehensions combined with the range() function. The following code demonstrates how to generate an arithmetic sequence of length 5, with 2 as the first term, and a tolerance of 3:

a = [2 + 3 * i for i in range(5)]
print(a)

Output result:

[2, 5, 8, 11, 14]

range(5) returns a sequence of 5-element integers [0, 1, 2, 3, 4], then uses a list comprehension to evaluate each element i and store the result in list a.

It should be noted that since the range() function starts from 0 by default, the first item needs to be added to the calculation. If you want to use other values ​​as the first item, you can pass in the start parameter:

a = [10 + 3 * i for i in range(5)]
print(a)

Output result:

[10, 13, 16, 19, 22]

5. Use range() to generate a sequence of integers in reverse order

In addition to the sequence of integers in positive order, it is sometimes necessary to generate a sequence of integers in reverse order (reverse order). In Python, you can use step=-1 to achieve this function. For example, the following code demonstrates how to generate a reversed sequence from 9 to 0:

for i in range(9, -1, -1):
    print(i)
9
8
7
6
5
4
3
2
1
0

In this example, range(9, -1, -1) returns an integer sequence [9, 8, 7, 6, 5, 4, 3, 2, 1, 0] containing 10 elements, which is then traversed in the for loop the sequence and output each element.

It should be noted that when generating a reverse sequence, the stop parameter must be smaller than the start parameter, and the step must be a negative number. If the condition is not met, the range() function will return an empty sequence.

Guess you like

Origin blog.csdn.net/m0_58857684/article/details/130844579