Python Learning 1: sequence basics and python sequence Basics
By NiceCui
- This document is not reposted. If you need to reposted, you must obtain the author's consent. Thank you.
- Link: http://www.cnblogs.com/NiceCui/p/7858473.html
- Email: moyi@moyibolg.com
- Date:
Python Learning 1: sequence Basics
I. Sequence Introduction
In Python, some types of variables store multiple data, just like containers. sequences are like an orderly team, like the neat Great Wall, storing a variety of data, they are arranged in a certain order. The sequence is a set of ordered data. A data in a sequence is called an element of a sequence. A sequence can contain one or more elements, or it can have no empty sequence of any elements.
Ii. Sequence Classification
There are two types of sequences: Tuple and List ).
The main difference between the two is that once an effective sequence is established, each element in the distant group cannot be modified or changed, and it will become a fixed element. Therefore, tuples are like a special table, and the data is fixed. Many people call it a "value table ".
3. Create tuples and lists
1 '''2 Created on November 18, 2017 3 4 @ author: NiceCui 5 '''6' ----------------------- tuple tuples ---------------------------- '7 8 tuple = (2, 3, "good ", "hello tuple", 666, "hello") 9 10 print (tuple [0]) 11 print (tuple [1]) 12 print (tuple [2]) 13 print (tuple [3]) 14 15' result: '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 python written on eclipse. If you do not know how to install python plug-ins using eclipse, you can see how to install Python plug-ins using eclipse in my python articles;
Installation plugin Tutorial: http://www.cnblogs.com/NiceCui/p/7858107.html
From the example above, we can see that the same sequence can contain different types of elements, which is also a reflection of the python dynamic type. In addition, sequence elements can be not only basic types of data, it can also be another type of sequence. This is also a little different from the java language. Using the Python language to write a sequence may seem very simple and very powerful.
4. nested and fixed display
List nested 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'15 '>>> hello'16 '''
Tuples cannot change the reason of data. A single tuple is rarely created, but the sequence can add and modify elements. Therefore, sequences are often used to create an empty table;
1 '''2 empty list 3''' 4 5 next_list = []
V. Sequential Data Reading
The small example above shows how to use subscript to locate a single element. Of course, you can also use range reference to locate multiple elements.
Basic style of range reference
1. Sequence name [lower limit: Upper Limit: Step Size]
The lower limit indicates the start subscript, and the upper limit indicates the end subscript. Locate the element between the start and end subscript according to the step interval.
If the default step is 1, that is, every one element between the upper limit and the lower limit will appear in the result. Referencing multiple elements will become a new sequence. Next we will make 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]) # subscript 0 ~ All 5 elements output 8 9 print (list [2:]) # subscript 2 ~ The last element outputs 10 11 print (list []) # subscript 0 2 4 all outputs 12 13 print (list [2: 0:-1]) # subscript 2 1 element outputs 14 15 sliced = list [2: 0:-1] 16 17 type (sliced) # The result of the paradigm 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 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]) # Return the last and third elements of the sequence 14 15 print (list [1:-1]) # returns the last 2nd elements and the last 2nd elements of the sequence.