1.代码
#组织列表
#排序:
persons = [‘Charlie’,‘Bob’,‘Alice’,‘David’]
#临时排序,原列表不变,返回一个排序后的新列表。
sorted_persons = sorted(persons)
print(persons)
#永久排序,原列表将会修改
persons.sort()
print(persons)
#函数len()获取列表长度
print(len(persons))

#遍历列表:第一种方法:直接遍历列表
persons = [‘Charlie’,‘Bob’,‘Alice’,‘David’]
for p in persons:
print§
#for语句用于循环,注意for语句最后的:, 和print之前的缩进。

#遍历列表: 第二种方法: 使用range()函数遍历数字,然后再按照数字来索引列表元素
for i in range(0,len(persons)):
print(persons[i])

for i in range(0,3):
print(i)
print(persons[i])

#列表的简单统计计算
number = [1,2,3,4,5,6]
print(sum(numbers))
print(sum(numbers)/len(numbers))

#列表解析
squares =[value**2 for value in range(1,5)]
print(squares)

2.输入代码
python-07
python-07

3.输出结果
python-07

相关文章:

  • 2021-08-17
  • 2021-06-19
  • 2021-08-13
  • 2022-12-23
  • 2022-12-23
  • 2021-11-15
  • 2022-03-09
  • 2021-04-08
猜你喜欢
  • 2021-05-18
  • 2021-08-02
  • 2022-12-23
  • 2021-09-12
  • 2022-02-10
  • 2021-11-21
  • 2021-06-23
相关资源
相似解决方案