Python3 list.sort()方法

sort()方法排序列表中的對象,使用 func 比較(如果給定)。

語法

下麵是 sort()方法的語法 -
list.sort([func])

參數

NA

返回值

此方法不返回任何值,但反轉列表中給定的對象。

示例

下麵的示例演示 sort()方法的使用。
#!/usr/bin/python3
list1 = ['physics', 'Biology', 'chemistry', 'maths']
list1.sort()
print ("list now : ", list1)

當我們運行上面的程式,會產生以下結果 -
list now :  ['Biology', 'chemistry', 'maths', 'physics']


上一篇: Python3字串 下一篇: Python3列表