More Summary Statistics March 5, 2013 CS0931 - Intro. to Comp. for the Humanities and Social Sciences 1.

Slides:



Advertisements
Similar presentations
Chapter 4 Computation Bjarne Stroustrup
Advertisements

ThinkPython Ch. 10 CS104 Students o CS104 n Prof. Norman.
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?
Chapter 3 DATA: TYPES, CLASSES, AND OBJECTS. Chapter 3 Data Abstraction Abstract data types allow you to work with data without concern for how the data.
CS324e - Elements of Graphics and Visualization A Little Java A Little Python.
Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
String and Lists Dr. Benito Mendoza. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list List.
COMP 14 Introduction to Programming Miguel A. Otaduy May 18, 2004.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
INTRODUCTION TO PYTHON PART 3 - LOOPS AND CONDITIONAL LOGIC CSC482 Introduction to Text Analytics Thomas Tiahrt, MA, PhD.
Identifiers and Assignment Statements. Data structures In any programming language you need to refer to data The simplest way is with the actual data.
Python.
Introduction to Python Lecture 1. CS 484 – Artificial Intelligence2 Big Picture Language Features Python is interpreted Not compiled Object-oriented language.
COMP 1001: Introduction to Computers for Arts and Social Sciences Programming in Scratch Monday, May 16, 2011.
Introduction to Python
Lists in Python.
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
Now String theory To Do: Lists This stuff hurts my brane. when you learn string theory from google images… Goal: Thinking like a machine You should now.
Variables and Expressions CMSC 201 Chang (rev )
Lists CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
How to Read Code Benfeard Williams 6/11/2015 Susie’s lecture notes are in the presenter’s notes, below the slides Disclaimer: Susie may have made errors.
Code Grammar. Syntax A set of rules that defines the combination of symbols and expressions.
Xin Liu Feb 4, * Use midterm questions for previous 231/217 midterms for practices * ams
CS161 Topic #16 1 Today in CS161 Lecture #16 Prepare for the Final Reviewing all Topics this term Variables If Statements Loops (do while, while, for)
CS 1 with Robots Variables, Data Types & Math Institute for Personal Robots in Education (IPRE)‏ Sec 9-7 Web Design.
Introduction to Strings Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg 1.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
Variables and Expressions CMSC 201. Today we start Python! Two ways to use python: You can write a program, as a series of instructions in a file, and.
Variables 1. What is a variable? Something whose value can change over time This value can change more than once over the time period (no limit!) Example:
Python Mini-Course University of Oklahoma Department of Psychology Day 3 – Lesson 11 Using strings and sequences 5/02/09 Python Mini-Course: Day 3 – Lesson.
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
Midterm Review Important control structures Functions Loops Conditionals Important things to review Binary Boolean operators (and, or, not) Libraries (import.
11 Using CSS to Control Appearance Beginning ASP.NET in C# and VB Chapter 3.
CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING.
1 CS 177 Week 6 Recitation Slides Review for Midterm Exam.
Values, Types, and Variables. Values Data Information Numbers Text Pretty much anything.
Functions Chapter 4 Python for Informatics: Exploring Information Slightly modified by Recep Kaya Göktaş on March 2015.
Primitive Data Types. int This is the type you are familiar with and have been using Stores an integer value (whole number) between -2,147,483,648 (-2.
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #003 (February 14, 2015)
String and Lists Dr. José M. Reyes Álamo. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list.
Loop Structures and Booleans Zelle - Chapter 8 Charles Severance - Textbook: Python Programming: An Introduction to Computer Science,
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Prof. Jeremy.
String and Lists Dr. José M. Reyes Álamo.
CMSC201 Computer Science I for Majors Lecture 03 – Operators
CS170 – Week 1 Lecture 3: Foundation Ismail abumuhfouz.
Introduction to Python
Computer Programming Fundamentals
Basic operators - strings
CMSC201 Computer Science I for Majors Lecture 03 – Operators
Introduction to Strings
Introduction to Strings
Decision Structures, String Comparison, Nested Structures
Introduction to Python
Decision Structures, String Comparison, Nested Structures
Loop Structures and Booleans Zelle - Chapter 8
String and Lists Dr. José M. Reyes Álamo.
To Start Create C:\100 folder
Introduction to Strings
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Nate Brunelle Today: Conditional Decision Statements
Thinking about programming
Introduction to Strings
Types, Truth, and Expressions
Data Types Every variable has a given data type. The most common data types are: String - Text made up of numbers, letters and characters. Integer - Whole.
Class code for pythonroom.com cchsp2cs
Introduction to Strings
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Presentation transcript:

More Summary Statistics March 5, 2013 CS Intro. to Comp. for the Humanities and Social Sciences 1

The Big Picture CS Intro. to Comp. for the Humanities and Social Sciences 2 Overall Goal Build a Concordance of a text Locations of words Frequency of words Today: Summary Statistics Administrative stuff Nitpicky Python details A new kind of statement Count the number of words in Moby Dick Compute the average word length of Moby Dick Find the longest word in Moby Dick Get the vocabulary size of Moby Dick

Administrative Stuff CS Intro. to Comp. for the Humanities and Social Sciences 3 Homeworks are shifting back half a week TA office hours are probably changing Code you submit as homework must work! – (You will lose points for errors or incorrect answers) Homework must be done in Python 2.6.6

The Big Picture CS Intro. to Comp. for the Humanities and Social Sciences 4 Overall Goal Build a Concordance of a text Locations of words Frequency of words Today: Summary Statistics Administrative stuff Nitpicky Python details A new kind of statement Count the number of words in Moby Dick Compute the average word length of Moby Dick Find the longest word in Moby Dick Get the vocabulary size of Moby Dick

Literals vs. Variables CS Intro. to Comp. for the Humanities and Social Sciences 5 "How does Python know what's a variable?" A literal is a piece of data that we give directly to Python – 'hello' is a string ( str ) literal – So are "hey there" and '''wazzup''' – 5 is an integer ( int ) literal – 32.8 is a floating-point ( float ) literal

Literals vs. Variables CS Intro. to Comp. for the Humanities and Social Sciences 6 "How does Python know what's a variable?" Variable names are made up of: – Letters (uppercase and lowercase) – Numbers (but only after the first letter) – Underscores Names for functions and types follow the same rules Anything else must be a literal or operator!

Literals vs. Variables CS Intro. to Comp. for the Humanities and Social Sciences 7 "How does Python know what's a variable?" fileName = "MobyDick.txt" print( 'fileName' + " = " + fileName )

The Big Picture CS Intro. to Comp. for the Humanities and Social Sciences 8 Overall Goal Build a Concordance of a text Locations of words Frequency of words Today: Summary Statistics Administrative stuff Nitpicky Python details A new kind of statement Count the number of words in Moby Dick Compute the average word length of Moby Dick Find the longest word in Moby Dick Get the vocabulary size of Moby Dick

Review: Basic Types Integers Floats Strings Booleans CS Intro. to Comp. for the Humanities and Social Sciences TrueFalse'12''hi''Moby'

New Type: Booleans Either True or False – Note the capitalization CS Intro. to Comp. for the Humanities and Social Sciences 10 >>> x = True >>> x True >>> y = False >>> y False

New Type: Booleans Either True or False – Note the capitalization New Operators CS Intro. to Comp. for the Humanities and Social Sciences 11 Numerical Operators OperatorExampleResult Sum Difference 1 – 2 Remember

New Type: Booleans Either True or False – Note the capitalization New Operators CS Intro. to Comp. for the Humanities and Social Sciences 12 Numerical Operators OperatorExampleResult Sum Difference 1 – 2 Remember Boolean Operators OperatorExampleResult Equality 1 == 2 Inequality 1 != 2 Less Than 1 < 2 Less Than or Equal To 1 <= 2 Greater Than 1 > 2 Greater Than or Equal To 1 >= 2

New Type: Booleans Either True or False – Note the capitalization New Operators CS Intro. to Comp. for the Humanities and Social Sciences 13 Numerical Operators OperatorExampleResult Sum Difference 1 – 2 Remember Boolean Operators OperatorExampleResult Equality 1 == 2False Inequality 1 != 2True Less Than 1 < 2True Less Than or Equal To 1 <= 2True Greater Than 1 > 2False Greater Than or Equal To 1 >= 2False

New Type: Booleans Either True or False – Note the capitalization New Operators These are expressions Assignments have only one equals sign. CS Intro. to Comp. for the Humanities and Social Sciences 14 Boolean Operators OperatorExampleResult Equality 1 == 2False Inequality 1 != 2True Less Than 1 < 2True Less Than or Equal To 1 <= 2True Greater Than 1 > 2False Greater Than or Equal To 1 >= 2False

Boolean Types Last Boolean Operators: and, or and not CS Intro. to Comp. for the Humanities and Social Sciences 15 Boolean Operators OperatorExamplesResult and(4<5) and (6<3) or(4<5) or (6<3) notnot(4<5)

Boolean Types Last Boolean Operators: and, or and not CS Intro. to Comp. for the Humanities and Social Sciences 16 Boolean Operators OperatorExamplesResult and(4<5) and (6<3)True and False or(4<5) or (6<3)True or False notnot(4<5)not(True)

Boolean Types Last Boolean Operators: and, or and not CS Intro. to Comp. for the Humanities and Social Sciences 17 Boolean Operators OperatorExamplesResult and(4<5) and (6<3)True and FalseFalse or(4<5) or (6<3)True or FalseTrue notnot(4<5)not(True)False

Boolean Types Last Boolean Operators: and, or and not CS Intro. to Comp. for the Humanities and Social Sciences 18 Boolean Operators OperatorExamplesResult and(4<5) and (6<3)True and FalseFalse or(4<5) or (6<3)True or FalseTrue notnot(4<5)not(True)False More ExamplesResult (4<5) and ((6<3) or (5==5)) (5==4) or (not(6<3))

Boolean Types Last Boolean Operators: and, or and not CS Intro. to Comp. for the Humanities and Social Sciences 19 Boolean Operators OperatorExamplesResult and(4<5) and (6<3)True and FalseFalse or(4<5) or (6<3)True or FalseTrue notnot(4<5)not(True)False More ExamplesResult (4<5) and ((6<3) or (5==5)) True and (False or True) (5==4) or (not(6<3))

Boolean Types Last Boolean Operators: and, or and not CS Intro. to Comp. for the Humanities and Social Sciences 20 Boolean Operators OperatorExamplesResult and(4<5) and (6<3)True and FalseFalse or(4<5) or (6<3)True or FalseTrue notnot(4<5)not(True)False More ExamplesResult (4<5) and ((6<3) or (5==5)) True and (False or True) True (5==4) or (not(6<3))

Boolean Types Last Boolean Operators: and, or and not CS Intro. to Comp. for the Humanities and Social Sciences 21 Boolean Operators OperatorExamplesResult and(4<5) and (6<3)True and FalseFalse or(4<5) or (6<3)True or FalseTrue notnot(4<5)not(True)False More ExamplesResult (4<5) and ((6<3) or (5==5)) True and (False or True) True (5==4) or (not(6<3))False or not(False)

Boolean Types Last Boolean Operators: and, or and not CS Intro. to Comp. for the Humanities and Social Sciences 22 Boolean Operators OperatorExamplesResult and(4<5) and (6<3)True and FalseFalse or(4<5) or (6<3)True or FalseTrue notnot(4<5)not(True)False More ExamplesResult (4<5) and ((6<3) or (5==5)) True and (False or True) True (5==4) or (not(6<3))False or not(False) True

Review: Statements Expression Statements Assignment Statements For Statements If Statements CS Intro. to Comp. for the Humanities and Social Sciences 23 Gives you output Stores a value in a variable “For each element in myList, do something” If A is true, then do something, otherwise do something else

Boolean Statements ( If Stmts) “If something’s true, do A” CS Intro. to Comp. for the Humanities and Social Sciences 24 def compare(x, y): if x > y: print(x, ' is greater than ', y)

Boolean Statements ( If Stmts) “If something’s true, do A, otherwise, do B” CS Intro. to Comp. for the Humanities and Social Sciences 25 def compare(x, y): if x > y: print(x, ' is greater than ', y) else: print(x, ' is less than or equal to ', y)

Boolean Statements ( If Stmts) “If something’s true, do A, otherwise, check something else; if that's true, do B, otherwise, do C” CS Intro. to Comp. for the Humanities and Social Sciences 26 def compare(x, y): if x > y: print(x, ' is greater than ', y) else: if x < y: print(x, ' is less than ', y) else: print(x, ' is equal to ', y)

Review: Other Things Lists (a type of data structure) CS Intro. to Comp. for the Humanities and Social Sciences 27 [0,1,2][‘hi’,’there’][‘hi’,0.0] [1,2,3,4,5,True,False,`true’,’one]

Review: Other Things Lists (a type of data structure) Files (an object that we can open, read, close) CS Intro. to Comp. for the Humanities and Social Sciences 28 [0,1,2][‘hi’,’there’][‘hi’,0.0] myFile = open(fileName,`r’) [1,2,3,4,5,True,False,`true’,’one]

The Big Picture CS Intro. to Comp. for the Humanities and Social Sciences 29 Overall Goal Build a Concordance of a text Locations of words Frequency of words Today: Summary Statistics Administrative stuff Nitpicky Python details A new kind of statement Count the number of words in Moby Dick Compute the average word length of Moby Dick Find the longest word in Moby Dick Get the vocabulary size of Moby Dick

A Shortcut to List Length CS Intro. to Comp. for the Humanities and Social Sciences 30 Preloaded Functions len ListInteger >>> len([3, 47, 91, -6, 18]) >>> uselessList = ['contextless', 'words'] >>> len(uselessList) >>> creature = 'woodchuck' >>> len(creature)

Python Functions CS Intro. to Comp. for the Humanities and Social Sciences 31 Preloaded Functions len List OR StringInteger

Python Functions CS Intro. to Comp. for the Humanities and Social Sciences 32 Preloaded Functions len List OR StringInteger float Number (as an Integer, Float, or String) Float int Number (as an Integer, Float, or String) Integer str Integer, Float, String, or List String These functions cast a variable of one type to another type

Python Functions CS Intro. to Comp. for the Humanities and Social Sciences 33 Preloaded Functions len List OR StringInteger float Number (as an Integer, Float, or String) Float int Number (as an Integer, Float, or String) Integer str Integer, Float, String, or List String range Two Integers 1.Start Index (Inclusive) 2.End Index (Exclusive) List of Integers These functions cast a variable of one type to another type

The Big Picture CS Intro. to Comp. for the Humanities and Social Sciences 34 Overall Goal Build a Concordance of a text Locations of words Frequency of words Today: Summary Statistics Administrative stuff Nitpicky Python details A new kind of statement Count the number of words in Moby Dick Compute the average word length of Moby Dick Find the longest word in Moby Dick Get the vocabulary size of Moby Dick

Compute the Average Word Length of Moby Dick CS Intro. to Comp. for the Humanities and Social Sciences 35 def avgWordLengthInMobyDick(): '''Gets the average word length in MobyDick.txt''' return avg

Compute the Average Word Length of Moby Dick CS Intro. to Comp. for the Humanities and Social Sciences 36 def avgWordLengthInMobyDick(): '''Gets the average word length in MobyDick.txt''' myList = readMobyDick() s = 0 for word in myList: s = s + len(word) avg = s/float(len(myList)) return avg

Is our Program Accurate? >>> MDList = readMobyDick() >>> MDList[0:99] ['CHAPTER', '1', 'Loomings', 'Call', 'me', 'Ishmael.', 'Some', 'years', 'ago--never', 'mind', 'how', 'long', 'precisely--', 'having', 'little', 'or', 'no', 'money', 'in', 'my', 'purse,', 'and', 'nothing', 'particular', 'to', 'interest', 'me', 'on', 'shore,', 'I', 'thought', 'I', 'would', 'sail', 'about', 'a', 'little', 'and', 'see', 'the', 'watery', 'part', 'of', 'the', 'world.', 'It', 'is', 'a', 'way', 'I', 'have', 'of', 'driving', 'off', 'the', 'spleen', 'and', 'regulating', 'the', 'circulation.', 'Whenever', 'I', 'find', 'myself', 'growing', 'grim', 'about', 'the', 'mouth;', 'whenever', 'it', 'is', 'a', 'damp,', 'drizzly', 'November', 'in', 'my', 'soul;', 'whenever', 'I', 'find', 'myself', 'involuntarily', 'pausing', 'before', 'coffin', 'warehouses,', 'and', 'bringing', 'up', 'the', 'rear', 'of', 'every', 'funeral', 'I', 'meet;', 'and'] CS Intro. to Comp. for the Humanities and Social Sciences 37

Break CS Intro. to Comp. for the Humanities and Social Sciences 38 Alphabet Maps of the UK and Ireland (url on site)

The Big Picture CS 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: Summary Statistics Administrative stuff Nitpicky Python details A new kind of statement Count the number of words in Moby Dick Compute the average word length of Moby Dick Find the longest word in Moby Dick Get the vocabulary size of Moby Dick

Get the Longest Word in Moby Dick CS Intro. to Comp. for the Humanities and Social Sciences 40 def getLongestWordInMobyDick(): '''Returns the longest word in MobyDick.txt''' return longestword

Get the Longest Word in Moby Dick CS Intro. to Comp. for the Humanities and Social Sciences 41 def getLongestWordInMobyDick(): '''Returns the longest word in MobyDick.txt''' myList = readMobyDick() longestword = "" for word in myList: if len(word) > len(longestword): longestword = word return longestword

Get the Longest Word in Moby Dick CS Intro. to Comp. for the Humanities and Social Sciences 42 def getLongestWordInMobyDick(): '''Returns the longest word in MobyDick.txt''' myList = readMobyDick() longestword = "" for word in myList: if len(word) > len(longestword): longestword = word return longestword Is our program accurate?

The Big Picture CS Intro. to Comp. for the Humanities and Social Sciences 43 Overall Goal Build a Concordance of a text Locations of words Frequency of words Today: Summary Statistics Administrative stuff Nitpicky Python details A new kind of statement Count the number of words in Moby Dick Compute the average word length of Moby Dick Find the longest word in Moby Dick Get the vocabulary size of Moby Dick

Boolean Expressions on Strings CS Intro. to Comp. for the Humanities and Social Sciences 44 Boolean Operators on Strings OperatorExampleResult Equality ‘a’ == ‘b’False Inequality ‘a’ != ‘b’True Less Than 'a' < 'b'True Less Than or Equal To 'a' <= 'b'True Greater Than 'a' > 'b'False Greater Than or Equal To 'a' >= 'b'False

Writing a vocabSize Function CS Intro. to Comp. for the Humanities and Social Sciences 45 def vocabSize(): myList = readMobyDick() uniqueList = noReplicates() return len(uniquelist)

Writing a vocabSize Function CS Intro. to Comp. for the Humanities and Social Sciences 46 def vocabSize(): myList = readMobyDick() uniqueList = noReplicates() return len(uniquelist) def noReplicates(wordList): '''takes a list as argument, returns a list free of replicate items. slow implementation.''' def isElementOf(myElement,myList): '''takes a string and a list and returns True if the string is in the list and False otherwise.'''

What does slow implementation mean? Re-comment the lines and run vocabSize() – Hint: Ctrl-C (or Command-C) will abort the call. CS Intro. to Comp. for the Humanities and Social Sciences 47

What does slow implementation mean? Re-comment the lines and run vocabSize() – Hint: Ctrl-C (or Command-C) will abort the call. There’s a faster way to write noReplicates() – What if we can sort the list? ['a','a','a','and','and','at',…,'zebra'] CS Intro. to Comp. for the Humanities and Social Sciences 48