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?
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.
-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
B numbers[:-1] gives tuple (10,9,12,1) sorted(numbers[:-1]) returns sorted list (1,9,10,12)
numbers is a tuple, then slice a tuple without last element, except the list. For last, the first four elements from the tuple are sorted, and sorted returns a sorted list with 4 numbers in increasing order.
United States Tendenze
- 1. #VERZUZ 25.2K posts
- 2. No Limit 20.6K posts
- 3. Mia X 572K posts
- 4. Wayne 31.9K posts
- 5. #HalloweenHavoc 34.4K posts
- 6. Birdman 3,541 posts
- 7. Tamar 6,405 posts
- 8. Master P 3,529 posts
- 9. Snoop 7,349 posts
- 10. Juvie 2,451 posts
- 11. Bama 14.8K posts
- 12. Mississippi State 6,102 posts
- 13. Silkk 1,326 posts
- 14. South Carolina 17K posts
- 15. Choppa Style 1,314 posts
- 16. Kelly Price N/A
- 17. Tatum 19.4K posts
- 18. Marcel Reed 1,759 posts
- 19. Semaj Morgan N/A
- 20. Turk 572K posts
Something went wrong.
Something went wrong.