Module 2 quiz | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Module 2 quiz

Fill in the blanks to print the first element of the list,if it contains even number of elements. List = [ 1, 2, 3 , 4 ] If___(list) % 2 == 0_ Print(list [ _ ]) Hello guys, please i need an explanation , so as to understand this question and the answer.

15th Jun 2020, 1:10 PM
Oriola oluwafemi
Oriola oluwafemi - avatar
2 Answers
+ 4
>>CORRECT CODE List=[1,2,3,4] if len(List)%2==0: print(List[0]) >>There are error in your code List = [ 1, 2, 3 , 4 ] If___(list) % 2 == 0_ In line 2 error-- # "If" should be replaced by "if" # "list" should be replaced by "List" Print(list [ _ ]) In line 3 error-- # Indentation error.the print statement should be inside the if block. #in place of "Print" it should be "print". #"list" should be replaced by "List". >>EXPLANATION List=[1,2,3,4] if len(List)%2==0: #👆 here we have used "len" since in question you have asked if it has even length.so finding the length of list and then finding the remainder by taking "%". If its equal to 0 means it's of even length and then it will print the first element of the list else the condition will be false. print(List[0]) #print first element if of even length.
15th Jun 2020, 1:33 PM
SOUMYA
SOUMYA - avatar
+ 1
Thanks
15th Jun 2020, 6:20 PM
Oriola oluwafemi
Oriola oluwafemi - avatar