Preamble

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.

Strings

  • Strings are sequences of characters delimited by quotation marks.
  • Can use single, double, or even triple quotation marks. But you can't mix them.
  • We will use double quotation marks, because it's considered good style.
In [14]:
"Twas brillig in the slithy tove"
print("Twas brillig in the slithy tove")
Twas brillig in the slithy tove

Variables

  • Variables are like containers. They have a label (a variable name) and can hold a value.

Here is a variable that contains a string.

In [15]:
jabberwocky = "Did gyre and gimble in the wabe:"
print(jabberwocky)
Did gyre and gimble in the wabe:
  • Strings have a length. You can find it using the len() function
In [19]:
print(len("Twas brillig in the slithy tove"))
jabberwocky = "Did gyre and gimble in the wabe:"
print(len(jabberwocky))
31
32

Indexing

  • Individual characters in strings can be identified using an index.
  • This is done by adding square brackets to the end of a string (value or variable)
  • Indexes start from 0 (It's a computer science thing.)

In [20]:
spam = "Hello"
print(spam[0])
print(spam[1])
print(spam[2])
H
e
l

Trying out string indexes

Create a variable. Assign a string to it. Try out string indexes.

  • What happens when you use an index that is too big?
  • How big is too big?
  • What happens when you use an index that is too small?

Back to the Caesar Cipher

How will string indexing be useful for Caesar cipher program?

In [ ]:
alphabet = ""

Doing Math

+ addition

- subtraction

* multiplication

/ division

% remainder (modulo)

We can do math on the string indexes to find the encrypted letter!

In [23]:
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])
p
q
r
s

Looking Up a Character in a String

  • Python is an object-oriented language. This means we can ask questions of objects.
  • Strings are objects.
  • Ask a string to find where a letter occurs.

.find(letter)

In [36]:
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)
w
z
d
v

String Concatenation

  • Two strings can be added together using the + operator. This is called string concatenation.
  • Python concatenates the strings exactly. It doesn't add extra spaces.
In [39]:
print("Robin" + " " + "Brave")

spam = "email"
eggs = "bacon"

print (spam + eggs)
Robin Brave
emailbacon

Decision Structures

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.

In [41]:
temperature = 10

if (temperature < 5):
    print("Wear a coat.")

Boolean Expressions and Relational Operators

We can use decision structures to adjust string indexes that are too big or too small.

if the index is too big then ...

In [56]:
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]))
27
25: z
26:  
0: a
1: b

Repetition Structures

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.

In [59]:
for number in [1, 2, 3, 4]:
    print(number)

for character in "Faculty of Information":
    print(character)
1
2
3
4
F
a
c
u
l
t
y
 
o
f
 
I
n
f
o
r
m
a
t
i
o
n

How might we use this in encryption?

In [ ]: