I have been trying to be comfortable with the sum() in python, I did understand the basic function of sum but as a mathematica backgroud,I was just inquistive to know can we use sum in python in the same way like we do in mathematica for example consider this mathematica module:
Sq[a_, b_] := Module[{m, n}, m = Max[a, b]; n = Min[a, b];Sum[(m - r + 1) (n - r + 1), {r, 1, n}]]
Now,could it be possible to write the sum part like that? I mean:
Sum[(m - r + 1) (n - r + 1), {r, 1, n}]
Trying to covert this in python,I think of something like this:
sum((m - r + 1) (n - r + 1) in xrange(1,n+1))
but doesn't seems to be working! so my question how to get it work?
sum((m - r + 1) * (n - r + 1) for r in xrange(1,n+1))
*
.f(x) for x in xes
is the general format of a list comprehension, where you want x
to iterate through every element of xes
, and give back the value f(x)
.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With