List Slices

Slice operator also works on lists:
>>> t = ['a', 'b', 'c', 'd', 'e', 'f']
>>> t[1:3]
['b', 'c']
>>> t[:4]
['a', 'b', 'c', 'd']
>>> t[3:]
['d', 'e', 'f']
>>>t=[:]
['a', 'b', 'c', 'd', 'e', 'f']
The same thig works for strings. If you omit the first index, the slice starts at the beginning. If you omit the second index, the slice goes to the end. So if you omit both, the slice is a copy of the whole list.
A slice operator on the left side of an assignment can update multiple elements:
>>> t[1:3] =['xzy', 'yyz']
>>> t
['a', 'xzy', 'yyz', 'd', 'e', 'f']

Nema komentara:

Objavi komentar