Is there a way for a Python program to create new variables, loops, etc within itself? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Is there a way for a Python program to create new variables, loops, etc within itself?

13th May 2021, 11:02 PM
Rodrigo Nicolás Baca Moreno
Rodrigo Nicolás Baca Moreno - avatar
2 Answers
+ 2
Yes. You could literal prompt used for commands and execute them. Or generate code and execute that. The command you need is eval().
13th May 2021, 11:10 PM
Jerry Hobby
Jerry Hobby - avatar
+ 2
You might have a look at the exec() function. [ Be careful to use that. ] >> a = exec("for i in range(10): print(i)")
14th May 2021, 12:06 AM
Md. Faheem Hossain
Md. Faheem Hossain - avatar