### Python 3.5计算前100个奇数的平均值

568 观看

5回复

81 作者的声誉

``````    i=1
for i in range(100):
a= i+2
b=a/i
print(b)
print(a)
print(i)
``````

### 回应 (5)

2

62407 作者的声誉

``````>>> list(range(1, 200, 2))
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53, 55, 57, 59, 61, 63, 65, 67, 69, 71, 73, 75, 77, 79, 81, 83, 85, 87, 89, 91, 93, 95, 97, 99, 101, 103, 105, 107, 109, 111, 113, 115, 117, 119, 121, 123, 125, 127, 129, 131, 133, 135, 137, 139, 141, 143, 145, 147, 149, 151, 153, 155, 157, 159, 161, 163, 165, 167, 169, 171, 173, 175, 177, 179, 181, 183, 185, 187, 189, 191, 193, 195, 197, 199]
``````

``````>>> from statistics import mean
>>> print(mean(range(1, 200, 2)))
100
``````

``````>>> odd = range(1, 200, 2)
>>> print(sum(odd) / len(odd))
100.0
``````

``````total = 0
length = 0
for i in range(1, 200, 2):
total += i
length += 1

print(total / length)
# 100.0
``````

``````n = 199
total = 0
length = 0
while n > 0:
total += n
length += 1
n -= 2

print(total / length)
``````

0

880 作者的声誉

``````sum([i for i in range(1,200,2)])
``````

-1

923 作者的声誉

``````In [1]: b = sum(range(1,100,2))

In [2]: a = b/len(range(1,100,2))

In [3]: b
Out[3]: 2500

In [4]: a
Out[4]: 50
``````

b =前100个奇数之和a =前100个奇数

1

5499 作者的声誉

``````i=1
total=0
count=0
for i in range(100):
if i%2 != 0:
total=total+i
count=count+1
average=total/count
print 'total %d' % (total)
print 'count %d' % (count)
print 'average %d' % (average)
``````

1

1281 作者的声誉

``````1 + 3 + 5 + ... + 2n+1 = n^2
``````

``` def avgOfFirsrtNOdd(n): return n ```