Tuesday, November 19, 2019

sort method in python

a1=[30,5,14,8,0]
a1.sort()
print(a1)

output:
[0, 5, 8, 14, 30]

example2:

a1=[30,5,14,8,0]
a1.sort()
print(a1)
a2=["bat","ant","dog","cat"]
a2.sort()
print(a2)

output:
[0, 5, 8, 14, 30]
['ant', 'bat', 'cat', 'dog']

example3:
if upper case strings and lowercase strings are present then it will shows the uppercase string first then it will shows the lower case strings

a1=[30,5,14,8,0]
a1.sort()
print(a1)
a2=["Bat","ant","Dog","Cat"]
a2.sort()
print(a2)

output:
[0, 5, 8, 14, 30]
['Bat', 'Cat', 'Dog', 'ant']

example 4:
strings and integers with in list then they will not sorted .compiler will shows the error

a1=[10,'B',20,'C',4,'A']
a1.sort()
print(a1)

output:
TypeError: '<' not supported between instances of 'str' and 'int'

No comments:

Post a Comment

python class topic video