0

what is the different between ?

list =[101,23,403,54,45]; list.sort(); print(list); and list =[101,23,403,54,45]; list=list.sort(); print(list);

31st May 2021, 9:19 AM
𝗣𝗮𝘄𝗮𝗻 𝗞𝘂𝗺𝗮𝗿 🅟︎🅚︎
𝗣𝗮𝘄𝗮𝗻 𝗞𝘂𝗺𝗮𝗿 🅟︎🅚︎ - avatar
7 Réponses
+ 1
in python you can do list sorting by two ways: sorted(my_list) return a sorted copy of my_list (my_list is kept unchanged) my_list.sort() sort in-place (my_list is updated to be sorted, no return value -- ie: None is returned)
31st May 2021, 5:47 PM
visph
visph - avatar
+ 5
Pawan Kumar In second case actually you are printing list.sort() not list so list.sort() will print None.
31st May 2021, 9:25 AM
A͢J
A͢J - avatar
+ 1
Hmm I understand
31st May 2021, 1:40 PM
𝗣𝗮𝘄𝗮𝗻 𝗞𝘂𝗺𝗮𝗿 🅟︎🅚︎
𝗣𝗮𝘄𝗮𝗻 𝗞𝘂𝗺𝗮𝗿 🅟︎🅚︎ - avatar
0
But in case 5 print
31st May 2021, 1:09 PM
𝗣𝗮𝘄𝗮𝗻 𝗞𝘂𝗺𝗮𝗿 🅟︎🅚︎
𝗣𝗮𝘄𝗮𝗻 𝗞𝘂𝗺𝗮𝗿 🅟︎🅚︎ - avatar
0
name="pawan"; get=len(name); print(get);
31st May 2021, 1:10 PM
𝗣𝗮𝘄𝗮𝗻 𝗞𝘂𝗺𝗮𝗿 🅟︎🅚︎
𝗣𝗮𝘄𝗮𝗻 𝗞𝘂𝗺𝗮𝗿 🅟︎🅚︎ - avatar
0
Pawan Kumar Here you are getting length of string which returns integer. In above case you are printing sort() function which print None because function sort() doesn't return anything so if you print that function then None will be display.
31st May 2021, 1:23 PM
A͢J
A͢J - avatar
0
Pawan Kumar len () function returns integer value but sort() doesn't returns anything so it will display None.
31st May 2021, 1:24 PM
A͢J
A͢J - avatar