Python Learning Notes (10)--Combined data type (sequence type)

Source: Internet
Author: User

A sequence is a set of data that has a sequential relationship, is a one-dimensional element vector, the element type can be different, like a sequence of mathematical elements, the elements are guided by the ordinal, by subscript access to the specific elements of the sequence. A sequence type is a base class type, a string type, a primitive type, and a list type that belongs to a sequence type.

Ordinal definition, positive increment ordinal, inverse decrement ordinal. A[0]-A[5] a[-6]-a[-1]

Sequence type operation:

In Ss+  ts*n or nss[i]s[i:j:k] or s[i:j]s[::-1] Sequence inverse sequence type functions and methods: Len (s) min (s) max (s) s.index (x) or S.index (x,i,j) s.count (x) s X number of occurrences

Tuple type:

A tuple is a sequence type that, once created, cannot be modified, is created with parentheses (), or tuple (), and the elements are separated by commas, with or without parentheses, such as return to a tuple type.

>>> createtur="Cat","Dog","Tiger">>>Createtur ('Cat','Dog','Tiger')>>> color= (122, Createtur,"Blue")>>>Color (122, ('Cat','Dog','Tiger'),'Blue')
>>> Color[-2][2]
' Tiger '

Element types integrate all common operations of sequence types and cannot be modified after they are created.

List type:

A list is an extension of a sequence type that can be modified at will after it is created. Using square brackets [], or list creation, the elements are separated by commas, the element types can be different, and the list has no length limit. The list assignment is just a list of different names, but it is the same memory location, similar to a pointer.

>>>ls=["Cat","Dog","Tiger",1024x768]>>>ls[1:2]=[1,2,3,4]>>>ls['Cat',1,2,3,4,'Tiger',1024x768]>>> dells[::3]>>>ls[1,2,4,'Tiger']>>>ls*2[1,2,4,'Tiger',1,2,4,'Tiger']

Ls.append (x) adds an element x to the LS

Ls.clear () Remove all elements from the list

Ls.copy () generates a new list that assigns all elements in the LS

Ls.insert (I,X) inserts x in the first position

Ls.pop (i) remove the element I position in the list and delete it

Ls.remove (x) removes the first element x that appears in the list

Ls.reverse () Reverses the elements in the list

Del Ls[i] or del ls[i:j:k]

Scenarios for sequence types:

A representation of a set of data, traversed. For item in LS:FOR item in TP:

If you do not want the data to be changed by the program, you can convert the Narimoto group type.

ls =[1,2,3,4,5]>>> lt=tuple (ls )>>> lt (12345)

Python learning Note (10)--Combined data type (sequence type)

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.