Encryption & Artificial Intelligence
Reading Questions
Quiz
Ord & Chr
- Encryption requires performing mathematical operations on text
- Can’t perform mathematical operations on strings
- For example,
'a' + 3produces an error - But each character in a string is represented as a number in memory
- Can convert a character to is numerical representation
Then perform mathematical operations
num = ord('a') num = num + 3)Then convert back to character
num = ord('a') num = num + 3) print(chr(num))Can also use this to convert letters to numbers in the range 0-25
num = ord('c') - ord('a') print(num)
String Module
- The string module has a few variables that make classifying characters easy
- The variable
string.ascii_lowercaseis"abcdefghijklmnopqrstuvwxyz" - The variable
string.ascii_uppercaseis"ABCDEFGHIJKLMNOPQRSTUVWXYZ" - The variable
string.digitsis"0123456789" So to test if a letter is upper case is:
import string print('A' in string.ascii_uppercase)Can also use this to convert letters to numbers in the range 0-25 using the find method
import string print(string.ascii_uppercase.find('C'))