1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
|
def square_of_sum(L): sum = 0 for x in L: sum += x * x return sum
print square_of_sum([1, 2, 3, 4, 5])
import math def quadratic_equation(a, b, c): t = math.sqrt(b * b - 4 * a * c) return (-b + t) / (2 * a), ( -b - t )/ (2 * a)
print quadratic_equation(2, 3, 0)
def move(n, a, b, c): if n ==1: print a, '-->', c return move(n-1, a, c, b) print a, '-->', c move(n-1, b, a, c)
move(4, 'A', 'B', 'C')
def power(x, n=2): s = 1 while n > 0: n = n - 1 s = s * x return s
power(5) power(5,3)
def average(*args): sum = 0.0 if len(args) == 0: return sum for x in args: sum += x return sum / len(args)
print average() print average(1, 2) print average(1, 2, 2, 3, 4)
|