Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- # P2PU - Python Programming - Chapter 5 - Datatypes
- # Chapter 8 - Lists
- print 'Python for Informatics - Chapter 8: Lists'
- # Exercise 8.1
- print '\n# Exercise 8.1\n'
- def chop(t):
- del t[0]
- del t[len(t)-1]
- return None
- def middle(t):
- return t[1:len(t)-1]
- letters = ['a', 'b', 'c', 'd', 'e', 'f']
- print 'list before function chop:', letters
- chop(letters)
- print 'list after function chop :', letters
- print
- letters = ['a', 'b', 'c', 'd', 'e', 'f']
- print 'original list before function middle:', letters
- nletters = middle(letters)
- print 'original list after function middle :', letters
- print 'new list after function middle :', nletters
- # Exercise 8.2
- inp = raw_input('Press <enter> to continue')
- print '\n# Exercise 8.2\n'
- fname = 'mbox.txt'
- try:
- ffile = open(fname)
- except:
- print 'File', fname, 'cannot be opened!'
- exit()
- for line in ffile:
- words = line.split()
- # if it's an empty line continue
- # if there are less then 3 words continue
- if len(words) < 3: continue
- if words[0] != 'From': continue
- print words[2]
- # Exercise 8.3
- inp = raw_input('Press <enter> to continue')
- print '\n# Exercise 8.3\n'
- fname = 'mbox.txt'
- try:
- ffile = open(fname)
- except:
- print 'File', fname, 'cannot be opened!'
- exit()
- for line in ffile:
- words = line.split()
- # assignment says use and but looks easier with or
- if len(words) < 3 or words[0] != 'From': continue
- print words[2]
- # Exercise 8.4
- inp = raw_input('Press <enter> to continue')
- print '\n# Exercise 8.4\n'
- fname = 'romeo.txt'
- wordlist = list()
- try:
- ffile = open(fname)
- except:
- print 'File', fname, 'cannot be opened!'
- exit()
- for line in ffile:
- words = line.split()
- for word in words:
- if word in wordlist: continue
- wordlist.append(word)
- wordlist.sort()
- print wordlist
- # Exercise 8.5
- inp = raw_input('Press <enter> to continue')
- print '\n# Exercise 8.5\n'
- fname = 'mbox-short.txt'
- count = 0
- try:
- ffile = open(fname)
- except:
- print 'File', fname, 'cannot be opened!'
- exit()
- for line in ffile:
- words = line.split()
- # assignment says use and but looks easier with or
- if len(words) < 3 or words[0] != 'From': continue
- print words[1]
- count += 1
- print 'There were %d lines in the file with From as the first word.' % count
- # Exercise 8.6
- inp = raw_input('Press <enter> to continue')
- print '\n# Exercise 8.6\n'
- numlist = list()
- while True:
- number = raw_input('Enter a number: ')
- if number == 'done' : break;
- try:
- numlist.append(float(number))
- except:
- print "Error, please enter a number, or 'done' to finish"
- if len(numlist) == 0:
- # Preventing ValueError when list is empty
- print 'No minimum and maximum values available for an empty list.'
- else:
- print 'Maximum =', max(numlist)
- print 'Minimum =', min(numlist)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement