CMPT 120 Lecture 10 – Unit 2 – Cryptography and Encryption – The realm of secret codes Python – Range function
Last Lecture, we did … https://repl.it/repls/AssuredHarmfulBots
Let’s implement our program now! Problem Statement: Write a program that encrypts messages using a transposition algorithm called “odd&even” odd&even transposition algorithm: Create a cypher that is made of 2 strings String 1 contains the characters in odd positions String 2 contains the characters in even positions
Let’s decrypt our message! Problem Statement: Write a program that decrypts messages that have been encrypted using the transposition algorithm odd&even
Our first decryption algorithm In order to implement our first decryption algorithm, we need to know … String concatenation Arithmetic operator Indexing Slicing len( ) function range( ) function
Reading Review String Indexing: "Hello World!"[6] -> message = "slicing" message[ ] -> String Slicing: message[ : ] -> message[ : : ] -> "123456789"[2:8:3] ->
Reading Review range( ) function What will we see printed on the Python Interpreter shell window if : range(10) range(1, 11) range(0, 30, 5) range(0, 10, -3) range(0, -10, -1) range(0) To see the sequence produced by the function range( ) on the Python Interpreter shell window : list(range(...))
Review - Syntax of a for loop Can be a string <statement outside (before) the loop> for <iterating variable> in <sequence> : <first statement to be repeated> <second statement to be repeated> ... <last statement to be repeated> <statement outside (after) the loop> Can be a list Can be produced using range(…)
Review - Syntax of a for loop <statement outside (before) the loop> for <iterating variable> in <sequence> : <first statement to be repeated> <second statement to be repeated> ... <last statement to be repeated> <statement outside (after) the loop> Important – About Indentation Statements inside the loop (i.e., statements executed at each iteration of the loop) are the statements indented with respect to the for keyword Statements outside the loop (before and after the loop) are the statements that are not indented with respect to the for keyword – these statements are considered to be at the same level of indentation as the for loop
Review - range( ) function Very useful in for loop Syntax: range([start,] stop [,step])) Produces a list of integers How does it work? If theLength = 5 Then range(theLength) produces [0, 1, 2, 3, 4]
Next Lecture Let’s practice what we have learnt so far Practice Exam 2 -> paper-based