Python _ data type --, python Data Type

Source: Internet
Author: User

Python _ data type --, python Data Type

Names = ['one', 'two', 'three ', 'four', 'five'] # list slice print (names [0:]) # ['one ', 'two', 'three ', 'four', 'five'] print (names [1:]) # ['two', 'three ', 'four ', 'five'] print (names [1:-1]) # ['two', 'three ', 'four'] print (names [1:-]) # ['two', 'four'], print (names [1: 2]) from left to right # ['two ', 'Four'] print (names [1:-2]) # ['two'], meaning that print (names [3: :-1]) # ['four ', 'three', 'two', 'one'], that is, starting from the fourth, from the right to the left, # Add names. append ('kt ') # Add, automatically add names at the end of the list. insert (1, 'lx ') # insert in the second position # modify, directly assign a value to the index location names [1] = 'glx' # change the second digit to glxnames [2: 4] = ['B ', 'C'] # change the 3, 4 digits to 'B'. 'C' should be replaced with the list because the retrieved index is a list. # Delete names. remove ('B') # remove Method delete the content of the index to be specified delete = names. pop (2) # delete the third-digit content. In the pop method, retrun can return the deleted content, that is, the returned value is the deleted element print (delete) # print out the pop deletion content as cdel delete # directly delete the entire delete variable # other operations # calculate the number of times a certain element appears print (names. count ('glx') # display the number of times 'glx' appears # method extend, add the list ba = [, 3] B = [, 5, 6]. extend (B) print (a) # [1, 2, 3, 4, 5, 6] print (B) # [4, 5, 6] # method index, search for the position of an element in the list print (names) # ['one', 'glx', 'four ', 'five', 'kt'] print (names. index ('kt ') #4, obtain the fifth position of kt in c = [1, 2, 3] print (c. index (2) # index first position 2 captured by default output # Method reverse flip list names. reverse () # ['kt ', 'five', 'four', 'glx', 'one'] print (names) # method sort sorts the list, from small to large x = [2, 5, 8, 6, 4, 5] x. sort () # [2, 4, 5, 5, 6, 8] x1 = x. sort () # None, that is, the sort method does not write the return value, so no return value is returned. Therefore, Noneprint (x) # In the list method, only pop has the returned value print (x1) y = ['D', 'F', 'A', 'C'] print (y. sort () # Noney. sort () # ['D', 'A', 'C', 'F'] print (y) y. sort (reverse = True) # ['F', 'C', 'A', 'D'], sort, and print (y) in reverse order)
Summary:

In the slice: [start: end: step]

Start indicates the starting index position.
End indicates the position where the index is terminated. If it is null, it indicates a boundary. It can be a left boundary or a right boundary.
Step represents a step, while controlling the direction

Summary:

Query:

Index, subscript slice. count ('elemental ') # Find the number of times an element appears in the list. index ('elemental') # Find the position where the element first appears in the list

Delete:

. Pop ('index') # deletes the index position element and returns a value. remove ('elemental) # Delete the specified del element in the list # You can directly Delete the entire list. clear () # clear the entire list

Add:

. Append ('new element') # Add a new element to the end of the list. insert (index, 'new element') # insert a new element in the index position. extend (list1, list2) # extend list2 after list1

Change:

List ['index'] = 'new elemental 'list [index1: index2] = list # If the list is retrieved, the imported list is also used. Otherwise, the empty list will be deleted.

Sort:

. Sort () # sort by ASCII code in ascending order. sort (reverse = True) # reverse order. reverse () # reverse Order

  

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.