Getting started with the zero-learning Python (iii) sequence _python

Source: Internet
Author: User

Sequence sequence

A sequence (sequence) is a set of ordered elements.

(Strictly speaking, it's a collection of objects, but given that we haven't introduced the "object" concept for the time being)

A sequence can contain one or more elements, or it can have no elements.

The basic data types that we have previously described can be used as elements of a sequence. The element can also be another sequence, and other objects that we'll be introducing later.

There are two kinds of sequences: tuple (fixed-value table; translation to tuple) and list (table)

Copy Code code as follows:

>>>S1 = (2, 1.3, ' love ', 5.6, 9, False) # S1 is a tuple
>>>S2 = [True, 5, ' smile '] # s2 is a list
>>>print S1,type (S1)
>>>print S2,type (S2)

The main difference between tuple and list is that once established, the elements of the tuple can no longer be changed, and the individual elements of the list may be changed.

A sequence as an element of another sequence

Copy Code code as follows:

>>>S3 = [1,[3,4,5]]

Empty sequence

Copy Code code as follows:

>>>S4 = []

The reference to the element

The subscript for the sequence element starts with 0:

Copy Code code as follows:

>>>print S1[0]
>>>print S2[2]
>>>print S3[1][2]

Because the list element can be changed, you can assign a value to one of the elements of the list:

Copy Code code as follows:

>>>S2[1] = 3.0
>>>print S2

If you do this to tuple, you will get the wrong hint.

So, you can see that the reference to the sequence is implemented by S[<INT>, and int is subscript

Other ways of referencing

Scope Reference: Basic style [lower limit: Upper limit: Step size]

Copy Code code as follows:

>>>print S1[:5] # from start to subscript 4 (the elements of subscript 5 are not included)
>>>print s1[2:] # from Subscript 2 to the end
>>>print S1[0:5:2] # from subscript 0 to subscript 4 (subscript 5 not included), every 2 to take an element (subscript 0,2,4 Element)
>>>print S1[2:0:-1] # from subscript 2 to subscript 1

As you can see from the above, if the CAP is specified when the range is quoted, the upper limit itself is not included.

Tail element Reference

Copy Code code as follows:

>>>print S1[-1] # The last element of a sequence
>>>print S1[-3] # The penultimate element of a sequence

Similarly, if s1[0:-1], then the last element is not referenced (again, excluding the upper element itself)

string is a tuple

A string is a special element, so you can perform related operations on tuples.

Copy Code code as follows:

>>>str = ' abcdef '
>>>print Str[2:4]

Summarize

Tuple element immutable, List element variable

Sequence of references s[2], S[1:8:2]

A string is a tuple

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.