1

experts i want to get the sum of the elements generated by for loop(x) in python.I wrote a programme for that but i am not getting the sum of x values.please help.

import numpy as np

f=np.arange(1,2,1)

for i in f:
    #print(i)
    for m in np.arange(1,6,1):
        x=((4.6)*(m*2)*(5)**2)*(i)/62
        print(x)
        #print(np.sum(x))
1
  • It looks like you were printing out intermediate values as you were writing this code to get a better sense of what was happening. Well done. Commented Dec 6, 2020 at 11:27

3 Answers 3

2

As I suppose, you want to compute the sum of all generated values.

The first, "classic" and not very efficient method is to create a list, append each generaed element to this list and then compute the sum of this list:

f = np.arange(1,2)
tbl = []
for i in f:
    for m in np.arange(1,6):
        x = ((4.6)*(m*2)*(5)**2)*(i)/62
        print(f'{i}, {m}: {x:.4f}')
        tbl.append(x)
print(sum(tbl))

The result is:

1, 1: 3.7097
1, 2: 7.4194
1, 3: 11.1290
1, 4: 14.8387
1, 5: 18.5484
55.64516129032258

But a more efficient and numpythonic option is to use np.fromfunction method to generate the whole Numpy array in one go and then compute its sum:

arr = np.fromfunction(lambda i, m: 4.6 * m * 2 * 5 ** 2 * i / 62, (2, 6))
print(arr)
print(arr.sum())

Note that I removed unncessary parentheses from your code.

Your function can actually be yet more simplified to:

lambda i, m: i * m * 9.2 * 5 ** 2 / 62

This time the result is:

[[ 0.          0.          0.          0.          0.          0.        ]
 [ 0.          3.70967742  7.41935484 11.12903226 14.83870968 18.5483871 ]]
55.64516129032258

Edit

From one of other answers I see that you may be interested in a sequence of cumulative sums of your values.

After you generate arr, you can generate such a sequence with:

np.cumsum(arr[1:,1:])

even for more than one rows (values of i). Test it e.g. for shape of (3, 6).

Sign up to request clarification or add additional context in comments.

Comments

1

Declare and initialize a variable outside the loop and add x to that variable.

import numpy as np

f=np.arange(1,5,1)

for i in f:
    sum = 0
    for m in np.arange(1,6,1):
        x=((4.6)*(m*2)*(5)**2)*(i)/62
        sum += x
    print(i, sum)

Output:

1 55.64516129032258
2 111.29032258064515
3 166.93548387096774
4 222.5806451612903

6 Comments

btw Don't use sum as a variable name. It is a built-in.
sum is not a keyword in python. Following code will return False, let me know if I am missing something #### import keyword print('sum' in keyword.kwlist)
@glkshu Please tell what is your excepted output in both cases np.arange(1,2,1) and np.arange(1,5,1)
sum is not a keyword in python. It is a built-in, just like enumerate, list or zip.
Ok. got your point. Updated code. Please check if this works
|
0

Just append your x to a list and use sum().

import numpy as np



f=np.arange(1,2,1)

for i in f:
    items = []
    for m in np.arange(1,6,1):
        x=((4.6)*(m*2)*(5)**2)*(i)/62
        items.append(x)
    print(sum(items))

3 Comments

Well, I don't know what answers you are looking for. What are the expected values for your two scenarios?
Sorry, I still don’t know what answers you are looking for. Perhaps you could update your question with some sample inputs and outputs.
Ok, now printing multiple sums.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.