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
![python_spaces's tweet image. 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](https://pbs.twimg.com/media/FhhhXulUcAEA0Zx.jpg)
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 🧵👇
![python_spaces's tweet image. 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 🧵👇](https://pbs.twimg.com/media/FhCXtuEakAEguwk.jpg)
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
![python_spaces's tweet image. 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](https://pbs.twimg.com/media/FhhhXulUcAEA0Zx.jpg)
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]) []
-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) [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)
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 الاتجاهات
- 1. Chauncey Billups 101K posts
- 2. #PlayersOverProfits 2,679 posts
- 3. Terry Rozier 92.2K posts
- 4. Candace 35.6K posts
- 5. $INTC 36.1K posts
- 6. MANE EN BILLBOARD 21K posts
- 7. Aaron Jones 2,353 posts
- 8. Binance 218K posts
- 9. #RepublicansAreAWOL 7,909 posts
- 10. The Anomaly 2,181 posts
- 11. Dragon 86.3K posts
- 12. Mafia 132K posts
- 13. Ti West 3,805 posts
- 14. Stephen A 26.2K posts
- 15. Fallout 4 1,171 posts
- 16. Rob Schneider N/A
- 17. Bethesda 9,174 posts
- 18. Changpeng Zhao 23.7K posts
- 19. Bannon 7,843 posts
- 20. Pete Rose 2,412 posts
Something went wrong.
Something went wrong.