What will be the output of the following statements? i. list1 = [12,32,65,26,80,10] list1.sort() print(list1) ii. list1 = [12,32,65,26,80,10] sorted(list1) print(list1) iii. list1 = [1,2,3,4,5,6,7,8,9,10] list1[::-2] list1[:3] + list1[3:] iv. list1 = [1,2,3,4,5] list1[len(list1)-1]
Question
What will be the output of the following statements?
i.
list1 = [12,32,65,26,80,10]
list1.sort()
print(list1)
ii.
list1 = [12,32,65,26,80,10]
sorted(list1)
print(list1)
iii.
list1 = [1,2,3,4,5,6,7,8,9,10]
list1[::-2]
list1[:3] + list1[3:]
iv.
list1 = [1,2,3,4,5]
list1[len(list1)-1]
✨ Free AI Tools for You
Powered by Chatterbot AI
Answer
Output for the given statements:
i.
list1 = [12, 32, 65, 26, 80, 10]
list1.sort()
print(list1)
Output:
[10, 12, 26, 32, 65, 80]
Explanation: The sort()
method modifies list1
in place to be in ascending order.
ii.
list1 = [12, 32, 65, 26, 80, 10]
sorted(list1)
print(list1)
Output:
[12, 32, 65, 26, 80, 10]
Explanation: The sorted()
function returns a new sorted list, leaving list1
unchanged.
iii.
list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
list1[::-2]
list1[:3] + list1[3:]
Output:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Explanation: The slicing operations list1[::-2]
and list1[:3] + list1[3:]
do not modify list1
. They produce new lists which are not assigned to any variable. So, the original list1
remains unchanged.
iv.
list1 = [1, 2, 3, 4, 5]
list1[len(list1)-1]
Output:
5
Explanation: list1[len(list1)-1]
returns the last element of list1
, which is 5.
India's 1st AI Doubt Solver for CBSE, JEE, and NEET
Ask a Question for Freeand then it's just ₹212 a month