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

PROBLEM ONE: For the following question, all answers should be based on the the

ID: 3739531 • Letter: P

Question

PROBLEM ONE:

For the following question, all answers should be based on the the list s provided:

s = [1,2,3,4,5,6,7,8,9,10]

Cases:

a. for i in range(s):

               print(i)

b. for i in s:

               print(i)

c. for i in s:

               print(s[i])

d. for i in s:

               print(s(i))

e. for i in range(len(s)):

               print(i)

f. for i in range(len(s)):

               print(s[i])

g. for i in range(11):

               print(i)

h. for i in range(len(10)):

               print(i)

Questions:

a. For each of the cases a-h, identify which of the cases prints out each VALUE of the list s.

b. For each of the cases a-h, identify which of the cases prints out each INDEX of the list s.

c. For each of the cases a-h, identify which cases 1) results in an error and 2) what is the cause of the error.

Explanation / Answer

(a) It will show an error as we are passing s ( a list ) as argument in range() function. but the function only accepts integer arguments. So, it causes error.

(b) It prints each each VALUE of the list s.

It is because we are using the in operator in for loop which returns one element at a time to variable i.

(c) It will cause error. It is because the value of i is the elements of list. When the last element is i, then i = 11.

So, when we try to access 12th element in list i.e : s[11], it cause index out of bounds error.

(d) It causes error as we are using s(i) to access the i th element. The correct way to access element in a list is

s[i]

(e) The rogram prints  out each INDEX of the list s.

It is because the loop iterates from 0 to len(s) , so it prints the indexes of list.

(f) It prints out each VALUE of the list s.

As we saw in (e), the value of i is each index of list. SO, we are printing the elements at index i. Hence, the program prints out each VALUE of the list s

(g)

The length of list is 10.

The loop runs from 0 to 10 as the last element i.e : 11 is not included in function range().

Hence, the loop iterates from 0 to 10. So, it neither prints the VALUE nor the INDEX.

(h) It shows error as we are passing int as argument in len() funciotn which is incorrect. Hence, it shows error. The arguement should be of type list.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote