Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC 131: Introduction to Computer Science

Similar presentations


Presentation on theme: "CSC 131: Introduction to Computer Science"— Presentation transcript:

1 CSC 131: Introduction to Computer Science
Dr. Curry Guinn

2 Quick Info Dr. Curry Guinn CIS 2025 guinnc@uncw.edu
Office Hours: MTR: 10:00am-11:00m and by appointment Teaching Assistant: Zach Krepps Tuesday, 8:00am-9:30am, 12:45pm-3:45pm Thursday, 8:00am-9:30am Office: GTA office – off of CIS 2004 lab

3 CSC Tutoring Lab! A free CSC Tutoring Lab has been created this semester for CSC 131, CSC 133, and CSC 231. It will be located in CIS 2004 on the following schedule: The tutors are Steve Lawrence, Tori Pruim, and Daniel Orsa. At least 2 tutors will be present during those hours. Monday 5pm-9pm Tuesday 6pm-9pm Wednesday 5pm-8pm Thursday

4 IT Career Day next week Tuesday, September 19, in the CIS building between 3:30-7:00. Find an internship! Find a job! Learn about possible career paths. Network! Link: Please register.

5 RTP Bus Trip (Also free!)
Visit companies in Research Triangle Link: Registration required. Seats will fill. Do it today!

6 For Next Class, Tuesday Homework 4 is due tonight, 11:59pm, September 14 Quiz 6 due Monday night

7 Today Computing the value of pi using a series For loops

8 Calculating Pi This converges VERY slowly How slowly?
How many terms do you need to be accurate to 2 decimal places? 5 decimal places? 10 decimal places?

9 Calculating Pi First, let’s use a for-loop to explore how this series converges on Pi. The for loop will control how many terms in the series we add. Inside of the loop we can print out the current estimate of Pi.

10 How many terms do we need for high accuracy?
How many terms to have the estimate accurate to 5 decimal places? A for-loop is not as good here since we don’t know how many times the loop will iterate. Use a while loop. Stop when the accuracy is within 5 decimal places. Print out how many times it had to loop. How about 10 decimal places?

11 File I/O Why do we read and write to files?

12 What are text files? They contain “text” But what is that?

13 What are binary files? They contain binary data But what is that?

14 For this class, we will only use text files
Yay! Text files are going to contain strings If the file contains multiple lines, the last character on each line is ‘\n’ The special character for END OF LINE, sometimes abbreviated as EOL

15 To open a file for reading
myFile = open(“TheNameOfTheFile.txt”, “r”) That second argument indicates that this file will be “read from” – nothing can be written to the file.

16 Where does Python look for the file when it tries to open it?
It looks in the directory where the .py file is that is being run. In IDLE, that means your input files should be in the same directory where you created your myProgram.py file.

17 You can override where it looks for the file
You could have your files in a subdirectory: myFile = open(“dataFiles/data.txt”, “r”) OR, you could give a full path name starting with the root drive: myFile = open(“c:/users/guinnc/documents/data.txt”, “r”)

18 Remember to close the file when you are done with it
myFile.close()

19 Reading Text Files The readline method returns as a string the next line of a text file, including the end-of-line character, \n. When the end-of-file is reached, it returns an empty string Intoduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 8.2 Using Text Files

20 Reading from a Text File
Intoduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 8.2 Using Text Files

21 input_file = open('myfile.txt','r')
It is also possible to read the lines of a file by use of the for statement, input_file = open('myfile.txt','r') for line in input_file: Using a for statement, all lines of the file will be read one by one. Using a while loop, however, lines can be read until a given value is found. Intoduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 8.2 Using Text Files

22 Let’s Try It Intoduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 8.2 Using Text Files

23 To open a file for writing, the open function is used as shown below,
Opening for Writing To open a file for writing, the open function is used as shown below, . Intoduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 8.2 Using Text Files

24 This code copies the contents of the input file, 'myfile
This code copies the contents of the input file, 'myfile.txt', line by line to the output file, 'myfile_copy.txt'. Intoduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 8.2 Using Text Files

25 Finally, when writing to a file, data is first placed in an area of memory called a buffer. Only when the buffer becomes full is the data actually written to the file. (This makes reading and writing files more efficient.) Since the last lines written may not completely fill the buffer, the last buffer’s worth of data may not be written. The close() method flushes the buffer to force the buffer to be written to the file. Intoduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 8.2 Using Text Files

26 Let’s program Let’s create a program that opens a file for writing.
Let’s loop 10,000 times and write a single random integer on each line of the file. Remember that “write” must take a string as an argument rather than an integer. How do we convert that integer to a string. Then close the file. Open the file in a text editor and look at it.

27 Let’s make another program
The goal of this program is to open the file you just created and compute the average of the numbers. It opens the file that you just created for reading. It reads in each line, converts that string to an int, adds it to a running sum, and increases a counter keeping track of how many lines have been read. When the loop is done, take that sum and divide it by the count. Don’t forget to close the file when you are done with it.

28 For Next Class, Thursday
Homework 4 is due tonight, 11:59pm, September 14


Download ppt "CSC 131: Introduction to Computer Science"

Similar presentations


Ads by Google