- sort() and sorted() both are the python in-build function which are used to sort the data of list and string.
- sort() is only applicable for list data type.
- sorted() is a function which sort the list as well as other data type like string,set data type etc.
Syntax-
list.sort()
sorted(seq)
program-
l=[3,7,4,1,2,9,0]
print("List is",l)
l.sort()
print("Sorted list is",l)
print("Sorted list is",sorted(l))
s="mytext"
print("String is ",s)
print("String to sorted list is-",sorted(s))
l=[4,3,1,2]
s=set(l)
print("Sorted set is-",sorted(s))
List is [3, 7, 4, 1, 2, 9, 0]
Sorted list is [0, 1, 2, 3, 4, 7, 9]
Sorted list is [0, 1, 2, 3, 4, 7, 9]
String is mytext
String to sorted list is- ['e', 'm', 't', 't', 'x', 'y']
Sorted set is- [1, 2, 3, 4]