Python Basic learning Article Python sequence

Source: Internet
Author: User

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

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.