t_05.4up - Engineering Computation: Tutorial 05 Problem...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Engineering Computation: Tutorial 05 Problem Solving with Computing, using the Python Language Questions from Week 04? Programming Exercises from Zelle textbook 1 Pg. 160 - 162 # 1, 2, 9, 11, 13. 2 Whats in your toolbox? 1 Print statement, string literals 2 Basic calculator, integer division 3 Variables 4 Built-in functions 5 Math library functions 6 Straight-line (non-branching) programs 7 Branching programs 8 Python lists 9 For loops 10 Nested for loops 11 Strings 12 Converting between number system using Python 13 Matplotlib 14 Algorithms 3 Todays Topics: 1 Working with Files 2 Graphics 4 Working with Files Things you can do with files: 1 Write to files. 2 Read from files. To be able to do the above, you need to be able to: 1 Open files. 2 Close files. 5 Recall: Writing to the Python Shell print "This program prints a string." What would this do? print "This program \nprints a string." What would this do? print "\tThis program prints a string." Lets say you want to write the output of our program to a file instead of the Python Shell. We need to develop an algorithm to do this. 6 Writing to a File Bind a variable to a file using the built-in function called open() . <variable> = open(<"filename">, <"mode">) "filename" <"filename"> is a string. The name of the file you want your program to write to. If the file doesnt exist, it will be created. If the file exists, running your program will overwrite it! "mode" "<mode>" is a string. Tells the program what you want to do to the file. "w" for write. "r" for read. "a" for append. 7 <variable> = open(<"filename">, <"mode">) 1. open file for writing o u t f i l e = open ( output . txt , w) 2. process the file outfile.write("This program prints a string.") 3. close the file o u t f i l e . c l o s e () 8 check your results After running your program, check the directory where you saved your program file. There should be a file called output.txt. Open it. Whats in the file? 9 Reading from a File Bind a variable to a file using the built-in function called open() . <variable> = open(<"filename">, <"mode">) 1. open file for reading i n f i l e = open ( output . txt , r ) 2. process the file contents=infile.read() print contents 3. close the file i n f i l e . c l o s e () 10 What is the result?...
View Full Document

Page1 / 10

t_05.4up - Engineering Computation: Tutorial 05 Problem...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online