Lists and tuples can be used as normal "arrays", and he can save any number of arbitrary types of Python objects, as well as arrays that access elements through the number 0 index, lists and tuples can store different types of objects, and lists and tuples have several important differences. List elements are included with ([]), the number and value of elements can be changed, and tuples are included with ({}) and cannot be changed. Tuples can be viewed as read-only lists
>>> lis = [' Xiaoyuan ', 25]
In a sequence, you can include other sequences, that is, lists can contain list tuples, and so on.
>>> lis = [' Xiaoyuan ', 25]
>>> Lis_jo = [' John ', 30]
>>> date = [Lis,lis_jo]
>>> Date
[' Xiaoyuan ', [' John ', 30]]
Note: There is also a data structure named container (container) in Python. A container is basically any object that contains other objects. Sequence types (such as lists, tuples) and mapped types (such as dictionaries). Each element in the sequence has its own number, and each element of the map has a name (also called a key). Follow-up will be introduced, as is not a sequence nor a mapped container type, such as set (set), post-order introduction
2, the general operation of the sequence
All sequence types can perform certain operations, including: index (Indexing), Shard (sliceing), plus (adding), multiply (multiplying), and check that each element belongs to the member of the sequence (judging whether the element exists in the sequence). There is also the length of the computed sequence, which identifies the maximum and minimum elements of the built-in function. There is also an important operation to be explained in the future, iterative (iteration). The meaning of iterating over a sequence.
(1) All elements in the index sequence are numbered this number is incremented starting from 0:
>>> lis = [' Xiaoyuan ', 25]
>>> Lis_jo = [' John ', 30]
>>> date = [Lis,lis_jo]
>>> Date
[' Xiaoyuan ', [' John ', 30]]
>>> sta = ' Hellow '
>>> Sta[0]
' H '
>>> Sta[-1]
' W '
>>> Date[0]
[' Xiaoyuan ', 25]
This shows that "-1" indicates the last element
(2) sharding
① we can use shards to manipulate a range of elements
>>> tist = [1,2,3,4,5,6]
>>> new = "ABCDEFG"
>>> Tist[1:3]
[2, 3]
>>> New[0:4]
' ABCD '
>>> tist[2:] #从第二个元素到最后
[3, 4, 5, 6]
② What do we do when we want to take elements from behind? I believe everyone knows that it is using ' – '
>>> tist = [1,2,3,4,5,6]
>>>
>>> Tist[-3:-1]
[4, 5]
>>> tist[-4:] #这里也就是从倒数第四个元素到最后
[3, 4, 5, 6]
Little Exercise 1
We know that the domain name is divided into three parts by www.xxxx.cn. CN for China, XXXX for domain name, www, for the World Wide Web so what do we want to remove the domain name?
>>> url = ' www.aaa.cn '
>>> name = url[4:-3]
>>> Name
' AAA '
>>> print ' Donaim name:< ' +name+ ' > ' + ' type: ' +url[-2:]
Donaim NAME:<AAA>TYPE:CN
③ step, which is the length of the step to take the element, when the step is 1, it will output the element
>>> tist = [1,2,3,4,5,6,7,8,9,10]
>>> Tist[0:10:1]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> Tist[0:10:2]
[1, 3, 5, 7, 9]
If we want to take the element out from the beginning to the last of each of the 3 elements, that's all you need.
>>> Tist[::3]
[1, 4, 7, 10]
(4) Additional element list.append
>>> lis = [1,2,3,4,5,6]
>>> Lis.append (7)
>>> Lis
[1, 2, 3, 4, 5, 6, 7]
(5) List delete element del list[]
>>> Lis
[1, 2, 3, 4, 5, 6, 7]
>>> del Lis[0]
>>> Lis
[2, 3, 4, 5, 6, 7]
(6) List length: Len ()
>>> Len (LIS)
6
(7) Find if an element contains: "5" in Lis when Present returns True (TRUE) otherwise false (false)
>>> 5 in Lis
True
>>> 1 in Lis
False
(8) Turn the list into a string and define the connection method "_". Join (LIS)
>>> Name_lis = ["A", "B", "C"]
>>> ' _ '. Join (Name_lis)
' A_b_c '
(9) Sequence addition
>>> [1,2,3]+[4,5,6]
[1, 2, 3, 4, 5, 6]
>>> ' hell ' + ' world! '
' hellworld! '
(10) multiplication *
>>> ' hell '
' Hellhellhellhellhell '
>>> [1]*5
[1, 1, 1, 1, 1]
List function, because a string cannot be modified as a list so, converting a string to a list requires using the list (the list function applies to all types of sequences)
>>> tup = ' Asdfghj '
>>> List (TUP)
[' A ', ' s ', ' d ', ' f ', ' g ', ' H ', ' J ']
(12) Assigning values to elements
>>> tup = List (tup)
>>> Tup
[' A ', ' s ', ' d ', ' f ', ' g ', ' H ', ' J ']
>>> tup[0] = ' A '
>>> Tup
[' A ', ' s ', ' d ', ' f ', ' g ', ' H ', ' J ']
Little Exercise 2:
Requirements write a welcome message that requires the format of the Solutionkeys box to be output and transformed based on what the user has entered
650) this.width=650; "title=" image "style=" border-left-0px; border-right-width:0px; border-bottom-width:0px; border-top-width:0px "border=" 0 "alt=" image "src=" http://s3.51cto.com/wyfs02/M02/7A/25/ Wkiol1ajaafh251saaami6ettzi967.png "width=" 244 "height="/>
Inpot = Raw_input ("Your Name:") #欢迎信息
Text = Len (inpot)
SCR = 17+text #设置一个长度
left = (SCR)//2 #设置前方空格的空隙
#下方输出的内容可以根据输入字符串的长度进行变化
Print #输出空
print ' * left + ' + ' + '-' * (SCR) + ' + '
print ' * left + ' | ' + ' *SCR + ' | '
print ' * left + ' | ' + ' Nice to meet you: ' +inpot+ ' | '
print ' * left + ' | ' + ' *SCR + ' | '
print ' * left + ' + ' + '-' * (SCR) + ' + '
Little Exercise 2