Riju21

11_slicing

Mar 27th, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.35 KB | None | 0 0
  1. # slicing
  2.  
  3. x = [1, 2, 3, 4, 5, 7]
  4.  
  5. x1 = x[1:3]    # elements between 1 to 3
  6. print(x1)  # ans: [2,3]
  7.  
  8. x2 = x[:4]     # elements till 5
  9. print(x2)  # ans: [1,2,3,4]
  10.  
  11. x3 = x[3:-1]
  12. print(x3)  # ans: [4,5]
  13.  
  14. x4 = x[:-5]
  15. print(x4)  #ans: [1]
  16.  
  17. x5 = x[1:6:2]  # skip 1 index every time
  18. print(x5)  # [2,4,7]
  19.  
  20. x6 = x[::-1]   # reverse the list
  21. print(x6)
Advertisement
Add Comment
Please, Sign In to add comment