I've been far from python for a while. when I try bellow code it gives me Index Error
n = int(input())
array = []
for i in range(n):
array[i] = i+1
I've been far from python for a while. when I try bellow code it gives me Index Error
n = int(input())
array = []
for i in range(n):
array[i] = i+1
Use append method:
n = int(input())
array = []
for i in range(n):
array.append(i+1)
Your error appears because you are calling array[0]
which doesn't exist and trying to assign value to it.
If you decide to create a list, then you should append elements to your list:
myList = list()
list.append(element)
You can also create a dictionary where you can index elements:
myDict = dict()
mydict[i] = element
I made an obvious mistake because I'm learning C right know. in C you can do it in this way:
for (i = 0; i < n; i++)
{
array [i] = i+1
}
but in python you must put your element you want to insert inside a bracket:
for i in range(n):
array += [i+1]