p-03-列表 联系

列表是一种非常简单的数组。他们可以包含任意的类型的变量,包含任意多个变量。同时列表的遍历是非常简单的。

1
2
3
4
5
6
7
8
9
10
11
mylist = []
mylist.append(1)
mylist.append(2)
mylist.append(3)
print(mylist[0]) # prints 1
print(mylist[1]) # prints 2
print(mylist[2]) # prints 3

# prints out 1,2,3
for x in mylist:
print(x)

如果你视图访问一个不存在的列表,将会报错而不是异常

1
2
mylist = [1,2,3]
print(mylist[10])

联系

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

numbers = []
strings = []
names = ["John", "Eric", "Jessica"]

# write your code here
second_name = names[1]

for i in range(1,4):
numbers.append(i)

for s in ["hello","world"]:
strings.append(s)



# this code should write out the filled arrays and the second name in the names list (Eric).
print(numbers)
print(strings)
print("The second name on the names list is %s" % second_name)