Python starts with small white-list, tuple operations

Source: Internet
Author: User

The list is one of the most commonly used data types in the future, with lists that allow the most convenient storage, modification, and so on for the data to be defined list

1 names = ['Alex','tenglan','Eric ']

To access the elements in the list by subscript, the index is counted starting at 0

1>>>Names[0]2 'Alex'3>>> names[2]4 'Eric'5>>> names[-1]6 'Eric'7>>> Names[-2]#and you can take it backwards.8 'Tenglan'
Slice
1 " "2 #切片, take multiple elements3 #1, take the head not to take the tail, from left to right value4 #2, the last element is-15 " "6 ImportCopy7name = ["AA","BB","CC","DD","EE","FF"]8 9 Print(Name[0:2])Ten Print(Name[:2]) One Print(name[2:-1]) A Print(name[2:]) - Print(Name[0::2])#equals Name[::2] 2 is represented, every other element, takes a
Append Append 1 name.append ("GG") 2 print(name)inserting insert 1 name.insert (2,"bb") 2 print(name)Modify 1 name[3] = "Bb" 2 print(name)Delete ①del②remove③pop
1 del name[2]2print(name)34 name.remove (" DD ")    # Name.pop (Name.index ("DD"))  index Remove label Delete specified element 5print(name)6  7 name.pop ()           # Delete list last value 8print(name)

Expand Extend
1 name_2 = ["aa""bb""cc"  ]2name.extend (name_2)3print(name)

Statistics count 1 print(Name.count ("aa")) # count Occurrences Flip Reverse 1 name.reverse () 2 print(name)Sorting sort 1 name.sort () 2 print(name)Copying: Copy 1 name2 = Name.copy () 2 print(name2)

1. Shallow copy

1P1 = ["Sex",["Saving", 500]]2 Print(P1)3P2 =copy.copy (P1)4 Print(P2)5P1[0] =" Boy"6P2[0] ="Girl"7P1[1][1] =" +"8 Print(P1)9 Print(P2)#Copy the second memory address, point to the data address in memory

2. Deep copy

1P3 = ["name", ["Saving", 666]]#deep Copy, completely independent, not affected by the previous object2 Print(P3)3P4 =copy.deepcopy (P3)4 Print(P4)5P3[0] ="A"6 Print(P3,P4)7P4[0] ="B"8 Print(P3,P4)9P3[1][1] = 888Ten Print(P3,P4)

Tuples: tuples are actually similar to the list, but also to save a group of numbers, it is not once created, it can not be modified, so also called read-only list

Tuple representation Method:

1 names = ("zhangsan","LiSi","Wangwu  ","zhaoliu","zhuba","  qianqi")

It has only 2 methods, one is count, one is index, it is finished

Python starts with small white-list, tuple operations

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.