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.

8/16/2020 1:35:51 PM

Jash Sampat

+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

+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.

+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.

+1

kirit sampat you will eventually reach this module in the python tutorial: https://www.sololearn.com/learn/Python/2453/ and it should help you. In the mean time I would just work thru the lessons and practice coding what you are learning. In a few weeks time, you will be able to answer questions like you are asking without much problem. 😉👍 Happy Coding!

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