while loops; file I/O; introduction to lists

Slides:



Advertisements
Similar presentations
Course A201: Introduction to Programming 10/28/2010.
Advertisements

2/7/2008. >>> Overview * boolean * while * random * tuples.
Unit 6 File processing Special thanks to Roy McElmurry, John Kurkowski, Scott Shawcroft, Ryan Tucker, Paul Beck for their work. Except where otherwise.
Week 2 Classes, Objects and lists review Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise.
Week 5 while loops; logic; random numbers; tuples Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except.
Week 4 Strings, if/else, return, user input Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where.
Strings, if/else, return, user input
Week 2 expressions, variables, for loops Special thanks to Scott Shawcroft, Ryan Tucker, Paul Beck, Hélène Martin, Kim Todd, John Kurkowski, and Marty.
Week 7 Lists Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise noted, this work is licensed.
Week 6 review; file processing Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise noted,
2/28/2008. >>> Overview Arrays in Python – a.k.a. Lists Ranges are Lists Strings vs. Lists Tuples vs. Lists Map-Reduce Lambda Review: Printing to a file.
Week 6 review; file processing Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise noted,
INTRODUCTION TO PYTHON PART 4 – TEXT AND FILE PROCESSING CSC482 Introduction to Text Analytics Thomas Tiahrt, MA, PhD.
Unit 5 while loops; logic; random numbers; tuples Special thanks to Roy McElmurry, John Kurkowski, Scott Shawcroft, Ryan Tucker, Paul Beck for their work.
Programming for Linguists An Introduction to Python 24/11/2011.
Lecture 06 – Reading and Writing Text Files.  At the end of this lecture, students should be able to:  Read text files  Write text files  Example.
Iterators, Linked Lists, MapReduce, Dictionaries, and List Comprehensions... OH MY! Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their.
Week 7 Review and file processing. >>> Methods Hello.java public class Hello { public static void main(String[] args){ hello(); } public static void hello(){
Unit 2 Expressions and variables; for loops Special thanks to Roy McElmurry, John Kurkowski, Scott Shawcroft, Ryan Tucker, Paul Beck for their work. Except.
Unit 6 File processing Special thanks to Roy McElmurry, John Kurkowski, Scott Shawcroft, Ryan Tucker, Paul Beck for their work. Except where otherwise.
Higher Order Functions Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise noted, this.
Copyright 2008 by Pearson Education Building Java Programs Chapter 6: File Processing Lecture 6-2: Advanced file input reading: self-check: #7-11.
Week 2 expressions, variables, for loops Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise.
Week 2 expressions, variables, for loops Special thanks to Scott Shawcroft, Ryan Tucker, Paul Beck, Hélène Martin, Kim Todd, John Kurkowski, and Marty.
Exploration Seminar 4 Basics Special thanks to Scott Shawcroft, Ryan Tucker, Paul Beck and Roy McElmurry for their work on these slides. Except where otherwise.
If/else, return, user input, strings
Python Files and Lists. Files  Chapter 9 actually introduces you to opening up files for reading  Chapter 14 has more on file I/O  Python can read.
MapReduce, Dictionaries, List Comprehensions Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where.
11/13/07. >>> Overview * lists/arrays * reading files * writing files.
Prof. Katherine Gibson Prof. Jeremy Dixon
CMSC201 Computer Science I for Majors Lecture 10 – File I/O
Higher Order Functions
CSc 110, Autumn 2017 Lecture 19: While loops and File Input
CSc 110, Autumn 2017 Lecture 20: Line-Based File Input
CSc 110, Spring 2017 Lecture 18: Line-Based File Input
CSc 110, Autumn 2017 Lecture 21: Line-Based File Input
Containers and Lists CIS 40 – Introduction to Programming in Python
CSc 120 Introduction to Computer Programing II
Last Class We Covered File I/O How to open a file
CSc 110, Autumn 2017 Lecture 18: While loops and File Input
Introduction to Python
review; file processing
Week 1 Review Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise noted, this work is.
Week 4 gur zntvp jbeqf ner fdhrnzvfu bffvsentr
CSc 110, Autumn 2016 Lecture 16: File Input.
Introduction to Python
Building Java Programs
Building Java Programs
Adapted from slides by Marty Stepp and Stuart Reges
Week 7 Lists Special thanks to Roy McElmurry, John Kurkowski, Scott Shawcroft, Ryan Tucker, Paul Beck for their work. Except where otherwise noted, this.
CSc 110, Spring 2017 Lecture 17: Line-Based File Input
expressions, variables, for loops
expressions, variables, for loops
while loops; logic; random numbers; tuples
Adapted from slides by Marty Stepp and Stuart Reges
Classes, Objects and lists
CSc 110, Autumn 2016 Lecture 17: Line-Based File Input
CSc 110, Spring 2018 Lecture 22: Line-Based File Input
Last Class We Covered Escape sequences File I/O Uses a backslash (\)
Reading and Writing Files
CMSC201 Computer Science I for Majors Lecture 13 – File I/O
CSc 110, Spring 2018 Lecture 27: Lists that change size and File Output Adapted from slides by Marty Stepp and Stuart Reges.
Building Java Programs
CSc 110, Spring 2018 Lecture 21: Line-Based File Input
CSc 110, Autumn 2016 Programming feel like that?
Lambda Functions, MapReduce and List Comprehensions
Last Class We Covered File I/O How to open a file
Building Java Programs
Adapted from slides by Marty Stepp and Stuart Reges
Building Java Programs
Presentation transcript:

while loops; file I/O; introduction to lists Week 6 while loops; file I/O; introduction to lists Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise noted, this work is licensed under: http://creativecommons.org/licenses/by-nc-sa/3.0

while Loops while test: statements sentinel.py 1 2 3 4 5 6 7 8 9 10 # Sums integers entered by the user # until -1 is entered, using a sentinel loop. sum = 0 number = input("Enter a number (-1 to quit)? ")? while number != -1: sum += number print "The total is", sum

Random Numbers from random import * randint(min, max) choice(sequence) returns a random integer in range [min, max] inclusive choice(sequence) returns a randomly chosen value from the given sequence (the sequence can be a range, a string, an array, ...) >>> from random import * >>> randint(1, 5) 2 5 >>> choice(range(4, 20, 2)) 16 >>> choice("hello") 'e'

Reading Files name = open("filename") opens the given file for reading, and returns a file object name.read() - file's entire contents as a string name.readline() - next line from file as a string name.readlines() - file's contents as a list of lines the lines from a file object can also be read using a for loop >>> f = open("hours.txt") >>> f.read() '123 Susan 12.5 8.1 7.6 3.2\n 456 Brad 4.0 11.6 6.5 2.7 12\n 789 Jenn 8.0 8.0 8.0 8.0 7.5\n'

File Input Template A template for reading files in Python: name = open("filename") for line in name: statements >>> input = open("hours.txt") >>> for line in input: ... print line.strip() # strip() removes \n 123 Susan 12.5 8.1 7.6 3.2 456 Brad 4.0 11.6 6.5 2.7 12 789 Jenn 8.0 8.0 8.0 8.0 7.5

Exercise Write a function input_stats that accepts a file name as a parameter and that reports the longest line in the file. example input file, carroll.txt: Beware the Jabberwock, my son, the jaws that bite, the claws that catch, Beware the JubJub bird and shun the frumious bandersnatch. expected output: >>> input_stats("carroll.txt") longest line = 42 characters the jaws that bite, the claws that catch,

Exercise Solution def input_stats(filename): input = open(filename) longest = "" for line in input: if len(line) > len(longest): longest = line print "Longest line =", len(longest) print longest

Lists list: Python's equivalent to Java's array (but cooler) Declaring: name = [value, value, ..., value] or, name = [value] * length Accessing/modifying elements: (same as Java) name[index] = value >>> scores = [9, 14, 18, 19, 16] [9, 14, 18, 19, 16] >>> counts = [0] * 4 [0, 0, 0, 0] >>> scores[0] + scores[4] 25

Indexing Lists can be indexed using positive or negative numbers: >>> scores = [9, 14, 12, 19, 16, 18, 24, 15] [9, 14, 12, 19, 16, 18, 24, 15] >>> scores[3] 19 >>> scores[-3] 18 index 1 2 3 4 5 6 7 value 9 14 12 19 16 18 24 15 -8 -7 -6 -5 -4 -3 -2 -1

Slicing slice: A sub-list created by specifying start/end indexes name[start:end] # end is exclusive name[start:] # to end of list name[:end] # from start of list name[start:end:step] # every step'th value >>> scores = [9, 14, 12, 19, 16, 18, 24, 15] >>> scores[2:5] [12, 19, 16] >>> scores[3:] [19, 16, 18, 24, 15] >>> scores[:3] [9, 14, 12] >>> scores[-3:] [18, 24, 15] index 1 2 3 4 5 6 7 value 9 14 12 19 16 18 24 15 -8 -7 -6 -5 -4 -3 -2 -1

Other List Abilities Lists can be printed (or converted to string with str()). Find out a list's length by passing it to the len function. Loop over the elements of a list using a for ... in loop. >>> scores = [9, 14, 18, 19] >>> print "My scores are", scores My scores are [9, 14, 18, 19] >>> len(scores) 4 >>> total = 0 >>> for score in scores: ... print "next score:", score ... total += score assignment score: 9 assignment score: 14 assignment score: 18 assignment score: 19 >>> total 60

Ranges, Strings, and Lists The range function returns a list. Strings behave like lists of characters: len indexing and slicing for ... in loops >>> nums = range(5) >>> nums [0, 1, 2, 3, 4] >>> nums[-2:] [3, 4] >>> len(nums) 5

String Splitting split breaks a string into a list of tokens. name.split() # break by whitespace name.split(delimiter) # break by delimiter join performs the opposite of a split delimiter.join(list) >>> name = "Brave Sir Robin" >>> name[-5:] 'Robin' >>> tokens = name.split() ['Brave', 'Sir', 'Robin'] >>> name.split("r") ['B', 'ave Si', ' Robin'] >>> "||".join(tokens) 'Brave||Sir||Robin'

Tokenizing File Input Use split to tokenize line contents when reading files. You may want to type-cast tokens: type(value) >>> f = open("example.txt") >>> line = f.readline() >>> line 'hello world 42 3.14\n' >>> tokens = line.split() >>> tokens ['hello', 'world', '42', '3.14'] >>> word = tokens[0] 'hello' >>> answer = int(tokens[2]) 42 >>> pi = float(tokens[3]) 3.14

Exercise Recall the hours.txt data: 123 Susan 12.5 8.1 7.6 3.2 456 Brad 4.0 11.6 6.5 2.7 12 789 Jenn 8.0 8.0 8.0 8.0 7.5 Recreate the Hours program from lecture in Python: Susan (ID#123) worked 31.4 hours (7.85 / day) Brad (ID#456) worked 36.8 hours (7.36 / day) Jenn (ID#789) worked 39.5 hours (7.9 / day)

Exercise Answer hours.py 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 file = open("hours.txt") for line in file: tokens = line.split() id = tokens[0] name = tokens[1] # cumulative sum of this employee's hours hours = 0.0 days = 0 for token in tokens[2:]: hours += float(token) days += 1 print name, "(ID#" + str(id) + ") worked", \ hours, "hours (" + str(hours / days), "/ day)"

Exercise Recall the Internet Movie Database (IMDb) data: 1 9.1 196376 The Shawshank Redemption (1994) 2 9.0 139085 The Godfather: Part II (1974) 3 8.8 81507 Casablanca (1942) Recreate the Movies program from lecture in Python: Search word? part Rank Votes Rating Title 2 139085 9.0 The Godfather: Part II (1974) 40 129172 8.5 The Departed (2006) 95 20401 8.2 The Apartment (1960) 192 30587 8.0 Spartacus (1960) 4 matches.

Exercise Answer movies.py 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 search_word = raw_input("Search word? ") matches = 0 file = open("imdb.txt") for line in file: tokens = line.split() rank = int(tokens[0]) rating = float(tokens[1]) votes = int(tokens[2]) title = " ".join(tokens[3:]) # does title contain searchWord? if search_word.lower() in title.lower(): matches += 1 print rank, "\t", votes, "\t", rating, "\t", title print matches, "matches."

Writing Files name = open("filename", "w") name = open("filename", "a") opens file for write (deletes any previous contents) , or opens file for append (new data is placed after previous data) name.write(str) - writes the given string to the file name.close() - closes file once writing is done >>> out = open("output.txt", "w") >>> out.write("Hello, world!\n") >>> out.write("How are you?") >>> out.close() >>> open("output.txt").read() 'Hello, world!\nHow are you?'