Slice breaks in Python

Source: Internet
Author: User

Recently learning Python, feeling the slicer is a bit difficult to understand. I looked up some information on the Internet and made a summary.

To understand a slicer, you first need to know that the array starts at 0, and that the last slice is-1.

Let's first define the array a=[1,2,3,4,5]

Several formats for slices:

1. A[b:c] Understanding: Starting with the array value B (including) until the end of the array value C (not included)

Example: Print A[2:4] A of 2:4 is 3,4,5, because the last value of the slice is not included, so the output is [3,4]

2. A[b:] Understanding: Start with the index value B (including) to all subsequent values (the array has how many values are to all values)

Example: Print a[1:] A 1th of the array is, to the back all values, so the output is [2,3,4,5]

3. A[:b] Understanding: From the No. 0 value of the index to B (not included)

Example: Print A[:3] Index No. 0 is 1, 3rd is 4 (not included), so the output is [three-in-one]

4. A[b:-1] Understanding: From Start B to index last (not included)

Example: Print A[2:-1] The second of the index is 3, the last one is 5 (not included), the output is [3,4]

5. Assignment of slices

I. Assigning a new value to a slice

Example: a[0,2] = [3,4] output a result [3,4,3,4,5]

Two. Assign a value greater than the length of the slice

Example: a[1:4]=[1,2] output [1,1,2,5] can be found, the length is smaller

Slice breaks in Python

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.