Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

General Question: In PYTHON, what does indexing do? For example, for the followi

ID: 3747301 • Letter: G

Question

General Question:

In PYTHON, what does indexing do?

For example, for the following code that finds the sample variance of a mean for set of values, why do I have to use "range(len(array))" and not just "len(array)" ??? because both will iterate through the array and find the value at each index right?

and why do i have to use "(array[i] - mean)" instead of just "(i - mean)". Same thing woudn't array[i] and i both return the same thing???

def my_sample_var(array):
  
mean = sum(array)/len(array)
sample_var = 0
array_length = len(array)
  
for i in range(len(array)):
sample_var += (array[i] - mean)**2
return 1/(array_length - 1) * sample_var

  

Explanation / Answer

range(len(array)) is a generator and it gives/generates values to i from 0 to len(array)-1 every-time through loop. len(array) is just a single number which represents the length of the array. array[i] is the element at ith location in the array. for example if array is [5, 3, 8, 6]. i values go from 0 to 3. and element at 0th index is 5. which means array[0] is 5.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote