Advertisement
arahpaul

list_slicing

Nov 21st, 2019
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.40 KB | None | 0 0
  1. our_list = [1,1,1, 2, "Joy", 3.2, 7, 8, "Peter", 1]
  2.  
  3. print(our_list[0:3])
  4. #returns the first to third item
  5. print(our_list[:3])
  6. #alternaively, we write this to return the first to third item
  7. print(our_list[2:7])
  8. #returns the 3rd to 7th items
  9. print(our_list[2:])
  10. #returns from the third item to the last item
  11. print(our_list[-4:])
  12. #returns the last four items i.e counting backwards
  13. print(our_list[-6:-2])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement