列表是序列式的数据类型,可以通过下标来访问列表中的某一个元素或者一些元素。它可以保存任何数目、任意对象类型的非常灵活的容器。
alist = [123, 1.23, 'abc', [1, a]]
name = ["Avata", "Titannic", "The Dark Knight Rises", "Jurassic Park", "Dances with Wolves"] year = [2009, 1997, 2012, 1997, 1990] directors = ["James Cameron", "James Cameron", "Christopher Nolan", "Steven Spielberg", "Kevin Costner"] print name[0] print name[1] print name[-1] print name[-3] print name[-1][0] print name[1:3] print name[:2] print name[1:] avata = ["Avata", 2009, "James Cameron"] titannic = ["Titannic", 1997, "James Cameron"] theDarkKnightRises = ["The Dark Knight Rises", 2012, "Christopher Nolan"] jurassicPark = ["Jurassic Park", 1997, "Steven Spielberg"] dancesWithWolves = ["Dances with Wolves", 1990, "Kevin Costner"]
name = ["Lili", "A3", "Tom", "Meta", "Jack"] name.append("Joy") print name othername = ["Frank", "Jim"] print name print othername name.extend(othername) print name name.insert(1,"Bob") print name
name = ["Lili", "A3", "Tom", "Meta", "Jack"] del name[2] print name name.remove("Jack") print name del name print name
name[2] = "Tim"
list1 = [1, 2, 3] list2 = ["a", "b", "c"] list3 = list1 + list2 print list3 list1.extend(list2) print list1 list1 = list1 * 2 print list1 list2 = list2 * 3 print list2 a = "@" h = "_" h *= 3 print a,h,a
names = ["Lili", "A3", "Tom", "Meta", "Jack"] for name in names: print name print [name for name in names] print [letter for name in names for letter in name[0]]
for name in names: for letter in name[0]: print letter是不是感觉很不一样,它在Python中使用频率非常的高~~~~
原文地址:http://blog.csdn.net/sin_geek/article/details/40979069