A sequence is a collection of functions that have indexes and slices. Tuples, lists, and strings have the ability to access a specific value through an index, or to return a slice through a slice, so that tuples, lists, and strings belong to the sequence.
Example: Index function of a sequence
#!/usr/bin/env python#-*-coding=utf-8-*-#Using GPL v3.3#Author: [email protected]#Index OperationsTuple = ("Apple","Banana","Grape","Orange") List= ["Apple","Banana","Grape","Orange"]str="Apple"PrintTuple[0]PrintTuple[-1]PrintList[0]PrintList[-1]PrintStr[0]PrintSTR[-1]
Output Result:
----------python2.7-----------Normal termination
Example: The Shard function of a sequence
#!/usr/bin/env python#-*-coding=utf-8-*-#Using GPL v3.3#Author: [email protected]#Shard OperationsTuple = ("Apple","Banana","Grape","Orange") List= ["Apple","Banana","Grape","Orange"]str="Apple"PrintTuple[:3]PrintTuple[3:]PrintTuple[1:-1]Printtuple[:]PrintList[:3]PrintList[3:]PrintList[1:-1]Printlist[:]PrintStr[:3]PrintStr[3:]PrintStr[1:-1]Printstr[:]
Output Result:
----------python2.7----------('Apple','Banana','Grape')('Orange',) ('Banana','Grape')('Apple','Banana','Grape','Orange')['Apple','Banana','Grape']['Orange']['Banana','Grape']['Apple','Banana','Grape','Orange']applepplapple output completed (0 seconds elapsed)-Normal termination
Attention:
Shard Seq[:3] Represents the value of the entire sequence from the 1th element of the sequence to the 3rd element, The Shard [:].
Tuples and lists have the attributes of a sequence, but they are also more distinct.
1, tuples are only a set of data that can be read, and tuples do not provide a way to sort and find.
2, the list of data can be read, can also write, and provides a wealth of operation methods, support sorting, find operations.
3. The difference between a tuple and a list
|
Support for negative indexing |
Support Sharding |
Support for adding, deleting, modifying |
Support sorting, finding |
The composition of the data |
Meta-group |
Is |
Is |
Whether |
Whether |
A set of different meanings of data |
List |
Is |
Is |
Is |
Is |
A set of data of the same meaning |
Python Basic learning Article Python sequence