Is it possible to name the variables in Python by programs:
For instance, something like:
for i in range(3):
var_%%i = i
And after the piece of code, we will have three variables: var_0
, var_1
and var_2
.
Is it possible to name the variables in Python by programs:
For instance, something like:
for i in range(3):
var_%%i = i
And after the piece of code, we will have three variables: var_0
, var_1
and var_2
.
Is it possible to name the variables in Python by programs
Yes, but it is really, extremely bad practice.
And after the piece of code, we will have three variables: var_0, var_1 and var_2.
If you really want to do this, you could use exec
(note - REALLY bad idea, security issues and all that):
for i in range(3):
exec("var_" + str(i) + " = i")
However, DO NOT DO THIS. IT IS BAD PROGRAMMING PRACTICE
See this question for a more practical solution involving dictionaries.
Also, see this answer about a second approach, and why it is VERY, very bad to use either of the approaches.
But seriously.