Python Quiz #4 What is the output of the following code snippet? A) 10 B) [1, 9, 10, 12] C) [11, 13, 14, 15] D) Error
Answer to the previous question: C
Python Quiz # 3 What is the output of this code snippet? A) ['abc', 'y', 'z'] B) [1, 2, 3] C) ['x', 'y', 'z'] D) All of the above Answer for Quiz # 2 in the thread 🧵👇
That's a wrap! I hope you learnt something new today. Follow me @python_spaces for more such content. Like and RT the first tweet to share with your friends. Cheers 🎉
Python Quiz #4 What is the output of the following code snippet? A) 10 B) [1, 9, 10, 12] C) [11, 13, 14, 15] D) Error
How did you end up with the answer?? Can you please explain?
How did you end up with the answer?? Can you please explain?
How did you end up with the answer?? Can you please explain?
B as the array is sorted by values of > or < but not before the last index is removed by the offset of -1
B... numbers[:-1] = 10,9,12,1 sorted(numbers[:-1]) = [1,9,10,12]
B this is because python sees the list at the end as the last value and since we’re using negative indexes to locate the last value, it’s not part of the sorted data
>>> nums=10,9,12,1,[11,13,15,14] >>> sorted(nums[:-1]) [1, 9, 10, 12] >>> sorted(nums[:-2]) [9, 10, 12] >>> sorted(nums[:0]) []
B) [1, 9, 10, 12] because sorted() Return a new list containing all items from the iterable in ascending order.
B numbers[:-1] gives tuple (10,9,12,1) sorted(numbers[:-1]) returns sorted list (1,9,10,12)
-1 index contains the whole list [11,13,15,14] for our list numbers... numbers[:-1] gives the output except the values at index -1.. Finally, sorted sorts the list in ascending order as: 1,9,10,12
United States Trendy
- 1. Austin Reaves 36.8K posts
- 2. #LakeShow 2,624 posts
- 3. Trey Yesavage 35.1K posts
- 4. Jake LaRavia 4,431 posts
- 5. Blue Jays 61K posts
- 6. #LoveIsBlind 4,094 posts
- 7. jungwoo 93.5K posts
- 8. doyoung 73.9K posts
- 9. #AEWDynamite 22.3K posts
- 10. Rudy Gobert 1,014 posts
- 11. #Lakers N/A
- 12. Snell 13.4K posts
- 13. Pelicans 4,204 posts
- 14. Jeremy Lin N/A
- 15. Devin Booker 1,124 posts
- 16. Anthony Davis 4,071 posts
- 17. #WorldSeries 66.7K posts
- 18. Wolves 62.6K posts
- 19. Kacie 1,736 posts
- 20. Happy Birthday Kat N/A
Something went wrong.
Something went wrong.