Lecture 7
Previously Iteration. Date and time structures. Modularisation.
Today Pseudo code. File handling.
Pseudo code Pseudocode is an informal high-level description of the operating principle of a computer program or other algorithm. It uses the structural conventions of a programming language, but is intended for human reading rather than machine reading. Pseudo code typically omits details that are not essential for human understanding of the algorithm, such as variable declarations, system-specific code and some subroutines. http://en.wikipedia.org/wiki/pseudocode
Pseudo code The rules of Pseudo code are reasonably straightforward. All statements showing "dependency" are to be indented. These include while, do, for, if, switch.
Pseudo code - can you write this program? Examples: If student's grade is greater than or equal to 60 Print "passed" else Print "failed" Set total to zero Set grade counter to one While grade counter is less than or equal to ten Input the next grade Add the grade into the total Set the class average to the total divided by ten Print the class average.
Pseudo code keywords to use For looping and selection, The keywords that are to be used include Do While...EndDo; Do Until...Enddo; Case...EndCase; If...Endif; Call... with (parameters); Call; Return...; Return; When; Always use scope terminators for loops and iteration, even though Python does not use them.
Pseudo code keywords to use As verbs, use the words Generate, Compute, Process, etc. Words such as set, reset, increment, compute, calculate, add, sum, multiply,... print, display, input, output, edit, test, etc. with careful indentation tend to foster desirable pseudo code. Do not include data declarations in your pseudo code.
Pseudo code. Pseudo code is a feature of assignment 1.
Pseudo code - Task Partial requirement A technician must check a substance s temperature every 15 minutes. If the substance s temperature does not exceed 102.5 degrees Celsius, then the technician does nothing. However, if the temperature is greater than 102.5 degrees Celsius, the technician must turn down the vat s heater, wait 5 minutes, and check the temperature again. The technician repeats these steps until the temperature does not exceed 102.5 degrees Celsius.
Pseudo code - Task Write pseudo code for the preceding partial requirement.
Reading and writing files. In order for a program to work with a file, the program must create a file object in memory. A file object is an object that is associated with a specific file, and provides a way for the program to work with that file. In the program, a variable references the file object. This variable is used to carry out any operations that are performed on the file. file_variable = open( filename, mode)
Reading and writing files. file_variable = open( filename, mode) In the general format: file_variable is the name of the variable that will reference the file object. This is also called a file handle. filename is a string specifying the name of the file. mode is a string specifying the mode (reading, writing, etc.) in which the file will be opened.
Reading and writing files. Some of the main Python file modes. Mode r w a Description Open a file for reading only. The file cannot be changed or written to. Open a file for writing. If the file already exists, erase its contents. If it does not exist, create it. Open a file to be written to. All data written to the file will be appended to its end. If the file does not exist, create it.
Reading and writing files. For example, suppose the file customers.txt contains customer data, and we want to open for reading. Here is an example of how we would call the open function: customer_file = open('customers.txt', 'r') After this statement executes, the file named customers.txt will be opened, and the variable customer_file will reference a file object that we can use to read data from the file.
Reading and writing files. Suppose we want to create a file named sales.txt and write data to it. Here is an example of how we would call the open function: sales_file = open('sales.txt', 'w') Note: When you use the 'w' mode you are creating the file on the disk. If a file with the specified name already exists when the file is opened, the contents of the existing file will be erased.
Reading and writing files. When you pass a file name that does not contain a path as an argument to the open function, the Python interpreter assumes that the file s location is the same as that of the program. So customer_file = open('customers.txt', 'r') Python will look in the program folder.
Reading and writing files. If you want to open a file in a different location, you can specify a path as well as a filename in the argument that you pass to the open function. However- If you specify a path in a string literal (particularly on a Windows computer), be sure to prefix the string with the letter r. Here is an example: customer_file = open(r'c:\users\phil\customers.txt', 'r')
Reading and writing files. customer_file = open(r'c:\users\phil\customers.txt', 'r') The r prefix specifies that the string is a raw string. This causes the Python interpreter to read the backslash characters as literal backslashes. Without the r prefix, the interpreter would assume that the backslash characters were part of escape sequences, and an error would occur.
Writing Data to a File This is how you write data to a file. file_variable.write( string) The file first must be opened for writing (using the 'w' or 'a' mode) or an error will occur. Let s assume that customer_file references a file object, and the file was opened for writing with the 'w' mode. Here is an example of how we would write the string HND Students to the file: customer_file.write( HND Students ') This is how you close a file. customer_file.close()
Writing Data to a File Do the (file_write.py) program in the Lab07 (on the wiki) WHAT does the \n do?
Reading Data From a File If a file has been opened for reading (using the 'r' mode) you can use the file object s read method to read its entire contents into memory. When you call the read method, it returns the file s contents as a string. In Python we use infile.read method to read data from a file. Do Lab07 (file_read.py)
Reading Data From a File Although the read method allows you to easily read the entire contents of a file with one statement, many programs need to read and process the items that are stored in a file one line at a time. In Python you can use the readline method to read a line from a file. (A line is simply a string of characters that are terminated with a \n.) Do Lab07 (create_file.py) and (line_read.py)
Reading Data From a File Quite often a program must read the contents of a file without knowing the number of items that are stored in the file. This presents a problem if you want to write a program that processes all of the items in the file, however many there are. We know how to read lines from a file - We could use a loop to read all of the items in the file, but you need a way of knowing when the end of the file has been reached.
Reading end of File In Python, the readline method returns an empty string ('') when it has attempted to read beyond the end of a file. This makes it possible to write a while loop that determines when the end of a file has been reached. Here is the general algorithm, in pseudo code: Open the file Use readline to read the first line from the file While the value returned from readline is not an empty string: Process the item that was just read from the file Use readline to read the next line from the file. EndWhile Close the file
Reading Data From a File NOTE: In this algorithm we call the readline method just before entering the while loop. The purpose of this method call is to get the first line in the file, so it can be tested by the loop. This initial read operation is called a priming read.
Reading Data - flowchart Shape used for input (read) and output (write)
Summary what we have done today - Pseudo code Reading and writing files.
Next Time GUI s part 1.
Finally Continue with lab07 exercises.