列表是最常用的Python数据类型,它可以作为一个方括号内的逗号分隔值出现。列表的数据项不需要具有相同的类型。
Python列表赋值
list1 = [‘physics’, ‘chemistry’, 1997, 2000];
list2 = [1, 2, 3, 4, 5 ];
list3 = [“a”, “b”, “c”, “d”];
Python调用列表中的值
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]
*输出结果:
list1[0]: physics
list2[1:5]: [2, 3, 4, 5]
Python更新列表中的值
更新列表中的值一般有两种方法,
一、直接赋值(针对具体列表的某个元素),如
list = [‘physics’, ‘chemistry’, 1997, 2000]
list[2] = 2001
print list[2]
*输出结果
2001
二、使用append()方法对列表进行添加(append()方法是在列表末尾添加新的对象,一般使用append()方法时,都会初始化一个列表,添加的时候一般是组合而成的列表,实际情况,后面的demo有具体展现)
如
li1=[]
li2=[1, 2, 3, 4, 5]
li1.append(li2)
print li1
*输出结果
<