��chapter 18-19 list, for loops · 2019. 10. 1. · title: ��chapter 18-19...
TRANSCRIPT
![Page 1: ��Chapter 18-19 List, For Loops · 2019. 10. 1. · Title: ��Chapter 18-19 List, For Loops Author: jkb Created Date: 10/1/2019 12:41:50 PM](https://reader035.vdocuments.site/reader035/viewer/2022071419/611898ed0ad81d3c9a301650/html5/thumbnails/1.jpg)
Strings and Random Numbers
Caesar and Vigenère CiphersJohn K. Bennett
IWKS 2300Fall 2019
![Page 2: ��Chapter 18-19 List, For Loops · 2019. 10. 1. · Title: ��Chapter 18-19 List, For Loops Author: jkb Created Date: 10/1/2019 12:41:50 PM](https://reader035.vdocuments.site/reader035/viewer/2022071419/611898ed0ad81d3c9a301650/html5/thumbnails/2.jpg)
String Special CharactersSpecial characters can be inserted in a string using an escape
sequence, which is a backslash (\) followed by another character.
Here are some common escape sequences:
\" Double Quote \\ Backslash
\n Newline \t Horizontal Tab
print("Favorite Color:\n\t\"Glow in the Dark\"")
Favorite Color:
"Glow in the Dark"
![Page 3: ��Chapter 18-19 List, For Loops · 2019. 10. 1. · Title: ��Chapter 18-19 List, For Loops Author: jkb Created Date: 10/1/2019 12:41:50 PM](https://reader035.vdocuments.site/reader035/viewer/2022071419/611898ed0ad81d3c9a301650/html5/thumbnails/3.jpg)
Single or Double Quotes: Your Choice
Strings can be written using either single or double quotes, your choice.
primary = 'Python'
secondary = "English"
Using single quotes means no need to escape double quotes:
Using double quotes means no need to escape single quotes:
print('So you must be "the one"?')
print("Margaret's house is blue.")
![Page 4: ��Chapter 18-19 List, For Loops · 2019. 10. 1. · Title: ��Chapter 18-19 List, For Loops Author: jkb Created Date: 10/1/2019 12:41:50 PM](https://reader035.vdocuments.site/reader035/viewer/2022071419/611898ed0ad81d3c9a301650/html5/thumbnails/4.jpg)
Strings Are Like Lists (almost)
Strings are like lists containing characters:
my_name = "Jack"
print(my_name[0])
J
my_name = "Jack"
my_name[0] = "T" # produces item assignment error
But unlike lists, strings cannot be modified
![Page 5: ��Chapter 18-19 List, For Loops · 2019. 10. 1. · Title: ��Chapter 18-19 List, For Loops Author: jkb Created Date: 10/1/2019 12:41:50 PM](https://reader035.vdocuments.site/reader035/viewer/2022071419/611898ed0ad81d3c9a301650/html5/thumbnails/5.jpg)
Strings Are Iterables
You can iterate over each character of a string.
for char in "IWKS 2300"
print(char)
I
W
K
S
2
3
0
0
![Page 6: ��Chapter 18-19 List, For Loops · 2019. 10. 1. · Title: ��Chapter 18-19 List, For Loops Author: jkb Created Date: 10/1/2019 12:41:50 PM](https://reader035.vdocuments.site/reader035/viewer/2022071419/611898ed0ad81d3c9a301650/html5/thumbnails/6.jpg)
Spliting strings with .split()To separate the words in a string into a list, call .split() on the string.
my_str = "Python is really cool"
word_list = my_str.split()
# wordlist will be ["Python", "is", ... ]
for word in word_list:
print(word)
Python
is
really
cool
Note: The dot (".") above is called the accessor operator (though most programmers simply call it the dot operator). This syntax allows us to use a function that is specific to a certain data type on the object.
![Page 7: ��Chapter 18-19 List, For Loops · 2019. 10. 1. · Title: ��Chapter 18-19 List, For Loops Author: jkb Created Date: 10/1/2019 12:41:50 PM](https://reader035.vdocuments.site/reader035/viewer/2022071419/611898ed0ad81d3c9a301650/html5/thumbnails/7.jpg)
Using .split()
The input() function returns a string containing the line that the user typed. Often you need to split that input, e.g.:
line = input("What is your full name?")
words = line.split()
firstname = words[0]
lastname = words[1]
![Page 8: ��Chapter 18-19 List, For Loops · 2019. 10. 1. · Title: ��Chapter 18-19 List, For Loops Author: jkb Created Date: 10/1/2019 12:41:50 PM](https://reader035.vdocuments.site/reader035/viewer/2022071419/611898ed0ad81d3c9a301650/html5/thumbnails/8.jpg)
Joining Strings With .join().join() does the opposite of .split(). It also allows you to specify
what you want to use as a separator.
separator = "@"
word_list = ["jkb", "ucdenver.edu"]
email = separator.join(wordlist)
print(email)
![Page 9: ��Chapter 18-19 List, For Loops · 2019. 10. 1. · Title: ��Chapter 18-19 List, For Loops Author: jkb Created Date: 10/1/2019 12:41:50 PM](https://reader035.vdocuments.site/reader035/viewer/2022071419/611898ed0ad81d3c9a301650/html5/thumbnails/9.jpg)
String Formatting - Old StyleThree basic formatting specifiers (C-Style) are:● %s - String● %d - Integers● %f - Floatsname = 'John'
i_age = 21
f_age = 21.5
print('Hello, %s. Are you %d or %f?' %(name, i_age, f_age))
print()
print("We can print %f or %.2f. How?" %(f_age, f_age))
Hello, John. Are you 21 or 21.500000?
We can print 21.500000 or 21.50. How?
![Page 10: ��Chapter 18-19 List, For Loops · 2019. 10. 1. · Title: ��Chapter 18-19 List, For Loops Author: jkb Created Date: 10/1/2019 12:41:50 PM](https://reader035.vdocuments.site/reader035/viewer/2022071419/611898ed0ad81d3c9a301650/html5/thumbnails/10.jpg)
String Formatting - New StyleThe "new style" string formatting gets rid of the %-operatorsyntax, and instead calls the .format() function on a string object.
name = 'John'
i_age = 21
f_age = 21.5
print('Hello, {}. Are you {} or {}?'.format(name, i_age, f_age))
print()
print('Hello, {name}. You are {age}.'.format(name=name, age=i_age))
Hello, John. Are you 21 or 21.5?
Hello, John. You are 21.
![Page 11: ��Chapter 18-19 List, For Loops · 2019. 10. 1. · Title: ��Chapter 18-19 List, For Loops Author: jkb Created Date: 10/1/2019 12:41:50 PM](https://reader035.vdocuments.site/reader035/viewer/2022071419/611898ed0ad81d3c9a301650/html5/thumbnails/11.jpg)
New New String Formatting: f-strings
Python 3.6+ offers a new formatting string approach: "f-strings". This new way of formatting strings lets you use embedded Python expressions inside string constants.
name = 'John'
i_age = 21
f_age = 21.5
print(f"Hello, {name}. Aren't you really {i_age - 3}?")
Hello, John. Aren't you really 18?
![Page 12: ��Chapter 18-19 List, For Loops · 2019. 10. 1. · Title: ��Chapter 18-19 List, For Loops Author: jkb Created Date: 10/1/2019 12:41:50 PM](https://reader035.vdocuments.site/reader035/viewer/2022071419/611898ed0ad81d3c9a301650/html5/thumbnails/12.jpg)
ASCII Character Representation
![Page 13: ��Chapter 18-19 List, For Loops · 2019. 10. 1. · Title: ��Chapter 18-19 List, For Loops Author: jkb Created Date: 10/1/2019 12:41:50 PM](https://reader035.vdocuments.site/reader035/viewer/2022071419/611898ed0ad81d3c9a301650/html5/thumbnails/13.jpg)
Character Manipulation in Python
The Python function ord() returns the ordinal ASCII number of a character, e.g.,
print(ord('A'))results in:65
Similarly, the Python function chr() is the inverse of ord(), e.g., print(chr(122))
results in:z
![Page 14: ��Chapter 18-19 List, For Loops · 2019. 10. 1. · Title: ��Chapter 18-19 List, For Loops Author: jkb Created Date: 10/1/2019 12:41:50 PM](https://reader035.vdocuments.site/reader035/viewer/2022071419/611898ed0ad81d3c9a301650/html5/thumbnails/14.jpg)
Character Manipulation in Python
The Python character methods isupper()and islower() return Trueif the character is uppercase or lower case, respectively.Running this code:
lchar = 'a'
uchar = 'G'
if lchar.islower():
print (lchar + ' is a lowercase letter')
if uchar.isupper():
print (uchar + ' is an uppercase letter')
will produce the following output:a is a lowercase letterG is an uppercase letter
![Page 15: ��Chapter 18-19 List, For Loops · 2019. 10. 1. · Title: ��Chapter 18-19 List, For Loops Author: jkb Created Date: 10/1/2019 12:41:50 PM](https://reader035.vdocuments.site/reader035/viewer/2022071419/611898ed0ad81d3c9a301650/html5/thumbnails/15.jpg)
How to Generate a Random Number
Import the Random Library:import random
To create a random integer that ranges from [a, b]random.randint(a, b)
To create a random float that ranges from [0, 1)random.random() # no parameters, includes 0, excludes 1!
![Page 16: ��Chapter 18-19 List, For Loops · 2019. 10. 1. · Title: ��Chapter 18-19 List, For Loops Author: jkb Created Date: 10/1/2019 12:41:50 PM](https://reader035.vdocuments.site/reader035/viewer/2022071419/611898ed0ad81d3c9a301650/html5/thumbnails/16.jpg)
How to generate a random integer that ranges from 1 to 10?
How to generate a random integer that ranges from 0 to 7?
>>> random.randint(1, 10)
>>> random.randint(0, 7)
How to generate a random float that ranges from 0 to 5?
>>> random.random() * 5 # number from [0, 5)
How to generate a random float that ranges from 1 to 6?
>>> random.random() * 5 + 1 # number from [1, 6)
![Page 17: ��Chapter 18-19 List, For Loops · 2019. 10. 1. · Title: ��Chapter 18-19 List, For Loops Author: jkb Created Date: 10/1/2019 12:41:50 PM](https://reader035.vdocuments.site/reader035/viewer/2022071419/611898ed0ad81d3c9a301650/html5/thumbnails/17.jpg)
Example of Generating Random Numbers
![Page 18: ��Chapter 18-19 List, For Loops · 2019. 10. 1. · Title: ��Chapter 18-19 List, For Loops Author: jkb Created Date: 10/1/2019 12:41:50 PM](https://reader035.vdocuments.site/reader035/viewer/2022071419/611898ed0ad81d3c9a301650/html5/thumbnails/18.jpg)
Practice: How Do You…
• Generate a random integer that ranges from [5, 8]:
• Generate a random float that ranges from [0, 2.5]:
• Generate a random float that ranges from [3, 7]:
• Generate a random float that ranges from [3, 7.5]:
• Generate a random float that ranges from [2.5 to 3.75]:
![Page 19: ��Chapter 18-19 List, For Loops · 2019. 10. 1. · Title: ��Chapter 18-19 List, For Loops Author: jkb Created Date: 10/1/2019 12:41:50 PM](https://reader035.vdocuments.site/reader035/viewer/2022071419/611898ed0ad81d3c9a301650/html5/thumbnails/19.jpg)
Practice: How Do You…
• Generate a random integer that ranges from [5, 8]:
• Generate a random float that ranges from [0, 2.5]:
• Generate a random float that ranges from [3, 7]:
• Generate a random float that ranges from [3, 7.5]:
• Generate a random float that ranges from [2.5 to 3.75]:
random.randint(5, 8)
![Page 20: ��Chapter 18-19 List, For Loops · 2019. 10. 1. · Title: ��Chapter 18-19 List, For Loops Author: jkb Created Date: 10/1/2019 12:41:50 PM](https://reader035.vdocuments.site/reader035/viewer/2022071419/611898ed0ad81d3c9a301650/html5/thumbnails/20.jpg)
Practice: How Do You…• Generate a random integer that ranges from [5, 8]:
• Generate a random float that ranges from [0, 2.5]:
• Generate a random float that ranges from [3, 7]:
• Generate a random float that ranges from [3, 7.5]:
• Generate a random float that ranges from [2.5 to 3.75]:
random.randint(5, 8)
random.random() * 2.5
![Page 21: ��Chapter 18-19 List, For Loops · 2019. 10. 1. · Title: ��Chapter 18-19 List, For Loops Author: jkb Created Date: 10/1/2019 12:41:50 PM](https://reader035.vdocuments.site/reader035/viewer/2022071419/611898ed0ad81d3c9a301650/html5/thumbnails/21.jpg)
Practice: How Do You…• Generate a random integer that ranges from [5, 8]:
• Generate a random float that ranges from [0, 2.5]:
• Generate a random float that ranges from [3, 7]:
• Generate a random float that ranges from [3, 7.5]:
• Generate a random float that ranges from [2.5 to 3.75]:
random.randint(5, 8)
random.random() * 2.5
random.random() * 4 + 3
![Page 22: ��Chapter 18-19 List, For Loops · 2019. 10. 1. · Title: ��Chapter 18-19 List, For Loops Author: jkb Created Date: 10/1/2019 12:41:50 PM](https://reader035.vdocuments.site/reader035/viewer/2022071419/611898ed0ad81d3c9a301650/html5/thumbnails/22.jpg)
Practice: How Do You…• Generate a random integer that ranges from [5, 8]:
• Generate a random float that ranges from [0, 2.5]:
• Generate a random float that ranges from [3, 7]:
• Generate a random float that ranges from [3, 7.5]:
• Generate a random float that ranges from [2.5 to 3.75]:
random.randint(5, 8)
random.random() * 2.5
random.random() * 4 + 3
random.random() * 4.5 + 3
![Page 23: ��Chapter 18-19 List, For Loops · 2019. 10. 1. · Title: ��Chapter 18-19 List, For Loops Author: jkb Created Date: 10/1/2019 12:41:50 PM](https://reader035.vdocuments.site/reader035/viewer/2022071419/611898ed0ad81d3c9a301650/html5/thumbnails/23.jpg)
Practice: How Do You…• Generate a random integer that ranges from [5, 8]:
• Generate a random float that ranges from [0, 2.5]:
• Generate a random float that ranges from [3, 7]:
• Generate a random float that ranges from [3, 7.5]:
• Generate a random float that ranges from [2.5 to 3.75]:
random.randint(5, 8)
random.random() * 2.5
random.random() * 4 + 3
random.random() * 4.5 + 3
random.random() * 1.25 + 2.5
![Page 24: ��Chapter 18-19 List, For Loops · 2019. 10. 1. · Title: ��Chapter 18-19 List, For Loops Author: jkb Created Date: 10/1/2019 12:41:50 PM](https://reader035.vdocuments.site/reader035/viewer/2022071419/611898ed0ad81d3c9a301650/html5/thumbnails/24.jpg)
Each letter in the plaintext message is replaced by a letter some
fixed number of positions from that letter in the alphabet. For
example, with a left shift of three (the "key" purportedly used by
Caesar), D would be replaced by G, E would become H, e.g.:Plaintext Alphabet: Cipher Alphabet:
Caesar Cipher
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
X Y Z G H I J K L M N O P Q R S T U V W X Y Z A B C
To encrypt a message, we replace each plain alphabet letter
of the plaintext message with the cipher alphabet letter
corresponding to the plain letter. Example:
Plaintext: WE WILL ATTACK AT DAWN UNLESS ITS RAINING
Ciphertext: ZH ZLOO DWWDFN DW GDZQ XQOHVV LWV UDLQLQJ
To decrypt the message, we apply the opposite shift.
![Page 25: ��Chapter 18-19 List, For Loops · 2019. 10. 1. · Title: ��Chapter 18-19 List, For Loops Author: jkb Created Date: 10/1/2019 12:41:50 PM](https://reader035.vdocuments.site/reader035/viewer/2022071419/611898ed0ad81d3c9a301650/html5/thumbnails/25.jpg)
Caesar Cipher Encryption
![Page 26: ��Chapter 18-19 List, For Loops · 2019. 10. 1. · Title: ��Chapter 18-19 List, For Loops Author: jkb Created Date: 10/1/2019 12:41:50 PM](https://reader035.vdocuments.site/reader035/viewer/2022071419/611898ed0ad81d3c9a301650/html5/thumbnails/26.jpg)
The "Vigenère Cipher" improves upon the Caesar cipher by
applying it differently for each letter, using the letters of a
"keyword" to determine the specific Caesar shift to use for each
letter. The Vigenère cipher is named for Blaise de Vigenère, who
wrote of it, but the cipher was actually invented by Giovan
Battista Bellaso in 1553, who was building upon the work of
others, most notably, Johannes Trithemius, who created the
tabula recta, on which the Vigenère cipher is based.
The tabula recta is just a table of all possible Caesar ciphers.
The Vigenère Cipher chooses a particular Caesar cipher for
each letter based upon a Key.
Vigenère Cipher
![Page 27: ��Chapter 18-19 List, For Loops · 2019. 10. 1. · Title: ��Chapter 18-19 List, For Loops Author: jkb Created Date: 10/1/2019 12:41:50 PM](https://reader035.vdocuments.site/reader035/viewer/2022071419/611898ed0ad81d3c9a301650/html5/thumbnails/27.jpg)
Tabula Recta
A Vigenère cipher will use
only as many Caesar
cipher keys in the Tabula
Recta as there are unique
letters in the Key. If the
message is longer than the
Key, the letters of the Key
will be reused in the same
order
![Page 28: ��Chapter 18-19 List, For Loops · 2019. 10. 1. · Title: ��Chapter 18-19 List, For Loops Author: jkb Created Date: 10/1/2019 12:41:50 PM](https://reader035.vdocuments.site/reader035/viewer/2022071419/611898ed0ad81d3c9a301650/html5/thumbnails/28.jpg)
Plaintext: WE WILL ATTACK AT DAWN UNLESS ITS RAINING
KEY: COMPUTERSCIENCEISAWESOME
Ciphertext: YS IXFE EKLCKO NV HION QRDSEW KHE GUBRZFI
Vigenère Cipher Example If the first letter of the plaintext message is W, and the first letter
of the Key is C, row C and column W of the Tabula Recta yields
a Y. So, to encrypt a message, we replace each plain alphabet
letter of the Plaintext message with the Ciphertext letter found
in the tabula recta at the location determined by the Key.
![Page 29: ��Chapter 18-19 List, For Loops · 2019. 10. 1. · Title: ��Chapter 18-19 List, For Loops Author: jkb Created Date: 10/1/2019 12:41:50 PM](https://reader035.vdocuments.site/reader035/viewer/2022071419/611898ed0ad81d3c9a301650/html5/thumbnails/29.jpg)
Vigenère Cipher Implementation"Write a Python program that implements the Vigenère
cipher, allowing the user to specify the plaintext and the key
(for encryption), or the ciphertext and the key (for decryption
Make sure you handle both upper case and lower case
letters, but numbers and other characters (like spaces)
should be passed through unmodified." Keys can only
contain letters.
Hint 1: You do not have to build a tabula recta table to do
the encryption/decryption.
Hint 2: Get the Caesar cipher working first, then extend it to
Vigenère.
![Page 30: ��Chapter 18-19 List, For Loops · 2019. 10. 1. · Title: ��Chapter 18-19 List, For Loops Author: jkb Created Date: 10/1/2019 12:41:50 PM](https://reader035.vdocuments.site/reader035/viewer/2022071419/611898ed0ad81d3c9a301650/html5/thumbnails/30.jpg)
Vigenère Cipher Implementation
1. For each letter in the plaintext, extract the corresponding
letter from the key (wrapping the key as necessary).
2. Use the ordinal number of the key letter (minus the
ordinal number of ‘A’ or ‘a’, as appropriate), as the
keyshift for that plaintext character.
3. Then just apply the Caesar cipher for the plaintext letter
with that keyshift.
4. Repeat for each letter in the plaintext message.