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

list 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]) How does this works?

16th Apr 2020, 8:39 AM
PRIYANKA K
PRIYANKA K - avatar
2 Answers
+ 2
nums = [10, 9, 8, 7, 6, 5] nums[0] = nums[1] - 5 # change 1st element with 2nd element minus 5 (9 - 5 -> 4) print(nums) # see how <nums> had changed, 1st element is now 4, not 10 anymore if 4 in nums: # does <num> contain 4? yes print(nums[3]) # print the 4th list element -> 7 else: # if <nums> doesn't contain 4 print(nums[4]) # print the 5th list element -> 6
16th Apr 2020, 9:00 AM
Ipang
+ 1
What don't you understand ? The "in" ?
16th Apr 2020, 8:54 AM
Gwlanbzh
Gwlanbzh - avatar