I know how assign items to empty array using append :
matri=[]
def matr_meth(n):
for i in range(1,n 1):
matri.append(i)
return matri
print(matr_meth(6))
but here I saw another way that I don't get how can we assign the index to the name of array matri =[i] , I would get it if matri[i] =i 1 for exaple cuz alawys array is compared to index like c and c maybe , second in the new method it won't work if I define the matri=[] before the method same as I did in the append way:
def matr_meth(n):
matri=[]
for i in range(1,n 1):
matri =[i]
return matri
print(matr_meth(6))
CodePudding user response:
You've stumbled upon two related but distinct questions here. Let's start with the first one. [i], on its own, is not an array subscript operator. It's a list with one element in it. So
matri = [i]
is really just
matri = matri [i]
or, more verbosely,
my_temporary_list = [i]
matri = matri my_temporary_list
and is defined on lists to be concatenation. So it's just a fancier way of putting the two lists together. It's not really useful in this example (I would definitely use .append if I only had one element to append), but if you have a whole list (whose length might be greater than one), it's quicker to append it in one go. For instance, your for loop
for i in range(1,n 1):
matri =[i]
can actually be written as
matri = range(1, n 1)
= on list can take any iterable, such as range, as an argument.
Now for your second question.
def matr_meth(n):
matri=[]
for i in range(1,n 1):
matri = [i]
return matri
Here, matri is a local variable in matr_meth.
matri=[]
def matr_meth(n):
for i in range(1,n 1):
matri.append(i)
return matri
Here, matri is a global variable, plain and simple. Where it gets nebulous is
matri=[]
def matr_meth(n):
for i in range(1,n 1):
matri = [i]
return matri
Now we're assigning to matri both inside and outside of the function (remember, = is just a shortcut for = and together). So Python could reasonably assume that matri is a global variable, or it could reasonably assume that you meant to make a new local variable inside the function. To avoid action-at-a-distance errors, Python makes the conservative choice to assume you meant matri to be a new local variable. Since you intended the opposite in this case, just use the global keyword.
matri=[]
def matr_meth(n):
global matri
for i in range(1,n 1):
matri = [i]
return matri
CodePudding user response:
Lists in Python support the addition operator:
>>> [1, 2, 3] [4, 5]
[1, 2, 3, 4, 5]
With that loop you're effectively extending the matri list with single-element lists, i.e.:
| i | matri | [i] | matri [i] |
| - | --------------- | --- | ---------------------------- |
| 1 | [] | [1] | [] [1] |
| 2 | [1] | [2] | [1] [2] |
| 3 | [1, 2] | [3] | [1, 2] [3] |
...
| 6 | [1, 2, 3, 4, 5] | [6] | [1, 2, 3, 4, 5] [6] |
CodePudding user response:
def matr_meth(n):
matri=[]
for i in range(1,n 1):
matri = [i]
return matri
You initialized matri=[] as an empty list, which is a list. [i] is also a list. If you apply the operator between two lists, it is essentially appending the second list to the first list.
In the for loop you are appending every [i] to matri. matri keeps changing in the for loop. You can view it by using the print function.
def matr_meth(n):
matri=[]
for i in range(1,n 1):
matri = [i]
print('At the ' str(i) '-th iteration, matri is: ' )
print(matri)
return matri
matr_meth(6)
Output:
At the 1-th iteration, matri is:
[1]
At the 2-th iteration, matri is:
[1, 2]
At the 3-th iteration, matri is:
[1, 2, 3]
At the 4-th iteration, matri is:
[1, 2, 3, 4]
At the 5-th iteration, matri is:
[1, 2, 3, 4, 5]
At the 6-th iteration, matri is:
[1, 2, 3, 4, 5, 6]
