In [1]:
s = "test message"
In [2]:
s[0]
Out[2]:
't'
In [3]:
s.find("s")
Out[3]:
2
In [4]:
alphabet =   "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
code_alpha = "QWERTYUIOPASDFGHJKLZXCVBNM"
In [5]:
alphabet.find("C")
Out[5]:
2
In [6]:
code_alpha[2]
Out[6]:
'E'
In [7]:
len(alphabet)
Out[7]:
26
In [8]:
[n^2 for n in range(10)]
Out[8]:
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
In [9]:
def sub_encipher(plaintext):
    return [code_alpha[alphabet.find(c)] for c in plaintext]
In [10]:
"".join(sub_encipher("HELLOTHEBESTTHINGINTHEWORLD"))
Out[10]:
'ITSSGZITWTLZZIOFUOFZITVGKSR'
In [11]:
def sub_decipher(ciphertext):
    return [alphabet[code_alpha.find(c)]\
            for c in ciphertext]
In [12]:
"".join(sub_decipher('ITSSGZITWTLZZIOFUOFZITVGKSR'))
Out[12]:
'HELLOTHEBESTTHINGINTHEWORLD'

Challenge: make a cipher that shifts each letter by 3 places in the alphabet, wrapping around at the end of the element

In [13]:
def shift3_encipher(plaintext):
    return [alphabet[(alphabet.find(c)+3) % 26]\
            for c in plaintext]
In [14]:
shift3_encipher("HELLOZ")
Out[14]:
['K', 'H', 'O', 'O', 'R', 'C']
In [15]:
def shift_encipher(plaintext, key):
    return [alphabet[(alphabet.find(c)+key) % 26]\
            for c in plaintext]
In [16]:
"".join(shift_encipher("HELLOZ",3))
Out[16]:
'KHOORC'
In [17]:
shift_encipher('KHOORC',-2)
Out[17]:
['I', 'F', 'M', 'M', 'P', 'A']
In [18]:
factorial(26)
Out[18]:
403291461126605635584000000
In [19]:
random()
Out[19]:
0.08491290372396332
In [20]:
random?
In [21]:
randint(1,5)
Out[21]:
4
In [22]:
randint(0,25)
Out[22]:
11