COMP1D04: From Scratch to Apps: Foundations of Computational Thinking and Literacy for Problem Solving
Topic 2: Simple Programming /
Textual AnalysisGrace Ngai
Much of this course and lots of the lecture notes were inspired by or derived from Brown University’s CS931. Our thanks go to Prof
Shriram Krishnamurthi and Hammurabi Mendes for their kind permission in allowing us to use their materials.
COMP1D04: From Scratch to Apps: Foundations of Computational Thinking and Literacy for Problem Solving
Up to now…Define the problem Find the Data
Write the Instructions
Solution
Computer (Google Docs)
XML data from Legco records
Make a big spreadsheet table
CSV format
Use X’s and Y’s votes to compare how similar they are to each other.
COMP1D04: From Scratch to Apps: Foundations of Computational Thinking and Literacy for Problem Solving
Textual AnalysisDefine the problem Find the Data
Write the Instructions
Solution
Computer
Build a concordance of a text - Locations of words - Frequencies of words
COMP1D04: From Scratch to Apps: Foundations of Computational Thinking and Literacy for Problem Solving
What is a Concordance?• An alphabetical index of all words in a text
Word LocationsApple 43, 57, 90, 103Boy 3, 6, 7, 8, 9Cat 33, 75, 90…
• Useful for understanding patterns of usages of words
COMP1D04: From Scratch to Apps: Foundations of Computational Thinking and Literacy for Problem Solving
Google n-gram viewer• What happens when we get to analyse lots and lots
and lots and lots of books, newspapers, etc?
COMP1D04: From Scratch to Apps: Foundations of Computational Thinking and Literacy for Problem Solving
https://books.google.com/ngrams
COMP1D04: From Scratch to Apps: Foundations of Computational Thinking and Literacy for Problem Solving
Textual AnalysisDefine the problem Find the Data
Write the Instructions
Solution
Computer
Build a concordance of a text - Locations of words - Frequencies of words
Word frequencies over time Author of texts Bias of authors (e.g. liberal media bias) Worldwide trends (e.g. oil vs. gold) …
Python
COMP1D04: From Scratch to Apps: Foundations of Computational Thinking and Literacy for Problem Solving
Python
piebald ball python, http://snakes-2013.blogspot.hk/2013/01/piebald-ball-python.html
COMP1D04: From Scratch to Apps: Foundations of Computational Thinking and Literacy for Problem Solving
Python• Also, a programming language
• Gives the computer instructions
• Just like a real language — has a grammar (syntax)
• I will back to school today.
• I will go back to school today.
• Also has meaning (semantics)
• I saw John in the park with the telescope.
COMP1D04: From Scratch to Apps: Foundations of Computational Thinking and Literacy for Problem Solving
Python Programming• Writing a Python program = Writing a program in the
Python language = Writing instructions for the computer in the Python language
• Interpreted mode: Interpreter runs the Python instructions line by line
• Reads in what you typed
• “Translates” to something the computer understands
• Executes it on the computer.
COMP1D04: From Scratch to Apps: Foundations of Computational Thinking and Literacy for Problem Solving
Python Interpreter — IDLE
COMP1D04: From Scratch to Apps: Foundations of Computational Thinking and Literacy for Problem Solving
COMP1D04: From Scratch to Apps: Foundations of Computational Thinking and Literacy for Problem Solving
Introduction to Python• Expressions are inputs that are evaluated by the computer
• Expressions return an answer (or an output)
>>> 4+2 6 >>> 4-2 2 >>> 4*2 8 >>> 4/2 2.0
Try these expressions in the IDLE window
IDLE prompt: Indicates that IDLE is ready for user input
input from user
output from computer
1. Expressions2. Assignments
a. Variables 3. Data Types
a. Integers b. Floats c. Strings d. Lists
COMP1D04: From Scratch to Apps: Foundations of Computational Thinking and Literacy for Problem Solving
Introduction to Python
We did the same thing in Google Sheets Assigned the value 4 to cell A1
• Assignments do not have an output. • They store values in the computer memory for future use.
1. Expressions 2. Assignments
a. Variables 3. Data Types
a. Integers b. Floats c. Strings d. Lists
COMP1D04: From Scratch to Apps: Foundations of Computational Thinking and Literacy for Problem Solving
Introduction to Python
We did the same thing in Google Sheets Renamed A1 to “myNumber”
• Assignments do not have an output. • They store values in the computer memory for future use.
1. Expressions 2. Assignments
a. Variables3. Data Types
a. Integers b. Floats c. Strings d. Lists
COMP1D04: From Scratch to Apps: Foundations of Computational Thinking and Literacy for Problem Solving
Introduction to Python• Assignments do not have an output. • They store values in the computer memory for future use.
>>> myNumber = 4 >>> 1. Expressions
2. Assignmentsa. Variables
3. Data Types a. Integers b. Floats c. Strings d. Lists
COMP1D04: From Scratch to Apps: Foundations of Computational Thinking and Literacy for Problem Solving
Introduction to Python• Assignments do not have an output. • They store values in the computer memory for future use.
variable expression
Computer Memory
Variable Name Value
myNumber 4
1. Expressions 2. Assignments
a. Variables3. Data Types
a. Integers b. Floats c. Strings d. Lists
>>> myNumber = 4 >>>
COMP1D04: From Scratch to Apps: Foundations of Computational Thinking and Literacy for Problem Solving
Introduction to Python• Assignments do not have an output. • They store values in the computer memory for future use.
>>> myNumber + 1 5 >>> (myNumber+2)*3 18
We can now use myNumber in expressions
Computer Memory
Variable Name Value
myNumber 4
1. Expressions 2. Assignments
a. Variables3. Data Types
a. Integers b. Floats c. Strings d. Lists
>>> myNumber = 4 >>>
COMP1D04: From Scratch to Apps: Foundations of Computational Thinking and Literacy for Problem Solving
Naming Variables• We can name variables anything we like in Python.
• As long as we follow some rules
• Can only have letters, numbers and underscores (no spaces, no special characters — no “-“, “!”, “~”)
• Must start with a letter
• Some words aren’t allowed (reserved words)
• And good if we follow some conventions
• Usually starts with a lowercase letter
• Usually describes the value that it is storing.
1. Expressions 2. Assignments
a. Variables3. Data Types
a. Integers b. Floats c. Strings d. Lists
>>> myNumber = 4 >>> myGPA = 3.3 >>> numOfEggs = 5
COMP1D04: From Scratch to Apps: Foundations of Computational Thinking and Literacy for Problem Solving
Reserved Words• Python allows us to name our variables almost anything we wish.
• But some words have special meaning in Python and we cannot use them.
• They are called reserved words.
and del for is raise
assert elif from lambda return
break else global not try
class except if or while
continue exec import pass yield
def finally in print
COMP1D04: From Scratch to Apps: Foundations of Computational Thinking and Literacy for Problem Solving
Introduction to Python• Python has different types of data
1. Expressions 2. Assignments
a. Variables 3. Data Types
a. Integersb. Floatsc. Strings d. Lists
>>> type(4) <class ‘int’> >>> type(4*2) <class ‘int’> >>> type(4+2) <class ‘int’> >>> type(4/2) <class ‘float’> >>> myGPA = 3.3 >>> type(myGPA) <class ‘float’>
• There are two kinds of numbers in Python.
• Integers are numbers without decimal places.
• Floats are numbers that have decimal places.
• Python decides whether the result of a numerical calculation is an integer or a float based on some rules.
COMP1D04: From Scratch to Apps: Foundations of Computational Thinking and Literacy for Problem Solving
Stuff we can do with numbers
• Python allows us a number of mathematical operations on numbers:
Operator Operation+ add- subtract* multiply/ divide** power% remainder
abs() absolute valueint() integral part onlyround() round to nearest whole number
>>> print(3+4)7>>> print(3*4)12>>> print(3-4)-1>>> print(3/4)0.75>>> print(10/3)3.3333333333333335>>> print(10%3)1>>> print(abs(5))5>>> print(abs(-3.5))3.5>>> print(int(5))5>>> print(int(3.5))3>>> print(round(3.5))4
COMP1D04: From Scratch to Apps: Foundations of Computational Thinking and Literacy for Problem Solving
Introduction to Python• Python has different types of data
1. Expressions 2. Assignments
a. Variables 3. Data Types
a. Integers b. Floats c. Stringsd. Lists
>>> “hi” ‘hi’ >>> word = “Hi there” >>> word ‘Hi there’
• Strings are sequences of characters (letters, numbers, spaces, special symbols, etc) surrounded by quotes.
COMP1D04: From Scratch to Apps: Foundations of Computational Thinking and Literacy for Problem Solving
Introduction to Python• We must put quotes around a string (otherwise Python doesn’t
know that it’s a string and doesn’t know what to do with it.
1. Expressions 2. Assignments
a. Variables 3. Data Types
a. Integers b. Floats c. Stringsd. Lists
>>> "hi" 'hi' >>> hi Traceback (most recent call last): File "<pyshell#13>", line 1, in <module> hi NameError: name 'hi' is not defined >>>
anything in red in IDLE is not a good sign.
COMP1D04: From Scratch to Apps: Foundations of Computational Thinking and Literacy for Problem Solving
Introduction to Python• We can do some arithmetic on strings, too!
1. Expressions 2. Assignments
a. Variables 3. Data Types
a. Integers b. Floats c. Stringsd. Lists
>>> x = "hi" >>> x + x ‘hihi’ >>> x ‘hi’ >>> y = x + x >>> y ‘hihi’ >>> z = “there” x+y+z ‘hihithere’ >>> x + “ “ + y + “ “ + z ‘hi hi there’
• The + operator concatenates (joins) strings together.
COMP1D04: From Scratch to Apps: Foundations of Computational Thinking and Literacy for Problem Solving
Introduction to Python• We can do some arithmetic on strings, too!
1. Expressions 2. Assignments
a. Variables 3. Data Types
a. Integers b. Floats c. Stringsd. Lists
>>> x = "hi" >>> x * 3 ‘hihihi’ >>> y = “there” >>> (x+y)*3 ‘hitherehitherehithere’ >>> x ‘hi’ >>> y ‘there’
• The * operator repeats strings.
• Note that using variables in arithmetic operations do not change the value stored inside the variable!
COMP1D04: From Scratch to Apps: Foundations of Computational Thinking and Literacy for Problem Solving
Introduction to Python• Python has different types of data
1. Expressions 2. Assignments
a. Variables 3. Data Types
a. Integers b. Floats c. Strings d. Lists
>>> [5, 10, 15] [5, 10, 15] >>> myList = [5, 10, 15] >>> myList [5, 10, 15] >>> stringList = [“hi”, “there”] >>> stringList [‘hi’, ‘there’]
• Lists are an ordered collection of items (values, “things”)
• Individual elements in a list are called elements
COMP1D04: From Scratch to Apps: Foundations of Computational Thinking and Literacy for Problem Solving
Introduction to Python• We can do some arithmetic on lists
• + concatenates, * repeats
>>> x + y [5, 10, 15, ‘hi, ‘there’] >>> x * 3 [5, 10, 15, 5, 10, 15, 5, 10, 15]
• We can only add lists to lists!>>> x + 3 Traceback (most recent call last): File "<pyshell#18>", line 1, in <module> x + 3 TypeError: can only concatenate list (not "int") to list
1. Expressions 2. Assignments
a. Variables 3. Data Types
a. Integers b. Floats c. Strings d. Lists
COMP1D04: From Scratch to Apps: Foundations of Computational Thinking and Literacy for Problem Solving
Introduction to Python• We can get an element from a list
• Called indexing into a list • i is the index• myList[i] gets the i-th element from the list.
• Usually pronounced “my-list-sub-i” (like myListi) • The first element of a list is at index zero!
>>> myList = [5, 10, 15] >>> myList[0] 5 >>> myList[1] 10 >>> myList[2] 15
• What would do?>>> myList[1] = 4
1. Expressions 2. Assignments
a. Variables 3. Data Types
a. Integers b. Floats c. Strings d. Lists
COMP1D04: From Scratch to Apps: Foundations of Computational Thinking and Literacy for Problem Solving
Introduction to Python• We can also get a range from a list
• myList[i:j] • i is the index of the first element, j is the index just after the last element• Or: i is the start index (inclusive), and j is the end index (exclusive) • Also called slicing a list
>>> myList [5, 10, 15] >>> myList[0:2] [5, 10] >>> myList[1:3] [10, 15] >>> newList = [1, 1, 2, ”three”, 5, 8, “thirteen”, “cat”] >>> newList [1, 1, 2, ‘three’, 5, 8, ‘thirteen’, ’cat’] >>> newList[2:6] [2, ‘three’, 4, 8] >>> newList [1, 1, 2, ‘three’, 5, 8, ‘thirteen’, ’cat’]
1. Expressions 2. Assignments
a. Variables 3. Data Types
a. Integers b. Floats c. Strings d. Lists
COMP1D04: From Scratch to Apps: Foundations of Computational Thinking and Literacy for Problem Solving
Introduction to Python• We can also use indices and slices on strings
1. Expressions 2. Assignments
a. Variables 3. Data Types
a. Integers b. Floats c. Stringsd. Lists
>>> myString ‘Hong Kong Polytechnic University’ >>> myString[0] ‘H’ >>> myString[5] ‘K’ >>> myString[4] ‘ ' >>> myString[0:4] ‘Hong’ >>> myString[0:8] ‘Hong Kon’
COMP1D04: From Scratch to Apps: Foundations of Computational Thinking and Literacy for Problem Solving
Introduction to Python• Review: Assignments
1. Expressions 2. Assignments
a. Variables 3. Data Types
a. Integers b. Floats c. Strings d. Lists
Command MemoryVariable Name
Value
x = 1 x 1
myNumber = 5/2 x 2.5
myString = “Hong Kong” myString ‘Hong Kong’
myList = [1, 3, 5, 7, 9] myList [1, 3, 5, 7, 9]
newList = [44, -1, 66, 29, “cat”] newList [44, -1, 66, 29, “cat”]
blankString = “” blankString ‘’
newList[1] = 55/5 newList [44, 11.0, 66, 29, “cat”]
COMP1D04: From Scratch to Apps: Foundations of Computational Thinking and Literacy for Problem Solving
So far…• Expressions
• Evaluate input and returns some output
• Variable assignments: <variable> = <expression>
• Store the value of the expression in the variable
• There is always a equals sign in an assignment
• Variables can be named many things, as long as we follow rules.
• Variables can hold many types of data: numbers, strings, lists
• List assignments: <listVariable>[<index>] = <expression>
COMP1D04: From Scratch to Apps: Foundations of Computational Thinking and Literacy for Problem Solving
Relating back to Google Sheets…
• Google sheets has different types of data:
• Numbers (start with a digit, or a -)
• Strings (non-digit start, or start with a ‘)
• Formulae (start with =)
• Ranges (B2, B2:B4, B2:D5)
• Errors (#N/A)
• Blanks
COMP1D04: From Scratch to Apps: Foundations of Computational Thinking and Literacy for Problem Solving
Sequential Operation• Remember how in Google sheets, if we put “=A1” in B2,
and we changed the value in A1, B2 automatically updates?
• That doesn’t work for Python.
• In Python, the programming statements (the commands) are operated sequentially.
• Each statement is executed once, from top to bottom.
• We assigned a value to y once (y = x + 4). At the point when that assignment happened, the value of x was 3. So the value that gets put” into y is 3 + 4 = 7.
• Even though we change the value of x afterwards, the value of y doesn’t change.
79.5Hello, Grace77>>>
COMP1D04: From Scratch to Apps: Foundations of Computational Thinking and Literacy for Problem Solving
More sequential operation• When we say y = x + 4
• Python evaluates the RHS of the equation first.
• If the RHS has variables, Python looks up the values from the memory table.
• RHS = x + 4 = 3 + 4 = 7
• Then Python looks at the LHS
• LHS of an equals sign in Python can only have one variable.
• If there is not already an assigned value in the memory table for the variable on the left, Python makes space for it.
• The RHS value is then stored into the memory table.
COMP1D04: From Scratch to Apps: Foundations of Computational Thinking and Literacy for Problem Solving
Assignments
• Evaluate RHS: 5
• There is no entry in the table for x: create one.
• Put the value of the RHS into the memory table as the “value” for x.
Name Value
x = 5
Name Value
x
Name Value
x 5
COMP1D04: From Scratch to Apps: Foundations of Computational Thinking and Literacy for Problem Solving
Assignments (cont’d)• Supposing the next statement is:
• Evaluate RHS: 13
• There’s already a entry for x: do nothing.
• Put the value from the RHS into the value for x
x = 13
Name Value
x 5
Name Value
x 5
Name Value
x 13
COMP1D04: From Scratch to Apps: Foundations of Computational Thinking and Literacy for Problem Solving
What if we have x = x + 1• If x is already defined (let’s say that the
current value of x is 13)
• Evaluate RHS: x + 1
• Look up x in the memory table: get 13
• Add 1 to 13, get 14
• There’s already a memory spot for x: do nothing
• Put the value of the RHS into the “value” for x
Name Value
x 13
Name Value
x 13
Name Value
x 14
COMP1D04: From Scratch to Apps: Foundations of Computational Thinking and Literacy for Problem Solving
Another example for x = x + 1
• This time, x is not previously defined (i.e. we have never assigned anything to it in this program, or since we opened IDLE)
• Evaluate RHS: x + 1
• Lookup x — can’t find it!
• ERROR!!!!!!
Name Value
COMP1D04: From Scratch to Apps: Foundations of Computational Thinking and Literacy for Problem Solving
Some details• Evaluate:
• Compute
• Look up variable names to find values
• Do math or string or list operations in the order specified to combine these values and get a result.
COMP1D04: From Scratch to Apps: Foundations of Computational Thinking and Literacy for Problem Solving
More details• We saw indexing and slicing before
• Some shortcuts:
• Leaving out the begin or end of a slice means “stuff from the beginning” or “stuff including and after” respectively
• myList[:3] — everything in myList from the beginning up to but not including myList[3]
• myList[2:] — everything in myList including and after myList[2]
>>> myList = [1, 3, -1, -9]>>> myList[2:][-1, -9]>>> myList[:1][1]>>> myList[:3][1, 3, -1]
COMP1D04: From Scratch to Apps: Foundations of Computational Thinking and Literacy for Problem Solving
More details• The same tricks that work on lists also work on
strings>>> myString = "Hong Kong Polytechnic University">>> myString[:5]'Hong '>>> myString[23]'n'>>> myString[:23]'Hong Kong Polytechnic U'>>> myString[19]'i'>>> myString[19:]'ic University'>>> myString[19:23]'ic U'
COMP1D04: From Scratch to Apps: Foundations of Computational Thinking and Literacy for Problem Solving
One last trick (for the time being)
• We have also seen how we can have lists of strings.
• How can we say “I want the first letter of the second string in the list?” • Index first, to get the element
(which will be a string) • Then index (or slice) into the
string • Remember to use a [] for each
time we index or slice!
>>> myWords = ["Hong", "Kong", "Polytechnic", “University"]>>>>>> myWords[0]'Hong'>>> myWords[0][0]'H'>>> myWords[0][0:2]'Ho'>>> myWords[3]'University'>>> myWords[3][10:]''>>> myWords[3][6:]'sity'>>>
Do Activity 2-1 Task 1
COMP1D04: From Scratch to Apps: Foundations of Computational Thinking and Literacy for Problem Solving
Interactive Python• We have tried the interactive mode on IDLE
COMP1D04: From Scratch to Apps: Foundations of Computational Thinking and Literacy for Problem Solving
Non-interactive Python — Python program
COMP1D04: From Scratch to Apps: Foundations of Computational Thinking and Literacy for Problem Solving
Program Files
• We can save our code in a Python program file.
• Python programs have the “.py” extension
• e.g. example.py, HelloWorld.py
Finish Task 2
Top Related