Python basic 03 Sequence

Source: Internet
Author: User

Author: vamei Source: http://www.cnblogs.com/vamei welcome reprint, please also keep this statement. Thank you!

Thank you.Thunder424Error Correction

 

Data Type: Sequence)

1. sequence is a groupOrderedOfElementOfSet

(Strictly speaking, it is a collection of objects, but we have not introduced the concept of "object", so we will talk about elements for the moment)

A sequence can contain one or moreElementOr it can be an element without any elements.Empty Sequence

Elements can be the basic data type we mentioned earlier, can be another sequence, or other objects we will introduce later.

There are two types of sequences: tuple (Value table.Tuples) And list (Table)

>>> S1 = (2, 1.3, 'love', 5.6, 9, 12, 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 a tuple is created,Each element of the tuple cannot be changed, while each element of the list can be changed..

Elements of a sequence as another sequence

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

Empty Sequence

>>> S4 = []

 

2. Reference of sequence elements:

Sequential ElementSubscriptStarting from 0:

>>> Print S1 [0]

>>> Print S2 [2]

>>> Print S3 [1] [2]

Since the list element can be changed, you can assign a value to an element in the list:

>>> S2 [1] = 3.0

>>> Print S2

If you perform this operation on tuple, an error is returned.

Therefore, we can see that the reference of the sequence passes throughS [<int>]Implement, Int Is subscript

3) Other reference methods:

Range reference: basic style[Lower limit: Upper Limit: Step Size]

>>> Print S1 [: 5]# From start to subscript 4 (the element of subscript 5 is not included)

>>> Print S1 [2:]# From subscript 2 to final

>>> Print S1 [0: 5]# From subscript 0 to subscript 4 (subscript 5 is not included), take an element at every 2 (subscript is 0, 2, 4)

>>> Print S1 [2: 0:-1]# From subscript 2 to subscript 1

From the above we can see that when the range reference, if the upper limit is specified, then thisThe upper limit is not included..

Tail element reference

>>> Print S1 [-1]# The last element of the sequence

>>> Print S1 [-3]# The last and third elements of the sequence

Similarly, if S1 [0:-1], the last element will not be referenced (again,Not includedUpper Limit element itself)

4)A string is a tuple.

Therefore, tuple operations can be performed.

>>> STR = 'abcdef'

>>> Print STR [2: 4]

 

Summary:

The tuple element is unchangeable, And the list element is variable.

Reference of a sequence s [2], s []

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.