Python Mini-Course - PowerPoint PPT Presentation

About This Presentation
Title:

Python Mini-Course

Description:

Case study: Word play. 05/02/09. Python Mini-Course: Day 4 Lesson 13. 1. Lesson objectives ... Read in values from a text file. Create useful functions for ... – PowerPoint PPT presentation

Number of Views:91
Avg rating:3.0/5.0
Slides: 11
Provided by: troys9
Learn more at: https://www.ou.edu
Category:
Tags: course | mini | python | wordplay

less

Transcript and Presenter's Notes

Title: Python Mini-Course


1
Day 4 Lesson 13Case study Word play
  • Python Mini-Course
  • University of Oklahoma
  • Department of Psychology

2
Lesson objectives
  1. Read in values from a text file
  2. Create useful functions for identifying patterns
    in text

3
Files in Python
  • Files are objects
  • http//www.python.org/doc/2.5.2/lib/bltin-file-obj
    ects.html
  • Use the open statement to instantiate a file
    object
  • fin open('words.txt', 'r')
  • print fin

4
Files in Python
  • File methods include
  • f.readline()
  • f.write()
  • f.close()

5
Reading a line
  • fin open('words.txt', 'r')
  • line fin.readline()
  • print line
  • line fin.readline()
  • print word

6
Reading a line
  • word line.strip()
  • print word
  • word fin.readline().strip()
  • print word
  • fin.close()

7
Iterating through the lines
  • fin open('words.txt')
  • for line in fin
  • word line.strip()
  • print word
  • fin.close()

8
Exercise 9.1
  • Write a program that reads words.txt and prints
    only the words with more than 20 characters (not
    counting whitespace)

9
Exercise 9.2
  1. Write a function called has_no_e that returns
    True if the given word doesnt have the letter
    e in it.
  2. Modify your program from the previous section to
    print only the words that have no e and compute
    the percentage of the words in the list have no
    e.

10
Suggested exercises
  • Any (or all) of the remaining Exercises from
    Chapters 8 and 9 of Think Python
Write a Comment
User Comments (0)
About PowerShow.com