Get the App
SLTechnology News&Howtos  ›  Development  › 

How python implements the slicing operation of the list to allow the index to go out of range

Shulou Source: shulou.com Published: 2022-06-01 11:37:23 09月17日 Update

Today, I would like to share with you how python implements the slicing operation of the list that allows the index to go beyond the scope of the relevant knowledge points, detailed content, clear logic, I believe that most people still know too much about this knowledge, so share this article for your reference, I hope you can get something after reading this article, let's take a look at it.

The list slicing operation allows indexes to be out of range:

Add: the solution to the out-of-bounds index of python3 error list

Python3 error:

IndexError: list index out of rang

This may be because there are no elements or not enough in the list when fetching the index, so it can not be taken out naturally, and it will be reported that it is out of bounds. In this way, we can give this code an if judgment, so that if it has elements or enough elements in it, it will not report an error!

Add: how does Python_ use slices to take a portion of the list and use it

I want to take a part of the list and use it. I can create a slice and specify the index of the first element and the last element I need to use.

Use an example to illustrate the use of slicing # to create a numeric list, which represents that I have 100 pages of articles, and then paging to display mage=list (range (1101)) max_count=len (mage) # use len () to get the length of the list, n=0while n learned in the previous section

Tags: Indexes elements knowledge articles scope content boundaries different large not enough code representatives examples most that is numbers articles methods times more Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei vpn Shulou Information NVidia MySQL