Advertisement
Guest User

P2PU Python Course: Datatypes

a guest
Mar 15th, 2013
462
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.48 KB | None | 0 0
  1. """
  2.  
  3. Created on Thu Mar 14 16:17:35 2013
  4.  
  5. Extra exercise
  6.  
  7. @author: casa
  8.  
  9. """
  10.  
  11. #Define farmers
  12.  
  13. farmers =['John','Tim','Malcolm'] #list
  14.  
  15. print farmers
  16.  
  17. farmers.append('Steven')
  18.  
  19. print type(farmers)
  20.  
  21. #Define Farmers Booths
  22.  
  23. booths=dict()
  24.  
  25. counter=0
  26.  
  27. for farmer in farmers:
  28.  
  29. counter=counter+1
  30.  
  31. booths[farmer]=counter
  32.  
  33. print booths
  34.  
  35. print type(booths)
  36.  
  37. print booths.items()
  38.  
  39. print type(booths.items()) #a list of tuples
  40.  
  41. #Define Farmers' Slogans
  42.  
  43. slogan=dict()
  44.  
  45. for farmer in farmers:
  46.  
  47. print farmer
  48.  
  49. slogan[farmer]=raw_input("Enter the slogan for this farmer: ")
  50.  
  51. print slogan
  52.  
  53. print type(slogan)
  54.  
  55. print slogan.items() #prints list of tuples
  56.  
  57. """
  58.  
  59. Created on Fri Mar 15 17:35:24 2013
  60.  
  61. exercise 8.6
  62.  
  63. @author: casa
  64.  
  65. """
  66.  
  67. l = list()
  68.  
  69. while (True) :
  70.  
  71. input = raw_input('Enter a number: ')
  72.  
  73. if input == 'done' : break
  74.  
  75. l.append(float(input))
  76.  
  77. print 'The lowest number is: ', min(l)
  78.  
  79. print 'The highest number is: ', max(l)
  80.  
  81. average =sum(l)/len(l)
  82.  
  83. print 'The average is: ', average
  84.  
  85. """
  86.  
  87. Created on Thu Mar 14 19:58:55 2013
  88.  
  89. exercise 8.5
  90.  
  91. @author: casa
  92.  
  93. """
  94.  
  95. counter = 0
  96.  
  97. fname = '/home/casa/.mozilla-thunderbird/c8yu1aiw.default/ImapMail/mail.freeknowledge.eu/INBOX.sbd/FreeK.sbd/Board'
  98.  
  99. fname = raw_input('Enter the file name: ')
  100.  
  101. try:
  102.  
  103. filehandle = open (fname)
  104.  
  105. except:
  106.  
  107. print 'File %s cannot be opened.' % fname
  108.  
  109. exit()
  110.  
  111. for line in filehandle:
  112.  
  113. words = line.split()
  114.  
  115. if line.startswith('From') :
  116.  
  117. # print 'Debug:', words
  118.  
  119. for word in words :
  120.  
  121. if '@' in word :
  122.  
  123. counter = counter + 1
  124.  
  125. word = word.strip('<')
  126.  
  127. word = word.strip('>')
  128.  
  129. print word
  130.  
  131. print "There are %d lines that start with 'From' as the first word in the file %s" % (counter, fname)
  132.  
  133. """
  134.  
  135. Created on Thu Mar 14 19:14:34 2013
  136.  
  137. exercise 8.4
  138.  
  139. @author: casa
  140.  
  141. """
  142.  
  143. fname = raw_input('Enter the file name: ')
  144.  
  145. try:
  146.  
  147. filehandle = open (fname)
  148.  
  149. except:
  150.  
  151. print 'File %s cannot be opened.' % fname
  152.  
  153. exit()
  154.  
  155. wordlist = list()
  156.  
  157. for line in filehandle:
  158.  
  159. line = line.rstrip()
  160.  
  161. print line
  162.  
  163. words = line.split()
  164.  
  165. for word in words:
  166.  
  167. if not word in wordlist :
  168.  
  169. wordlist.append(word)
  170.  
  171. print '\n'
  172.  
  173. wordlist.sort()
  174.  
  175. print wordlist
  176.  
  177. """
  178.  
  179. Created on Thu Mar 14 18:28:45 2013
  180.  
  181. exercise 6.5
  182.  
  183. @author: casa
  184.  
  185. """
  186.  
  187. str = "X-DSPAM-Confidence: 0.8475"
  188.  
  189. i=str.find (':')
  190.  
  191. print i
  192.  
  193. number = str[i+1:]
  194.  
  195. print number
  196.  
  197. print type(number)
  198.  
  199. print number.strip()
  200.  
  201. print float(number)
  202.  
  203. print type(float(number))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement