Python入门习题大全——索引
想想可存储到列表的东西,如山岳、河流、国家、城市、语言或者你喜欢的任何东西。编写一个程序,在其中创建一个包含这些元素的列表,然后,对于之前所用到的每个函数,都至少使用一次来处理这个列表。
# 尝试使用各个函数
cars = ['Aa', 'bb', 'cC']
print(cars)
print(cars[0])
print(cars[1].title())
message = cars[2] + ' hahaha'
print(message)
cars[2] = 'Dd'
print(cars)
cars.append('EE')
print(cars)
cars.insert(0, 'FF')
print(cars)
del cars[0]
print(cars)
cars2 = cars.pop()
print(cars)
print(cars2)
cars3 = cars.pop(2)
print(cars)
print(cars3)
cars.remove('bb')
print(cars)
car = 'Aa'
cars.remove(car)
print(cars)
cars = ['AA', 'Bb', 'Ff', 'CC']
print(cars)
cars.sort()
print(cars)
cars.sort(reverse=True)
print(cars)
cars = ['AA', 'CC', 'BB']
print(cars)
print(sorted(cars))
print(cars)
print(sorted(cars, reverse=True))
print(cars)
cars.reverse()
print(cars)
cars.reverse()
print(cars)
len = len(cars)
print(len)
输出为:
['Aa', 'bb', 'cC']
Aa
Bb
cC hahaha
['Aa', 'bb', 'Dd']
['Aa', 'bb', 'Dd', 'EE']
['FF', 'Aa', 'bb', 'Dd', 'EE']
['Aa', 'bb', 'Dd', 'EE']
['Aa', 'bb', 'Dd']
EE
['Aa', 'bb']
Dd
['Aa']
[]
['AA', 'Bb', 'Ff', 'CC']
['AA', 'Bb', 'CC', 'Ff']
['Ff', 'CC', 'Bb', 'AA']
['AA', 'CC', 'BB']
['AA', 'BB', 'CC']
['AA', 'CC', 'BB']
['CC', 'BB', 'AA']
['AA', 'CC', 'BB']
['BB', 'CC', 'AA']
['AA', 'CC', 'BB']
3