This program is iPython Notebook. I'll be using it to lecture, because I can have text and running code in the same document.
The text cells are formatted using "Markdown," which is similar to what Wikipedia uses.
"Twas brillig in the slithy tove"
print("Twas brillig in the slithy tove")
Here is a variable that contains a string.
jabberwocky = "Did gyre and gimble in the wabe:"
print(jabberwocky)
print(len("Twas brillig in the slithy tove"))
jabberwocky = "Did gyre and gimble in the wabe:"
print(len(jabberwocky))
spam = "Hello"
print(spam[0])
print(spam[1])
print(spam[2])
Create a variable. Assign a string to it. Try out string indexes.
How will string indexing be useful for Caesar cipher program?
alphabet = ""
+ addition
- subtraction
* multiplication
/ division
% remainder (modulo)
We can do math on the string indexes to find the encrypted letter!
input_text = "Blue jays"
input_text[0] # This should be B
cipher_key = 15
alphabet = "abcdefghijklmnopqrstuvwxyz"
print(alphabet[0 + cipher_key])
print(alphabet[1 + cipher_key])
print(alphabet[2 + cipher_key])
print(alphabet[3 + cipher_key])
.find(letter)
alphabet = "abcdefghijklmnopqrstuvwxyz"
input_text = "twas"
cipher_key = 3
character = input_text[0]
character_position = alphabet.find(character)
new_position = character_position + cipher_key
new_letter = alphabet[new_position]
print(new_letter)
character = input_text[1]
character_position = alphabet.find(character)
new_position = character_position + cipher_key
new_letter = alphabet[new_position]
print(new_letter)
character = input_text[2]
character_position = alphabet.find(character)
new_position = character_position + cipher_key
new_letter = alphabet[new_position]
print(new_letter)
character = input_text[3]
character_position = alphabet.find(character)
new_position = character_position + cipher_key
new_letter = alphabet[new_position]
print(new_letter)
print("Robin" + " " + "Brave")
spam = "email"
eggs = "bacon"
print (spam + eggs)
So far, we've been writing programs that do the same thing repeatedly. If order for a program to be more reactive, we need a way for it to make decisions. Depending on whether something is the case, it takes one action or another.
temperature = 10
if (temperature < 5):
print("Wear a coat.")
We can use decision structures to adjust string indexes that are too big or too small.
if the index is too big then ...
alphabet = "abcdefghijklmnopqrstuvwxyz "
index = 25
print(len(alphabet))
print(str(index) + ": " + str(alphabet[index]))
index = index + 1
print(str(index) + ": " + str(alphabet[index]))
index = index + 1
# print(alphabet[27])
if index >= len(alphabet):
index = index - len(alphabet)
print(str(index) + ": " + str(alphabet[index]))
index = index + 1
if index >= len(alphabet):
index = index - len(alphabet)
print(str(index) + ": " + str(alphabet[index]))
So far, we have been repeating actions by repeating lines of code.
If we are going to encrypt longer strings, we need to make this easier.
This is where repetition structures come in.
for number in [1, 2, 3, 4]:
print(number)
for character in "Faculty of Information":
print(character)
How might we use this in encryption?