第二周python学习笔记 列表、元组、字典
一、列表
1. 访问列表中的值
list1 = ['physics', 'chemistry', 1997, 2000]; list2 = [1, 2, 3, 4, 5, 6, 7 ]; print "list1[0]: ", list1[0] print "list2[1:5]: ", list2[1:5]
2. 更新列表
list = ['physics', 'chemistry', 1997, 2000]; print list[2]; list[2] = 2001; print list[2];
3. 删除列表
list1 = ['physics', 'chemistry', 1997, 2000]; print list1; del list1[2]; print "After deleting value at index 2 : " print list1;
4. python列表脚本操作符
|
5. python列表函数
|
|
6. 列表联系操作
#定义列表 name = ['lzf','sxs','ty','ly','zj','xbl','fwy','zt','czp','zj','lw','jwj','xx'] #统计sj数量 shuliang = name.count("zj") print (shuliang) #末尾添加 name.append("haha") print (name) #定义name2,添加到name中。 name2 = ['run','de','ji','tu'] name.extend(name2) print (name) #获取sj索引位置 wei = name.index("zj") print (wei) #在索引4处添加 zhang name.insert(4,"zhang") print (name) #删除-2的元素 name.pop (-2) print (name) #循环删除列表中所有zj for i in range(name.count("zj")): name.remove("zj") print (name)
7. 其它
#以格式显示字符串 print("%3s %7s %3s %7s"%("ID","商品","数量","单价")) #字符串显示在中间 welcome_msg = 'Welcome to SUNNY Shopping Mall'.center(50,'-') #显示时间 import time ISOTIMEFORMAT = '%Y-%m-%d %X' this_time = time.strftime(ISOTIMEFORMAT, time.localtime()) print ("当前时间:",this_time)
浙公网安备 33010602011771号