Amount () in python

I try to be convenient with sum () in python, I understood the main function of sum, but as a mathematical backgroud, I was just inquisitive to know if we can use the sum in python in the same way as we do in mathematics, for example, consider this mathematical 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, is it possible to record such an amount? I mean:

 Sum[(m - r + 1) (n - r + 1), {r, 1, n}] 

Trying to hide this in python, I am thinking of something like this:

 sum((m - r + 1) (n - r + 1) in xrange(1,n+1)) 

but doesn't seem to work! so my question is how to make it work?

+4
source share
1 answer
 sum((m - r + 1) * (n - r + 1) for r in xrange(1,n+1)) 
  • There is no implicit multiplication between integers, so you need * .
  • f(x) for x in xes is a general list comprehension format where you want x iterate through each xes element and return the value of f(x) .
+13
source

All Articles