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/list2.py
Views: 729
1
"""
2
列表常用操作
3
- 列表连接
4
- 获取长度
5
- 遍历列表
6
- 列表切片
7
- 列表排序
8
- 列表反转
9
- 查找元素
10
11
Version: 0.1
12
Author: 骆昊
13
Date: 2018-03-06
14
"""
15
16
17
def main():
18
fruits = ['grape', 'apple', 'strawberry', 'waxberry']
19
fruits += ['pitaya', 'pear', 'mango']
20
# 循环遍历列表元素
21
for fruit in fruits:
22
print(fruit.title(), end=' ')
23
print()
24
# 列表切片
25
fruits2 = fruits[1:4]
26
print(fruits2)
27
# fruit3 = fruits # 没有复制列表只创建了新的引用
28
fruits3 = fruits[:]
29
print(fruits3)
30
fruits4 = fruits[-3:-1]
31
print(fruits4)
32
fruits5 = fruits[::-1]
33
print(fruits5)
34
35
36
if __name__ == '__main__':
37
main()
38
39