0

I'm a beginner of python and I have a question

I defined a function named testfunc like below

def testfunc(num) :
    result = num ** 2
    return result

This is just for test so I made very simple function

And I want to declare many variables.

x1 = testfunc(1)
x2 = testfunc(2)
x3 = testfunc(3)

like this..

If there are a little variables, I can write all of them.

But how about in case of many variables? (about 1000 or 100000)

Is there any good way to declare many variables using loop?

  • 1
    Use a list. `xs = [testfunc(i) for i in range(1000)]` – Aran-Fey Feb 21 '18 at 04:50
  • thanks.. It could be one of solution but I want to declare variables independently, not in list.. I really appreciate to you. I should remember this way – youngjo89 Feb 21 '18 at 04:55
  • There's an answer in that post that explains how to spawn variables dynamically. Just scroll down a bit. – Aran-Fey Feb 21 '18 at 04:56

0 Answers0