3.1 a list (list creation and access)
Array: The same type of data in a pile consisting of together with the basic requirement is the same type of data
since the data type is not python, the python no array, but a list of python
List: integer, floating-point numbers, strings, objects, can ever list
1. Create a list
member = ['weivid','heiye','mitu','wei'] #数组
print(member)
number = [1,2,3,4,4,566,6] #数组
mix = [1, 'weivid', 23,4, [1,2,4,5]] #列表,可以嵌套列表
print(mix)
Empty list:
empty = [] #在编程中通常创建一个空列表,以备后续进行访问添加
2. Add to the list of elements
1) append method
This method append function belongs to the scope of the member, it is necessary to add that he is a representative of .append Functions
That object. method ()
member.append('wangwei')
print(len(member))
print(member)
2) extend the method,
#extend operation target is also a parameter but it can be added to it in the form of a list of two or more
member.extend(['nihao', 'dudu'])
print(member)
3) insert method
These two methods are carried out at the end of the list of additional
insert (parameter 1, parameter 2)
# parameters represents a position of the insertion, from the start position 0 indicates the first object, i.e., two parameters to be inserted
member.insert(0,'bengbeng')
print(member)
3. Exercises
1. add elements to the list, and explain the difference between these methods
#append, extend, INSERT
#append extend and are added at the end of the list, append the parameter is added to the end of the list to add the single
#extend is added to a list in the original list
mem = [1, 2, 'wang']
print(mem)
mem.append('wei')#添加单个元素 wei
print(mem)
mem.extend(['nihao',1,3])# 添加两个元素作为列表添加
print(mem)
mem.append(['meimei', 3, 4])# 添加一个元素 为列表
print(mem)
operation result:
2. Suppose given the following list:
= #member [ 'weivid', 'heiye', 'NiHao', 'kuaile', 'SuanShu']
# column of the table to claim the
#member = [ 'weivid', 88 , 'heiye', 90, 'nihao ', 85,' kuaile ', 90,' suanshu ', 88]
Method # a
method using # insert () and append () modified
member = ['weivid','heiye','nihao','kuaile','suanshu']
member.insert(1,88)
member.insert(3,90)
member.insert(5,85)
member.insert(7,90)
member.append(88)
print(member)
The result:
# The second method directly modify coverage
3. Use loop for printing member = [ 'weivid', 88, 'heiye', 90, 'nihao', 85, 'kuaile', 90, 'suanshu', 88]
member = ['weivid',88,'heiye',90,'nihao',85,'kuaile',90,'suanshu',88]
for i in member:
print(i)
The result:
write a program to print 1
print("\ncase 1:")
count = 0
length = len(member)
while count < length:
print(member[count],member[count+1])
count += 2
The result:
write a program to print 2
print("\ncase 2:")
for each in range(length):
if each%2 == 0:
print(member[each],member[each+1])
operation result:
4. What is the procedure to print?
old = [1,2,3,4,5]
new = old
old = [6]
print(new)
Print [1,2,3,4,5]