List Methods

Python has built-in methods that are designed to perform operations on lists. For append adds a new element to the end of a list:
>>>t = ['a','b','c']
>>>t.append('d')
>>>print t
['a', 'b', 'c', 'd']
Extend method takes a list as an argument and appends all of the elements:
t1 = ['a', 'b', 'c']
t2 = ['d', 'e']
t1.extend(t2)
print t1
['a', 'b', 'c', 'd', 'e']
Sort method arranges the elements of the list from low to high:
>>> t = ['b', 'c', 'a', 'z', 'd']
>>> t.sort()
>>> print t 
['a', 'b', 'c', 'd', 'z']
Most list methods are void type. This means that they don’t return any result at all (shortly they return None type). If you write t = t.sort(), you will be disappointed with the result.
>>> t = t.sort()
>>> t

Nema komentara:

Objavi komentar