Advertisement
Guest User

Untitled

a guest
Jul 10th, 2012
1,392
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.44 KB | None | 0 0
  1. '''
  2. Exercise 8.1 Write a function called chop that takes a list and modifies it, removing the first
  3. and last elements, and returns None.
  4. Then write a function called middle that takes a list and returns a new list that contains all but the first and last elements.
  5. '''
  6. def chop():
  7.     list1=[1,2,3,4,5,6,7,8]
  8.     list1.pop(0)
  9.     list1.pop(len(list1)-1)
  10.     return None
  11. def middle():
  12.     list2=[1,2,3,4,5,6,7,8]
  13.     list2.pop(0)
  14.     list2.pop(len(list2)-1)
  15.     return list2
  16.  
  17. '''
  18. Exercise 8.3 Rewrite the guardian code in the above example without two if statements.
  19. Instead use a compound logical expression using the and logical operator with a single
  20. if statement.
  21. '''
  22. fhand = open('mbox-short.txt')
  23. count = 0
  24. for line in fhand:
  25.     words = line.split()
  26.     # print 'Debug:', words
  27.     if len(words) == 0 or words[0] != 'From': continue
  28.     print words[2]
  29.  
  30. '''
  31. Exercise 8.4 Download a copy of the file from www.py4inf.com/code/romeo.txt
  32. Write a program to open the file romeo.txt and read it line by line. For each line, split the line into a list of words using the split function.
  33. '''
  34. fhand = open('romeo.txt')
  35. for line in fhand:
  36.     words=line.split()
  37.     print words
  38.  
  39. '''
  40. Exercise 8.5 Write a program to read through the mail box data and when you find line that
  41. starts with “From”, you will split the line into words using the split function. We are interested
  42. in who sent the message which is the second word on the From line.
  43. From stephen.marquard@uct.ac.za Sat Jan 5 09:14:16 2008
  44. You will parse the From line and print out the second word for each From line and then you will
  45. also count the number of From (not From:) lines and print out a count at the end.
  46. '''
  47. fhand=open('mail.txt')
  48. count=0
  49. for line in fhand:
  50.     words=line.split()
  51.     if words[0]=='From':
  52.         count=count+1
  53.         print words[1]
  54.     elif words[0]!='From':
  55.         continue
  56. print 'there are ',count,'lines that start with From'
  57.  
  58. '''
  59. Exercise 8.6 Rewrite the program that prompts the user for a list of numbers and prints out
  60. the maximum and minimum of the numbers at the end when the user enters “done”. Write the
  61. program to store the numbers the user enters in a list and use the max() and min() fuctions to
  62. compute the maximum and minimum numbers after the loop completes.
  63. '''
  64. a=raw_input('Enter a number:')
  65. list=[]
  66. while(a!='done'):
  67.     list.append(a)
  68.     a=raw_input('Enter a number:')
  69. print list
  70. print max(list)
  71. print min(list)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement