
8-1
By:
Mars83 on
Oct 9th, 2011 | syntax:
Python | size: 0.73 KB | hits: 67 | expires: Never
#! /usr/bin/env python3.2
# -*- coding: utf-8 -*-
# main.py
""" Task: Exercise 8.1
Write a function called chop that takes a list and modifies it, removing
the first and last elements, and returns None.
Then write a function called middle that takes a list and returns a new
list that contains all but the first and last elements.
"""
''' Functions '''
def chop(t):
del t[0] # first item; => t.pop(0)
del t[-1] # last item; => t.pop(-1)
def middle(t):
return t[1:-1]
''' Test '''
test = list()
for i in range(1, 11): # 1, 2, 3, ..., 8, 9, 10
test += [i]
chop(test)
print(test) # 2, 3, 4, ..., 7, 8, 9
test = middle(test)
print(test) # 3, 4, 5, 6, 7, 8