Don't like ads? PRO users don't see any ads ;-)
Guest

8-1

By: Mars83 on Oct 9th, 2011  |  syntax: Python  |  size: 0.73 KB  |  hits: 67  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  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.