x=1
code(x)=5
print(code1)
I want this to work andit print "5" could anybody help me do this in python.
x=1
code(x)=5
print(code1)
I want this to work andit print "5" could anybody help me do this in python.
You can use "code" as a list.
x=1
code=[0,0]
code[x]=5
print(code[1])
or as a dict:
x=1
code={}
code[x]=5
print(code[1])
depends on how you will use "code".
myList=[1,2,3,4,5,6]
print myList[2]
will print 3. You can assign an index to a variable and do this:
index=2
print myList[index]
will print 3.
As the other answer suggested, you'll better use dictionary or a list for this.
But for your question you could it like this:
x = 1
exec(('code' + x) + '=5')
print code1
code1
will be 5.
code=[0,0]
x=1
code[x]=5
print(code[1])