python学习
列表的进一步运用
1.减少元素
(1)del
place = ['lasa', 'chengdu', 'litang', 'xian', 'lundon']
del place[0]
#输出
['chengdu', 'litang', 'xian', 'lundon']
(2)pop()
place = ['lasa', 'chengdu', 'litang', 'xian', 'lundon']
print(place.pop(0))
#输出为
['chengdu', 'litang', 'xian', 'lundon']
区别:pop()在去掉元素后还能够继续使用他
2.增加元素
(1)append()
name = ['yebenhao','luyuhao','liuyufan']
name.append('hewenhao')
print(name)
(2)insert()
name = ['yebenhao','luyuhao','liuyufan']
name.insert(0,'chenyifan')
print(name)
3.sort,sorted,reverse的用法
place = ['lasa', 'chengdu', 'litang', 'xian', 'lundon']
print(sorted(place))
print(place)
print(sorted(place,reverse=True))
print(place)
print(place.reverse())
print(place)
print(place.reverse())
print(place)
print(place.sort())
print(place)
print(place.sort(reverse=True))
#输出为
4.列表的循环
names = ['yebenhao','luyuhao','liuyufan']
for name in names:
print(f'{name.title()},is dog.')
print("They are my friends.")
#输出为
5.列表元素值最大和最小,求和
numbers = list(range(1,100000))
print(sum(numbers))
print(max(numbers))
print(min(numbers))
6.列表和range的用法
numbers = list(range(3,30,3))
for number in numbers:
print(number)
7.复制列表
a = [0,1,2,3,4]
b = a[:]
print(a)
print(b)
只看以上和
a =[0,1,2,3,4]
b = a
print(a)
print(b)
输出之后没有差别,但是如果输出以下
a = [0,1,2,3,4]
b = a[:]
a.append(4)
b.insert(0,4)
print(a)
print(b)
#输出为
[0, 1, 2, 3, 4, 4]
[4, 0, 1, 2, 3, 4]
如果仍用第一种写法
a = [0,1,2,3,4]
b = a
a.append(4)
b.insert(0,4)
print(a)
print(b)
#输出为
标签:name,python,笔记,列表,place,numbers,print,chengdu
From: https://www.cnblogs.com/102204216zxf/p/16736384.html