def average(*args):
sum = 0.0
if len(args) ==0:
return sum
else:
for i in args:
sum += i
av = sum /len(args)
return av
print average()
print average(1, 2)
print average(1, 2, 2, 3, 4)
sum = 0.0
if len(args) ==0:
return sum
else:
for i in args:
sum += i
av = sum /len(args)
return av
print average()
print average(1, 2)
print average(1, 2, 2, 3, 4)
2018-07-26
x1 = 1
d = 3
n = 100
x100 = x1 + d * (n - 1)
s = (x1 + x100) * n / 2
print s
d = 3
n = 100
x100 = x1 + d * (n - 1)
s = (x1 + x100) * n / 2
print s
2018-07-26
x1 = 1
d = 3
n = 100
x100 = 1+(n-1)*3
s = (x1+x100)*100/2
print s
d = 3
n = 100
x100 = 1+(n-1)*3
s = (x1+x100)*100/2
print s
2018-07-26
import math
def quadratic_equation(a, b, c):
q = b*b -4*a*c
if q == 0:
x = -b/2*a
return x
elif q > 0:
x1 = (-b+math.sqrt(q))/(2*a)
x2 = (-b-math.sqrt(q))/(2*a)
return x1 , x2
print quadratic_equation(2, 3, 0)
print quadratic_equation(1, -6, 5)
def quadratic_equation(a, b, c):
q = b*b -4*a*c
if q == 0:
x = -b/2*a
return x
elif q > 0:
x1 = (-b+math.sqrt(q))/(2*a)
x2 = (-b-math.sqrt(q))/(2*a)
return x1 , x2
print quadratic_equation(2, 3, 0)
print quadratic_equation(1, -6, 5)
2018-07-26
def square_of_sum(L):
squ_sum =0
for i in L:
squ_sum += i*i
return squ_sum
print square_of_sum([1, 2, 3, 4, 5])
print square_of_sum([-5, 0, 5, 15, 25])
squ_sum =0
for i in L:
squ_sum += i*i
return squ_sum
print square_of_sum([1, 2, 3, 4, 5])
print square_of_sum([-5, 0, 5, 15, 25])
2018-07-26
在python3以上版本已经没有itervalues()等方法了,全部去掉了iter前缀,都是是dict.values(),dict.keys()
2018-07-26
代码如下:(注意缩进,直接按tab键)
for x in [0, 1,2,3,4,5,6,7,8,9 ]:
for y in [ 0,1,2,3,4,5,6,7,8,9]:
if x < y:
print x*10+y
for x in [0, 1,2,3,4,5,6,7,8,9 ]:
for y in [ 0,1,2,3,4,5,6,7,8,9]:
if x < y:
print x*10+y
2018-07-24
print 'hello,python'
print "hello,python"
print "hello,python"
2018-07-24
N = 100
x = 0
sum=0.0
while x < N:
x = x + 1
if x%2==1:
sum=sum+x
print sum
x = 0
sum=0.0
while x < N:
x = x + 1
if x%2==1:
sum=sum+x
print sum
2018-07-24