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

Please have it written in Python. Write a for loop that iterates over a list of

ID: 3837126 • Letter: P

Question

Please have it written in Python.

Write a for loop that iterates over a list of numbers num list and prints the numbers in the list whose square is divisible by 8. For example, if num list is [2, 3, 4, 5, 6, 7, 8, 9], then the numbers 4 and 8 should be printed. Given list_ a = [30, 1, 12, 14, 10, 0] How many elements are in 1st? What is the index of the first element in list_ a? What is the index of the last element in list_ a? What is list_ a[2]? What is list_ a[-2]? Given list_1 = [3, 1, 2, 4, 0], what is the return value of each of the following statement? [x for x in list_1 if x > 1] [x * x for x in list_1] Using list comprehension, create the following lists [10, 8, 6, 4, 2] [0, 2, 4, 6, 8] What are list_ x and list_ y after the following lines of code? List_1 = [1, 43] List_ y = [x for x in list_ x] List_ x[0] = 22

Explanation / Answer

Write a for loop that iterates over a list of number num_list and prints the numbers in the
list whose square is divisible by 8.
for num in num_list:
   if (num * num) % 8 == 0:
       print num,
print

B. Given list_a = [30, 1, 12, 14, 10, 0]
a) How many elements are in lst?
print len(list_a)   #Will print the number of elements in the list, i.e., 6.
b) What is the index of the first element in list_a? Always, the list index in python starts with 0.
So, the index of the first element in list_a is 0.
c) What is the index of the last element in list_a?
len(list_a)-1 is the index of the last element. The index starts from 0, and will continue
till n-1, where n is the number of elements in the list. In this case, the index of last
element is: 5.
d) What is list_a[2]? This points to the 3rd element in the list, the first being list_a[0],
and second being list_a[1]. Therefore, list_a[2], i.e., the third element is: 12.
e) What is list_a[-2]? This will print the penultimate element in the list. -2 represent the
second element from the last in reverse direction. So, list_a[-2] is 10 in this case.