python中求1到100的和(循环变量的和)
>>> list(range(10)) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> list(range(1,10)) [1, 2, 3, 4, 5, 6, 7, 8, 9] >>> list(range(1,11)) [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] >>> sum = 0 >>> for i in range(1,101): sum = sum + i >>> print(sum) 5050 >>> test1 = [2,5,8,3] >>> sum = 0 >>> for i in test1: sum = sum + i >>> print(sum)
18