writing your first python programcs.brown.edu/courses/cs0931/2012/lectures/lec2-2.pdf · python...
TRANSCRIPT
![Page 1: Writing your First Python Programcs.brown.edu/courses/cs0931/2012/lectures/LEC2-2.pdf · Python Functions CS0931 - Intro. to Comp. for the Humanities and Social Sciences 15 >>> myList](https://reader035.vdocument.in/reader035/viewer/2022062602/5ede2324ad6a402d66696dea/html5/thumbnails/1.jpg)
Writing your First Python Program
February 28, 2012
CS0931 - Intro. to Comp. for the Humanities and Social Sciences 1
![Page 2: Writing your First Python Programcs.brown.edu/courses/cs0931/2012/lectures/LEC2-2.pdf · Python Functions CS0931 - Intro. to Comp. for the Humanities and Social Sciences 15 >>> myList](https://reader035.vdocument.in/reader035/viewer/2022062602/5ede2324ad6a402d66696dea/html5/thumbnails/2.jpg)
Textual Analysis
CS0931 - Intro. to Comp. for the Humanities and Social Sciences 2
Define Problem
Find Data
Write a set of instructions
Solution
Python
Build a Concordance of a text • Locations of words • Frequency of words
• Word frequencies across time • Determine authorship
• Count labels to determine liberal media bias
ACTACGTCGACTACGATCACGATCGCGCGATCACGTATTTACGATCAGCTACGATCGATCTACGATCGTAGCTGTGATCG
![Page 3: Writing your First Python Programcs.brown.edu/courses/cs0931/2012/lectures/LEC2-2.pdf · Python Functions CS0931 - Intro. to Comp. for the Humanities and Social Sciences 15 >>> myList](https://reader035.vdocument.in/reader035/viewer/2022062602/5ede2324ad6a402d66696dea/html5/thumbnails/3.jpg)
The Big Picture
CS0931 - Intro. to Comp. for the Humanities and Social Sciences 3
Overall Goal Build a Concordance of a text
• Locations of words • Frequency of words
Today • Briefly review expressions, assignments, & types • Learn about defining functions • Learn how to read in a text file and create a list of words • Write a program to count the number of words in Moby Dick
![Page 4: Writing your First Python Programcs.brown.edu/courses/cs0931/2012/lectures/LEC2-2.pdf · Python Functions CS0931 - Intro. to Comp. for the Humanities and Social Sciences 15 >>> myList](https://reader035.vdocument.in/reader035/viewer/2022062602/5ede2324ad6a402d66696dea/html5/thumbnails/4.jpg)
Last Class
CS0931 - Intro. to Comp. for the Humanities and Social Sciences 4
1. Expressions • Evaluate input and returns some output (calculator)
2. Assignments: <variable> = <expression> • Store the value of the expression in the variable
instead of outputting the value. • There is always an equals sign in an assignment • Variables can be named many things
3. Types • Integers vs. Floats (Decimals) • Strings in single quotes • Lists are sets of other types • We can index into Strings & Lists
• Indexed starting at 0!
General Rule: Expressions for a particular type will output that same type!
![Page 5: Writing your First Python Programcs.brown.edu/courses/cs0931/2012/lectures/LEC2-2.pdf · Python Functions CS0931 - Intro. to Comp. for the Humanities and Social Sciences 15 >>> myList](https://reader035.vdocument.in/reader035/viewer/2022062602/5ede2324ad6a402d66696dea/html5/thumbnails/5.jpg)
ACT2-1
• Do Task 1
CS0931 - Intro. to Comp. for the Humanities and Social Sciences 5
1. Expressions • Evaluate input and returns some output (calculator)
2. Assignments: <variable> = <expression> • Store the value of the expression in the variable instead of
outputting the value. • There is always an equals sign in an assignment • Variables can be named many things
3. Types • Integers vs. Floats (Decimals) • Strings in single quotes • Lists are sets of other types • We can index into Strings & Lists
• Indexed starting at 0!
General Rule: Expressions for a
particular type will output that same type!
![Page 6: Writing your First Python Programcs.brown.edu/courses/cs0931/2012/lectures/LEC2-2.pdf · Python Functions CS0931 - Intro. to Comp. for the Humanities and Social Sciences 15 >>> myList](https://reader035.vdocument.in/reader035/viewer/2022062602/5ede2324ad6a402d66696dea/html5/thumbnails/6.jpg)
The Big Picture
CS0931 - Intro. to Comp. for the Humanities and Social Sciences 6
Overall Goal Build a Concordance of a text
• Locations of words • Frequency of words
Today • Briefly review expressions, assignments, & types • Learn about defining functions • Learn how to read in a text file and create a list of words • Write a program to count the number of words in Moby Dick
![Page 7: Writing your First Python Programcs.brown.edu/courses/cs0931/2012/lectures/LEC2-2.pdf · Python Functions CS0931 - Intro. to Comp. for the Humanities and Social Sciences 15 >>> myList](https://reader035.vdocument.in/reader035/viewer/2022062602/5ede2324ad6a402d66696dea/html5/thumbnails/7.jpg)
Python Functions
Functions are new commands that we define
• Allows us to run many statements at one time.
CS0931 - Intro. to Comp. for the Humanities and Social Sciences 7
>>> myList = [2,5,9]
>>> def avg3(someList):
s = someList[0] + someList[1] + someList[2]
avg = s/3.0
return avg
>>>
![Page 8: Writing your First Python Programcs.brown.edu/courses/cs0931/2012/lectures/LEC2-2.pdf · Python Functions CS0931 - Intro. to Comp. for the Humanities and Social Sciences 15 >>> myList](https://reader035.vdocument.in/reader035/viewer/2022062602/5ede2324ad6a402d66696dea/html5/thumbnails/8.jpg)
Python Functions
Functions are new commands that we define
• Allows us to run many statements at one time.
CS0931 - Intro. to Comp. for the Humanities and Social Sciences 8
>>> myList = [2,5,9]
>>> def avg3(someList):
s = someList[0] + someList[1] + someList[2]
avg = s/3.0
return avg
>>> avg3(myList)
5.333333333333333
>>> myList = [1,2,3]
>>> finalValue = avg3(newList)
>>> finalValue
>>> 2.0
![Page 9: Writing your First Python Programcs.brown.edu/courses/cs0931/2012/lectures/LEC2-2.pdf · Python Functions CS0931 - Intro. to Comp. for the Humanities and Social Sciences 15 >>> myList](https://reader035.vdocument.in/reader035/viewer/2022062602/5ede2324ad6a402d66696dea/html5/thumbnails/9.jpg)
Python Functions
Functions are new commands that we define
• Allows us to run many statements at one time.
CS0931 - Intro. to Comp. for the Humanities and Social Sciences 9
>>> myList = [2,5,9]
>>> def avg3(someList):
s = someList[0] + someList[1] + someList[2]
avg = s/3.0
return avg
>>> avg3(myList)
5.333333333333333
>>> myList = [1,2,3]
>>> finalValue = avg3(myList)
>>> finalValue
>>> 2.0
WARNING: do not name a variable sum. It is a predefined function (it turns purple in IDLE)
![Page 10: Writing your First Python Programcs.brown.edu/courses/cs0931/2012/lectures/LEC2-2.pdf · Python Functions CS0931 - Intro. to Comp. for the Humanities and Social Sciences 15 >>> myList](https://reader035.vdocument.in/reader035/viewer/2022062602/5ede2324ad6a402d66696dea/html5/thumbnails/10.jpg)
Python Functions
CS0931 - Intro. to Comp. for the Humanities and Social Sciences 10
>>> myList = [2,5,9]
>>> def avg3(sL):
s = sL[0] + sL[1] + sL[2]
avg = s/3.0
return avg
>>> avg3(myList)
5.333333333333333
>>> myList = [1,2,3]
>>> finalValue = avg3(myList)
>>> finalValue
>>> 2.0
Variables
Name Value
Preloaded Functions
Name Inputs Outputs
type() expression type
…
New Functions
Name Inputs Outputs
“Inputs” are also called Arguments.
![Page 11: Writing your First Python Programcs.brown.edu/courses/cs0931/2012/lectures/LEC2-2.pdf · Python Functions CS0931 - Intro. to Comp. for the Humanities and Social Sciences 15 >>> myList](https://reader035.vdocument.in/reader035/viewer/2022062602/5ede2324ad6a402d66696dea/html5/thumbnails/11.jpg)
Python Functions
CS0931 - Intro. to Comp. for the Humanities and Social Sciences 11
>>> myList = [2,5,9]
>>> def avg3(sL):
s = sL[0] + sL[1] + sL[2]
avg = s/3.0
return avg
>>> avg3(myList)
5.333333333333333
>>> myList = [1,2,3]
>>> finalValue = avg3(myList)
>>> finalValue
>>> 2.0
Variables
Name Value
myList [2,5,9]
Preloaded Functions
Name Inputs Outputs
type() expression type
…
New Functions
Name Inputs Outputs
![Page 12: Writing your First Python Programcs.brown.edu/courses/cs0931/2012/lectures/LEC2-2.pdf · Python Functions CS0931 - Intro. to Comp. for the Humanities and Social Sciences 15 >>> myList](https://reader035.vdocument.in/reader035/viewer/2022062602/5ede2324ad6a402d66696dea/html5/thumbnails/12.jpg)
Python Functions
CS0931 - Intro. to Comp. for the Humanities and Social Sciences 12
>>> myList = [2,5,9]
>>> def avg3(sL):
s = sL[0] + sL[1] + sL[2]
avg = s/3.0
return avg
>>> avg3(myList)
5.333333333333333
>>> myList = [1,2,3]
>>> finalValue = avg3(myList)
>>> finalValue
>>> 2.0
Variables
Name Value
myList [2,5,9]
Preloaded Functions
Name Inputs Outputs
type expression type
…
New Functions
Name Inputs Outputs
avg3 list float
Input
Output
![Page 13: Writing your First Python Programcs.brown.edu/courses/cs0931/2012/lectures/LEC2-2.pdf · Python Functions CS0931 - Intro. to Comp. for the Humanities and Social Sciences 15 >>> myList](https://reader035.vdocument.in/reader035/viewer/2022062602/5ede2324ad6a402d66696dea/html5/thumbnails/13.jpg)
Python Functions
CS0931 - Intro. to Comp. for the Humanities and Social Sciences 13
>>> myList = [2,5,9]
>>> def avg3(sL):
s = sL[0] + sL[1] + sL[2]
avg = s/3.0
return avg
>>> avg3(myList)
5.333333333333333
>>> myList = [1,2,3]
>>> finalValue = avg3(myList)
>>> finalValue
>>> 2.0
Variables
Name Value
myList [2,5,9]
Preloaded Functions
Name Inputs Outputs
type expression type
…
New Functions
Name Inputs Outputs
avg3 list float
Invoke avg3
![Page 14: Writing your First Python Programcs.brown.edu/courses/cs0931/2012/lectures/LEC2-2.pdf · Python Functions CS0931 - Intro. to Comp. for the Humanities and Social Sciences 15 >>> myList](https://reader035.vdocument.in/reader035/viewer/2022062602/5ede2324ad6a402d66696dea/html5/thumbnails/14.jpg)
Python Functions
CS0931 - Intro. to Comp. for the Humanities and Social Sciences 14
>>> myList = [2,5,9]
>>> def avg3(sL):
s = sL[0] + sL[1] + sL[2]
avg = s/3.0
return avg
>>> avg3(myList)
5.333333333333333
>>> myList = [1,2,3]
>>> finalValue = avg3(myList)
>>> finalValue
>>> 2.0
Variables
Name Value
myList [2,5,9]
Preloaded Functions
Name Inputs Outputs
type expression type
…
New Functions
Name Inputs Outputs
avg3 list float
Invoke avg3
avg3 Variables
Name Value
sL [2,5,9]
![Page 15: Writing your First Python Programcs.brown.edu/courses/cs0931/2012/lectures/LEC2-2.pdf · Python Functions CS0931 - Intro. to Comp. for the Humanities and Social Sciences 15 >>> myList](https://reader035.vdocument.in/reader035/viewer/2022062602/5ede2324ad6a402d66696dea/html5/thumbnails/15.jpg)
Python Functions
CS0931 - Intro. to Comp. for the Humanities and Social Sciences 15
>>> myList = [2,5,9]
>>> def avg3(sL):
s = sL[0] + sL[1] + sL[2]
avg = s/3.0
return avg
>>> avg3(myList)
5.333333333333333
>>> myList = [1,2,3]
>>> finalValue = avg3(myList)
>>> finalValue
>>> 2.0
Variables
Name Value
myList [2,5,9]
Preloaded Functions
Name Inputs Outputs
type expression type
…
New Functions
Name Inputs Outputs
avg3 list float
Invoke avg3
avg3 Variables
Name Value
sL [2,5,9]
s 16
![Page 16: Writing your First Python Programcs.brown.edu/courses/cs0931/2012/lectures/LEC2-2.pdf · Python Functions CS0931 - Intro. to Comp. for the Humanities and Social Sciences 15 >>> myList](https://reader035.vdocument.in/reader035/viewer/2022062602/5ede2324ad6a402d66696dea/html5/thumbnails/16.jpg)
Python Functions
CS0931 - Intro. to Comp. for the Humanities and Social Sciences 16
>>> myList = [2,5,9]
>>> def avg3(sL):
s = sL[0] + sL[1] + sL[2]
avg = s/3.0
return avg
>>> avg3(myList)
5.333333333333333
>>> myList = [1,2,3]
>>> finalValue = avg3(myList)
>>> finalValue
>>> 2.0
Variables
Name Value
myList [2,5,9]
Preloaded Functions
Name Inputs Outputs
type expression type
…
New Functions
Name Inputs Outputs
avg3 list float
Invoke avg3
avg3 Variables
Name Value
sL [2,5,9]
s 16
avg 5.3333333333
![Page 17: Writing your First Python Programcs.brown.edu/courses/cs0931/2012/lectures/LEC2-2.pdf · Python Functions CS0931 - Intro. to Comp. for the Humanities and Social Sciences 15 >>> myList](https://reader035.vdocument.in/reader035/viewer/2022062602/5ede2324ad6a402d66696dea/html5/thumbnails/17.jpg)
Python Functions
CS0931 - Intro. to Comp. for the Humanities and Social Sciences 17
>>> myList = [2,5,9]
>>> def avg3(sL):
s = sL[0] + sL[1] + sL[2]
avg = s/3.0
return avg
>>> avg3(myList)
5.333333333333333
>>> myList = [1,2,3]
>>> finalValue = avg3(myList)
>>> finalValue
>>> 2.0
Variables
Name Value
myList [2,5,9]
Preloaded Functions
Name Inputs Outputs
type expression type
…
New Functions
Name Inputs Outputs
avg3 list float
Invoke avg3
![Page 18: Writing your First Python Programcs.brown.edu/courses/cs0931/2012/lectures/LEC2-2.pdf · Python Functions CS0931 - Intro. to Comp. for the Humanities and Social Sciences 15 >>> myList](https://reader035.vdocument.in/reader035/viewer/2022062602/5ede2324ad6a402d66696dea/html5/thumbnails/18.jpg)
Python Functions
CS0931 - Intro. to Comp. for the Humanities and Social Sciences 18
>>> myList = [2,5,9]
>>> def avg3(sL):
s = sL[0] + sL[1] + sL[2]
avg = s/3.0
return avg
>>> avg3(myList)
5.333333333333333
>>> myList = [1,2,3]
>>> finalValue = avg3(myList)
>>> finalValue
>>> 2.0
Variables
Name Value
myList [2,5,9]
Preloaded Functions
Name Inputs Outputs
type expression type
…
New Functions
Name Inputs Outputs
avg3 list float
Returned value
![Page 19: Writing your First Python Programcs.brown.edu/courses/cs0931/2012/lectures/LEC2-2.pdf · Python Functions CS0931 - Intro. to Comp. for the Humanities and Social Sciences 15 >>> myList](https://reader035.vdocument.in/reader035/viewer/2022062602/5ede2324ad6a402d66696dea/html5/thumbnails/19.jpg)
Python Functions
CS0931 - Intro. to Comp. for the Humanities and Social Sciences 19
>>> myList = [2,5,9]
>>> def avg3(sL):
s = sL[0] + sL[1] + sL[2]
avg = s/3.0
return avg
>>> avg3(myList)
5.333333333333333
>>> myList = [1,2,3]
>>> finalValue = avg3(myList)
>>> finalValue
>>> 2.0
Variables
Name Value
myList [2,5,9]
[1,2,3]
Preloaded Functions
Name Inputs Outputs
type expression type
…
New Functions
Name Inputs Outputs
avg3 list float
![Page 20: Writing your First Python Programcs.brown.edu/courses/cs0931/2012/lectures/LEC2-2.pdf · Python Functions CS0931 - Intro. to Comp. for the Humanities and Social Sciences 15 >>> myList](https://reader035.vdocument.in/reader035/viewer/2022062602/5ede2324ad6a402d66696dea/html5/thumbnails/20.jpg)
Python Functions
CS0931 - Intro. to Comp. for the Humanities and Social Sciences 20
>>> myList = [2,5,9]
>>> def avg3(sL):
s = sL[0] + sL[1] + sL[2]
avg = s/3.0
return avg
>>> avg3(myList)
5.333333333333333
>>> myList = [1,2,3]
>>> finalValue = avg3(myList)
>>> finalValue
>>> 2.0
Variables
Name Value
myList [2,5,9]
[1,2,3]
Preloaded Functions
Name Inputs Outputs
type expression type
…
New Functions
Name Inputs Outputs
avg3 list float
Invoke avg3
avg3 Variables
Name Value
sL [1,2,3]
s 6
avg 2.0
![Page 21: Writing your First Python Programcs.brown.edu/courses/cs0931/2012/lectures/LEC2-2.pdf · Python Functions CS0931 - Intro. to Comp. for the Humanities and Social Sciences 15 >>> myList](https://reader035.vdocument.in/reader035/viewer/2022062602/5ede2324ad6a402d66696dea/html5/thumbnails/21.jpg)
Python Functions
CS0931 - Intro. to Comp. for the Humanities and Social Sciences 21
>>> myList = [2,5,9]
>>> def avg3(sL):
s = sL[0] + sL[1] + sL[2]
avg = s/3.0
return avg
>>> avg3(myList)
5.333333333333333
>>> myList = [1,2,3]
>>> finalValue = avg3(myList)
>>> finalValue
>>> 2.0
Variables
Name Value
myList [2,5,9]
[1,2,3]
finalValue 2.0
New Functions
Name Inputs Outputs
avg3 list float
![Page 22: Writing your First Python Programcs.brown.edu/courses/cs0931/2012/lectures/LEC2-2.pdf · Python Functions CS0931 - Intro. to Comp. for the Humanities and Social Sciences 15 >>> myList](https://reader035.vdocument.in/reader035/viewer/2022062602/5ede2324ad6a402d66696dea/html5/thumbnails/22.jpg)
Python Functions
CS0931 - Intro. to Comp. for the Humanities and Social Sciences 22
>>> myList = [2,5,9]
>>> def avg3(sL):
s = sL[0] + sL[1] + sL[2]
avg = s/3.0
return avg
>>> avg3(myList)
5.333333333333333
>>> myList = [1,2,3]
>>> finalValue = avg3(myList)
>>> finalValue
>>> 2.0
Variables
Name Value
myList [2,5,9]
[1,2,3]
finalValue 2.0
New Functions
Name Inputs Outputs
avg3 list float
![Page 23: Writing your First Python Programcs.brown.edu/courses/cs0931/2012/lectures/LEC2-2.pdf · Python Functions CS0931 - Intro. to Comp. for the Humanities and Social Sciences 15 >>> myList](https://reader035.vdocument.in/reader035/viewer/2022062602/5ede2324ad6a402d66696dea/html5/thumbnails/23.jpg)
Python Functions
CS0931 - Intro. to Comp. for the Humanities and Social Sciences 23
>>> def someFunction(inputs):
output = <some expression>
return output
Function Definition
Function Inputs (Optional)
Function Output (Optional)
Indentation Matters!!
![Page 24: Writing your First Python Programcs.brown.edu/courses/cs0931/2012/lectures/LEC2-2.pdf · Python Functions CS0931 - Intro. to Comp. for the Humanities and Social Sciences 15 >>> myList](https://reader035.vdocument.in/reader035/viewer/2022062602/5ede2324ad6a402d66696dea/html5/thumbnails/24.jpg)
ACT2-1
• Do Task 2
CS0931 - Intro. to Comp. for the Humanities and Social Sciences 24
>>> def someFunction(inputs):
output = <some expression>
return output
![Page 25: Writing your First Python Programcs.brown.edu/courses/cs0931/2012/lectures/LEC2-2.pdf · Python Functions CS0931 - Intro. to Comp. for the Humanities and Social Sciences 15 >>> myList](https://reader035.vdocument.in/reader035/viewer/2022062602/5ede2324ad6a402d66696dea/html5/thumbnails/25.jpg)
Module Files
Allow us to save functions ( ‘.py’ extension )
• Download ACT2-1.py from the website and open it in IDLE. Take a moment to look at it.
• Run…Run Module (or press F5)
• To write your own file: – File…New Window
– Write your function definitions. Save the file.
– Run…Run Module (or press F5)
CS0931 - Intro. to Comp. for the Humanities and Social Sciences 25
![Page 26: Writing your First Python Programcs.brown.edu/courses/cs0931/2012/lectures/LEC2-2.pdf · Python Functions CS0931 - Intro. to Comp. for the Humanities and Social Sciences 15 >>> myList](https://reader035.vdocument.in/reader035/viewer/2022062602/5ede2324ad6a402d66696dea/html5/thumbnails/26.jpg)
CS0931 - Intro. to Comp. for the Humanities and Social Sciences 26
Break
Wed
.
http://xkcd.com/930/
![Page 27: Writing your First Python Programcs.brown.edu/courses/cs0931/2012/lectures/LEC2-2.pdf · Python Functions CS0931 - Intro. to Comp. for the Humanities and Social Sciences 15 >>> myList](https://reader035.vdocument.in/reader035/viewer/2022062602/5ede2324ad6a402d66696dea/html5/thumbnails/27.jpg)
The Big Picture
CS0931 - Intro. to Comp. for the Humanities and Social Sciences 27
Overall Goal Build a Concordance of a text
• Locations of words • Frequency of words
Today • Briefly review expressions, assignments, & types • Learn about defining functions • Learn how to read in a text file and create a list of words • Write a program to count the number of words in Moby Dick
![Page 28: Writing your First Python Programcs.brown.edu/courses/cs0931/2012/lectures/LEC2-2.pdf · Python Functions CS0931 - Intro. to Comp. for the Humanities and Social Sciences 15 >>> myList](https://reader035.vdocument.in/reader035/viewer/2022062602/5ede2324ad6a402d66696dea/html5/thumbnails/28.jpg)
Working with Files
1. Save poem.txt from the webpage.
2. Right-click and select ‘Properties’
3. Note the file location (C:\Users\Anna\Desktop…)
4. In python, write an assignment statement that stores the file location as a string.
CS0931 - Intro. to Comp. for the Humanities and Social Sciences 28
>>> fileName = "C:\Users\Anna\Desktop\poem.txt“
![Page 29: Writing your First Python Programcs.brown.edu/courses/cs0931/2012/lectures/LEC2-2.pdf · Python Functions CS0931 - Intro. to Comp. for the Humanities and Social Sciences 15 >>> myList](https://reader035.vdocument.in/reader035/viewer/2022062602/5ede2324ad6a402d66696dea/html5/thumbnails/29.jpg)
Working with Files
CS0931 - Intro. to Comp. for the Humanities and Social Sciences 29
Preloaded Functions
Name Inputs Outputs
type Expression Type Name
open Two Strings 1. File Name 2. “r” for read (for now)
File
>>> fileName = "C:\Users\Anna\Desktop\poem.txt"
File is a NEW Type
![Page 30: Writing your First Python Programcs.brown.edu/courses/cs0931/2012/lectures/LEC2-2.pdf · Python Functions CS0931 - Intro. to Comp. for the Humanities and Social Sciences 15 >>> myList](https://reader035.vdocument.in/reader035/viewer/2022062602/5ede2324ad6a402d66696dea/html5/thumbnails/30.jpg)
Working with Files
CS0931 - Intro. to Comp. for the Humanities and Social Sciences 30
Preloaded Functions
Name Inputs Outputs
type Expression Type Name
open Two Strings 1. File Name 2. “r” for read (for now)
File
>>> fileName = "C:\Users\Anna\Desktop\poem.txt"
>>> myFile = open(fileName,"r")
File is a NEW Type
![Page 31: Writing your First Python Programcs.brown.edu/courses/cs0931/2012/lectures/LEC2-2.pdf · Python Functions CS0931 - Intro. to Comp. for the Humanities and Social Sciences 15 >>> myList](https://reader035.vdocument.in/reader035/viewer/2022062602/5ede2324ad6a402d66696dea/html5/thumbnails/31.jpg)
Working with Files
CS0931 - Intro. to Comp. for the Humanities and Social Sciences 31
Preloaded Functions
Name Inputs Outputs
type Expression Type
open Two Strings 1. File Name 2. “r” for read (for now)
File
read
(On a File) none String
>>> fileName = "C:\Users\Anna\Desktop\poem.txt"
>>> myFile = open(fileName,"r")
![Page 32: Writing your First Python Programcs.brown.edu/courses/cs0931/2012/lectures/LEC2-2.pdf · Python Functions CS0931 - Intro. to Comp. for the Humanities and Social Sciences 15 >>> myList](https://reader035.vdocument.in/reader035/viewer/2022062602/5ede2324ad6a402d66696dea/html5/thumbnails/32.jpg)
Working with Files
CS0931 - Intro. to Comp. for the Humanities and Social Sciences 32
Preloaded Functions
Name Inputs Outputs
type Expression Type
open Two Strings 1. File Name 2. “r” for read (for now)
File
read
(On a File) none String
>>> fileName = "C:\Users\Anna\Desktop\poem.txt"
>>> myFile = open(fileName,"r")
>>> fileString = myFile.read()
![Page 33: Writing your First Python Programcs.brown.edu/courses/cs0931/2012/lectures/LEC2-2.pdf · Python Functions CS0931 - Intro. to Comp. for the Humanities and Social Sciences 15 >>> myList](https://reader035.vdocument.in/reader035/viewer/2022062602/5ede2324ad6a402d66696dea/html5/thumbnails/33.jpg)
Working with Files
CS0931 - Intro. to Comp. for the Humanities and Social Sciences 33
Preloaded Functions
Name Inputs Outputs
type Expression Type
open Two Strings 1. File Name 2. “r” for read (for now)
File
read
(On a File) none String
close
(On a File) none none
>>> fileName = "C:\Users\Anna\Desktop\poem.txt"
>>> myFile = open(fileName,"r")
>>> fileString = myFile.read()
>>> myFile.close()
![Page 34: Writing your First Python Programcs.brown.edu/courses/cs0931/2012/lectures/LEC2-2.pdf · Python Functions CS0931 - Intro. to Comp. for the Humanities and Social Sciences 15 >>> myList](https://reader035.vdocument.in/reader035/viewer/2022062602/5ede2324ad6a402d66696dea/html5/thumbnails/34.jpg)
Working with Files
CS0931 - Intro. to Comp. for the Humanities and Social Sciences 34
>>> fileString
'Sarah Cynthia Sylvia Stout\nWould not take the garbage
out!\nShe\'d scour the pots and scrape the pans,\nCandy
the yams and spice the hams,\nAnd though her daddy would
scream and shout,\nShe simply would not take the garbage
out.\nAnd so it piled up to the ceilings:\nCoffee grounds,
potato peelings,\nBrown bananas, rotten peas,\nChunks of
sour cottage cheese.\nIt filled the can, it covered the
floor,\nIt cracked the window and blocked the door\nWith
bacon rinds and chicken bones,\nDrippy ends of ice cream
cones,\nPrune pits, peach pits, orange peel,\nGloppy
glumps of cold oatmeal,\nPizza crusts and withered
greens,\nSoggy beans and tangerines,\nCrusts of black
burned buttered toast,
...
Because the hour is much too late.\nBut children, remember
Sarah Stout\nAnd always take the garbage out!'
- Shel Silverstein
![Page 35: Writing your First Python Programcs.brown.edu/courses/cs0931/2012/lectures/LEC2-2.pdf · Python Functions CS0931 - Intro. to Comp. for the Humanities and Social Sciences 15 >>> myList](https://reader035.vdocument.in/reader035/viewer/2022062602/5ede2324ad6a402d66696dea/html5/thumbnails/35.jpg)
Working with Files
CS0931 - Intro. to Comp. for the Humanities and Social Sciences 35
>>> fileString
'Sarah Cynthia Sylvia Stout\nWould not take the garbage
out!\nShe\'d scour the pots and scrape the pans,\nCandy
the yams and spice the hams,\nAnd though her daddy would
scream and shout,\nShe simply would not take the garbage
out.\nAnd so it piled up to the ceilings:\nCoffee grounds,
potato peelings,\nBrown bananas, rotten peas,\nChunks of
sour cottage cheese.\nIt filled the can, it covered the
floor,\nIt cracked the window and blocked the door\nWith
bacon rinds and chicken bones,\nDrippy ends of ice cream
cones,\nPrune pits, peach pits, orange peel,\nGloppy
glumps of cold oatmeal,\nPizza crusts and withered
greens,\nSoggy beans and tangerines,\nCrusts of black
burned buttered toast,
...
Because the hour is much too late.\nBut children, remember
Sarah Stout\nAnd always take the garbage out!'
- Shel Silverstein
Escape Characters ‘\’ means interpret the NEXT character differently. • \n : “new line” • \’ : “apostrophe” • \t : “tab”
![Page 36: Writing your First Python Programcs.brown.edu/courses/cs0931/2012/lectures/LEC2-2.pdf · Python Functions CS0931 - Intro. to Comp. for the Humanities and Social Sciences 15 >>> myList](https://reader035.vdocument.in/reader035/viewer/2022062602/5ede2324ad6a402d66696dea/html5/thumbnails/36.jpg)
Working with Files
CS0931 - Intro. to Comp. for the Humanities and Social Sciences 36
Preloaded Functions
Name Inputs Outputs
type Expression Type
open Two Strings 1. File Name 2. “r” for read (for now)
File
read
(On a File) none String
close
(On a File) none none
split
(On a String) (optional) delimiter List of Strings
>>> myList = fileString.split()
![Page 37: Writing your First Python Programcs.brown.edu/courses/cs0931/2012/lectures/LEC2-2.pdf · Python Functions CS0931 - Intro. to Comp. for the Humanities and Social Sciences 15 >>> myList](https://reader035.vdocument.in/reader035/viewer/2022062602/5ede2324ad6a402d66696dea/html5/thumbnails/37.jpg)
Working with Files
CS0931 - Intro. to Comp. for the Humanities and Social Sciences 37
>>> myList
['Sarah', 'Cynthia', 'Sylvia', 'Stout', 'Would', 'not',
'take', 'the', 'garbage', 'out!', "She'd", 'scour', 'the',
'pots', 'and', 'scrape', 'the', 'pans,', 'Candy', 'the',
'yams', 'and', 'spice', 'the', 'hams,', 'And', 'though',
'her', 'daddy', 'would', 'scream', 'and', 'shout,', 'She',
'simply', 'would', 'not', 'take', 'the', 'garbage',
'out.', 'And', 'so', 'it', 'piled', 'up', 'to', 'the',
'ceilings:', 'Coffee', 'grounds,', 'potato', 'peelings,',
'Brown',
...
'an', 'awful', 'fate,', 'That', 'I', 'cannot', 'now',
'relate', 'Because', 'the', 'hour', 'is', 'much', 'too',
'late.', 'But', 'children,', 'remember', 'Sarah', 'Stout',
'And', 'always', 'take', 'the', 'garbage', 'out!']
![Page 38: Writing your First Python Programcs.brown.edu/courses/cs0931/2012/lectures/LEC2-2.pdf · Python Functions CS0931 - Intro. to Comp. for the Humanities and Social Sciences 15 >>> myList](https://reader035.vdocument.in/reader035/viewer/2022062602/5ede2324ad6a402d66696dea/html5/thumbnails/38.jpg)
Activity
• Do Task 3
CS0931 - Intro. to Comp. for the Humanities and Social Sciences 38
Preloaded Functions
Name Inputs Outputs
type Expression Type
open Two Strings 1. File Name 2. “r” for read (for now)
File
read
(On a File) none String
close
(On a File) none none
split
(On a String) (optional) delimiter List of Strings
![Page 39: Writing your First Python Programcs.brown.edu/courses/cs0931/2012/lectures/LEC2-2.pdf · Python Functions CS0931 - Intro. to Comp. for the Humanities and Social Sciences 15 >>> myList](https://reader035.vdocument.in/reader035/viewer/2022062602/5ede2324ad6a402d66696dea/html5/thumbnails/39.jpg)
The Big Picture
CS0931 - Intro. to Comp. for the Humanities and Social Sciences 39
Overall Goal Build a Concordance of a text
• Locations of words • Frequency of words
Today • Briefly review expressions, assignments, & types • Learn about defining functions • Learn how to read in a text file and create a list of words • Write a program to count the number of words in Moby Dick
![Page 40: Writing your First Python Programcs.brown.edu/courses/cs0931/2012/lectures/LEC2-2.pdf · Python Functions CS0931 - Intro. to Comp. for the Humanities and Social Sciences 15 >>> myList](https://reader035.vdocument.in/reader035/viewer/2022062602/5ede2324ad6a402d66696dea/html5/thumbnails/40.jpg)
Python For Statements (For Loops)
“For each element in list myList, do something”
CS0931 - Intro. to Comp. for the Humanities and Social Sciences 40
>>> myList = [1,2,3]
>>> for element in myList:
print element
1
2
3
>>>
![Page 41: Writing your First Python Programcs.brown.edu/courses/cs0931/2012/lectures/LEC2-2.pdf · Python Functions CS0931 - Intro. to Comp. for the Humanities and Social Sciences 15 >>> myList](https://reader035.vdocument.in/reader035/viewer/2022062602/5ede2324ad6a402d66696dea/html5/thumbnails/41.jpg)
Python For Statements (For Loops)
“For each element in list myList, do something”
CS0931 - Intro. to Comp. for the Humanities and Social Sciences 41
>>> myList = [1,2,3]
>>> for element in myList:
print element
1
2
3
>>>
Print Statement
![Page 42: Writing your First Python Programcs.brown.edu/courses/cs0931/2012/lectures/LEC2-2.pdf · Python Functions CS0931 - Intro. to Comp. for the Humanities and Social Sciences 15 >>> myList](https://reader035.vdocument.in/reader035/viewer/2022062602/5ede2324ad6a402d66696dea/html5/thumbnails/42.jpg)
Python For Statements (For Loops)
“For each element in list myList, do something”
CS0931 - Intro. to Comp. for the Humanities and Social Sciences 42
>>> myList = [1,2,3]
>>> for element in myList:
print element
1
2
3
>>>
List
![Page 43: Writing your First Python Programcs.brown.edu/courses/cs0931/2012/lectures/LEC2-2.pdf · Python Functions CS0931 - Intro. to Comp. for the Humanities and Social Sciences 15 >>> myList](https://reader035.vdocument.in/reader035/viewer/2022062602/5ede2324ad6a402d66696dea/html5/thumbnails/43.jpg)
Python For Statements (For Loops)
“For each element in list myList, do something”
CS0931 - Intro. to Comp. for the Humanities and Social Sciences 43
>>> myList = [1,2,3]
>>> for element in myList:
print element
1
2
3
>>>
List
Variable
![Page 44: Writing your First Python Programcs.brown.edu/courses/cs0931/2012/lectures/LEC2-2.pdf · Python Functions CS0931 - Intro. to Comp. for the Humanities and Social Sciences 15 >>> myList](https://reader035.vdocument.in/reader035/viewer/2022062602/5ede2324ad6a402d66696dea/html5/thumbnails/44.jpg)
Python For Statements (For Loops)
“For each element in list myList, do something”
CS0931 - Intro. to Comp. for the Humanities and Social Sciences 44
>>> myList = [1,2,3]
>>> for num in myList:
print num
1
2
3
>>>
List
Variable
![Page 45: Writing your First Python Programcs.brown.edu/courses/cs0931/2012/lectures/LEC2-2.pdf · Python Functions CS0931 - Intro. to Comp. for the Humanities and Social Sciences 15 >>> myList](https://reader035.vdocument.in/reader035/viewer/2022062602/5ede2324ad6a402d66696dea/html5/thumbnails/45.jpg)
Python For Statements (For Loops)
“For each element in list myList, do something”
CS0931 - Intro. to Comp. for the Humanities and Social Sciences 45
>>> myList = [1,2,3]
>>> for num in myList:
print num
1
2
3
>>>
List
Variable Indentation Matters!!
![Page 46: Writing your First Python Programcs.brown.edu/courses/cs0931/2012/lectures/LEC2-2.pdf · Python Functions CS0931 - Intro. to Comp. for the Humanities and Social Sciences 15 >>> myList](https://reader035.vdocument.in/reader035/viewer/2022062602/5ede2324ad6a402d66696dea/html5/thumbnails/46.jpg)
Word Count for Shel’s Poem
CS0931 - Intro. to Comp. for the Humanities and Social Sciences 46
def countWordsInShel():
'''Returns the number of words in the poem.'''
myList = readShel()
# the 'count' variable counts the number of words
count = 0
for word in myList:
count = count + 1
print "There are ",count," words in the poem."
return count
![Page 47: Writing your First Python Programcs.brown.edu/courses/cs0931/2012/lectures/LEC2-2.pdf · Python Functions CS0931 - Intro. to Comp. for the Humanities and Social Sciences 15 >>> myList](https://reader035.vdocument.in/reader035/viewer/2022062602/5ede2324ad6a402d66696dea/html5/thumbnails/47.jpg)
Word Count for Shel’s Poem
CS0931 - Intro. to Comp. for the Humanities and Social Sciences 47
def countWordsInShel():
'''Returns the number of words in the poem.'''
myList = readShel()
# the 'count' variable counts the number of words
count = 0
for word in myList:
count = count + 1
print "There are ",count," words in the poem.“
return count
![Page 48: Writing your First Python Programcs.brown.edu/courses/cs0931/2012/lectures/LEC2-2.pdf · Python Functions CS0931 - Intro. to Comp. for the Humanities and Social Sciences 15 >>> myList](https://reader035.vdocument.in/reader035/viewer/2022062602/5ede2324ad6a402d66696dea/html5/thumbnails/48.jpg)
Word Count for Shel’s Poem
CS0931 - Intro. to Comp. for the Humanities and Social Sciences 48
def countWordsInShel():
'''Returns the number of words in the poem.'''
myList = readShel()
# the 'count' variable counts the number of words
count = 0
for word in myList:
count = count + 1
print("There are ",count," words in the poem.“)
return count
Program Description (triple quotes)
Comment (#)
Print Statement
Good Programming Practices: Documentation!
![Page 49: Writing your First Python Programcs.brown.edu/courses/cs0931/2012/lectures/LEC2-2.pdf · Python Functions CS0931 - Intro. to Comp. for the Humanities and Social Sciences 15 >>> myList](https://reader035.vdocument.in/reader035/viewer/2022062602/5ede2324ad6a402d66696dea/html5/thumbnails/49.jpg)
Activity
• Do Task 4
CS0931 - Intro. to Comp. for the Humanities and Social Sciences 49
![Page 50: Writing your First Python Programcs.brown.edu/courses/cs0931/2012/lectures/LEC2-2.pdf · Python Functions CS0931 - Intro. to Comp. for the Humanities and Social Sciences 15 >>> myList](https://reader035.vdocument.in/reader035/viewer/2022062602/5ede2324ad6a402d66696dea/html5/thumbnails/50.jpg)
The Big Picture
CS0931 - Intro. to Comp. for the Humanities and Social Sciences 50
Overall Goal Build a Concordance of a text
• Locations of words • Frequency of words
Today • Briefly review expressions, assignments, & types • Learn about defining functions • Learn how to read in a text file and create a list of words • Write a program to count the number of words in Moby Dick • There’s a shortcut…
![Page 51: Writing your First Python Programcs.brown.edu/courses/cs0931/2012/lectures/LEC2-2.pdf · Python Functions CS0931 - Intro. to Comp. for the Humanities and Social Sciences 15 >>> myList](https://reader035.vdocument.in/reader035/viewer/2022062602/5ede2324ad6a402d66696dea/html5/thumbnails/51.jpg)
A Shortcut to List Length
CS0931 - Intro. to Comp. for the Humanities and Social Sciences 51
Preloaded Functions
Name Inputs Outputs
type Expression Type
open Two Strings 1. File Name 2. “r” for read (for now)
File
read
(On a File) none String
close
(On a File) none none
split
(On a String) (optional) delimiter List of Strings
len List Integer
>>> len(myList)
“Inputs” are also called Arguments.
![Page 52: Writing your First Python Programcs.brown.edu/courses/cs0931/2012/lectures/LEC2-2.pdf · Python Functions CS0931 - Intro. to Comp. for the Humanities and Social Sciences 15 >>> myList](https://reader035.vdocument.in/reader035/viewer/2022062602/5ede2324ad6a402d66696dea/html5/thumbnails/52.jpg)
Python For Statements (For Loops)
“For each element in list myList, do something”
CS0931 - Intro. to Comp. for the Humanities and Social Sciences 52
>>> myList = [1,2,3]
>>> for i in range(0,3):
print myList[i]
1
2
3
>>>
![Page 53: Writing your First Python Programcs.brown.edu/courses/cs0931/2012/lectures/LEC2-2.pdf · Python Functions CS0931 - Intro. to Comp. for the Humanities and Social Sciences 15 >>> myList](https://reader035.vdocument.in/reader035/viewer/2022062602/5ede2324ad6a402d66696dea/html5/thumbnails/53.jpg)
Python For Statements (For Loops)
“For each element in list myList, do something”
CS0931 - Intro. to Comp. for the Humanities and Social Sciences 53
>>> myList = [1,2,3]
>>> for i in range(0,3):
print myList[i]
1
2
3
>>>
Preloaded Functions
range Two Integers 1. Start Index (Inclusive) 2. End Index (Exclusive)
List of Integers
![Page 54: Writing your First Python Programcs.brown.edu/courses/cs0931/2012/lectures/LEC2-2.pdf · Python Functions CS0931 - Intro. to Comp. for the Humanities and Social Sciences 15 >>> myList](https://reader035.vdocument.in/reader035/viewer/2022062602/5ede2324ad6a402d66696dea/html5/thumbnails/54.jpg)
Python For Statements (For Loops)
“For each element in list myList, do something”
CS0931 - Intro. to Comp. for the Humanities and Social Sciences 54
>>> myList = [1,2,3]
>>> for i in range(0,3):
print myList[i]
1
2
3
>>>
List [0,1,2]
Preloaded Functions
range Two Integers 1. Start Index (Inclusive) 2. End Index (Exclusive)
List of Integers
![Page 55: Writing your First Python Programcs.brown.edu/courses/cs0931/2012/lectures/LEC2-2.pdf · Python Functions CS0931 - Intro. to Comp. for the Humanities and Social Sciences 15 >>> myList](https://reader035.vdocument.in/reader035/viewer/2022062602/5ede2324ad6a402d66696dea/html5/thumbnails/55.jpg)
Python For Statements (For Loops)
“For each element in list myList, do something”
CS0931 - Intro. to Comp. for the Humanities and Social Sciences 55
>>> myList = [1,2,3]
>>> for i in range(0,3):
print myList[i]
1
2
3
>>>
List [0,1,2]
Variable
![Page 56: Writing your First Python Programcs.brown.edu/courses/cs0931/2012/lectures/LEC2-2.pdf · Python Functions CS0931 - Intro. to Comp. for the Humanities and Social Sciences 15 >>> myList](https://reader035.vdocument.in/reader035/viewer/2022062602/5ede2324ad6a402d66696dea/html5/thumbnails/56.jpg)
Python For Statements (For Loops)
“For each element in list myList, do something”
CS0931 - Intro. to Comp. for the Humanities and Social Sciences 56
>>> myList = [1,2,3]
>>> for i in range(0,3):
print myList[i]
1
2
3
>>>
List [0,1,2]
Variable
Q: What if we don’t know the length of the list?
![Page 57: Writing your First Python Programcs.brown.edu/courses/cs0931/2012/lectures/LEC2-2.pdf · Python Functions CS0931 - Intro. to Comp. for the Humanities and Social Sciences 15 >>> myList](https://reader035.vdocument.in/reader035/viewer/2022062602/5ede2324ad6a402d66696dea/html5/thumbnails/57.jpg)
Python For Statements (For Loops)
“For each element in list myList, do something”
CS0931 - Intro. to Comp. for the Humanities and Social Sciences 57
>>> myList = [1,2,3]
>>> for i in range(0,len(myList)):
print myList[i]
1
2
3
>>>
List [0,1,2]
Variable
Q: What if we don’t know the length of the list?
![Page 58: Writing your First Python Programcs.brown.edu/courses/cs0931/2012/lectures/LEC2-2.pdf · Python Functions CS0931 - Intro. to Comp. for the Humanities and Social Sciences 15 >>> myList](https://reader035.vdocument.in/reader035/viewer/2022062602/5ede2324ad6a402d66696dea/html5/thumbnails/58.jpg)
Python For Statements (For Loops)
“For each element in list myList, do something”
CS0931 - Intro. to Comp. for the Humanities and Social Sciences 58
>>> def printList(list):
for i in range(0,len(list)):
print list[i]
return
Preloaded Functions
range Two Integers 1. Start Index (Inclusive) 2. End Index (Exclusive)
List of Integers
![Page 59: Writing your First Python Programcs.brown.edu/courses/cs0931/2012/lectures/LEC2-2.pdf · Python Functions CS0931 - Intro. to Comp. for the Humanities and Social Sciences 15 >>> myList](https://reader035.vdocument.in/reader035/viewer/2022062602/5ede2324ad6a402d66696dea/html5/thumbnails/59.jpg)
Python For Statements (For Loops)
“For each element in list myList, do something”
CS0931 - Intro. to Comp. for the Humanities and Social Sciences 59
>>> def printList(list):
for i in range(0,len(list)):
print list[i]
return
List [0,1,…len(list)-1]
Variable Indentation
Matters!!
Returns NOTHING!
![Page 60: Writing your First Python Programcs.brown.edu/courses/cs0931/2012/lectures/LEC2-2.pdf · Python Functions CS0931 - Intro. to Comp. for the Humanities and Social Sciences 15 >>> myList](https://reader035.vdocument.in/reader035/viewer/2022062602/5ede2324ad6a402d66696dea/html5/thumbnails/60.jpg)
Python Summary
CS0931 - Intro. to Comp. for the Humanities and Social Sciences 60
1. Statements • Expressions: evaluates input and returns some output • Assignments: <variable> = <expression> • Print Statements: no parentheses • For Statements
2. Types • Integers & Floats • Strings • Lists • Files
3. Function Definitions
![Page 61: Writing your First Python Programcs.brown.edu/courses/cs0931/2012/lectures/LEC2-2.pdf · Python Functions CS0931 - Intro. to Comp. for the Humanities and Social Sciences 15 >>> myList](https://reader035.vdocument.in/reader035/viewer/2022062602/5ede2324ad6a402d66696dea/html5/thumbnails/61.jpg)
Function Summary
CS0931 - Intro. to Comp. for the Humanities and Social Sciences 61
Preloaded Functions
Name Inputs Outputs
type Expression Type
open Two Strings 1. File Name 2. “r” for read (for now)
File
read
(On a File) none String
close
(On a File) none none
split
(On a String) (optional) delimiter List of Strings
len List Integer
range Two Integers 1. Start Index (Inclusive) 2. End Index (Exclusive)
List of Integers
“Inputs” are also called Arguments.
![Page 62: Writing your First Python Programcs.brown.edu/courses/cs0931/2012/lectures/LEC2-2.pdf · Python Functions CS0931 - Intro. to Comp. for the Humanities and Social Sciences 15 >>> myList](https://reader035.vdocument.in/reader035/viewer/2022062602/5ede2324ad6a402d66696dea/html5/thumbnails/62.jpg)
General Rules for Writing Functions
• Variables used within function definitions should be one of two things: 1. An input (also called an argument)
2. Previously assigned within the function def.
• Do not modify arguments within a function definition (define new variables instead)
• Do not have nested function definitions.
• Use only the returned values outside the function definition.
CS0931 - Intro. to Comp. for the Humanities and Social Sciences 62