Python Forum
Operations on indexed variables in loop - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Operations on indexed variables in loop (/thread-20493.html)



Operations on indexed variables in loop - Fibulavie - Aug-14-2019

Hello,
Beginner in Python and having spent about 4 hours in this very simple operation, I now rely on your expertise. I'm used to Javascript and in JS you can when you have sets of variables defined (say they're array or matrices), create a loop on those variables indexes to create new ones.
The input variables are defined such as v1, v2, v3, ..., v10 and I want to create new variables such as v11, v12, ..., v20; using the numbers in the name of the variables in the loop index
in my language it'd be:
for i in range (0,10):
	v[i+10]=v[i]/2
Must precise that I'm using Python code into an embedded software so it knows what are the v1, v2, etc...
Any idea what's wrong in here?
Many thanks!


RE: Operations on indexed variables in loop - fishhook - Aug-14-2019

(Aug-14-2019, 05:36 AM)Fibulavie Wrote: Any idea what's wrong in here?

All is wrong. You don't need to create a lot of the same variables, just use lists or dictionaries instead. Every book I read explains lists and dicts on the first ten pages.