t_05.4up - Questions from Week 04 Engineering Computation...

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

View Full Document Right Arrow Icon
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 What’s 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 Today’s Topics: 1 Working with Files 2 Graphics 4
Image of page 1

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

View Full Document Right Arrow Icon
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." Let’s 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 doesn’t 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 . t x t ” , ”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
Image of page 2
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. What’s 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 . t x t ” , ” 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?
Image of page 3

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

View Full Document Right Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern