In [1]:
l = [0, 10, 20, 30, 40, 50, 60]
print(l)
In [2]:
print(l[2:5])
In [3]:
print(l[:3])
In [4]:
print(l[3:])
In [5]:
print(l[:])
In [6]:
print(l[2:10])
In [7]:
print(l[5:2])
In [8]:
print(l[2:2])
In [9]:
print(l[10:20])
In [10]:
print(l[::2])
In [11]:
print(l[1::2])
In [12]:
print(l[::3])
In [13]:
print(l[2:5:2])
In [14]:
print(l[3:-1])
In [15]:
print(l[-2:])
In [16]:
print(l[-5:-2])
In [17]:
print(l[5:2:-1])
In [18]:
print(l[2:5:-1])
In [19]:
print(l[-2:-5:-1])
In [20]:
print(l[-2:2:-1])
In [21]:
print(l[5:2:-2])
In [22]:
print(l[::-1])
In [23]:
sl = slice(2, 5, 2)
print(sl)
In [24]:
print(type(sl))
In [25]:
print(l[sl])
In [26]:
sl = slice(2, 5)
print(sl)
In [27]:
print(l[sl])
In [28]:
sl = slice(2)
print(sl)
In [29]:
print(l[sl])
In [30]:
# sl = slice()
# TypeError: slice expected at least 1 arguments, got 0
In [31]:
sl = slice(None)
print(sl)
In [32]:
print(l[sl])
In [33]:
print(l)
In [34]:
l[2:5] = [200, 300, 400]
print(l)
In [35]:
l[2:5] = [-2, -3]
print(l)
In [36]:
l[2:4] = [2000, 3000, 4000, 5000]
print(l)
In [37]:
l[2:6] = [20000]
print(l)
In [38]:
# l[2:3] = 200
# TypeError: can only assign an iterable
In [39]:
l[1:4] = []
print(l)
In [40]:
l[20:60] = [-1, -2, -3]
print(l)
In [41]:
l[2:2] = [-100]
print(l)
In [42]:
print(l[:5:2])
In [43]:
l[:5:2] = [100, 200, 300]
print(l)
In [44]:
# l[:5:2] = [100, 200]
# ValueError: attempt to assign sequence of size 2 to extended slice of size 3
In [45]:
l_2d = [[0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10, 11]]
print(l_2d)
In [46]:
print(l_2d[1:3])
In [47]:
print([l[:2] for l in l_2d[1:3]])
In [48]:
l_2d_t = [list(x) for x in zip(*l_2d)]
print(l_2d_t)
In [49]:
print(l_2d_t[1])
In [50]:
l = [0, 10, 20, 30, 40, 50, 60]
print(l)
In [51]:
l_slice = l[2:5]
print(l_slice)
In [52]:
l_slice[1] = 300
print(l_slice)
In [53]:
print(l)
In [54]:
l_2d = [[0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10, 11]]
print(l_2d)
In [55]:
l_2d_slice = l_2d[1:3]
print(l_2d_slice)
In [56]:
l_2d_slice[0][1] = 400
print(l_2d_slice)
In [57]:
print(l_2d)
In [58]:
import copy
In [59]:
l_2d = [[0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10, 11]]
print(l_2d)
In [60]:
l_2d_slice_deepcopy = copy.deepcopy(l_2d[1:3])
print(l_2d_slice_deepcopy)
In [61]:
l_2d_slice_deepcopy[0][1] = 400
print(l_2d_slice_deepcopy)
In [62]:
print(l_2d)
In [63]:
s = 'abcdefg'
print(s)
In [64]:
print(s[2:5])
In [65]:
print(s[::-1])
In [66]:
# s[2:5] = 'CDE'
# TypeError: 'str' object does not support item assignment
In [67]:
t = (0, 10, 20, 30, 40, 50, 60)
print(t)
In [68]:
print(t[2:5])
In [69]:
# t[2:5] = (200, 300, 400)
# TypeError: 'tuple' object does not support item assignment