1.

What is slicing in Python?

Answer»
  • As the name suggests, ‘slicing’ is taking PARTS of.
  • Syntax for slicing is [start : stop : step]
  • start is the starting index from where to slice a list or tuple
  • stop is the ending index or where to sop.
  • step is the NUMBER of STEPS to jump.
  • Default value for start is 0, stop is number of items, step is 1.
  • Slicing can be done on strings, arrays, lists, and tuples.
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]print(numbers[1 : : 2]) #output : [2, 4, 6, 8, 10]


Discussion

No Comment Found