Use subscripts to directly modify elements in the class table
check
The method index in to find the element
names =['zhangsan','lisi','wangwu','ermazi','dazhutou']
x = names.index('zhangsan')# index 返回元素在列表中的下标,元素不存在则报错print(x)# 0
x =('lisi'in names)# in 判断元素是否在列表中,是返回True,否返回Falseprint(x)# True
list traversal
Traversal: access all the data once. Traversal is for iterable objects
while loop traverses
for...in loop through
names =['zhangsan','lisi','wangwu','ermazi','dazhutou']# while循环,通过访问每一个元素的下标来访问每一个数据
i =0while i <len(names):print(names[i])
i +=1# for..in循环,本质是不断的调用next方法查找下一个数据for i in names:print(i)
Bubble Sort
The list can be sorted directly using the sort built-in method
n =[6,5,3,1,8,7,2,4]
x =0while x <7:
x +=1
i =0while i <len(n)-1:if n[i]> n[i+1]:
n[i],n[i+1]= n[i+1],n[i]
i +=1print(n)
list sort reverse
Lists can be sorted using the built-in method sort
Using the built-in function sorted will not change the original list data, but will generate a new sorted data
Use the reverse method, which is equivalent to [::-1], to reverse the list