A sequence of basic python tutorials

Source: Internet
Author: User
sequence Sequence

Sequence (sequence) is a set of sequential elements

(Strictly speaking, it is a collection of objects, but given that we have not yet introduced the concept of "object", temporarily say elements)

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

The basic data types we mentioned earlier can be used as elements of a sequence. The element can also be another sequence, as well as other objects we will introduce later.

There are two types of sequences: tuple (fixed value table; also translated as tuple) and List (table)

The code is 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 a tuple and a list is that once established, each element of a tuple cannot be changed, and the elements of the list can be changed again.

A sequence as an element of another sequence

The code is as follows:


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


Empty sequence

The code is as follows:


>>>S4 = []



references to elements

The subscript of the sequence element starts with 0:

The code is as follows:


>>>print S1[0]

>>>print S2[2]

>>>print S3[1][2]


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

The code is as follows:


>>>S2[1] = 3.0

>>>print S2


If you do this to a tuple, you get an error message.

So, you can see that the reference to the sequence is implemented by s[ ], and int is subscript

Other reference methods

Range reference: Basic style [lower limit: Upper limit: Step length]

The code is 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 last

>>>print S1[0:5:2] # from subscript 0 to subscript 4 (subscript 5 not included), take one element every 2 (the element labeled 0,2,4)

>>>print S1[2:0:-1] # from subscript 2 to subscript 1


As can be seen from the above, the upper limit is not included in the range reference when the upper limit is indicated.

Trailing element reference

The code is as follows:


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

>>>print S1[-3] # third element of sequence countdown


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

string is a tuple

strings are a special element, so you can perform related operations on tuples.

The code is as follows:


>>>str = ' abcdef '

>>>print Str[2:4]



Summary

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.