Python learning one: A sequence-based explanation

Source: Internet
Author: User

Nicecui

    • This article declined to reprint, if necessary to obtain the author's consent, thank you.
    • This article link: http://www.cnblogs.com/NiceCui/p/7858473.html
    • Email: [Email protected]
    • Date: 2017-11-18

Python learning one: A sequence-based explanation

One: Introduction to the sequence

Some types of variables in Python, like containers, hold multiple data, the sequence is like an orderly team, like the neat Great Wall, stored a variety of data, they are arranged in a certain order together, very powerful, very beautiful, so that the sequence is an ordered set of data. A sequence contains a data that is called an element of a sequence. A sequence can contain one or more elements, or it can have no empty sequence of elements.

II: Sequence Classification

There are two types of sequences, tuple (tuple) and list .

The main difference between the two is that once a valid sequence is established, the elements of the far group can no longer be modified and changed, and will become a fixed set of elements. So the tuple is like a special table, the data is fixed, and many people call it "fixed value table".

Three: tuple and list creation

1 " "2 Created on November 18, 20173 4 @author: Nicecui5 " "6 '-----------------------A tuple of tuples------------------------------'7 8Tuple = (2,3,"Good","Hello tuple", 666,"Hello")9 Ten Print(tuple[0]) One Print(tuple[1]) A Print(tuple[2]) - Print(tuple[3]) -  the 'Results:' - '>>> 2' - '>>> 3' - '>>> Good' + '>>> Hello' -  + '-----------------------List------------------------------' A  atlist = [1, 2,"List", 6,"python"] -  - Print(list[0]) - Print(list[1]) - Print(list[2])
- " " in '>>> 1' - '>>> 2' to '>>> List'
‘‘‘

This is using the Python written on Eclipse, it's unclear how to use eclipse to install the Python plugin can see my Python essay classification has introduced how to use Eclipse to install the Python plugin;

Install plugin Tutorial: http://www.cnblogs.com/NiceCui/p/7858107.html

As can be seen from the above example, the same sequence can have different types of elements, which is also a manifestation of Python dynamic type, also, the sequence element can be not only the basic type of data, but also can be another type of sequence. This is also a bit different from the Java language, the use of the Python language to write the sequence will be very simple to feel the function is very powerful.

IV: Nested and fixed display

List inner nested list

1 " "2 Created on 2017-11-183 @author: Nicecui4 " "5 6Next_list = [1,[3,"Hello", 4,5]] 7 8 Print(next_list[0])9 Print(next_list[1][0])Ten Print(next_list[1][1]) One  A " " - ' >>> 1 ' - ' >>> 3 ' the ' >>> Hello ' - " "

Tuples can not change the reason of the data, rarely to create a tuple, but the sequence may add and modify elements, so the sequence is often used to create an empty table;

1 " " 2 Empty list 3 " " 4 5 next_list = []

V: Data reading of the sequence

Just above the small example has shown how to use the subscript to find a single element, of course, can also be referenced by the scope to find multiple elements

Basic styles for range references

1 sequence name [lower limit: Upper limit: Step size]

The lower bound indicates the starting subscript, and the upper limit indicates the end subscript. Between the start and end subscripts, the element is found at the interval of the step.

If the default step is 1, that is, every 1 elements between the upper and lower bounds will appear in the result. Referencing multiple elements becomes a new sequence. Let's take a small example:

1list = [1, 2,"List", 6,"python"]2 3 Print(list[0])4 Print(list[1])5 Print(list[2])6 7 Print(List[:6])#The elements of subscript 0 ~ 5 are output8 9 Print(list[2:])#Subscript 2 ~ The last element is outputTen  One Print(List[0:6:2])#Subscript 0 2 4 elements are output A  - Print(List[2:0:-1])#Subscript 2 1 elements are output -  theSliced = List[2:0:-1]  -  -Type (sliced)#The result of a canonical reference is a tuple

In addition to this, Python provides a tail-referenced syntax for referencing the elements of the tail of a sequence:

1 " "2 Created on 2017-11-183 @author: Nicecui4 " "5 6 if __name__=='__main__':7     Pass8 9list = [1, 2,"List", 6,"python"]Ten  One Print(List[-1])#returns the last element of a sequence A  - Print(List[-3])#returns the third element of the countdown sequence -  the Print(List[1:-1])#returns the 2nd and penultimate elements of a sequence

Python learning one: A sequence-based explanation

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.