Week 4

Overview

In Week 4, we will be exploring two very disparate topics:

The section on Unicode can get quite dense. Follow along as best as you can. You do not need to become a master of the features of Unicode, but you do need a general sense of how it works.

Objectives

By the end of this week, you should be able to:

Readings & Resources

All of the readings are in Canvas.

Activities & Assignments

Lesson

Flow Control - Loops

spam = 0
while spam < 5:
    print('Hello, world.')
    spam = spam + 1
Hello, world.
Hello, world.
Hello, world.
Hello, world.
Hello, world.

break Statements

while True:                         # (1)
    print('Please type your name.')
    name = input()                  # (2)
    if name == 'your name':         # (3)
        break                       # (4)
print('Thank you!')                 # (5)

for Loops and the range() Function

print('My name is')
for i in range(5):
    print('Jimmy Five Times (' + str(i) + ')')
My name is
Jimmy Five Times (0)
Jimmy Five Times (1)
Jimmy Five Times (2)
Jimmy Five Times (3)
Jimmy Five Times (4)
for i in range(12, 16):
    print(i)
12
13
14
15
for i in range(0, 10, 2):
    print(i)
0
2
4
6
8
for i in range(5, -1, -1):
    print(i)
5
4
3
2
1
0

Importing Modules

import random
for i in range(5):
    print(random.randint(1, 10))
4
1
8
4
1
import random, sys, os, math

Intro to ASCII

>>> ord('*')
42
>>> ord('9')
57

UNICODE

The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)

UNICODE Domain Phishing Attacks

Chrome, Firefox, and Opera users vulnerable to Unicode domain phishing attacks

Cryptography

For example, the message could be:

Hello there! The keys to the house are hidden under the flower pot.

For more info in writing cryptography in Python see:

Hacking Secret Ciphers with Python

The Caesar Cipher

ASCII, and Using Numbers for Letters

The chr() and ord() Functions

>>> chr(65)
'A'
>>> ord('A')
65
>>> chr(65+8)
'I'
>>> chr(52)
'4'
>>> chr(ord('F'))
'F'
>>> ord(chr(68))
68

Code for Caesar Cipher in Python

#Caesar Cypher Encryption

#Get the message to encrypt from the user
print('Enter your message:')
unencryptedMessage = input()

#Get the encryption key from the user.  Note the error handling to make sure we have a number from 1-26
key=0
while (key < 1 or key > 26):
    print('Enter the key number (1-26)')
    key = int(input())

print('The key is ' + str(key))

#We will start with an empty string as our encryptedMessage
encryptedMessage = ''

#For each symbol in the unencryptedMessage we will add an encrypted symbol into the encryptedMessage
for symbol in unencryptedMessage:
    if symbol.isalpha():
        num = ord(symbol)
        num += key

        if symbol.isupper():
            if num > ord('Z'):
                num -= 26
            elif num < ord('A'):
                num += 26
        elif symbol.islower():
            if num > ord('z'):
                num -= 26
            elif num < ord('a'):
                num += 26

        encryptedMessage += chr(num)
    else:
        encryptedMessage += symbol

print("Your encrypted message is:")
print(encryptedMessage)

Example Run:

Enter your message:
The wolves are coming!
Enter the key number (1-26)
4
The key is 4
Your encrypted message is:
Xli aspziw evi gsqmrk!

isalpha() String Method

>>> 'Hello'.isalpha()
True
>>> 'Forty two'.isalpha()
False
>>> 'Fortytwo'.isalpha()
True
>>> '42'.isalpha()
False
>>> ''.isalpha()
False

isupper() islower() String Methods

>>> 'HELLO'.isupper()
True
>>> 'hello'.isupper()
False
>>> 'hello'.islower()
True
>>> 'Hello'.islower()
False
>>> 'LOOK OUT BEHIND YOU!'.isupper()
True
>>> '42'.isupper()
False
>>> '42'.islower()
False
>>> ''.isupper()
False
>>> ''.islower()
False

Brute Force

Lwcjba uig vwb jm xtmiaivb, jcb kmzbiqvbg qa ijaczl.

Your translated text is:
1 Kvbiaz thf uva il wslhzhua, iba jlyahpuaf pz hizbyk.
2 Juahzy sge tuz hk vrkgygtz, haz ikxzgotze oy ghyaxj.
3 Itzgyx rfd sty gj uqjfxfsy, gzy hjwyfnsyd nx fgxzwi.
4 Hsyfxw qec rsx fi tpiewerx, fyx givxemrxc mw efwyvh.
5 Grxewv pdb qrw eh sohdvdqw, exw fhuwdlqwb lv devxug.
6 Fqwdvu oca pqv dg rngcucpv, dwv egtvckpva ku cduwtf.
7 Epvcut nbz opu cf qmfbtbou, cvu dfsubjouz jt bctvse.
8 Doubts may not be pleasant, but certainty is absurd.
9 Cntasr lzx mns ad okdzrzms, ats bdqszhmsx hr zartqc.
10 Bmszrq kyw lmr zc njcyqylr, zsr acpryglrw gq yzqspb.
11 Alryqp jxv klq yb mibxpxkq, yrq zboqxfkqv fp xyproa.
12 Zkqxpo iwu jkp xa lhawowjp, xqp yanpwejpu eo wxoqnz.
13 Yjpwon hvt ijo wz kgzvnvio, wpo xzmovdiot dn vwnpmy.
14 Xiovnm gus hin vy jfyumuhn, von wylnuchns cm uvmolx.
15 Whnuml ftr ghm ux iextltgm, unm vxkmtbgmr bl tulnkw.
16 Vgmtlk esq fgl tw hdwsksfl, tml uwjlsaflq ak stkmjv.
17 Uflskj drp efk sv gcvrjrek, slk tvikrzekp zj rsjliu.
18 Tekrji cqo dej ru fbuqiqdj, rkj suhjqydjo yi qrikht.
19 Sdjqih bpn cdi qt eatphpci, qji rtgipxcin xh pqhjgs.
20 Rciphg aom bch ps dzsogobh, pih qsfhowbhm wg opgifr.
21 Qbhogf znl abg or cyrnfnag, ohg pregnvagl vf nofheq.
22 Pagnfe ymk zaf nq bxqmemzf, ngf oqdfmuzfk ue mnegdp.
23 Ozfmed xlj yze mp awpldlye, mfe npceltyej td lmdfco.
24 Nyeldc wki xyd lo zvokckxd, led mobdksxdi sc klcebn.
25 Mxdkcb vjh wxc kn yunjbjwc, kdc lnacjrwch rb jkbdam.
26 Lwcjba uig vwb jm xtmiaivb, jcb kmzbiqvbg qa ijaczl.