First time here? Check out the FAQ!

Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

answered 10 years ago

kcrisman gravatar image
for k in range(2,n + 2,2):
    print(eval("P_%s"%k))

Ugly, I admit. It's hard to just stick in these things. Niles' solution is better.

click to hide/show revision 2
No.2 Revision
for k in range(2,n + 2,2):
    print(eval("P_%s"%k))

Ugly, I admit. It's hard to just stick in these things. Niles' solution is better.

Edit: But if you insist (I note you also used print in your original example, so it was hard to anticipate your next issue)...

sage: n = 5
sage: P = list(var('P_%d' % i) for i in range(2*n + 1))
sage: for k in range(2,n + 2,2):
....:     tem = eval('P_%s' %k)
....:     x.subs(x=tem)
....:     
P_2
P_4
P_6