0. List can store some of what?
me: strings, numbers, lists, and so on and so forth.
Answer:
1. Which method add elements to the list there?
me:append()、extend()等
Reference answer: append (), extend () and insert ().
2.append () method and extend () methods are add elements to the end of the list, ask them what is the difference?
me:
In many cases both can be generic, but a big difference in processing time list elements:
Example:
>>> a = [1,2,3]; b=[4,5,6]
>>> a.append(b)
>>> a
[[1,2,3], [4,5,6]]
>>> a.extend(b)
[1,2,3,4,5,6]
As can be seen from the above example obvious difference. That append can only add an element, and extend steps to add more elements, but a plurality of elements necessary
Shall be used [] enclosed. There is also an insert () method, can be inserted into specified positions in the list.
Answer:
append () method is used as a parameter to the end of the element added to the list.
extend () method as a parameter list sucked despreading end of the list.
Sample code:
>>> name = ['F', 'i', 's', 'h'] >>> name.append('C') >>> name ['F', 'i', 's', 'h', 'C'] >>> name.extend(['.', 'c']) >>> name ['F', 'i', 's', 'h', 'C', '.', 'c'] >>> name.append(['o', 'm']) >>> name ['F', 'i', 's', 'h', 'C', '.', 'c', ['o', 'm']]
3. member.append ([ 'bamboo forest creek', 'Crazy obsession']) and member.extend ([ 'bamboo forest creek', 'Crazy obsession']) to achieve the same effect of it?
me: not the same, append () is added at the end of the list of elements, and extend () is a list of merger
4. A list name = [ 'F', 'i', 'h', 'C'], if you want to insert small turtle element 's' between the elements of 'i' and 'h', what methods should be used to insert?
me:name.insert(2,“s”)
Start work
0.
me:
i=0 member = [ 'small turtle', 88 'night', 90, 'lost', 85, 'Yi Jing', 90 'Qiuwu sun', 88] while 0 <= i <len(member): print(member[i]) i+=1
Answer:
member = [ 'small turtle', 88 'night', 90, 'lost', 85, 'Yi Jing', 90 'Qiuwu sun', 88] for each in member: print(each)
me:
i=0 member = [ 'small turtle', 88 'night', 90, 'lost', 85, 'Yi Jing', 90 'Qiuwu sun', 88] while 0 <= i <len(member): if i % 2 !=0: print(member[i]) else: print(member[i],end=" ") i+=1
member = [ 'small turtle', 88 'night', 90, 'lost', 85, 'Yi Jing', 90 'Qiuwu sun', 88] for i in range(0,len(member)-1,2): print(member[i : i+2])
Answer:
method one: count = 0 length = len(member) while count < length: print(member[count], member[count+1]) count += 2 Method Two: for each in range(len(member)): if each%2 == 0: print(member[each], member[each+1])