Python Programming in Context

Slides:



Advertisements
Similar presentations
Python Mini-Course University of Oklahoma Department of Psychology Day 2 – Lesson 8 Fruitful Functions 05/02/09 Python Mini-Course: Day 2 - Lesson 8 1.
Advertisements

Def f(n): if (n == 0): return else: print(“*”) return f(n-1) f(3)
Computer Science 111 Fundamentals of Programming I Files.
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.
Topics This week: File input and output Python Programming, 2/e 1.
What is Information Technology?
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. Chapter 13 Files and Exception Handling 1.
Computer System Overview Chapter 1. Operating System Exploits the hardware resources of one or more processors Provides a set of services to system users.
November 15, 2005ICP: Chapter 7: Files and Exceptions 1 Introduction to Computer Programming Chapter 7: Files and Exceptions Michael Scherger Department.
Introduction to Computing Using Python Regular expressions Suppose we need to find all addresses in a web page How do we recognize addresses?
Python File Handling. In all the programs you have made so far when program is closed all the data is lost, but what if you want to keep the data to use.
Python Programming in Context Chapter 2. Objectives To understand how computers can help solve real problems To further explore numeric expressions, variables,
Chapter 4 Tables.  Look at table on Page 142 ◦ Attributes  Creating a table together in class ◦ ◦ table row ◦ table header ◦ table data cell.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Chapter 1. Objectives To provide examples of computer science in the real world To provide an overview of common problem- solving strategies To introduce.
Python Mini-Course University of Oklahoma Department of Psychology Day 4 – Lesson 13 Case study: Word play 05/02/09 Python Mini-Course: Day 4 – Lesson.
11 1 Cookies CGI/Perl Programming By Diane Zak Objectives In this chapter, you will: Learn the difference between temporary and persistent cookies.
Fill the screen challenge! This is a starter activity and should take 3 minutes [ slide 1 ] 1.Log in to your computer 2.Open IDLE 3.In interactive mode,
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.
CIT 590 Intro to Programming Files etc. Announcements From HW5 onwards (HW5, HW6,…) You can work alone. You can pick your own partner. You can also stick.
Introduction to Java Files. Text Files A sequential collection of data stored on a permanent storage device Hard drive USB memory CD/DVD Has a name and.
IB103 Week 10 Files CHAPTER 19. Files RAM vs. Files of CD or HD Fasterslower Costs morecheaper Temporarypermanent Stores whilecopied to RAM to Executingexecute.
Guide to Programming with Python Chapter Seven Files and Exceptions: The Trivia Challenge Game.
Chapter 3 Working with Batches of Data. Objectives Understand vector class and how it can be used to collect, store and manipulate data. Become familiar.
Files Tutor: You will need ….
Python Programming in Context Chapter 7. Objectives To use Python lists as a means of storing data To implement a nontrivial data mining application To.
Python – May 12 Recap lab Chapter 2 –operators –Strings –Lists –Control structures.
Midterm Review Important control structures Functions Loops Conditionals Important things to review Binary Boolean operators (and, or, not) Libraries (import.
FILES. open() The open() function takes a filename and path as input and returns a file object. file object = open(file_name [, access_mode][, buffering])
Python – May 16 Recap lab Simple string tokenizing Random numbers Tomorrow: –multidimensional array (list of list) –Exceptions.
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.
9. ITERATIONS AND LOOP STRUCTURES Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
CIT 590 Intro to Programming Files etc. Agenda Files Try catch except A module to read html off a remote website (only works sometimes)
Scanner as an iterator Several classes in the Java standard class library Are iterators Actually, the Scanner class is an iterator  The hasNext returns.
1 Essential Computing for Bioinformatics Bienvenido Vélez UPR Mayaguez Lecture 3 High-level Programming with Python Part III: Files and Directories Reference:
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the basic properties and characteristics of external files ❏ To.
File I/O in C++ I. Using Input/Output Files A computer file is stored on a secondary storage device (e.g., disk); is permanent; can be used to provide.
Today… Files from the Web! Dictionaries. Lists of lists. Winter 2016CISC101 - Prof. McLeod1.
File Processing Upsorn Praphamontripong CS 1110
Working with Batches of Data
Relational Databases: Basic Concepts
Fundamentals of Python: First Programs
Python Programming Challenge
Chapter 7 Text Input/Output Objectives
Chapter 7 Text Input/Output Objectives
Module 5 Working with Data
File I/O File input/output Iterate through a file using for
Computer Programming Fundamentals
File Writing Upsorn Praphamontripong CS 1110
Learning Intention Learning Intention: To develop understanding of variables, data types, and comments in text based programming languages Context: Sequencing.
Chapter 7 Text Input/Output Objectives
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Reading and Writing Files
File IO and Strings CIS 40 – Introduction to Programming in Python
For -G7 programing language Teacher / Shamsa Hassan Alhassouni.
Fundamentals of Programming I Files
File I/O File input/output Iterate through a file using for
File I/O File input/output Iterate through a file using for
CS 1111 Introduction to Programming Fall 2018
© 2016 Pearson Education, Ltd. All rights reserved.
Relational Databases: Basic Concepts
15-110: Principles of Computing
Relational Databases: Basic Concepts
Topics Introduction to File Input and Output
Introduction to Computer Science
CYB 130 RANK Dreams Come True / cyb130rank.com.
CS 1111 Introduction to Programming Spring 2019
CS 1111 Introduction to Programming Spring 2019
Introduction to Computer Science
Control 9 / 25 / 19.
Presentation transcript:

Python Programming in Context Chapter 5

Objectives To use text files to store large data sets To access online data sources To introduce the while loop To introduce Boolean expressions

Text File Sequence of characters, organized into lines, stored on some external memory device (such as a hard drive) End of line markers (new line character)

Python Open a file Read from a file Write to a file open read readline readlines Write to a file Write

Python Iterate thru the file line by line Use split method to process the line

Listing 5.1 rainfile = open("rainfall.txt","r") for aline in rainfile: values = aline.split() print(values[0], "had",values[1],"inches of rain.") rainfile.close()

Listing 5.2 rainfile = open("rainfall.txt","r") outfile = open("rainfallInCM.txt","w") for aline in rainfile: values = aline.split() inches = float(values[1]) cm = 2.54 * inches outfile.write(values[0]+" "+str(cm)+"\n") rainfile.close() outfile.close()

Processing Earthquake Data 2.8 2006/10/19 02:02:10 62.391 -149.751 15.0 CENTRAL ALASKA 2.5 2006/10/19 00:31:15 20.119 -156.213 1.5 MAUI REGION, HAWAII 5.0 2006/10/18 21:15:51 4.823 -82.592 37.3 SOUTH OF PANAMA 2.6 2006/10/18 21:12:25 59.934 -147.904 30.0 GULF OF ALASKA 3.4 2006/10/18 20:59:21 36.540 -89.640 7.7 SOUTHEASTERN MISSOURI 2.7 2006/10/18 20:11:22 61.023 -151.418 60.0 SOUTHERN ALASKA 3.1 2006/10/18 16:40:15 20.282 -156.611 4.7 MAUI REGION, HAWAII 2.7 2006/10/18 14:12:19 59.808 -152.538 50.0 SOUTHERN ALASKA 2.8 2006/10/18 14:02:12 60.686 -151.871 90.0 KENAI PENINSULA, ALASKA 4.9 2006/10/18 12:10:01 1.758 127.488 127.0 HALMAHERA, INDONESIA 6.2 2006/10/18 10:45:36 -15.081 167.243 138.5 VANUATU

Listing 5.3 def makeMagnitudeList(): quakefile = open("earthquakes.txt","r") maglist = [ ] for aline in quakefile: vlist = aline.split() maglist.append(float(vlist[0])) return maglist

Processing Data from the Internet urllib.request module Internet behaves just like a text file

Listing 5.4 import urllib.request def countHead(url): page = urllib.request.urlopen(url) numHeadLines = 0 line = page.readline().decode('utf-8') while '<head>' not in line: while '</head>' not in line: numHeadLines = numHeadLines + 1 while "<body>" not in line: while line != "" and "</body>" not in line: print (line[:-1]) print ("number of lines in header = ", numHeadLines) page.close()

Correlating Data from Internet Using real data streams Stock market data

Listing 5.5 def correlation(xlist, ylist): xbar = mean(xlist) ybar = mean(ylist) xstd = standardDev(xlist) ystd = standardDev(ylist) num = 0.0 for i in range(len(xlist)): num = num + (xlist[i]-xbar) * (ylist[i]-ybar) corr = num / ((len(xlist)-1) * xstd * ystd) return corr

Listing 5.6 def stockCorrelate(ticker1, ticker2): url1 = urllib.request.urlopen('http://ichart.yahoo.com/table.csv?s=%s'%ticker1) url2 = urllib.request.urlopen('http://ichart.yahoo.com/table.csv?s=%s'%ticker2) t1Data = url1.readlines() t2Data = url2.readlines() t1Data = [line[0:-1].decode("utf-8").split(',') for line in t1Data[1:] ] t2Data = [line[0:-1].decode("utf-8").split(',') for line in t2Data[1:] ] t1Close = [] t2Close = [] for i in range(min(len(t1Data), len(t2Data))): if t1Data[i][0] == t2Data[i][0]: t1Close.append(float(t1Data[i][4])) t2Close.append(float(t2Data[i][4])) return correlation(t1Close, t2Close)