-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3.0列表管理.py
More file actions
84 lines (65 loc) · 2.29 KB
/
Copy path3.0列表管理.py
File metadata and controls
84 lines (65 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
bicycles = ['adhhd','fighr','bbobo']
#使用sort 方法永久性排序列表
bicycles.sort()
print(bicycles)
counts = ['111','222','333']
aaaa = ['111','222','333','111','222']
# sorted函数临时排序列表
print(sorted(aaaa))
print(aaaa)
counts.sort(reverse=True)
print(counts)
#倒着打印列表 reverse不是按照字母顺序倒着打印,而是按照列表元素的顺序倒着打印
print(bicycles) #第一步:将列表元素按照原来的顺序打印出来
bicycles.reverse() #第二步:将列表元素倒着排列
print(bicycles) #第三步:将列表元素按照倒着排列的顺序打印出来
#len函数计算列表长度
#和之前一样,len可以单独使用,也可以和其他函数结合使用
# len(bicycles) 其实这里单独调用一下没有什么用,通常我们会将len函数的结果赋值给一个变量,以便后续使用
counts_length = len(counts)
print(f"列表bicycles的长度是{len(bicycles)}")
print(f"列表counts的长度是{counts_length}")
# 遍历整个列表
for bicycle in bicycles:
print(bicycle)
# 在 for 循环中,可以对每个元素执行任意操作。下面来扩展前面的
# 示例,为每个元素添加一些文本,并将结果打印出来。我们可以使用 f-string 来格式化字符串,使输出更具可读性。
for bicycle in bicycles:
print(f"{bicycle} is a good bicycle\n")
print("All in all,I like all the bicyales")
pizzas = ["pepperoni","mushroom","green pepper"]
for pizza in pizzas:
print(f"I like eating {pizza}\n")
print("I like all pizzas!")
#使用range()函数来生成数字列表1到10
for value in range(1,11):
print(value)
#使用 range() 创建数值列表
numbers = list (range(1,6))
print(numbers)
for number in numbers:
print(number)
i = 0
squares = []
for value in range (1,11):
square = value ** 2
squares.append(square)
print(squares)
#练习4.3
for value in range(1,21):
print(value)
# mollions = list(range(1,1000001))
# print(min(mollions))
# print(max(mollions))
# print(sum(mollions))
odds = [value for value in range(1,21,2)]
print(odds)
threes = [value for value in range(3,31,3)]
for value in threes:
print(value)
lifangs = []
for value in range(1,11):
lifangs.append(value**3)
print(lifangs)
dudu = [value**2 for value in range(1,11)]
print(dudu)