In [1]:
l = [0, 10, 20, 30, 40, 50, 60]
print(l)


[0, 10, 20, 30, 40, 50, 60]

In [2]:
print(l[2:5])


[20, 30, 40]

In [3]:
print(l[:3])


[0, 10, 20]

In [4]:
print(l[3:])


[30, 40, 50, 60]

In [5]:
print(l[:])


[0, 10, 20, 30, 40, 50, 60]

In [6]:
print(l[2:10])


[20, 30, 40, 50, 60]

In [7]:
print(l[5:2])


[]

In [8]:
print(l[2:2])


[]

In [9]:
print(l[10:20])


[]

In [10]:
print(l[::2])


[0, 20, 40, 60]

In [11]:
print(l[1::2])


[10, 30, 50]

In [12]:
print(l[::3])


[0, 30, 60]

In [13]:
print(l[2:5:2])


[20, 40]

In [14]:
print(l[3:-1])


[30, 40, 50]

In [15]:
print(l[-2:])


[50, 60]

In [16]:
print(l[-5:-2])


[20, 30, 40]

In [17]:
print(l[5:2:-1])


[50, 40, 30]

In [18]:
print(l[2:5:-1])


[]

In [19]:
print(l[-2:-5:-1])


[50, 40, 30]

In [20]:
print(l[-2:2:-1])


[50, 40, 30]

In [21]:
print(l[5:2:-2])


[50, 30]

In [22]:
print(l[::-1])


[60, 50, 40, 30, 20, 10, 0]

In [23]:
sl = slice(2, 5, 2)
print(sl)


slice(2, 5, 2)

In [24]:
print(type(sl))


<class 'slice'>

In [25]:
print(l[sl])


[20, 40]

In [26]:
sl = slice(2, 5)
print(sl)


slice(2, 5, None)

In [27]:
print(l[sl])


[20, 30, 40]

In [28]:
sl = slice(2)
print(sl)


slice(None, 2, None)

In [29]:
print(l[sl])


[0, 10]

In [30]:
# sl = slice()
# TypeError: slice expected at least 1 arguments, got 0

In [31]:
sl = slice(None)
print(sl)


slice(None, None, None)

In [32]:
print(l[sl])


[0, 10, 20, 30, 40, 50, 60]

In [33]:
print(l)


[0, 10, 20, 30, 40, 50, 60]

In [34]:
l[2:5] = [200, 300, 400]
print(l)


[0, 10, 200, 300, 400, 50, 60]

In [35]:
l[2:5] = [-2, -3]
print(l)


[0, 10, -2, -3, 50, 60]

In [36]:
l[2:4] = [2000, 3000, 4000, 5000]
print(l)


[0, 10, 2000, 3000, 4000, 5000, 50, 60]

In [37]:
l[2:6] = [20000]
print(l)


[0, 10, 20000, 50, 60]

In [38]:
# l[2:3] = 200
# TypeError: can only assign an iterable

In [39]:
l[1:4] = []
print(l)


[0, 60]

In [40]:
l[20:60] = [-1, -2, -3]
print(l)


[0, 60, -1, -2, -3]

In [41]:
l[2:2] = [-100]
print(l)


[0, 60, -100, -1, -2, -3]

In [42]:
print(l[:5:2])


[0, -100, -2]

In [43]:
l[:5:2] = [100, 200, 300]
print(l)


[100, 60, 200, -1, 300, -3]

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)


[[0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10, 11]]

In [46]:
print(l_2d[1:3])


[[3, 4, 5], [6, 7, 8]]

In [47]:
print([l[:2] for l in l_2d[1:3]])


[[3, 4], [6, 7]]

In [48]:
l_2d_t = [list(x) for x in zip(*l_2d)]
print(l_2d_t)


[[0, 3, 6, 9], [1, 4, 7, 10], [2, 5, 8, 11]]

In [49]:
print(l_2d_t[1])


[1, 4, 7, 10]

In [50]:
l = [0, 10, 20, 30, 40, 50, 60]
print(l)


[0, 10, 20, 30, 40, 50, 60]

In [51]:
l_slice = l[2:5]
print(l_slice)


[20, 30, 40]

In [52]:
l_slice[1] = 300
print(l_slice)


[20, 300, 40]

In [53]:
print(l)


[0, 10, 20, 30, 40, 50, 60]

In [54]:
l_2d = [[0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10, 11]]
print(l_2d)


[[0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10, 11]]

In [55]:
l_2d_slice = l_2d[1:3]
print(l_2d_slice)


[[3, 4, 5], [6, 7, 8]]

In [56]:
l_2d_slice[0][1] = 400
print(l_2d_slice)


[[3, 400, 5], [6, 7, 8]]

In [57]:
print(l_2d)


[[0, 1, 2], [3, 400, 5], [6, 7, 8], [9, 10, 11]]

In [58]:
import copy

In [59]:
l_2d = [[0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10, 11]]
print(l_2d)


[[0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10, 11]]

In [60]:
l_2d_slice_deepcopy = copy.deepcopy(l_2d[1:3])
print(l_2d_slice_deepcopy)


[[3, 4, 5], [6, 7, 8]]

In [61]:
l_2d_slice_deepcopy[0][1] = 400
print(l_2d_slice_deepcopy)


[[3, 400, 5], [6, 7, 8]]

In [62]:
print(l_2d)


[[0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10, 11]]

In [63]:
s = 'abcdefg'
print(s)


abcdefg

In [64]:
print(s[2:5])


cde

In [65]:
print(s[::-1])


gfedcba

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)


(0, 10, 20, 30, 40, 50, 60)

In [68]:
print(t[2:5])


(20, 30, 40)

In [69]:
# t[2:5] = (200, 300, 400)
# TypeError: 'tuple' object does not support item assignment