Download presentation
Presentation is loading. Please wait.
Published byToby Price Modified over 7 years ago
1
ITNPBD2: Representing and Manipulating Data EXAM RE-VISION
John R. Woodward
2
outline Exam details FORMAT, ROOM Exam techniques.
3
Exam rules No paper notes
No mobiles – leave in your bag at side of room. You will have paper provided – but bring a pen or two pens!!! No talking in the exam. EXCEPT if you put your hand up and ask the examiner a question. BRING YOUR STUDENT ID
4
TIPS Look at past exam paper. BEFORE.
Check where the room is and arrive 15 minutes early Revise what you think you are weak at. READ the exam paper (say 15 minutes at start of exam) – look at the marks for each question to give you an idea of the work. Pace yourself. 3 questions in 2 hours – say 30 minutes for each question + 15 min at start/end of exam. Try to write something for each question. You can answer the questions in any order. E.G. 1a 3c 2a 3b 1c….
5
Start of exam – front page
ITNPBD2: Representing and Manipulating Data 2 hour exam Attempt ALL THREE questions. All questions carry equal marks. 33%, 33% AND 34% The distribution of marks among the parts of each question is indicated.
6
IMPORTANT NOTE Read the instructions on the front of each answer book carefully. It is essential that you write your student number on the front of each answer book. Also, when you have completed the examination, the number of answer books that you have used must be prominently written on the front of one book.
7
A few pointers The exam is not meant to check syntax
It is to check understanding. For example Given some code, “what does it print?” Or write some code No essays – but short one to two sentence answers (you can give an example if you like)
8
How to answer What do the following output to the screen?
print filter(lambda x: x % 2, [0,1,1,2,3,5,8,13,21,34,55]) TRY THIS – WHAT ARE YOUR ANSWERS??
9
How to answer What do the following four print statements output to the screen? print filter(lambda x: x % 2, [0,1,1,2,3,5,8,13,21,34,55]) [1, 1, 3, 5, 13, 21, 55]
10
Partly correct answers
[0,1,1,2,3,5,8,13,21,34,55] – original list [1, 1, 3, 5, 13, 21, 55] – correct answer [0, 2, 8, 34] ??? [0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1] ??? [f, t, t, …] ??? [1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0] ??? [t, f, f, ….] ???
11
Partly correct answers
[0,1,1,2,3,5,8,13,21,34,55] – original list [1, 1, 3, 5, 13, 21, 55] – 2 mark [0, 2, 8, 34] 1.5 mark [0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1] 1 mark [f, t, t, f, t, t, f, t, t, f, t] 1 mark [1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0] 0.5 marks What other possible answers are there???
12
outline Python Revision
Datatypes, data structures (lists, tuples, dictionaries) Regular expressions Functional programming (map, filter, reduce) XML, JSON,
13
dynamically typed language
In a dynamically typed language, every variable name is (unless it is null) bound only to an object. Names are bound to objects at execution time by means of assignment statements, and it is possible to bind a name to objects of different types during the execution of the program.
14
String Special Operators: Assume string variable a holds 'Hello' and variable b holds 'Python', then: Operator Description Example + Concatenation - Adds values on either side of the operator a + b will give HelloPython * Repetition - Creates new strings, concatenating multiple copies of the same string a*2 will give -HelloHello [] Slice - Gives the character from the given index a[1] will give e [ : ] Range Slice - Gives the characters from the given range a[1:4] will give ell in Membership - Returns true if a character exists in the given string H in a will give True not in Membership - Returns true if a character does not exist in the given string M not in a will give True
15
What will the following print?
str = "abcdefghijklmnopqrstuvwxyz" print str[0:5] print str[5:10] print str[22:25] print str[22:-1]
16
the following will print
str = "abcdefghijklmnopqrstuvwxyz" print str[0:5] print str[5:10] print str[22:25] print str[22:-1] abcde fghij wxy
17
What will the following print?
print filter(lambda x: x % 3, [10,9,8,7,6,5,4,3,2,1,0]) print map(lambda y: y+3, [4,3,2,1,0]) print map(lambda x:x-1 , map(lambda x:x+1, [2,4,6,8]))
18
print filter(lambda x: x % 3, [10,9,8,7,6,5,4,3,2,1,0])
print map(lambda y: y+3, [4,3,2,1,0]) print map(lambda x:x-1 , map(lambda x:x+1, [2,4,6,8])) [10, 8, 7, 5, 4, 2, 1] [7, 6, 5, 4, 3] [2, 4, 6, 8]
19
What will the following print?
def f(a,b): if (a== b): return a else: return b print reduce(f, [1,1,2,2,3])
20
def f(a,b): if (a== b): return a else: return b print reduce(f, [1,1,2,2,3]) 3 SHOW YOUR WORKING OUT (MARKS IF WRONG)
21
What will the following print?
def f(a,b): if (a== b): return a else: return b print reduce(f, [1,1,2,2,3,4])
22
def f(a,b): if (a== b): return a else: return b print reduce(f, [1,1,2,2,3,4]) 4
23
What will the following print?
def takeAList(list1): return reduce(lambda x, y:x+1, list1) print takeAList([1,2,3,4,5,6])
24
def takeAList(list1): return reduce(lambda x, y:x+1, list1) print takeAList([1,2,3,4,5,6]) 6
25
https://support.google.com/analytics/answer/1034324?hl=en-GB
Regular Expressions
26
Wildcards . Matches any single character (letter, number or symbol)
goo.gle matches gooogle, goodgle, goo8gle * Matches zero or more of the previous item The default previous item is the previous character.goo*gle matches gooogle, goooogle + Just like a star, except that a plus sign must match at least one previous item gooo+gle matches goooogle, but never google. ? Matches zero or one of the previous item labou?r matches both labor and labour | Lets you do an "or" match a|b matches a or b
27
Anchors ^ Requires that your data be at the beginning of its field
^site matches site but not mysite $ Requires that your data be at the end of its field site$ matches site but not sitescan Note: to understand why anchors are necessary, please read Tips for Regular Expressions at the bottom of this page.
28
Grouping () Use parenthesis to create an item, instead of accepting the default Thank(s|you) will match both Thanks andThankyou [] Use brackets to create a list of items to match to [abc] creates a list with a, b and c in it - Use dashes with brackets to extend your list [A-Z] creates a list for the uppercase English alphabet
29
Other \ Turns a regular expression character into an everyday character mysite\.com keeps the dot from being a wildcard
31
Regular Expressions (in Python)
32
List of Meta characters
. + ? * ^ $ [...] - [^...] | () {m,n}
33
. (dot) Matches any single character (many applications exclude newlines, and exactly which characters are considered newlines is flavor-, character-encoding-, and platform-specific, but it is safe to assume that the line feed character is included). Within POSIX bracket expressions, the dot character matches a literal dot. For example, a.c matches "abc", etc., but [a.c] matches only "a", ".", or "c".
34
Example . string1 = "Hello, world." if re.search(r".....", string1):
print string1 + " has length >= 5"
35
Example [.] literally a dot
string1 = "Hello, world." if re.search(r"....[.]", string1): print string1 + " has length >= 5 and contains a . (dot)"
36
+ Matches the preceding element one or more times. For example, ab+c matches "abc", "abbc", "abbbc", and so on, but not "ac". string1 = "Hello, world." if re.search(r"l+", string1): print 'There are one or more consecutive letter "l"' +\ "'s in " + string1
37
? Matches the preceding pattern element zero or one times.
string1 = "Hello, world." if re.search(r"H.?e", string1): print "There is an 'H' and a 'e' separated by 0-1 characters (Ex: He Hoe)"
38
* Matches the preceding element zero or more times. For example, ab*c matches "ac", "abc", "abbbc", etc. [xyz]* matches "", "x", "y", "z", "zx", "zyx", "xyzzy", and so on. (ab)* matches "", "ab", "abab", "ababab", and so on. string1 = "Hello, world." if re.search(r"e(ll)*o", string1): print "'e' followed by zero to many'll' followed by 'o' (eo, ello, ellllo)"
39
^ Matches the beginning of a line or string. string1 = "Hello World"
if re.search(r"^He", string1): print string1, "starts with the characters 'He'"
40
$ Matches the end of a line or string. string1 = "Hello World"
if re.search(r"rld$", string1): print string1, "is a line or string that ends with 'rld'"
41
[ ] Matches a single character that is contained within the brackets. For example, [abc] matches "a", "b", or "c". [a-z] specifies a range which matches any lowercase letter from "a" to "z". These forms can be mixed: [abcx-z] matches "a", "b", "c", "x", "y", or "z", as does [a-cx-z]. The - character is treated as a literal character if it is the last or the first (after the ^, if present) character within the brackets: [abc-], [-abc]. Note that backslash escapes are not allowed. The ] character can be included in a bracket expression if it is the first (after the ^) character: []abc].
42
Example [] #[] Denotes a set of possible character matches.
string1 = "Hello, world." if re.search(r"[aeiou]+", string1): print string1 + " contains one or more vowels."
43
Example | #| Separates alternate possibilities.
string1 = "Hello, world." if re.search(r"(Hello|Hi|Pogo)", string1): print "At least one of Hello, Hi, or Pogo is contained in " + string1
44
() Defines a marked subexpression. The string matched within the parentheses can be recalled later (see the next entry, \n). A marked subexpression is also called a block or capturing group.
45
Example () string1 = "Hello, world."
m_obj = re.search(r"(H..).(o..)(...)", string1) if re.search(r"(H..).(o..)(...)", string1): print "We matched '" + m_obj.group(1) + "' and '" + m_obj.group(2) + "' and '" + m_obj.group(3)+ "'"
47
Tabular Data Examples: csv, spreadsheets, relational DB tables
Limits the variables you can record to those with a column Variable Entry Value
48
More Flexiblility Tables are fine when every entry has the same variables associated with it, for example name, address, phone number They become more problematic when different entries have different variables Or some entries are lists, or objects themselves
49
Example Lets try and store the following facts:
Tom lives in Bridge of Allan He has three addresses He owns a house in Causewayhead
50
Example PersonID email 1 tom@work tom@home tom@gmail PersonID Name 1
HouseID 1 2 HouseID Line1 Line2 Postcode 1 1 High St Bridge of Allan FK9 4LA 2 1 Wallace St Causewayhead FK9 5QW Select Name, , Line1, Line2, Postcode FROM People, Houses, s, HousePeople WHERE People.PersonID=Houses.PersonID AND People.PersonID=HousePeople.PersonID AND People.PersonID= .PersonID AND HousePeople.HouseID=Houses.HouseID
51
XML eXtensible Markup Language
Extensible, meaning you can define your own tags Markup language means that data is stored and represented as text, with the structure of the data defined within the text in a way that is very general Now a very commonly used standard
52
Tree Structure XML is designed to represent data that can be arranged into a tree structure That turns out to be pretty much anything:
53
Tabular Data PersonID Name Address
54
Elements and Attributes
start tag + content + end tag is called an element, e.g.: elements can have no content, e.g. <br></br> or <br/> in XHTML start tags can contain attribute values, e.g. in XHTML: < all attribute values must be quoted (with single or double quotes)
55
Attributes or Tagged Data?
So would you use: <person name = “tom” </person> Or <person> <name>tom</name>
56
Answer Better to use the second example:
Allows meta data for each element <person> <name>tom</name> < </person> Allows multiple entries
57
Back to Our Example The XML for the example from earlier is
<person> <name>tom</name> <address> <line1>1 High St</line1> <line2>Bridge of Allan</line2> <postcode>FK9 4LA</postcode> </address> : </person>
58
JSON XML is powerful and very common
But it is rather large and cumbersome for some uses JSON (JavaScript Object Notation) is gaining popularity as an alternative
59
Object A JSON object in its simplest form is a set of name:value pairs
An object is enclosed in { } braces The name part is a string, so enclosed in “” The colon means equals The value can be a single value or an array of values Values can be objects themselves
60
Values in JSON String Like a Java string “Enclosed in double quotes” Escaped with \ Number – No more specific types such as int, float Object – An embedded JSON object Array – An array of values True / false null
61
Example of JSON { “Name”: “Tom”, “ ”: “Address”: [{“Line1”: “1 High St”,”Line2”: “Bridge of Allan”, “Postcode”: “FK9 4LA”}, {“Line1”: “1 Wallace St”,”Line2”: “Causewayhead”, “Postcode”: “FK9 5QW”},] }
62
JSON Arguable easier for humans to read Easy for programs to parse
Elegant and simple No definition files or schemata
63
JSON Data types are… String Number boolean
Stands for JavaScript Object Notation. Name: Value pairs { "brand": "Crocs", "color": "pink", "size": 9, "hasLaces": false } Data types are… String Number boolean
64
Reading JSON { begin object } end object [ begin array ] end array : separate key from value in an object , separate name from value
65
JSON Structure
66
JSON data types Object - { } String – “in double quotes”
Number e+45 Boolean – true false Null - null Array – []
67
Nested object–person contains head, hair
{ "person": { "name": "Lindsay Bassett", "heightInInches": 66, "head": { "hair": { "color": "light blond", "length": "short", "style": "A-line" }, "eyes": "green" }
68
Nested object – person contains head
{ "person": { "name": "Lindsay Bassett", "heightInInches": 66, "head": { "hair": { "color": "light blond", "length": "short", "style": "A-line" }, "eyes": "green" } Person object contains A head object. Person object has 3 Key:value pairs. Head contains 2 Key: Value pairs. Hair contains an object Consisting of 3 key:value pairs
69
Why won’t this code work
{ "promo": "Say "Bob's the best!" at checkout for free 8oz bag of kibble." }
70
Why won’t this code work
{ "promo": "Say "Bob's the best!" at checkout for free 8oz bag of kibble." } The code below will work "promo": "Say \"Bob's the best!\" at checkout for free 8oz bag of kibble."
71
Numbers in JSON Numbers can be Integer Decimal Negative Exponent
{ "widgetInventory": 289, "sadSavingsAccount": 22.59, "seattleLatitude": , "seattleLongitude": , "earthsMass": e+24 } Numbers can be Integer Decimal Negative Exponent
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.