pythonpython-2.7

How to do a math expression using x^2/y with range to print multiple outputs


I want this program to output x2/y, for y from 1 to 1000 and x from 1 to 1000.

I tried this code:

y = range(0, 1000)
x = range(0, 1000)
stuff = int(x ** 2 / y)
print stuff

But there is an error:

 Traceback (most recent call last):
   File "testingsci.py", line 3, in <module>
     stuff = int(x ** 2 / y)
 TypeError: unsupported operand type(s) for ** or pow(): 'list' and 'int'

How can I do this properly?


Solution

  • As a list comprehension:

    print [(x ** 2)/y for x in xrange(1, 1001) for y in xrange(1, 1001)]