The list is the most basic data structure in Python. Each element in the sequence is assigned a number-its position, or index, the first index is 0, the second index is 1, and so on. (Python is called a list, while other language classes are called "arrays")
Python has 6 built-in types of sequences, but the most common are lists and tuples.
Sequences can be performed by operations including indexing, slicing, adding, multiplying, and checking members.
A list is the most commonly used Python data type and can appear as a comma-separated value within a square bracket.
Data items for a list do not need to have the same type
Create a list by enclosing separate data items separated by commas in square brackets.
#!/usr/bin/env python# Create an array list a_list=[' Wang ', ' Li ', ' Zhang ', ' Zhao ', ' 1 ', ' 2 ', ' 3 ', ' Cheng ', ' Linux ', ' MySQL ', ' 9 ']# Print List position is 1 value print (a_list[1]) # print position 0-6 position index value (general Gu Tou disregard tail, result is 0-5 position index value output) print (a_list[:6 ]) # within several values that have been printed print (A_list[:6][2:5]) #在列表值 ' Li ' after inserting a ' chun ' content A_list.insert (2 , ' Chun ') print (a_list) #在列表后追加一个 ' He ' content a_list.append (' he ') print (a_list) #删除 ' Cheng ' content A_ List.remove (' Cheng ') print (a_list) #删除多个使用列表值得位置索引来删除del a_list[8:10]print (a_list) #每隔一个取一个值 ":: 2" is the print value print (A_list[::2]) #查询某个值是否在列表内print ("Zhao" in a_list) from the beginning to the end, separated by 2 characters Number of occurrences of the statistic element in the list print (A_list.count ("Zhao")) #sort Sort the values in the table, the numbers in front, the letters in the back list1 = [' 1 ', ' 6 ', ' 4 ', ' 3 ', ' 2 ', ' 5 ', ' Wang ', ' Zhao ', ' an ']list1.sort () print (List1) #获取该列表里值得个数print (len (list1)) #列表的拼截 use ' + ' #: list2 and list3 are synthesized into a list4 list. list2=[' 8 ', ' 7 ', ' 6 ']list3=[' 9 ', ' Ten ', ' One ']list4=list2+list3print (list4) #重复列表值使用 * multiplication sign:Repeat the value of list2 2 times. print (list2*2) #in Determine if the value exists in the list name=[' Wang ', ' Qian ', ' Sun ']if ' Wang ' in name: print (' wang exists in name ') #元组 -- use tuple to convert the list to Narimoto group form. Name1=tuple (name) print (name1) #可以使用list convert tuples to lists. yuan= (' Zhou ', ' Qing ', ' Zhen ') yuan1=list (yuan) print (yuan1) #元组和列表的区别, list with "" brackets, tuples with () parentheses, and tuples cannot be modified, added, deleted inside of the worthwhile. Other interception, merging is basically the same as the list.
The difference between a tuple and a list:
A Python tuple is similar to a list, except that the elements of a tuple cannot be modified.
Tuples use parentheses, and the list uses square brackets.
Tuple creation is simple, just add elements in parentheses and separate them with commas.
This article is from the "Carefree" blog, please be sure to keep this source http://birdcai.blog.51cto.com/11216068/1829525
Python Learning-3 days-lists and tuples