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 >>> cat_in_a_box = True>>> print cat_in_a_boxTrue>>> cat_in_a_hat = 10>>> print cat_in_a_hat10>>> cats = "are great">>> print catsare great Boolean Integer String
Variables Variables store one thing at a time!
Example <- Initial Value >>> cats = "are great">>> print catsare great>>> cats = "and fun">>> print catsand fun <- New Value
But ... Cats are Great AND Fun! >>> cats = "are great and fun">>> print catsare great and fun>>> cats = "are great">>> cats = cats + “ and fun” >>> print catsare great and fun Compute New Value From Old Value
Functions <- Function Definition >>> def loveCats(): We like to re-use our work Functions provide a mechanism to associate a series of Python statements with a name >>> def loveCats(): ... print “I love cats” ... >>> loveCats() I love cats <- Function Definition <- Function Execution <- Function Execution
Functions >>> def loveCats(color): ... print “I love ” +color+ “ cats” ... >>> loveCats(“orange”) I love orange cats >>> loveCats(“tuxedo”) I love tuxedo cats
>>> print x + “ and “ + y 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
<- Condition to check Conditionals Conditionals allow us to execute different statements based on if a condition is true or false >>> x = -1 >>> if x < 10: ... print x ... -1 <- Condition to check <- Code to execute if condition is true
Loops >>> str = “I love cats!” >>> for x in str: 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 >>> x = ord(“a”) >>> print x 97 A method to encode characters as numbers (Oliver will present more on this) >>> x = ord(“a”) >>> print x 97 >>> y = chr(97) >>> print y a
Modular Arithmetic >>> 5 % 4 1 Our encryption / decryption algorithm will rely on Modular Arithmetic (tutorial to follow) % is the modulo operator in Python >>> 5 % 4 1
Modular Arithmetic
12 11 1 10 2 9 3 8 4 7 5 6 ??? ??? What’s 4 hours after 3:00? What’s 6 hours after 7:00? 3 + 4 = 7 6 + 7 = 1
(remainder after division) Modulus 6 mod 12 + 7 = 13 mod 12 mod 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)
Based on modular arithmetic Cryptography RSA SHA (256, 512, ... ) DES Based on modular arithmetic
(image source: Wikimedia Commons) Cryptography Julius Caesar (image source: Wikimedia Commons)
Cryptography The Caesar Shift Cipher the quick brown fox jumps over the lazy dog WKH TXLFN EURZQ IRA MXPSV RYHU WKH ODCB GRJ The Caesar Shift Cipher
The Caesar Shift Cipher ? ABCDEFGHIJKLMNOPQRSTUVW ABCDEFGHIJKLMNOPQRSTUVWXYZ XYZ (Shift of 3) Encrypt(‘L’, 3) = ‘O’ Encrypt(‘Y’, 3) = ‘B’
ASCII ASCII Code Meaning < 32 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)