Python 简明教程
Python - List Exercises
Python List Exercise 1
Python程序查找给定列表中的唯一数字。
Python program to find unique numbers in a given list.
L1 = [1, 9, 1, 6, 3, 4, 5, 1, 1, 2, 5, 6, 7, 8, 9, 2]
L2 = []
for x in L1:
if x not in L2:
L2.append(x)
print (L2)
它将生成以下 output −
It will produce the following output −
[1, 9, 6, 3, 4, 5, 2, 7, 8]
Python List Exercise 2
python程序查找列表中所有数字的和。
Python program to find sum of all numbers in a list.
L1 = [1, 9, 1, 6, 3, 4]
ttl = 0
for x in L1:
ttl+=x
print ("Sum of all numbers Using loop:", ttl)
ttl = sum(L1)
print ("Sum of all numbers sum() function:", ttl)
它将生成以下 output −
It will produce the following output −
Sum of all numbers Using loop: 24
Sum of all numbers sum() function: 24