Chapter 5
The ____ function returns the numeric (ordinal) code of a single character. give example of what this looks like
ord in: ord("A") out: 65
Using ___ and ___ we can convert a string into and out of numeric form.
ord char
In ______ ______ encryption, there are separate keys for encrypting and decrypting the message.
public key
with cease cipher and vegenere cipher In a this system the _____ key is used for encrypting and decrypting messages. Everyone you know would need a copy of this key to communicate with you, but it needs to be kept a secret.
same
Strings are always sequences of characters, but _____ can be sequences of arbitrary values. they can have numbers strings or both
lists
how do you evaluate a string as an expression?
eval(<string>)
how do you encode an algorithm? how would this be done with a for loop?
get the message to encode for each character in the message: print the letter number of the character A for loop iterates over a sequence of objects, so the for loop looks like: for ch in <string>
what is the outline for a decoder?
get the sequence of numbers to decode message = "" for each number in the input: convert the number to the appropriate character add the character to the end of the message print the message
___________________ uses numbers 0 through _____ to represent the characters typically found on an (American) computer keyboard, and control codes that are used to coordinate
ASCII system (American Standard Code for Information Interchange) 127
The process of encoding information for the purpose of keeping it secret or transmitting it privately is called _______.
encryption
_________ is used when transmitting credit card and other personal information to a web site.
encryption
In public key systems, the _______ key is made publicly available, while the ________ key is kept private.
encryption decryption only person with decryption key can decrypt it.
How do we get the sequence of numbers to decode?
Read the input as a single string, then split it apart into substrings, each of which represents one number.
how does slicing work? use the greet= Hello Bob example to show this
The slice contains the substring beginning at position start and runs up to but *doesn't include the position end.* <var>[<start>:<end>:<step>] ex: in: greet[0:3] out: 'Hel' in: greet[2:7:2] out: 'loB' *if you do not put an <end> it will include the whole string(position end)
explain how the split method can be used for decoding
This will split a string into substrings based on spaces. in: "Hello string methods!".split() out: ['Hello', 'string', 'methods!']
explain two ways to get multiple values in a single input string
Turning it into a list using the split method Convert the string numbers into their corresponding numbers using eval
with ASCII: letters A-Z are represented by the values___-____, the lowercase versions have codes ___-____. how much memory is this?
65-90 97-122 1byte(8bits)
how do you get a string as input?
<variable>= input("<>:")
explain how to build a program that gives a three letter abbreviation for a month given its number value (use pos function)
#store names in one string months = "JanFebMarAprMayJunJulAugSepOctNovDec" find each position of the months by sub 1 from month number and multiplying by 3 (ex: Mar is the third month, so (3-1)*3=6)-remember"j"starts at 0 n= int(input("enter a month number...") pos= (n-1)*3 monthabbrev= months[pos:pos+3] print(monthAbbrev)
Unicode is a superset of ASCII, and the numbers __-___ have the same meaning in ASCII as they have in Unicode. ?UTF-8, UTF-16 are _____ length encoding?
0-128 variable
how are strings stored in a computer?
as a sequence of binary numbers (1's and 0's)
The ___ function converts a numeric code to the corresponding character. give example of what this looks like
chr in: chr(97) out: 'a'
"glues" two strings together (+)
concatenation
________ is the study of encryption methods.
cryptography
Let's say we want to enter a date in the format "05/24/2015" and output "May 24, 2015." How could we do that?
dateStr = input("Enter a date (mm/dd/yyyy): ") monthStr, dayStr, yearStr = dateStr.split("/") # convert monthStr to the month name months = ["January", "February", "March", "April","May", "June", "July", "August","September", "October", "November", "December"] monthStr = months[int(monthStr)-1] print("The converted date is:", monthStr, dayStr+",", yearStr) may need to int("dayStr")? so 05=5
how would you create a program that turns the day month and year into the mm/dd/yyyy format?
day, month, year = eval(input("Please enter day, month, and year numbers: ")) date = str(month)+"/"+str(day)+"/"+str(year)
To ________ the message, the receiving end needs an appropriate key so the encoding can be _______. what are two cipher examples
decrypt reversed Eg. Caesar cipher , Vegenere cipher
show how you would get someones username using first initial, first seven characters of last name:
in: #get users first and last name first= input("...:") last= input("...:") #concatenate first initial with 7 chars of last name uname= first[0]+last[:7]
Split can be used on characters other than space, by supplying the character as a ________. show an example of this
in: "32,24,25,57".split(",") out: ['32', '24', '25', '57']
how would you repeat the string "spam" 3 times using repetition?
in: "spam" * 3 out: 'spamspamspam
how would you combine the strings "spam" and "eggs" using concatenations?
in: "spam" + "eggs" out: 'spam eggs'
how do you put spaces between the letters in a string? use the string "Spam!" for an example
in: for ch in "Spam!" print(ch, end=" ") out: S p a m !
explain how appending lists can decode (also using join function)
inString = input("Please enter the Unicode-encoded message: ") # Loop through each substring and build Unicode message chars = [] for numStr in inString.split(): # convert digits to a number codeNum = int(numStr) # accumulate new character chars.append(chr(codeNum)) message= " ".join(chars) print (message)
explain how you would create a program to decode a unicode message
inString= input("enter unicode message...") #create accumulator variable message= " " for numStr in inString.split(): #convert digits to a number codeNum= int(numStr) # concatenate character to message message= message+chr(codeNum) print decoded message...
________ returns a string containing a single character from a larger string.
indexing
how can you access the individual characters in a string? how are they positioned? use example: greet= Hello Bob
indexing numbered from the left starting with 0. h-0 e-1 l-2 etc. input: print(greet[0]),greet[2],greet[4]) output: H l o
2 useful operations for chopping strings into smaller pieces:
indexing slicing
how do you find the length of a string?
len("<>")
when programming a decoder: The variable ______ is an accumulator variable, initially set to the empty string, the string with no characters (""). Each time through the loop, a number from the input is converted to the appropriate character and appended to the end of the accumulator.
message
explain how to write a program to convert a textual message into a sequence of numbers, utilizing the underlying Unicode encoding.
message = input("Please enter the message to encode: ") print("\nHere are the Unicode codes:") # Loop through the message and print out the Unicode values for ch in message: print(ord(ch), end=" ") print() # blank line before prompt
how many characters are in Unicode?
million +
how can you use a list in the months abbreviation example to make things simpler?
months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"] monthAbbrev = months[n-1] *subtract 1 from n because jan=0 not 1
Lists are ______, meaning they can be changed. ______ can not be changed.
mutable strings
Modern encryption converts messages into _______. Sophisticated mathematical formulas convert these numbers into new numbers - usually this transformation consists of combining the message with another value called the "_____"
numbers key
What is a string object?
objects have both data and operations (they "know stuff," and "do stuff.")
builds up a string by multiple concatenations of a string with itself (*)
repetition
explain these string methods: s.capitalize() s.title() s.center(width) s.count(sub) s.find(sub) s.join(list) s.ljust(width) s.rjust(width)
s.capitalize() - Copy of s with only the first character capitalized s.title() - Copy of s' first character of each word capitalized s.center(width) - Center s in a field of given width s.count(sub) - Count the number of occurrences of sub in s s.find(sub) - Find the first position where sub occurs in s s.join(list) - Concatenate list of strings into one large string using s as separator. s.ljust(width) - Like center, but s is left-justified s.rjust(width) - Like center, but s is right-justified
explain these string methods: s.lower() s.lstrip() s.replace(olds, newsub) s.rfind(sub) s.rstrip() s.split() s.upper()
s.lower() - Copy of s in all lowercase letters s.lstrip() - Copy of s with leading whitespace removed s.replace(oldsub, newsub) - Replace occurrences of oldsub in s with newsub s.rfind(sub) - Like find, but returns the right-most position s.rstrip() - Copy of s with trailing whitespace removed s.split() - Split s into a list of substrings s.upper() - Copy of s; all characters converted to uppercase
strings are really a special kind of _______.
sequence
The ______ method can be used to add an item at the end of a list. give an example
squares = [] for x in range(1,11): squares.append(x*x) We start with an empty list ([]) and each number from 1 to 100 is squared and appended to it ([1, 4, 9, ..., 100]).
how do you convert a number to a string? why might you need to do this?
str(<>) to add things like periods and commas, if we use "+" with interest python will not be able to interpret because it will think you are trying to add, so need to make into string
Text is represented in programs by the _____ data type.
string
a sequence of characters enclosed within quotation marks (") or apostrophes (').
string
The encoding/decoding programs we wrote use a ________ _______, where each character of the original message, known as the ________, is replaced by a corresponding symbol in the cipher alphabet. the resulting code is known as the _______
substitute cipher plaintext ciphertext
We can also access a contiguous sequence of characters, called a ____, through a process called _____.
substring slicing
a much larger standard that includes support for the characters of nearly all written languages
unicode
how do you index from the right side?
using negative indexes In a string of n characters, the last character is at position n-1 since we start counting with 0. sooo <var>[-1]= the last letter in the string
The most common use of personal computers is ____________.
word processing
what is the string format method?
{} within the template-string mark "slots" into which the values are inserted. Each slot has description that includes format specifier telling Python how the value for the slot should appear.