[Tutor] Help

Danny Yoo dyoo at hashcollision.org
Sat Mar 7 19:25:19 CET 2015


On Sat, Mar 7, 2015 at 7:39 AM, elie khairallah
<eliekhairallah1996 at gmail.com> wrote:
> hello , I would like to know if theres a way to change a variable's name
> after every iteration for example I want to make a function that stores a
> number in x_1 if i=1 and in x_2 if i=2.

Conceptually, I think you're looking for a list.  The "subscript"
you're using can be implemented as a list indexing operation.

e.g.:

############
x = [0, 0]
x[0] = 5
x[1] = 4
print x[0]
print x[1]
##############

The index we're using can be an arbitrary expression, not just a fixed number:

################
messages = [0] * 5
for i in range(5):
    messages[i] = ("This is %d" % i)
print messages
################


More information about the Tutor mailing list