Why : for slicing in Python returns syntax error?

  Kiến thức lập trình

Do you know the reason for the following error?

>>> h = [00,11,22,33,44,55,66,77,88,99]
>>> g = [len(h)/2:]
  File "<stdin>", line 1
    g = [len(h)/2:]
                 ^
SyntaxError: invalid syntax
>>> x = len(h)/2
>>> g = [x:]
  File "<stdin>", line 1
    g = [x:]
          ^
SyntaxError: invalid syntax
>>> h[2:5]
[22, 33, 44]
>>> g = [x:9]
  File "<stdin>", line 1
    g = [x:9]
          ^
SyntaxError: invalid syntax

I expected the colon to work as usual and give the second half of the list as a slice, it should work for when the end index is unknown, meaning a list with unknown length.

LEAVE A COMMENT