Python Boot Camp Addendum Slides
Download
Report
Transcript Python Boot Camp Addendum Slides
Python Boot Camp Addendum
Slides
Boolean Logic Truth Tables
• AND Logic
Argument 1
Operator
Argument 2
Result
True
AND
True
True
True
AND
False
False
False
AND
True
False
False
AND
False
False
Argument 1
Operator
Argument 2
Result
True
OR
True
True
True
OR
False
True
False
OR
True
True
False
OR
False
False
• OR Logic
In-Class Exercise
• Boolean Logic & Conditional Statements
• A fraud detection algorithm might be based
on the length of an email message and the
number of misspelled words. If the length
is short and the percentage of misspelled
words is sufficiently high there is a high
probability that the email is a phishing scam
– Threshold of % misspellings: 10%
– Threshold for number of words: 30
In-Class Exercise
• Boolean Logic & Conditional Statements
• Write a program that specifies these
characteristics of an email message:
– Total number of words: 25
– Words misspelled: 5
• Print out the word “Phishing Scam” if the
email is suspect and “Okay message” if it
should not be flagged as having a high
probability of being a phishing attempt.
In-Class Exercise
• Use a loop and the range() function to
print out even numbers starting with 8 and
ending with 20
raw_input() versus input()
• Do not use input() for the purpose we
needed yesterday
• input() evaluates the input as Python code
– Thus, it will cause an error if you try to input
string data
Survey
• Lecture pace:
–
–
–
–
Too slow: 3
About right: 7
About right, but code typing too fast: 3
Too fast: 2
• Other suggestions:
–
–
–
–
Explaining program logic is good: 4
More active coding: 2
Assign readings from text: 1
Speak louder: 1
Today’s Plan: 8/17
• Today’s Topics
–
–
–
–
HW review
Loops
File I/O
Start Lists (possibly)
• Reading for Tomorrow
– Chapter 8: Lists
– Chapter 9: Dictionaries
– Chapter 10: Tuples
Today’s Plan: 8/17
• Topics we’ll skip (for now)
– Output formatting (see Slide 99)
– Machine precision (in loops material)
Today’s Plan: 8/19
• Topics
– Homework
– Matrix Transpose
– More List Stuff
• Slices
• Sorting
• extend, pop, remove
–
–
–
–
–
Aliasing
Resources: Just Google It!
Histograms (???)
Test
Functions
Homework
• 8-4 (Severance)
• 10-4.py
• Possibly 5-1 (Severance)
Lists
• Transpose 2-dimensional
lists
𝑇
𝐴 =𝐵
0
1
2
0
1
2
𝑎
𝑑
𝑔
𝑏
𝑒
ℎ
𝑐
𝑓
𝑖
𝑇
0
=1
2
0
1
2
𝑎
𝑏
𝑐
𝑑
𝑒
𝑓
𝑔
ℎ
𝑖
• A[0][1] = b = B[1][0]
• For each i,j, B[j][i] = A[i][j]
B = []
A = [[0,1,2],[2,0,1],[1,2,0]]
for i in range(len(A)):
B.append([0,0,0])
print "Initial B matrix: ",B
for i in range(len(A)):
for j in range(len(A[i])):
B[j][i]=A[i][j]
print "A matrix: ", A
print "A transpose matrix: ", B
14
Lists
• Slices
test_list = [1,3,5,7,9,11]
– test_list[1:]
• Get elements 1 through the end of the list
– test_list[1:3]
• Get elements 1 through 2
– test_list[:3]
• Get elements from the beginning of the list element 2
– test_list[2]
• Get element 2
15
Lists
• Other methods that operate directly on lists
– .sort()
– .sort(reverse=True)
– .pop(index)
– .remove(value_to_remove)
t = [11,9,7,5,3,1]
t.sort()
print t
s = t.pop(2)
print s
print t
t.sort(reverse=True) t.remove(5)
print t
print t
16
Lists
• Other methods that operate directly on lists
– .extend(list_var)
t = [11,9,7,5,3,1]
w = [-1,-3,-5,-7]
t.extend(w)
print t
print w
17
Dictionaries
• Use key_list to print out all the keyvalue pairs in myDict “in order”
import random
myDict = {2:'two', 1:'one', 0:'zero', 4:'four', 3:'three'}
key_list = myDict.keys()
random.shuffle(key_list)
print "Shuffled key_list: ",key_list
key_list.sort()
print "Sorted key_list: ", key_list
for thisKey in key_list:
print thisKey,':',myDict[thisKey]
18
Aliasing
p = 3
q = p
p = 1
print p
print q
• This behavior is intuitive for numerical
quantities
19
Aliasing
p = [1,2,3]
q = p
q[1] = 9
print p
print q
• This is aliasing
• … not necessarily intuitive
• In this case, p and q both reference the
same location in memory (that’s aliasing)
– You are not creating a new object in memory
referenced by q
20
Aliasing
• How to avoid aliasing (when you want to):
– Make a copy of the list
• Be careful of unnecessary copies of lists
– Consumes memory
p = [1,2,3]
q = p[:]
q[1] = 9
print p
print q
21
Aliasing
• Can also use this method for lists and other
data types:
import copy
p = [0,1,2,3,4]
q = copy.copy(p)
q = copy.deepcopy(p)
22
Resources and Debugging
• When debugging or trying to find a way to
do something, Google it
– Give preference to answers from
stackoverflow.com
• Python reference site for version 2.7
– https://docs.python.org/2/reference/
23
Dictionaries
• Use a dictionary to create a histogram
– Histogram: counts the frequency of occurrence
for each value in a set of data
– Assume that the data is in a list
• For example, a histogram of this list,
– [0,1,3,5,2,3,1,0,0,3,4,5]
• is this:
– 0:3, 1:2, 2:1, 3:3, 4:1, 5:2
Value found in list
Number of times
a value appeared
24
Dictionaries
• hist = {}
– Creates a new empty dictionary
• Try this in 9-hist.py. What happens?
25
Dictionaries
• If a key does not exist we need to create it
– 9-hist1.py
26
Dictionaries
• It works. Can we do better?
– Use dictionary .get() function
– Creates a dictionary element if key is missing
– 9-hist2.py
27
Test
• PDF of test is in the box.wm.edu site
• PDF of test is also in Blackboard
– blackboard.wm.edu
• Submit test documents to Blackboard
– Program1.py, program2.py, 1 or 2 screen shot
documents
– Go to Assignments
– Click on Test
– Attach documents one at a time
Click here
Test
• Creating screen shots
– Snipping Tool
– Shift-Print Screen or Ctrl-Print
Screen
• Capture entire screen
– Alt-Print Screen
• Capture active window
Functions