sort() and sorted() function in python by R4R Team

- 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))


output-

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]

- Here output of sorted() function is always a list, but input may be list,string,set etc.




Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!