Tuesday, 19 September 2017

How to play with list in Python ?


EXAMPLE
fish = ['f1', 'f2', 'f3']
print(type(fish))
print(fish)


<class 'list'>
['f1', 'f2', 'f3']


fish.append('f4')
print(fish)


['f1', 'f2', 'f3', 'f4']



fish.insert(0, 'f0')
print(fish)

['f0', 'f1', 'f2', 'f3', 'f4']

more_fish = ['f6', 'f7', 'f8', 'f9', 'f10']
fish.extend(more_fish)
fish.extend(more_fish[0:2])

fish2 = fish.copy()
print(fish2)


['f0', 'f1', 'f2', 'f3', 'f4', 'f6', 'f7', 'f8', 'f9', 'f10', 'f6', 'f7']

fish.reverse()
print(fish)


['f7', 'f6', 'f10', 'f9', 'f8', 'f7', 'f6', 'f4', 'f3', 'f2', 'f1', 'f0']


fish_ages = [1,2,5,7,8,1]
print(fish_ages.count(1))



2

fish_ages.sort()
print(fish_ages)


[1, 1, 2, 5, 7, 8]





fish.clear()
print(fish)


[]

No comments:

Post a Comment

Note: only a member of this blog may post a comment.