03_python_列表简介


日期:2017-12-30 作者:秋的懵懂


# coding=utf-8
# python版本:python3.6.4


# ---------------------------------------------------------
# 列表
print('_______________________________________________')
print("列表:")

# []表示列表,元素用逗号,分隔开
bicycles = ['trek', 'cannondale', 'redline', 'specialized']
print(bicycles)

# 访问列表元素
print(bicycles[0])
print(bicycles[0].title())

# -1返回最后一个元素
print(bicycles[-1])
# -2返回倒数第二个元素
print(bicycles[-2])

print('_______________________________________________')
# ---------------------------------------------------------


# ---------------------------------------------------------
# 修改、添加、删除元素
print('\n\n_______________________________________________')
print("列表修改、添加、删除元素:")

motorcycles = ['honda', 'yamaha', 'suzuki']

# 修改
motorcycles[0] = 'ducati'
print(motorcycles)
# 添加
motorcycles.append('ducati')
print(motorcycles)
# 插入
motorcycles.insert(1, 'honda')
print(motorcycles)
# 删除
del motorcycles[0]
print(motorcycles)
# pop()删除
popped_motorcycles = motorcycles.pop()
print(motorcycles)
print(popped_motorcycles)
# pop()指定元素
first_owned = motorcycles.pop(0)
print(motorcycles)
print(first_owned)
# 根据值删除元素
motorcycles.remove('yamaha')
print(motorcycles)

print('_______________________________________________')
# ---------------------------------------------------------




# ---------------------------------------------------------
# 组织列表
print('\n\n_______________________________________________')
print("组织列表:")

cars = ['bmw', 'audi', 'toyota', 'subaru']
# 字母表顺序排
cars.sort()
print(cars)
# 字母表反序排
cars.sort(reverse = True)
print(cars)
# 临时排序
print(sorted(cars))
print(cars)
# 反转列表元素
cars.reverse()
print(cars)
# 确定列表长度
print(len(cars))

print('_______________________________________________')
# ---------------------------------------------------------




你可能感兴趣的:(03_python_列表简介)