T or F : An integer can’t be factored into more primitive parts
True
What is a string
A data structure which consists of smaller pieces of data
What is the form of a subscript operator
“String”[integer operator- position of a particular character in the string]
Ie:
»> name = “Alan Turing”
»> name[0] # Examine the first character
‘A’
subscript operator in loops: count- controlled loop
IN: data = “Hi there!”
IN: for index in range(len(data)):
IN: print(index, data[index])
OUT:
0 H
1 i
2
3 t
4 h
5 e
6 r
7 e
8 !
How to use a subscript operator to obtain a substring
Slicing- place colon in the subscript; an integer value can appear on either side of the colon
»> name = “myfile.txt” # The entire string
»> name[0:]
‘myfile.txt’
»> name[0:1] # The first character
‘m’
»> name[0:2] # The first two characters
‘my’
»> name[:len(name)] # The entire string
‘myfile.txt’
»> name[−3:] # The last three characters
‘txt’
»> name[2:6] # Drill to extract ‘file’
‘file’
Testing for a substring with the in operator
When used with strings, the left operand of in is a target substring and the right
operand is the string to be searched
* Returns True if target string is somewhere in search string, or False otherwise
* This code segment traverses a list of filenames and prints just the filenames
that have a .txt extension:
»> fileList = [“myfile.txt”, “myprogram.exe”, “yourfile.txt”]
»> for fileName in fileList:
if “.txt” in fileName:
print(fileName)
myfile.txt
yourfile.txt
Caesar cipher
replaces each character in plain text with a character a given distance away
How to decrypt Caesar cipher
Apply a method that uses the same distance value but looks to the left of each character for replacement value
ord and chr
ord - returns the ordinal position in the ASCII sequence
chr- is the inverse function
Block cipher
What number system do the arithmetic operations use
Decimal number system- base 10 number system
binary number system
T or F: The digits use in each system are counted from 1 to n - 1, where n is the system’s base
FALSE: The digits used in each system are counted from 0 to n − 1, where n is the system’s base
Positional notation
In positional notation, a digit has a positional value, determined by raising the base to the power specified by the position
(base^position)
converting binary to decimal
code of converting binary to decimal
How are integers converted from decimal to binary
Convert from octal to binary
Start by assuming that each digit in the octal number represents 3 digits in the corresponding binary number
Convert binary to octal
Begin at the right and factor the bits into groups of 3 bits each
convert hex to binary
replace each hex digit with the corresponding 4 bit binary number
convert binary to hex
factor the bits into groups of 4 and look up the corresponding hex digits
What is a method
<an>.<method>(<argument‐1>,..., <argument‐n>)
</method></an>
T or F: In Python, there are a variety of different data values
False: In Python, all data values are objects
How do you view a complete list/documentation of string methods
enter dir(str) at a shell prompt
and enter
help(str.<method‐name>) to receive documentation on an individual method