Advertisement
Mars83

8-1

Oct 9th, 2011
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.73 KB | None | 0 0
  1. #! /usr/bin/env python3.2
  2. # -*- coding: utf-8 -*-
  3.  
  4. # main.py
  5. """ Task: Exercise 8.1
  6.    Write a function called chop that takes a list and modifies it, removing
  7.    the first and last elements, and returns None.
  8.    Then write a function called middle that takes a list and returns a new
  9.    list that contains all but the first and last elements.
  10. """
  11.  
  12. ''' Functions '''
  13. def chop(t):
  14.     del t[0]    # first item; => t.pop(0)
  15.     del t[-1]   # last item; => t.pop(-1)
  16.  
  17. def middle(t):
  18.     return t[1:-1]
  19.  
  20. ''' Test '''
  21. test = list()
  22. for i in range(1, 11):  # 1, 2, 3, ..., 8, 9, 10
  23.     test += [i]
  24.  
  25. chop(test)
  26. print(test)             # 2, 3, 4, ..., 7, 8, 9
  27.  
  28. test = middle(test)
  29. print(test)             # 3, 4, 5, 6, 7, 8
  30.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement