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

Lists Operations

What is the result of this code? nums = [10, 9, 8, 7, 6, 5] nums[0] = nums[1] - 5 if 4 in nums: print(nums[3]) else: print(nums[4]) Explain me this question.

16th Aug 2020, 1:35 PM
Jash Sampat
4 Answers
+ 1
num[1] returns the elements at 1st index of nums that is 9 ,so 9-5=4 nums[0]=4 ,list looks like this now [4,9,8,7,6,5] if 4 in nums: check if number 4 is in list or not and since it is in list condition is satisfied and following statement is executed, print(num[3] which prints the element in num at 3rd index which is 7
16th Aug 2020, 1:40 PM
Abhay
Abhay - avatar
+ 7
kirit sampat , i am sure that it will be a big benefit for for your learning progress if you are going (to continue) to work through the python tutorial here in sololearn. To get successful in coding, you need to know and get familiar with these basics. also do practice with simple python challenges. Happy coding.
16th Aug 2020, 2:39 PM
Lothar
Lothar - avatar
+ 1
Output is 7 because, nums[0] = nums[1]-5 means nums[0] = 9-5 = 4 (nums[1] = 9). It adds 4 at the beginning of the list. The if condition test if 4 in the list. If 4 is in list it prints nums[3] = 7. I hope you understand.
16th Aug 2020, 1:44 PM
Allamprabhu Hiremath
0
num[1] returns the elements at 1st index of nums that is 9 ,so 9-5=4 nums[0]=4 ,list looks like this now [4,9,8,7,6,5] if 4 in nums: check if number 4 is in list or not and since it is in list condition is satisfied and following statement is executed, print(num[3] which prints the element in num at 3rd index which is 7
23rd Oct 2021, 1:28 PM
Ngozichukwu Iheanyi