函数
sbs-绝对值
n=-100s=abs(n)print(s)
列表
创建列表

all_in_list=[5.21,'hello word',True]print(all_in_list)
运行结果 [5.21, ‘hello word’, True]
列表的索引/切片/增删改
索引
all_in_list=[5.21,'hello word',True]print(all_in_list)t=all_in_list[0]print(t)
运行结果 [5.21, ‘hello word’, True]
5.21
切片
all_in_list=[5.21,'hello word',True]print(all_in_list)t=all_in_list[0:2]print(t)
运行结果 [5.21, ‘hello word’, True]
[5.21, ‘hello word’]
增加
末尾-增加
all_in_list=[5.21,'hello word',True]print(all_in_list)all_in_list.append('13.14')print(all_in_list)
运行结果 [5.21, ‘hello word’, True]
[5.21, ‘hello word’, True, ‘13.14’]
插入-增加
all_in_list=[5.21,'hello word',True]print(all_in_list)all_in_list.insert(1,'13.14')print(all_in_list)
运行结果 [5.21, ‘hello word’, True]
[5.21, ‘13.14’, ‘hello word’, True]
删除
单个删除(索引)
all_in_list=[5.21,'hello word',True]print(all_in_list)all_in_list.remove(True)print(all_in_list)
运行结果 [5.21, ‘hello word’, True]
[5.21, ‘hello word’]
多个删除(切片)
all_in_list=[5.21,'hello word',True]print(all_in_list)del all_in_list[1:2]print(all_in_list)
运行结果 [5.21, ‘hello word’, True]
[5.21, True]
修改
修改数字
all_in_list=[5.21,'hello word',True]print(all_in_list)all_in_list[1]=13.14print(all_in_list)
运行结果 [5.21, ‘hello word’, True]
[5.21, 13.14, True]
修改文字
all_in_list=[5.21,'hello word',True]print(all_in_list)all_in_list[1]='love you'print(all_in_list)
运行结果 [5.21, ‘hello word’, True]
[5.21, ‘love you’, True]
列表推导式
循环
x=[]for i in range(10):x.append(i**2)print(x)
运行结果 [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
推导式
x=[i**2 for i in range(10)]print(x)
运行结果 [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
求曲边图形面积
列表
import mathn=100width=2*math.pi/nx=[]y=[]for i in range(n):x.append(i*width)for i in x:y.append(abs(math.sin(i)))S=sum(y)*widthprint(S)
列表推导式
import mathn=100width=2*math.pi/nS=[abs(math.sin(i*width))*width for i in range(n)]print(sum(S))
运行结果 3.9986839661525218
