Ask Your Question
0

Why is sum(LIST[n], n, 0, 5) for a given list LIST wrong?

asked 2022-11-17 08:03:13 +0100

c.p. gravatar image

updated 2022-11-17 08:06:46 +0100

If I let

var('x,n')
monom=[x^n for n in range(5)]

then the $n$-th entry $x^n$ is obtained by monom[n]. Why cannot I sum all these? That is,

sum(monom[n], n, 0, 5)

yields error. How can I correctly make it to act as

sum(x^n, n, 0, 5)

?

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
1

answered 2022-11-18 18:49:52 +0100

Emmanuel Charpentier gravatar image

Sum accepts a list (or other iterable) as an argument. monom is an element of list.

You could write sum([monom[i] for i in range(5)]) to get what you mean...

edit flag offensive delete link more
1

answered 2022-11-17 12:23:23 +0100

achrzesz gravatar image

updated 2022-11-17 15:05:37 +0100

monom=[x^n for n in range(5)]
sum(monom)
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2022-11-17 08:03:13 +0100

Seen: 134 times

Last updated: Nov 18 '22