Before Reading Week. Lists. List methods. Nested Lists. Looping through lists using for loops. While loops

Similar documents
CSCA20 Worksheet Working with Files

CMSC201 Computer Science I for Majors

What we already know. more of what we know. results, searching for "This" 6/21/2017. chapter 14

CMSC201 Computer Science I for Majors

Text Input and Conditionals

1/25/2018. ECE 220: Computer Systems & Programming. Write Output Using printf. Use Backslash to Include Special ASCII Characters

Gradebook Export/Import Instructions

GIS 4653/5653: Spatial Programming and GIS. More Python: Statements, Types, Functions, Modules, Classes

age = 23 age = age + 1 data types Integers Floating-point numbers Strings Booleans loosely typed age = In my 20s

The Big Python Guide

File input and output if-then-else. Genome 559: Introduction to Statistical and Computational Genomics Prof. James H. Thomas

4. Inputting data or messages to a function is called passing data to the function.

1. What type of error produces incorrect results but does not prevent the program from running? a. syntax b. logic c. grammatical d.

Chapter 5: Compatibility of Data Files

program structure declarations and definitions expressions and statements more standard I/O

C: How to Program. Week /Mar/05

File input and output and conditionals. Genome 559: Introduction to Statistical and Computational Genomics Prof. James H. Thomas

LESSON 4. The DATA TYPE char

Lecture 3. Input, Output and Data Types

Should you know scanf and printf?

Starting chapter 5. l First open file, and say purpose read or write inputfile = open('mydata.txt', 'r') outputfile = open('myresults.

Functions. Genome 559: Introduction to Statistical and Computational Genomics Elhanan Borenstein

Chapter 2, Part I Introduction to C Programming

Unit 4. Input/Output Functions

CSCI 1100L: Topics in Computing Lab Lab 11: Programming with Scratch

CMSC201 Computer Science I for Majors

sed Stream Editor Checks for address match, one line at a time, and performs instruction if address matched

Programming for Engineers Introduction to C

Intro. Scheme Basics. scm> 5 5. scm>

Intro to Computer Programming (ICP) Rab Nawaz Jadoon

CS 106 Introduction to Computer Science I

MeltLab Reporting Text, CSV or Excel

Introduction to Computer Programming CSCI-UA 2. Review Midterm Exam 1

Textbook. Topic 8: Files and Exceptions. Files. Types of Files

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

Introduction to Computer Programming in Python Dr. William C. Bulko. Data Types

COP4020 Programming Assignment 1 - Spring 2011

Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program

Fundamentals of Programming Session 4

PYTHON. Values and Variables

Chapter 2 - Introduction to C Programming

while loops Genome 559: Introduction to Statistical and Computational Genomics Prof. James H. Thomas

Updating Users. Updating Users CHAPTER

for loops Genome 559: Introduction to Statistical and Computational Genomics Prof. James H. Thomas

CS201 Some Important Definitions

Iteration. # a and b are now equal # a and b are no longer equal Multiple assignment

CS 1301 Final Exam Review Guide

Introduction to Programming

Standard 11. Lesson 9. Introduction to C++( Up to Operators) 2. List any two benefits of learning C++?(Any two points)

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

CPSC 217 Midterm (Python 3 version)

OpenGlobal Virtuemart Product Feeds

Python lab session 1

University of Texas at Arlington, TX, USA

Sequences: Strings, Lists, and Files

Introduction to: Computers & Programming: Strings and Other Sequences

3 The Building Blocks: Data Types, Literals, and Variables

CSCA20 Worksheet Strings

Overview: Programming Concepts. Programming Concepts. Names, Values, And Variables

Overview: Programming Concepts. Programming Concepts. Chapter 18: Get With the Program: Fundamental Concepts Expressed in JavaScript

for loops Genome 559: Introduction to Statistical and Computational Genomics Prof. James H. Thomas

Contents. Jairo Pava COMS W4115 June 28, 2013 LEARN: Language Reference Manual

c++ keywords: ( all lowercase ) Note: cin and cout are NOT keywords.

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

for loops Genome 559: Introduction to Statistical and Computational Genomics Prof. James H. Thomas

File Input/Output in Python. October 9, 2017

The QuickStudy Guide for Zoho CRM

Chapter 1 Summary. Chapter 2 Summary. end of a string, in which case the string can span multiple lines.

Chapter 17. Fundamental Concepts Expressed in JavaScript

MP 3 A Lexer for MiniJava

INTRODUCTION. a Data File

Scheme: Strings Scheme: I/O

Introduction to C Programming. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

DSV Library for Lisp

Programming Languages & Translators. XML Document Manipulation Language (XDML) Language Reference Manual

CS 374 Fall 2014 Homework 2 Due Tuesday, September 16, 2014 at noon

Chapter 2 Input, Processing and Output. Hong Sun COSC 1436 Spring 2017 Jan 30, 2017

2 nd Week Lecture Notes

CS 25200: Systems Programming. Lecture 10: Shell Scripting in Bash

Reading data into R. 1. Data in human readable form, which can be inspected with a text editor.

Chapter 2: Overview of C++

LBSC 690: Information Technology Lecture 05 Structured data and databases

COSC 2P91. Bringing it all together... Week 4b. Brock University. Brock University (Week 4b) Bringing it all together... 1 / 22

CS 115 Lecture 4. More Python; testing software. Neil Moore

Variables, Constants, and Data Types

Converting File Input

CSC 108H: Introduction to Computer Programming. Summer Marek Janicki

How to bulk upload users

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

Full file at C How to Program, 6/e Multiple Choice Test Bank

Mail Merge Quick Reference Guide

Slide#1: A How-To Tutorial on How to Run Custom Reports in DOI LEARN.

UC Export Folders Version 3.5 for Worksite 8.x, 9.x x86

OCR Pseudocode to Python

Decaf Language Reference Manual

5. Excel Fundamentals

User Commands sed ( 1 )

CSV Import Guide. Public FINAL V

The MaSH Programming Language At the Statements Level

Reading and Writing Files. Keeping Data

Transcription:

Before Reading Week Lists List methods Nested Lists Looping through lists using for loops While loops

This Week and Maybe Next Special Characters Review Files What are they? Opening and closing files Reading and writing to files Comma Separated Value Files

Strings Using Conversion Specifiers We some'mes would like to insert values of variables into strings: A1 = 60 A2 = 75 A3 = 88 We would like: The average of 60, 75 and 88 is 74.33. How do we print this with our variables? >>>print( The average of, A1,,, A2, and, A3, is, (A1+A2+A3)/3) Does this work?

Strings Using Conversion Specifiers We displayed: The average of 60, 75 and 88 is 74.33333333333333. Q. What s wrong? A. Spacing is wrong around commas and periods. We have many more decimal places than wanted. Q. How can we fix it? A. Use conversion specifiers. >>>print( The average of %d, %d and %d is %.2f %(A1, A2, A3, (A1+A2+A3)/3.0)) The average of 60, 75 and 88 is 74.33.

Common Conversion Specifiers %d display the object as a decimal integer %f display the object as a floating point with 6 decimal places %.2f display the object as a floating point with 2 decimal places %s display the object as a string Q. What else do we use % for? A. Modulus. We say that % is overloaded.

Invisible Characters How do we indicate a tab, newline or or inside a string? \t tab \ double quote \n new 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 possibly 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? 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: <do something with the line> 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? A for loop automatically recognizes EOF. for line in myfile: <do something with the line> 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 applica@ons 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

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: )