函数

屏幕截图 2022-01-08 040249.png

sbs-绝对值

  1. n=-100
  2. s=abs(n)
  3. print(s)

运行结果 s=100

列表

创建列表

屏幕截图 2022-01-08 040608.png

  1. all_in_list=[5.21,'hello word',True]
  2. print(all_in_list)

运行结果 [5.21, ‘hello word’, True]

列表的索引/切片/增删改

屏幕截图 2022-01-08 043314.png

索引

  1. all_in_list=[5.21,'hello word',True]
  2. print(all_in_list)
  3. t=all_in_list[0]
  4. print(t)

运行结果 [5.21, ‘hello word’, True]
5.21

切片

  1. all_in_list=[5.21,'hello word',True]
  2. print(all_in_list)
  3. t=all_in_list[0:2]
  4. print(t)

运行结果 [5.21, ‘hello word’, True]
[5.21, ‘hello word’]

增加

末尾-增加

  1. all_in_list=[5.21,'hello word',True]
  2. print(all_in_list)
  3. all_in_list.append('13.14')
  4. print(all_in_list)

运行结果 [5.21, ‘hello word’, True]
[5.21, ‘hello word’, True, ‘13.14’]

插入-增加

  1. all_in_list=[5.21,'hello word',True]
  2. print(all_in_list)
  3. all_in_list.insert(1,'13.14')
  4. print(all_in_list)

运行结果 [5.21, ‘hello word’, True]
[5.21, ‘13.14’, ‘hello word’, True]

删除

单个删除(索引)

  1. all_in_list=[5.21,'hello word',True]
  2. print(all_in_list)
  3. all_in_list.remove(True)
  4. print(all_in_list)

运行结果 [5.21, ‘hello word’, True]
[5.21, ‘hello word’]

多个删除(切片)

  1. all_in_list=[5.21,'hello word',True]
  2. print(all_in_list)
  3. del all_in_list[1:2]
  4. print(all_in_list)

运行结果 [5.21, ‘hello word’, True]
[5.21, True]

修改

修改数字

  1. all_in_list=[5.21,'hello word',True]
  2. print(all_in_list)
  3. all_in_list[1]=13.14
  4. print(all_in_list)

运行结果 [5.21, ‘hello word’, True]
[5.21, 13.14, True]

修改文字

  1. all_in_list=[5.21,'hello word',True]
  2. print(all_in_list)
  3. all_in_list[1]='love you'
  4. print(all_in_list)

运行结果 [5.21, ‘hello word’, True]
[5.21, ‘love you’, True]

列表推导式

循环

  1. x=[]
  2. for i in range(10):
  3. x.append(i**2)
  4. print(x)

运行结果 [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

推导式

  1. x=[i**2 for i in range(10)]
  2. print(x)

运行结果 [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

求曲边图形面积

列表

  1. import math
  2. n=100
  3. width=2*math.pi/n
  4. x=[]
  5. y=[]
  6. for i in range(n):
  7. x.append(i*width)
  8. for i in x:
  9. y.append(abs(math.sin(i)))
  10. S=sum(y)*width
  11. print(S)

运行结果 3.998683966152523

列表推导式

  1. import math
  2. n=100
  3. width=2*math.pi/n
  4. S=[abs(math.sin(i*width))*width for i in range(n)]
  5. print(sum(S))

运行结果 3.9986839661525218