def average(*args):
if not args:
return 0.0
return float(sum(args))/len(args)
print average()
print average(1, 2)
print average(1, 2, 2, 3, 4)
if not args:
return 0.0
return float(sum(args))/len(args)
print average()
print average(1, 2)
print average(1, 2, 2, 3, 4)
2015-08-16
print [int(a + b + c) for a in '123456789' for b in '0123456789' for c in '0123456789' if a == c ]
2015-08-15
print r'''"To be, or not to be": that is the question.
Whether it's nobler in the mind to suffer.'''
Whether it's nobler in the mind to suffer.'''
2015-08-15
a是非空字符串,布尔运算时python将其当做true,所以第一行结果返回的是a,而a是字符串,所以最终结果就是输出字符串的内容。
b是空字符串,布尔运算时将其当做false,所以第二行的结果取决于后面的字符串,于是返回的就是后面字符串的内容。
b是空字符串,布尔运算时将其当做false,所以第二行的结果取决于后面的字符串,于是返回的就是后面字符串的内容。
2015-08-15
来个加判断的,当复习
print [x + y*10 + z*100 for x in range(0,10) for y in range(0,10) for z in range(1,10) if x == z]
print [x + y*10 + z*100 for x in range(0,10) for y in range(0,10) for z in range(1,10) if x == z]
2015-08-15
x1 = 1
d = 3
n = 100
x100 = x1 + (n-1)*3
s = (x1 + x100)*n/2
print s
d = 3
n = 100
x100 = x1 + (n-1)*3
s = (x1 + x100)*n/2
print s
2015-08-15
import math
def quadratic_equation(a, b, c):
return (-b + math.sqrt(b*b-4*a*c))/2/a, (-b - math.sqrt(b*b-4*a*c))/2/a
print quadratic_equation(2, 3, 0)
print quadratic_equation(1, -6, 5)
def quadratic_equation(a, b, c):
return (-b + math.sqrt(b*b-4*a*c))/2/a, (-b - math.sqrt(b*b-4*a*c))/2/a
print quadratic_equation(2, 3, 0)
print quadratic_equation(1, -6, 5)
2015-08-15
def square_of_sum(L):
sum = 0
for elem in L:
sum += elem * elem
return sum
print square_of_sum([1, 2, 3, 4, 5])
print square_of_sum([-5, 0, 5, 15, 25])
sum = 0
for elem in L:
sum += elem * elem
return sum
print square_of_sum([1, 2, 3, 4, 5])
print square_of_sum([-5, 0, 5, 15, 25])
2015-08-15