Last Week Lists List methods Nested Lists Looping through lists using for loops.

Slides:



Advertisements
Similar presentations
CATHERINE AND ANNIE Python: Part 3. Intro to Loops Do you remember in Alice when you could use a loop to make a character perform an action multiple times?
Advertisements

CC SQL Utilities.
Macro simple idea of textual substitution useful when you need a group of instructions or directives frequently.
Computer Science 111 Fundamentals of Programming I Files.
File input and output if-then-else Genome 559: Introduction to Statistical and Computational Genomics Prof. William Stafford Noble.
Last Week Looping though lists Looping using range While loops.
Files CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Files Introduction to Computing Science and Programming I.
Chapter 10.
1 10/29/07CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
Files in Python Input techniques. Input from a file The type of data you will get from a file is always string or a list of strings. There are two ways.
1 10/25/06CS150 Introduction to Computer Science 1 Reading from and Writing to Files.
Introduction to Array The fundamental unit of data in any MATLAB program is the array. 1. An array is a collection of data values organized into rows and.
Last Week Doctests Lists List methods Nested Lists Aliasing.
The if statement and files. The if statement Do a code block only when something is True if test: print "The expression is true"
A First Program CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington Credits: a significant part of.
Python Programming Chapter 6: Iteration Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
Chapter 3: Formatted Input/Output Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
ICAPRG301A Week 2 Strings and things Charles Babbage Developed the design for the first computer which he called a difference engine, though.
File I/O Michael Ernst UW CSE 140 Winter File Input and Output As a programmer, when would one use a file? As a programmer, what does one do with.
Python Mini-Course University of Oklahoma Department of Psychology Lesson 18 Using text files to share data with other programs 5/07/09 Python Mini-Course:
File I/O Ruth Anderson UW CSE 160 Spring File Input and Output As a programmer, when would one use a file? As a programmer, what does one do with.
More about Strings. String Formatting  So far we have used comma separators to print messages  This is fine until our messages become quite complex:
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
Files Tutor: You will need ….
1 Printing in Python Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external files.
File IO.  File Input/Output  StreamWriter  StreamReader  Text Files  Binary Files.
CS190/295 Programming in Python for Life Sciences: Lecture 6 Instructor: Xiaohui Xie University of California, Irvine.
File input and output and conditionals Genome 559: Introduction to Statistical and Computational Genomics Prof. James H. Thomas.
File I/O Ruth Anderson UW CSE 140 Winter File Input and Output As a programmer, when would one use a file? As a programmer, what does one do with.
Files A collection of related data treated as a unit. Two types Text
Escape sequence Certain characters, preceded by a backslash ( \ ), are known as escape sequences They are used to display certain characters, or as display.
Review: A Computational View Programming language common concepts: 1. sequence of instructions -> order of operations important 2. conditional structures.
Chapter 3: Formatted Input/Output 1 Chapter 3 Formatted Input/Output.
Python focus – files The open keyword returns a file object Opening a file myFile = open('C:\file.txt', arg) Optional argument The second argument controls.
NXT File System Just like we’re able to store multiple programs and sound files to the NXT, we can store text files that contain information we specify.
Python: File Directories What is a directory? A hierarchical file system that contains folders and files. Directory (root folder) Sub-directory (folder.
CSC 108H: Introduction to Computer Programming Summer 2011 Marek Janicki.
Prof. Katherine Gibson Prof. Jeremy Dixon
CMSC201 Computer Science I for Majors Lecture 10 – File I/O
Taken from notes by Dr. Neil Moore & Dr. Debby Keen
CSC 108H: Introduction to Computer Programming
Ruth Anderson UW CSE 160 Winter 2016
Exceptions and files Taken from notes by Dr. Neil Moore
File input and output Genome 559: Introduction to Statistical and Computational Genomics Prof. William Stafford Noble Notes from 2009: Sample problem.
Ruth Anderson UW CSE 160 Winter 2017
Python I/O.
Ruth Anderson UW CSE 160 Spring 2018
File Handling Programming Guides.
Introduction to pseudocode
File IO and Strings CIS 40 – Introduction to Programming in Python
Fundamentals of Programming I Files
File Handling.
Exceptions and files Taken from notes by Dr. Neil Moore
CS190/295 Programming in Python for Life Sciences: Lecture 6
Lesson 09: Lists Topic: Introduction to Programming, Zybook Ch 8, P4E Ch 8. Slides on website.
Fundamentals of Data Structures
Lesson 09: Lists Class Chat: Attendance: Participation
More for loops Genome 559: Introduction to Statistical and Computational Genomics Prof. William Stafford Noble.
How to save information in files open, write, close
CMSC201 Computer Science I for Majors Lecture 13 – File I/O
Topics Introduction to File Input and Output
Reading from and Writing to Files
Reading from and Writing to Files
Programming Techniques :: File Handling
How to read from a file read, readline, reader
Introduction to Computer Science
Introduction to Computer Science
Presentation transcript:

Last Week Lists List methods Nested Lists Looping through lists using for loops

This Week and Maybe Next While loops Special characters in strings Files –What are they? –Opening and closing files –Reading and writing to files –Comma Separated Files

While Loops Sometimes we need to loop until a condition is met. For example: –Ask user for a password twice –If the passwords don’t match, ask again until they match or the user quits

While Loop Example English example: Ask for password. Ask for password again. While the passwords don’t match: Ask again.

While Loop Example Python example: password1 = input(‘Enter password: ’) Ask for password again. While the passwords don’t match: Ask again.

While Loop Example Python example: password1 = input(‘Enter password: ’) password2 = input(‘Re-enter password: ’) While the passwords don’t match: Ask again.

While Loop Example Python example: password1 = input(‘Enter password: ’) password2 = input(‘Re-enter password: ’) while password1 != password2: Ask again.

While Loop Example Python example: password1 = input(‘Enter password: ’) password2 = input(‘Re-enter password: ’) while password1 != password2: password2 = input(‘Re-enter password:’)

Invisible Characters How do we indicate a tab, newline or ‘ or “ inside a string? \ttab\”double quote \nnew line \’single quote\\backslash These are called escape sequences. print(“This string has a \n newline and a \t tab.” This string has a newline and a tab.

Files Files – what are they? –Can think of as a collection of stored information –Computer thinks as a sequence of bits possible arranged in characters Files have names: –my_python.py, homework.txt, etc. What can we do with a file? –Read from a file and write to a file

Opening Files How do we open a file in Python? import doctest myfile = open(‘story.txt’, ‘r’) Open is the Python function Story.txt is the name of the file to be opened myfile is a variable that is assigned the file object (also called stream or reader). ‘r’ is a string indicating what we will do with the file (read, write, append)

Using Files After we are finished with a file we must close it: myfile.close() When we write to a file, we have two choices: Write: myfile = open(‘filename’, ‘w’) Append: myfile = open(‘filename’, ‘a’) write replaces the file filename append appends to the file filename

Reading Files There are many ways to read from a file. 1.Using a for loop: myfile = open(‘filename’, ‘r’) for line in myfile: 2.Read the whole file at once into a list of strings: myfile = open(‘filename’, ‘r’) list_of_lines = myfile.readlines()

Reading Files 3.Read the entire file into a string: myfile = open(‘filename’, ‘r’) s = myfile.read() 4.Read a specific number of characters: myfile = open(‘filename’, ‘r’) s = myfile.read(10) reads 10 characters s = myfile.read(10) reads then next 10 characters

Reading Files 5.Read one line at a time: myfile = open(‘filename’, ‘r’) line = myfile.readline() reads a line line = myfile.readline() reads the next line

End Of File (EOF) How do we know when we reach the end of the file? This method automatically recognizes EOF. for line in myfile: Here we have to check for the end of file: line = myfile.readline() reads the next line and s = myfile.read(10) reads 10 characters The EOF character is the empty string “”.

Writing to a file First open the file for writing or appending: myfile = open(‘story.txt’, ‘w’) start the story myfile = open(‘story.txt’, ‘a’) continue the story Then write to the file: myfile.write(‘Once upon a time…’) myfile.write(‘The end.’) Then close the file: myfile.close()

Reading from a CSV file Often we have comma-separated values data files: First Name, Last Name, Utorid Anna, Bretscher, bretsche Joe, Johnson, johnsonj Sally, Jordan, jordansa Why do we like CSV files? –Spreadsheet applications like excel understand it –Many other programming languages also understand them –Easy to generate ourselves

Reading from a CSV file How do we read comma-separated values data files: import io import csv csv_file = open(‘csv_filename.csv’, ‘r’) reader = csv.reader(csv_file) … for line in reader: # read like an ordinary file # but line is a list