上集回顾:python学习笔记(一)——hello world、变量、字符串、数字、禅
在python中,用方括号表示列表,用,分割列表中的元素
family = ['dad','mom','sister','brother','me']
print(family)
访问列表中的元素,只需指出列表名称,再指出元素索引,将其放入方括号中即可,索引以0为开始值。
print(family[0])
输出 dad
可以和之前的大小写结合起来,使代码更好看
print(family[2].title())
输出 :Sister
对于不知道长度,或者长度比较长的列表,还可以用负数来表示下标,倒数第一个为-1,倒数第二个为-2.以此类推
print(family[-1])
print(family[-2])
可以使用列表中的值来创建消息
message = "I have a " + family[2] + " in my family."
print(message)
输出:I have a sister in my family.
要修改列表元素,可指定列表名和要修改的元素的索引,再指定该元素的新值
list = ['l1','l2','l3','l4']
print("修改前:" + str(list))
list[0] = 'L1'
print("修改后:" + str(list) + '\n')
print("原列表:" + str(list))
# list.append('值')
list.append('l4')
print("在列表末尾增加新元素后:" + str(list))
# list.列表名(位置,'值')
list.insert(2,'l2.5')
print("在指定位置2插入新元素后:" + str(list) + '\n')
print("原列表:" + str(list))
del list[2]
print("执行del list[2]后的列表:" + str(list))
# 可通过在括号中输入下标来指定要删除的值,不输入默认为最后一个元素。
# 将list中指定元素弹出并存储到pop_list中
pop_list = list.pop(2)
print("执行pop(2)方法后的列表:" + str(list))
print("打印弹出的元素:" + pop_list)
pop() 和del语句的区别
如果删除元素之后不再使用该元素 就用del语句
如果删除之后还要再次使用该值,就使用pop()方法。
list.remove('l4')
print("执行remove('l4')后的列表:" + str(list))
# 将要取出的值存在一个新变量中,之后就可以通过调用该变量继续使用该元素
too_ugly = 'L1'
list.remove(too_ugly)
print("打印取出的元素too_ugly:" + too_ugly)
print("执行list.remove(too_ugly)后列表:" + str(list))
list = ['a1','d4','c3','b2']
print("原列表:" + str(list))
print("临时性排序的列表:" + str(sorted(list)))
print("执行完临时性排序后再次打印列表:" + str(list))
list.sort()
print("执行完永久性正序排列后的列表" + str(list))
注意:
sort是方法,所以使用方法为list.sort()
sorted是函数,所以使用方法为sorted(list)
print("临时性倒序排列的列表" + str(sorted(list,reverse=True)))
print("执行完临时性倒序排列的列表" + str(list))
list.sort(reverse=True)
print("执行完永久性倒序排列的列表" + str(list))
print(str(list))
reverse() 永久性反转列表元素排列顺序,不过恢复原列表只需再次执行reverse即可
```
list_reverse = ['sd','d','w','q','r','e']
list_reverse.reverse()
print(list_reverse)
list_reverse.reverse()
print(list_reverse)
```
len() 确定列表长度
print(len(list_reverse))
输出:6
names = ['name1','name2','name3']
print(names[0])
print(names[1])
print(names[2] + "\n")
message = " ,how are you going?"
print(names[0].title() + message)
print(names[1].title() + message)
print(names[2].title() + message + "\n")
traffic = ['walk','bus','bike']
print("I usually " + traffic[0] + " to school entrance,and than take the school " + traffic[1] + ".But sometimes I will by " + traffic[2])
inter_list = ['person1','person2','person3']
message = " ,would you like join a party in my house today's evening?"
print('Dear ' + inter_list[0].title() + message)
print('Dear ' + inter_list[1].title() + message)
print('Dear ' + inter_list[2].title() + message + "\n")
print(inter_list[2] + "can't coming")
inter_list[2] = 'insteadPerson1'
print('Dear ' + inter_list[0].title() + message)
print('Dear ' + inter_list[1].title() + message)
print('Dear ' + inter_list[2].title() + message + "\n")
message_newtable = " , I found a new table which is bigger than the old one"
inter_list.insert(0,'newPerson0')
inter_list.insert(1,'newPerson1')
inter_list.append('newPerson2')
print(inter_list)
print('Dear ' + inter_list[0].title() + message_newtable)
print('Dear ' + inter_list[1].title() + message_newtable)
print('Dear ' + inter_list[2].title() + message_newtable)
print('Dear ' + inter_list[3].title() + message_newtable)
print('Dear ' + inter_list[4].title() + message_newtable)
print('Dear ' + inter_list[5].title() + message_newtable + "\n")
print("I only can invite two guest")
delFirst = inter_list.pop()
delSecond = inter_list.pop(1)
delThird = inter_list.pop(1)
delFourth = inter_list.pop(1)
del_list = [delFirst,delSecond,delThird,delFourth]
message_sorry = ', I am sorry to tell you I can not invite you'
print(del_list[0].title() + message_sorry)
print(del_list[1].title() + message_sorry)
print(del_list[2].title() + message_sorry)
print(del_list[3].title() + message_sorry)
print(inter_list)
message_stillInvite = ', Don not forget to come'
print(inter_list[0].title() + message_stillInvite)
print(inter_list[1].title() + message_stillInvite)
del inter_list[0]
del inter_list[0]
print(inter_list)
desire = ['new zealand','hangzhou','yiwu','jingdezhen','tibet']
print(desire)
print(sorted(desire))
print(desire)
print(sorted(desire,reverse=True))
print(desire)
desire.reverse()
print(desire)
desire.reverse()
print(desire)
desire.sort()
print(desire)
desire.sort(reverse=True)
print(desire)
print(len(del_list))
countries = ['china','us','usa','german','french']
print(countries)
countries.append('cuba')
countries.insert(1,'australia')
print(countries)
del countries[2]
countries.pop(2)
countries.remove('german')
print(countries)
print(sorted(countries))
print(sorted(countries,reverse=True))
print(countries)
countries.reverse()
print(countries)
countries.sort()
print(countries)
countries.sort(reverse=True)
print(countries)