Presentation is loading. Please wait.

Presentation is loading. Please wait.

Teaching with Python Lukasz Ziarek SUNY Buffalo

Similar presentations


Presentation on theme: "Teaching with Python Lukasz Ziarek SUNY Buffalo"— Presentation transcript:

1 Teaching with Python Lukasz Ziarek SUNY Buffalo
Oliver Kennedy SUNY Buffalo Sarbani Banerjee Buff State

2 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’

3 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

4 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

5 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)

6 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

7 Variables Variables store one thing at a time!

8 Example <- Initial Value
>>> cats = "are great">>> print catsare great>>> cats = "and fun">>> print catsand fun <- New Value

9 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

10 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

11 Functions >>> def loveCats(color):
... print “I love ” +color+ “ cats” ... >>> loveCats(“orange”) I love orange cats >>> loveCats(“tuxedo”) I love tuxedo cats

12 >>> 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

13 <- 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

14 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

15 Encryption These are all the language constructs we need for encryption We need a few auxiliary components: Modular Arithmetic ASCII Encoding

16 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

17 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

18 Modular Arithmetic

19 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

20 (remainder after division)
Modulus 6 mod 12 + 7 = 13 mod 12 mod 12 = 1 mod 12 (remainder after division)

21 Modular Arithmetic Subtraction (6 mod 12) - (9 mod 12) = (9 mod 12)
Other Modulii (45 mod 60) + (30 mod 60) = (15 mod 60)

22 Based on modular arithmetic
Cryptography RSA SHA (256, 512, ... ) DES Based on modular arithmetic

23 (image source: Wikimedia Commons)
Cryptography Julius Caesar (image source: Wikimedia Commons)

24 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

25 The Caesar Shift Cipher
? ABCDEFGHIJKLMNOPQRSTUVW ABCDEFGHIJKLMNOPQRSTUVWXYZ XYZ (Shift of 3) Encrypt(‘L’, 3) = ‘O’ Encrypt(‘Y’, 3) = ‘B’

26 ASCII ASCII Code Meaning < 32 Control Codes (Ignore) 32 ‘ ’ (Space)
... Other Printable Characters 126 ‘~’ (Tilde) 127 Delete (Ignore) (95 codes)

27 Cooperative Task 1 Encrypt(A,i)

28 Cooperative Task 2 Decrypt(A,i)


Download ppt "Teaching with Python Lukasz Ziarek SUNY Buffalo"

Similar presentations


Ads by Google