CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
jackfrued

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: jackfrued/Python-100-Days
Path: blob/master/Day01-15/code/Day07/list1.py
Views: 729
1
"""
2
定义和使用列表
3
- 用下标访问元素
4
- 添加元素
5
- 删除元素
6
7
Version: 0.1
8
Author: 骆昊
9
Date: 2018-03-06
10
"""
11
12
13
def main():
14
fruits = ['grape', '@pple', 'strawberry', 'waxberry']
15
print(fruits)
16
# 通过下标访问元素
17
print(fruits[0])
18
print(fruits[1])
19
print(fruits[-1])
20
print(fruits[-2])
21
# print(fruits[-5]) # IndexError
22
# print(fruits[4]) # IndexError
23
fruits[1] = 'apple'
24
print(fruits)
25
# 添加元素
26
fruits.append('pitaya')
27
fruits.insert(0, 'banana')
28
print(fruits)
29
# 删除元素
30
del fruits[1]
31
fruits.pop()
32
fruits.pop(0)
33
fruits.remove('apple')
34
print(fruits)
35
36
37
if __name__ == '__main__':
38
main()
39
40