Python Learning 1: Detailed Explanation of Sequence Basics
Author: NiceCui
This article refuses to reprint, if you need to reprint, you need to get the author's consent, thank you.
Link to this article: http://www.cnblogs.com/NiceCui/p/7858473.html
Email: [email protected]
Date: 2017-11-18
Python Learning 1: Detailed explanation of sequence basics
1 : Sequence introduces
some types of Python Variables, like containers, contain multiple data, and the sequence is like an orderly team, like the neat Great Wall, storing all kinds of data, they are arranged in a certain order, very powerful and beautiful , so a sequence is an ordered collection of data. A piece of data that a sequence contains is called an element of the sequence. A sequence can contain one or more elements, or an empty sequence with no elements.
Two: Sequence
Classification There are two kinds of sequences, namely Tuple and List.
The main difference between the two is that once a valid sequence is established, each element 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 a "fixed value table".
Three: Tuple and List Building
1'''
2 Created on November 18, 2017
3
4 @author: NiceCui
5'''
6 '------------------ -----tuple tuple-----------------------------'
7
8 tuple = (2,3,"good","hello tuple",666,"你好")
9
10 print(tuple[0])
11 print(tuple[1])
12 print(tuple[2])
13 print(tuple[3])
14
15 '结果:'
16 '>>> 2'
17 '>>> 3'
18 '>>> good'
19 '>>> hello'
20
21 '-----------------------List 列表------------------------------'
22
23 list = [1,2,"list",6,"python"]
24
25 print(list[0])
26 print(list[1])
27 print(list[2])28 '''
29 '>>> 1'
30 '>>> 2'
31 '>>> list' '''
This is using python written on eclipse, students who don't know how to install python plug-in using eclipse can see how to use eclipse to install python plug-in in my Python essay classification;
installation plug-in tutorial: http://www.cnblogs.com/ NiceCui/p/7858107.html
As can be seen from the above example, the same sequence can contain elements of different types, which is also a manifestation of python dynamic type, and sequence elements can not only be basic types of data, but also Can be another type of sequence. This is also a bit different from the java language. Writing sequences in the Python language will be very simple and very powerful.
Four: Nested and fixed
display Nested list inside list
1 '''
2 Created on 2017-11-18
3 @author: NiceCui
4 '''
5
6 next_list = [1,[3,"hello",4 ,5]]
7
8 print(next_list[0])
9 print(next_list[1][0])
10 print(next_list[1][1])
11
12 '''
13 '>>> 1'
14 '>>> 3'
Tuples cannot change the reason for the data, and rarely create a tuple, but sequences can add and modify elements, so sequences are often used to create an empty list;
1 '''
2 Empty list
3 '''
4
5 next_list = []
5: Sequence data reading The small example
just above has shown how to use subscripts to find a single element, of course, you can also find multiple elements through a
range reference. Basic style of range reference
1 Sequence name[ Lower limit: upper limit: step]
The lower limit indicates the starting subscript, and the upper limit indicates the ending subscript. Between the start and end subscripts, elements are found by the interval of the step size.
If the default step size is 1, every other element between the upper and lower bounds will appear in the result. Referencing multiple elements will become a new sequence. Next do a small example:
1 list = [1,2,"list",6,"python"]
2
3 print(list[0])
4 print(list[1])
5 print(list[ 2])
6
7 print(list[:6]) # Subscripts 0 ~ 5 all output
8
9 print(list[2:]) # Subscript 2 ~ the last element all output
10
11 print(list[0 :6:2]) # Subscript 0 2 4 elements are output
12
13 print(list[2:0:-1]) # Subscript 2 1 elements all output
14
15 sliced = list[2:0:-1]
16
17 type(sliced) # The result of the norm reference is a tuple
In addition, Python also provides a tail reference syntax for referencing elements at the end of a sequence:
1 '''
2 Created on 2017-11-18
3 @author: NiceCui
4 '''
5
6 if __name__ == ' __main__':
7 pass
8
9 list = [1,2,"list",6,"python"]
10
11 print(list[-1]) # return the last element of the sequence
12
13 print(list[-3 ]) # Returns the third last element of the sequence
14
15 print(list[1:-1]) # Returns the second last element and the second last element of the sequence
<!--
#s3gt_translate_tooltip_mini { display: none !important; }
-->
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=326485902&siteId=291194637
Recommended
Ranking