Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "CISC101 Reminders Quiz 2 graded. Assn 2 sample solution is posted."— Presentation transcript:

1 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

2 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

3 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

4 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

5 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 Winter 2018 CISC101 - Prof. McLeod

6 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

7 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

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

9 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

10 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

11 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

12 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

13 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

14 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

15 Python Set Interpreter Demo
Winter 2018 CISC101 - Prof. McLeod

16 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

17 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

18 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

19 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

20 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

21 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

22 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

23 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

24 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

25 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

26 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

27 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

28 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

29 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


Download ppt "CISC101 Reminders Quiz 2 graded. Assn 2 sample solution is posted."

Similar presentations


Ads by Google