Tuesday, June 30, 2020

Inner Life of the Lists


See the below code lines.








Output is





Slices in Lists
A slice is an element of Python syntax that allows you to make a brand new copy of a list, or parts of a listIt actually copies the list's contents, not the list's name.







Output is




This inconspicuous part of the code described as [ : ] is able to produce a brand new list.


General form of the slice is given below.

myList [start : end]

  • start is the index of the first element included in the slice.
  • end is the index of the first element not included in the slice.


A slice of this form makes a new (target) list, taking elements from the source list - the elements of the indices from start to “end-1”.

Here not to “end” but to “end-1”. An element with an index equal to “end” is the first element which does not take part in the slicing.








The “newList” list will have “end – start” (3 - 1 = 2) elements - the ones with indices equal to 1 and 2 (but not 3).



Output is



No comments:

Post a Comment