Get the App
SLTechnology News&Howtos  ›  Development  › 

Comparison and example Analysis of enumrate and range of Python3

Shulou Source: shulou.com Published: 2022-06-03 00:08:52 09月20日 Update

Python3 enumrate and range comparison and example analysis, I believe that many inexperienced people do not know what to do, so this paper summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.

Comparison and example of enumrate and range of Python3

Preface

In Python, both enumrate and range are commonly used in for loops, the enumrate function is used to loop lists and elements at the same time, while the range () function can generate lists with varying ranges of values, and can be used in for loops are both iterable.

Overview of range

Range is used to generate a list of consecutive or step-sized numeric elements. Here are some basic usage and scenario examples.

Generate a digital sequence

# generate sequence for i in range (0,10): print (I) print ('-'* 40) # generate sequence for j in range (0,21,3): print (j) consisting of digital elements with 0-20 steps (intervals) of 3

Example result:

123456789While 369121518

Use range to traverse the modification list

The most common use scenario of range is to modify the list in a loop, that is, to modify the list by using the index of the list built by range.

L = [1Jing 2 Jing 3 Jing 4 Jing 5] for i in range (len (L)): l [I] = L [I] * * 2 print (L [I])

Example result:

1491625

Overview of enumrate

What if we want to get the index and sequence elements of the sequence? We can use enumrate to iterate over the index and elements of the sequence at the same time.

L = for I, value in enumerate (L): print (I,'-->', value) 0-> 11-- > 22-- > 33-- > 44-- > 5 after reading the above, have you mastered the methods of enumrate and range comparison and example analysis of Python3? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Tags: Examples sequences elements loops numbers indexes analysis content functions at the same time scenarios methods more results problems iterations helpless for this reason preface reason Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker Shulou Technology Linux Apple Redmi