Python-100-Days/Day01-15/code/Day07/list2.py

38 lines
706 B
Python
Raw Normal View History

2018-04-27 00:00:22 +08:00
"""
列表常用操作
2019-05-03 21:17:36 +08:00
- 列表连接
- 获取长度
- 遍历列表
- 列表切片
- 列表排序
- 列表反转
- 查找元素
2018-04-27 00:00:22 +08:00
Version: 0.1
Author: 骆昊
Date: 2018-03-06
"""
def main():
2019-05-03 21:17:36 +08:00
fruits = ['grape', 'apple', 'strawberry', 'waxberry']
fruits += ['pitaya', 'pear', 'mango']
# 循环遍历列表元素
for fruit in fruits:
print(fruit.title(), end=' ')
print()
# 列表切片
fruits2 = fruits[1:4]
print(fruits2)
# fruit3 = fruits # 没有复制列表只创建了新的引用
fruits3 = fruits[:]
print(fruits3)
fruits4 = fruits[-3:-1]
print(fruits4)
fruits5 = fruits[::-1]
print(fruits5)
2018-04-27 00:00:22 +08:00
if __name__ == '__main__':
2019-05-03 21:17:36 +08:00
main()