1.如何用python 中的for 循环 实现加法
用Python的for循环实现等差序列相加,例如(1+2+3+4+5+。+20)这样的算法,代码如下:
1
2
3
4
5
6
7
8
import math
temp = 0
for x in range(1,20):
temp += x
break
else:
print temp
#输入结果:210
2.python中怎么把文件中找到的字符‘+’转变成可以进行加法运算的加
1
2
3
4
5
6
7
8
9
10
# 挖坟
# 用字典处理,键是操作符,值是匿名函数
f1 =lambdaa,b:a+b
f2 =lambdaa,b:a-b
f3 =lambdaa,b:a*b
f4 =lambdaa,b:a/b
maps ={'+':f1,'-':f2,'*':f3,'/':f4}
# 比如说c是文件中的字符,a和b是要进行运算的数,则结果为
result =maps[c](a,b)
3.python 循环做加法求救
x=input()
y=[]
with open('1.txt','r') as f:
for i in f.readlines():
y.append(float(i.split()[1])+x)
print y 或者
i=input()
with open('1.txt','r') as f:
print map(lambda x: i+float(x.split()[1]),f.readlines())