What is the use of len() and sort() functions of LIST data type with
suitable example?
Answers
Answer:
Sort() is used to sort the list in ascending order and Len() returns the length of the list, basically len() returns how many elements are there in a list but it gives in int format by counting the elements of the list.
Python provides the sort( ) method to sort the elements of a list.
Let x be a list [2, 6, 1, 4, 7, 3, 5], then,
This method can be used as:
x.sort( )
This will sort the list 'x' in ascending order.
[1, 2, 3, 4, 5, 6, 7]
If we want to sort the elements of the list into descending order, then we can mention 'reverse=True' in the sort method as:
x.sort(reverse=True)
This will sort the list 'x' into descending order.
[7, 6, 5, 4, 3, 2, 1]
To know the length of a list, we can use the len( ) function.
This function gives the number of elements in the list.
This function can be used as:
len(x)
This will return the length as:
7