今天分享的是第三章的要点和课后习题的参考代码。
要点:
1、列表的定义:列表由一些列按特定顺序排列的元素组成。在Python中,用方括号([])来表示列表,并用逗号来分隔其中的元素.
2、列表元素的访问.
3、索引从0而不是1开始.
4、在列表中修改、添加和删除元素: append(item)在末尾添加元素、insert(location, item)在指定位置插入元素、del 删除特定元素、pop()删除末尾元素并返回之、pop(location)弹出指定位置的元素、remove(item)根据值删除元素.
5、sort()永久性排序,sorted(list)临时排序。通过向sorted()传递参数reverse = True 可以按与字母顺序相反的顺序显示列表.
6、reverse()倒转列表.
7、len()确定列表长度.
8、当列表不为空时,list[-1]能够访问最后一个元素.
参考代码:
3-1
names = ["Mike", "Jack", "Brown"] print(names[0]) print(names[1]) print(names[2])
运行结果:
Mike Jack Brown [Finished in 0.1s]
3-2
names = ["Mike", "Jack", "Brown"] print("How are you, " + names[0] + "?") print("How are you, " + names[1] + "?") print("How are you, " + names[2] + “?")
运行结果:
How are you. Mike? How are you. Jack? How are you. Brown? [Finished in 0.0s]
3-3略
3-4 —— 3-7
#3.4-3.7 names = ["Mike", "Jack", "Brown"] print("Would you like to have dinner with me tonignth, " + names[0] + ", " + names[1] + ", " + names[2] + "?") print(names[1] + " have an appointment tonignth.") names[1] = "Lucy" print("Would you like to have dinner with me tonignth, " + names[0] + ", " + names[1] + ", " + names[2] + "?") print("I have booked a bigger table.") names.insert(0, "Joe") names.insert(2, "May") names.append("Louis") print("Would you like to have dinner with me tonignth, " + names[0] + ", " + names[1] + ", " + names[2] + ", " + names[3] + ", " + names[4] + ", " + names[5] +"?") print("Sorry, the table won't be here until tomorrow. I can only invite two persons.") name = names.pop() print("Sorry, I can't invite you, " + name) name = names.pop() print("Sorry, I can't invite you, " + name) name = names.pop() print("Sorry, I can't invite you, " + name) name = names.pop() print("Sorry, I can't invite you, " + name) print("You are still in my list, " + names[0] + ".") print("You are still in my list, " + names[1] + ".") del names[0] del names[0] print(names)
运行结果:
Would you like to have dinner with me tonignth, Mike, Jack, Brown? Jack have an appointment tonignth. Would you like to have dinner with me tonignth, Mike, Lucy, Brown? I have booked a bigger table. Would you like to have dinner with me tonignth, Joe, Mike, May, Lucy, Brown, Louis? Sorry, the table won't be here until tomorrow. I can only invite two persons. Sorry, I can't invite you, Louis Sorry, I can't invite you, Brown Sorry, I can't invite you, Lucy Sorry, I can't invite you, May You are still in my list, Joe. You are still in my list, Mike. [] [Finished in 0.1s]
3-8
places = ["England", "America", "Germany", "Italy","France" ] print(places) print(sorted(places)) print(places) print(sorted(places,reverse = True)) print(places) places.reverse() print(places) places.reverse() print(places) places.sort() print(places) places.sort(reverse = True) print(places)
运行结果:
['England', 'America', 'Germany', 'Italy', 'France'] ['America', 'England', 'France', 'Germany', 'Italy'] ['England', 'America', 'Germany', 'Italy', 'France'] ['Italy', 'Germany', 'France', 'England', 'America'] ['England', 'America', 'Germany', 'Italy', 'France'] ['France', 'Italy', 'Germany', 'America', 'England'] ['England', 'America', 'Germany', 'Italy', 'France'] ['America', 'England', 'France', 'Germany', 'Italy'] ['Italy', 'Germany', 'France', 'England', 'America'] [Finished in 0.0s]
3-9
names = ["Mike", "Jack", "Brown"] print(len(names))
运行结果
3 [Finished in 0.0s]