Wednesday, November 20, 2019

sorting the element of the list in reverse using sort method

a1=[20,5,16,10,8]
a1.sort()
print(a1)
a1.sort(reverse=True)
print(a1)

output:

[5, 8, 10, 16, 20]
[20, 16, 10, 8, 5]

example:
if reverse is false then it will give normal sorting order only
a1=[20,5,16,10,8]
a1.sort()
print(a1)
a1.sort(reverse=True)
print(a1)
a1.sort(reverse=False)
print(a1)

output:
[5, 8, 10, 16, 20]
[20, 16, 10, 8, 5]
[5, 8, 10, 16, 20]

No comments:

Post a Comment

python class topic video