Chapter 4 Flashcards
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
- uses plaintext character to compute 2+ encrypted characters
- uses an invertible matrix
What number system do the arithmetic operations use
Decimal number system- base 10 number system
binary number system
- used to represent info in a digital computer
- its called the base 2 number system (0 and 1)
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
- Each digit or bit in binary number has positional value that is power of 2
- We occasionally refer to a binary number as a string of bits or a bit string
-to determine the integer quantity that a string of bits represents: multiply the value of each bit (0 or 1) by its positional value and add the results.
code of converting binary to decimal
- A positional value is computed by using the ** operator
“““
File: binarytodecimal.py
Converts a string of bits to a decimal integer.
”””
bitString = input(“Enter a string of bits: ”)
decimal = 0
exponent = len(bitString) − 1
for digit in bitString:
decimal = decimal + int(digit) * 2 ** exponent
exponent = exponent − 1
print(“The integer value is”, decimal)
Enter a string of bits: 1111
The integer value is 15
Enter a string of bits: 101
The integer value is 5
How are integers converted from decimal to binary
- One algorithm uses division and subtraction instead of multiplication and
addition - Repeatedly divides the decimal number by 2
- After each division, the remainder (either a 0 or 1) is placed at the beginning of a
string of bits - Quotient becomes the next dividend in the process
- Process continues while the decimal number is greater than 0
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