CISC101 Reminders Quiz 2 graded. Assn 2 sample solution is posted.

Slides:



Advertisements
Similar presentations
Today Quiz solutions are posted on the Grading page. Assignment 2 is posted. Due the first Friday after Reading Week. All about null Start File Input/Output.
Advertisements

Today Assignment 2 is posted. Due the first Friday after Reading Week. (“Virtual” lecture looked at the use of try/catch and try with resources blocks.
Introduction to Python Lecture 1. CS 484 – Artificial Intelligence2 Big Picture Language Features Python is interpreted Not compiled Object-oriented language.
Python Mini-Course University of Oklahoma Department of Psychology Lesson 17 Reading and Writing Files 5/10/09 Python Mini-Course: Lesson 17 1.
File I/O Ruth Anderson UW CSE 160 Spring File Input and Output As a programmer, when would one use a file? As a programmer, what does one do with.
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 1 due Friday, 7pm. RAD due next Friday. Presentations week 6. Today: –More details on functions,
FILES. open() The open() function takes a filename and path as input and returns a file object. file object = open(file_name [, access_mode][, buffering])
Today… Style, Cont. – Naming Things! Methods and Functions Aside - Python Help System Punctuation Winter 2016CISC101 - Prof. McLeod1.
Today… Strings: –String Methods Demo. Raising Exceptions. os Module Winter 2016CISC101 - Prof. McLeod1.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 next week. See next slide. Both versions of assignment 3 are posted. Due today.
Quiz 3 Topics Functions – using and writing. Lists: –operators used with lists. –keywords used with lists. –BIF’s used with lists. –list methods. Loops.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 this week – last section on Friday. Assignment 4 is posted. Data mining: –Designing functions.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Assignment 5 is posted. Exercise 8 is very similar to what you will be doing with assignment 5. Exam.
Quiz 4 Topics Aid sheet is supplied with quiz. Functions, loops, conditionals, lists – STILL. New topics: –Default and Keyword Arguments. –Sets. –Strings.
Today… Files from the Web! Dictionaries. Lists of lists. Winter 2016CISC101 - Prof. McLeod1.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
FILE I/O: Low-level 1. The Big Picture 2 Low-Level, cont. Some files are mixed format that are not readable by high- level functions such as xlsread()
Today… Loops and Drawing, Cont. –Two slightly more advanced demos. Collections Overview. Winter 2016CISC101 - Prof. McLeod1.
Topic: File Input/Output (I/O)
Chapter 7 Text Input/Output Objectives
Taken from notes by Dr. Neil Moore & Dr. Debby Keen
Chapter 7 Text Input/Output Objectives
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
Ruth Anderson UW CSE 160 Winter 2016
Exceptions and files Taken from notes by Dr. Neil Moore
Fall 2017 CISC124 9/21/2018 CISC124 First onQ quiz this week – write in lab. More details in last Wednesday’s lecture. Repeated: The quiz availability.
CISC101 Reminders Quiz 2 this week.
CISC101 Reminders Quiz 2 this week.
CISC101 Reminders Assn 3 due Friday, this week.
Ruth Anderson UW CSE 160 Winter 2017
Winter 2018 CISC101 11/16/2018 CISC101 Reminders
Winter 2018 CISC101 11/19/2018 CISC101 Reminders
Ruth Anderson UW CSE 160 Spring 2018
File Handling Programming Guides.
Topics Introduction to File Input and Output
Chapter 7 Files and Exceptions
CISC101 Reminders Quiz 1 grading underway Assn 1 due Today, 9pm.
CISC101 Reminders Slides have changed from those posted last night…
Winter 2018 CISC101 12/1/2018 CISC101 Reminders
File IO and Strings CIS 40 – Introduction to Programming in Python
Using files Taken from notes by Dr. Neil Moore
CISC101 Reminders Assn 3 due Friday, this week. Quiz 3 next week.
Exceptions and files Taken from notes by Dr. Neil Moore
Winter 2018 CISC101 12/1/2018 CISC101 Reminders
Winter 2018 CISC101 12/1/2018 CISC101 Reminders
Winter 2018 CISC101 12/2/2018 CISC101 Reminders
CISC101 Reminders Assn 3 due tomorrow, 7pm.
Winter 2018 CISC101 12/5/2018 CISC101 Reminders
CISC101 Reminders Quiz 1 grading underway Next Quiz, next week.
CISC101 Reminders Quiz 2 this week.
Winter 2019 CISC101 2/17/2019 CISC101 Reminders
Fall 2018 CISC124 2/24/2019 CISC124 Quiz 1 marking is complete. Quiz average was about 40/60 or 67%. TAs are still grading assn 1. Assn 2 due this Friday,
CISC101 Reminders All assignments are now posted.
CISC101 Reminders Assn 3 sample solution is posted.
CISC101 Reminders Labs start this week. Meet your TA! Get help with:
Topics Introduction to File Input and Output
CISC101 Reminders Assignment 2 due today.
Winter 2019 CISC101 4/16/2019 CISC101 Reminders
CISC101 Reminders Assignment 3 due next Friday. Winter 2019
Winter 2019 CISC101 4/29/2019 CISC101 Reminders
Winter 2019 CISC101 4/28/2019 CISC101 Reminders
Winter 2019 CISC101 5/17/2019 CISC101 Reminders
Winter 2019 CISC101 5/26/2019 CISC101 Reminders
Topics Introduction to File Input and Output
CISC101 Reminders Assignment 3 due today.
Winter 2019 CISC101 5/30/2019 CISC101 Reminders
CMPE212 – Reminders Assignment 2 due next Friday.
Ruth Anderson UW CSE 160 Winter 2016
Presentation transcript:

CISC101 Reminders Quiz 2 graded. Assn 2 sample solution is posted. Winter 2018 CISC101 12/4/2018 CISC101 Reminders Quiz 2 graded. Assn 2 sample solution is posted. Assn 3 due next week. Next quiz in week 9 (this is week 7). Winter 2018 CISC101 - Prof. McLeod Prof. Alan McLeod

Today Look at Generator Expressions again. What do you need to know for Assn 3? Sets. File Input/Output. Emphasis on Text File I/O. Winter 2018 CISC101 - Prof. McLeod

Aside: Generator Expressions Two more examples: >>> print([ i * i for i in range(10) ]) [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] >>> print(sum( i * i for i in range(10) )) 285 You can see the syntax: Expression followed by a for loop. Winter 2018 CISC101 - Prof. McLeod

Aside: Generator Expressions, Cont. You can even follow the loop with a conditional: >>> print([i*i for i in range(10) if i*i % 2==0]) [0, 4, 16, 36, 64] (Sorry about the bad style…) If you are creating a list, these things have been called List Comprehensions. Winter 2018 CISC101 - Prof. McLeod

Aside: Generator Expressions, Cont. One of the “nifty” aspects of using Python! You never *have* to use a generator expression, but they can make for some elegant code. See https://www.python.org/dev/peps/pep-0289/ Winter 2018 CISC101 - Prof. McLeod

Assn 3 Topics Read a text file into a list. Use the slice operator with strings. Build lists using the .append() method. Combine lists together (using +). Use set methods .intersection() and .difference(). Use len() BIF with lists and sets. Use in keyword to search sets or lists. Build functions! Winter 2018 CISC101 - Prof. McLeod

Assn 3 Topics, Cont. The assignment tells you: What you need to know to use sets (but we’ll cover them in class, anyways…). What functions you need to write. However, you will need to figure out what arguments the functions need and what they need to return. Winter 2018 CISC101 - Prof. McLeod

Assn 3 Topics, Cont. So, we still need to cover: Sets. Text File Input. Building Functions. Winter 2018 CISC101 - Prof. McLeod

Sets One of the Python collection types. Makes your work in assn 3 much easier! See section 5.4 in the Python Tutorial and section 4.9 in the Python Library Reference. Winter 2018 CISC101 - Prof. McLeod

Mathematical Set Operations Set Union operation: Set Intersection operation: All elements from both sets. Duplicates are eliminated. Just elements that are common to both sets. Winter 2018 CISC101 - Prof. McLeod

Mathematical Set Operations, Cont. Set Difference operation: There are other operations, but these are the most common ones. The assignment makes the most use of Intersection, but Difference might also be useful. The elements from set “A” that are not in set “B”. Winter 2018 CISC101 - Prof. McLeod

Sets, Cont. Suppose you have a set of candidate words and you have the dictionary of words as a set: In order to find out which of the candidate words are actually words, carry out an Intersection operation. If you have a set of the high probability words and a set of the low probability words carry out a difference to eliminate the words from the other set that are the same. Winter 2018 CISC101 - Prof. McLeod

Sets in Python A set is mutable (use frozenset if you want an immutable set). Use the set() BIF to create a set from a list. Duplicates will be eliminated. A set is hashed, not indexed. So the slice operator and other operations like sorting and indexing will not work. Things like in, len() and the for loop will work (but you don’t need any of these in the assignment). Winter 2018 CISC101 - Prof. McLeod

Sets in Python, Cont. To carry out an Intersection, invoke the .intersection method and supply the other set as an argument. For the Difference operation, invoke the .difference method. See some sample code on the next slide: Winter 2018 CISC101 - Prof. McLeod

Python Set Interpreter Demo Winter 2018 CISC101 - Prof. McLeod

File I/O (“Input/Output”) CISC101 File I/O (“Input/Output”) Files provide a convenient way to store and re-store to memory larger amounts of data. Use a data structure like a list to store the data in memory. Three kinds of file I/O: Text Binary Random access For simplicity we will concentrate on text I/O in this course. Text files can be read by Notepad, for example. Winter 2018 CISC101 - Prof. McLeod Prof. Alan McLeod

file_variable = open(filename, mode) Text File I/O, Cont. Syntax: file_variable = open(filename, mode) filename is the name of a file in the same folder as your program, as a string. mode is also a string: ‘r’ for reading only ‘w’ for writing only ‘a’ for appending to a file The default mode is ‘r’. Winter 2018 CISC101 - Prof. McLeod

Aside - Other File Modes + - read and write (same as w+ or a+) rb - binary read wb - binary write ab - binary append rb+ - binary read and write (same as wb+ and ab+) Winter 2018 CISC101 - Prof. McLeod

Another Aside - Newline Support Different OSs (Linux, Mac, Windows) may use different line termination sequences, such as '\n', '\r\n' or '\r'. If you have difficulty reading individual lines, specify the newline sequence using the keyword argument: newline='\n' in the open() BIF. Try different escape sequences, until the file is read properly. Winter 2018 CISC101 - Prof. McLeod

Text File Output Warning: if you open an existing file for writing using mode ‘w’, the old file will be overwritten with a new file - all the old contents will be lost. If you want to add to an existing file without erasing the old contents, use the ‘a’ mode for appending. If you do not provide a path, the file is created in the same folder as your program. If you do put a path in the filename, remember to use \\ as your folder delimiter. Winter 2018 CISC101 - Prof. McLeod

Text File Output, Cont. To write information to a file, use the write() method: file_variable.write(a_string) Note that the write() method does not add a line terminator to the end of the string, so if you want to write a line, and have the next output go to the next line, you need something like: file_variable.write(a_string + '\n') Winter 2018 CISC101 - Prof. McLeod

Text File Output, Cont. Once you are finished writing to the file don’t forget to close the file using: file_variable.close() If you don’t do this, you run the risk of leaving a corrupted file on your hard disk! This method also releases the file resource back to the OS. Winter 2018 CISC101 - Prof. McLeod

Sequential File Access Text file I/O uses sequential access. Think of having a little “pointer” in the file marking the end of what you have read. As you read (or write), the pointer moves ahead. The pointer cannot move backwards. The only way to re-read something is to close the file and open it again - this moves the “pointer” back to the beginning. start end Winter 2018 CISC101 - Prof. McLeod

Aside - Random File Access This can be used only with binary files: Seek a certain byte location in the file (you must know the exact structure of the file). Read or write data from this location. Seek again… Winter 2018 CISC101 - Prof. McLeod

Text File Input Use the open() method as shown above. Use the readline() method to read a line (up to and including a linefeed character). This method returns a string. You might wish to use something like the string rstrip() method on the string to remove the linefeed, and any other whitespace at the end of the string. Winter 2018 CISC101 - Prof. McLeod

Text File Input, Cont. Other file reading methods: read() – reads entire file and returns a single string. readlines() – reads entire file and returns a list of lines of text. Winter 2018 CISC101 - Prof. McLeod

Text File Input, Cont. Invoke the close() method when you are done reading. A for loop can simplify input, because a file object is iterable: for line in inFile: fileContentsList.append(line.rstrip()) Winter 2018 CISC101 - Prof. McLeod

File Read and Write Errors Suppose a problem occurs: File does not exist. Folder does not exist. You do not have read or write access in the folder you are attempting to use. Access fails partway through a read or write attempt. What happens and what should we do about it? See FileInput.py Winter 2018 CISC101 - Prof. McLeod

File Read and Write Errors, Cont. How about using a try/except structure to catch the FileNotFoundError exception? See RobustFileInput.py Winter 2018 CISC101 - Prof. McLeod