WCSTAFallConf2012

Download Report

Transcript WCSTAFallConf2012

Teaching with Python
Lukasz Ziarek SUNY Buffalo
Oliver Kennedy SUNY Buffalo
Sarbani Banerjee Buff State
Benefits of Python
Interpreted : Has an interactive mode that
students can play with
Simple : Low syntactic overhead - students
can focus on core concepts
Real : Used in many scientific applications
Pedigree : Named after ‘Monty Python’
Benefits of Python for
teachers
Lots of available material : we will provide you
with two full curriculums (including projects,
assignments, and assessment)
Many low cost text books
Interfaces with Lego NXT
Learning Objectives
Learn core syntax of
Python
Write a non-trivial
program in Python
(caesar en-cipher /
de-cipher)
Ideas how to bring Python
into your classroom
Variables
Syntax: must start with a letter or “_”, can contain
letters, numbers, or “_”
You can think of a variable as a box
Boxes store things, variables store things (values)
Example
Boolean
>>> cat_in_a_box = True>>> print
Integer
cat_in_a_boxTrue>>> cat_in_a_hat
= 10>>> print cat_in_a_hat10>>>
cats = "are great">>> print
String
catsare great
Variables
Variables store one thing at a time!
Example
<- Initial Value
>>> cats = "are
great">>> print
catsare great>>> cats<- New Value
= "and fun">>> print
catsand fun
But ... Cats are Great
AND Fun!
>>> cats = "are great and
fun">>> print catsare great
and fun>>> cats = "are
great">>> cats = cats + “ and
fun”
Compute
>>> print catsare great
and New Value
From Old Value
fun
Functions
We like to re-use our work
Functions provide a mechanism to associate a
series of Python statements with a name
<- Function Definition
>>> def loveCats():
... print “I love cats”
...
<- Function Execution
>>> loveCats()
I love cats
<- Function Execution
>>> loveCats()
I love cats
Functions
>>> def loveCats(color):
... print “I love ” +color+ “ cats”
...
>>> loveCats(“orange”)
I love orange cats
>>> loveCats(“tuxedo”)
I love tuxedo cats
Functions
>>> def loveCats(color):
... return “I love ” +color+ “ cats”
...
>>> x = loveCats(“orange”)
>>> print x
I love orange cats
>>> y = loveCats(“tuxedo”)
>>> print y
I love tuxedo cats
>>> print x + “ and “ + y
Conditionals
Conditionals allow us to execute different
statements based on if a condition is true or
false
>>> x = -1
>>> if x < 10: <- Condition to check
...
print x <- Code to execute
...
if condition is true
-1
Loops
Loops allow us to execute a piece of code some
number of times AND allow us to “traverse” a
structure
>>> str = “I love cats!”
>>> for x in str:
...
print x
...
I
l
o
Encryption
These are all the language constructs we
need for encryption
We need a few auxiliary components:
Modular Arithmetic
ASCII Encoding
ASCII
A method to encode characters as numbers
(Oliver will present more on this)
>>>
>>>
97
>>>
>>>
x = ord(“a”)
print x
y = chr(97)
print y
a
Modular Arithmetic
Our encryption / decryption algorithm will rely
on Modular Arithmetic (tutorial to follow)
% is the modulo operator in Python
>>> 5 % 4
1
Modular Arithmetic
11
12
1
10
2
9
3
8
4
7
6
5
What’s
+7
4 =after
7 ???
1
7:00?
3:00?
???6463hours
Modulus
6mod
+ 7 12
= 13 mod
mod12
12= 1 mod 12
(remainder after division)
Modular Arithmetic
Subtraction
(6 mod 12) - (9 mod 12)
= (9 mod 12)
Other Modulii
(45 mod 60) + (30 mod 60)
= (15 mod 60)
Cryptography
RSA
SHA (256, 512, ... )
DES
Based on modular arithmetic
Cryptography
Julius Caesar
(image source: Wikimedia
Commons)
Cryptography
WKH quick
the
TXLFN brown
EURZQ fox
IRA jumps
MXPSV over
RYHU the
WKH lazy
ODCB dog
GRJ
The Caesar Shift Cipher
The Caesar Shift Cipher
?
ABCDEFGHIJKLMNOPQRSTUVW
ABCDEFGHIJKLMNOPQRSTUVWXYZXYZ
(Shift of 3)
Encrypt(‘L’, 3) Encrypt(‘Y’,
= ‘O’
3) = ‘B’
ASCII
ASCII Code
< 32
Meaning
Control Codes (Ignore)
32
‘ ’ (Space)
...
Other Printable Characters
126
‘~’ (Tilde)
127
Delete (Ignore)
(95 codes)
Cooperative Task 1
Encrypt(A,i)
Cooperative Task 2
Decrypt(A,i)