-3

I need to have different results for different "i"s so whenever i call for example a_8, it should return me the exact value. but python doesnt assign diferrent "i"s to it and just knows a_i.

1.for i in range(10): a_i = i + 5

merv
  • 67,214
  • 13
  • 180
  • 245

2 Answers2

0

You can use a list:

a = []
for i in range(10): a.append(i+5)

or a dictionary:

a = {}
for i in range(10): a[i] = i+5

In both cases, you can access the value later with

for i in range(10): print(a[i])

What I should use on my case? Take a look at this answer.

Latra
  • 492
  • 3
  • 14
  • in this case the index of list is equal to the value of i, what if it was not like this or what if we had 2 variable i and j – Reza Bahmani Nov 20 '21 at 12:12
0

for i in range(10):
globals()['a_' + str(i)] = i + 5
print(a_6)

but i don't think you should use it, better use dicts instead.

Meison
  • 1