Chapter 4 Flashcards

1
Q

T or F : An integer can’t be factored into more primitive parts

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is a string

A

A data structure which consists of smaller pieces of data

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the form of a subscript operator

A

“String”[integer operator- position of a particular character in the string]

Ie:
»> name = “Alan Turing”
»> name[0] # Examine the first character
‘A’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

subscript operator in loops: count- controlled loop

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How to use a subscript operator to obtain a substring

A

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’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Testing for a substring with the in operator

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Caesar cipher

A

replaces each character in plain text with a character a given distance away

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How to decrypt Caesar cipher

A

Apply a method that uses the same distance value but looks to the left of each character for replacement value

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

ord and chr

A

ord - returns the ordinal position in the ASCII sequence
chr- is the inverse function

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Block cipher

A
  • uses plaintext character to compute 2+ encrypted characters
  • uses an invertible matrix
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What number system do the arithmetic operations use

A

Decimal number system- base 10 number system

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

binary number system

A
  • used to represent info in a digital computer
  • its called the base 2 number system (0 and 1)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

T or F: The digits use in each system are counted from 1 to n - 1, where n is the system’s base

A

FALSE: The digits used in each system are counted from 0 to n − 1, where n is the system’s base

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Positional notation

A

In positional notation, a digit has a positional value, determined by raising the base to the power specified by the position
(base^position)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

converting binary to decimal

A
  • 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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

code of converting binary to decimal

A
  • 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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

How are integers converted from decimal to binary

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Convert from octal to binary

A

Start by assuming that each digit in the octal number represents 3 digits in the corresponding binary number

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

Convert binary to octal

A

Begin at the right and factor the bits into groups of 3 bits each

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

convert hex to binary

A

replace each hex digit with the corresponding 4 bit binary number

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

convert binary to hex

A

factor the bits into groups of 4 and look up the corresponding hex digits

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

What is a method

A
  • behaves like a function
  • always called with a given data value called an object
  • syntax:

<an>.<method>(<argument‐1>,..., <argument‐n>)
</method></an>

23
Q

T or F: In Python, there are a variety of different data values

A

False: In Python, all data values are objects

24
Q

How do you view a complete list/documentation of string methods

A

enter dir(str) at a shell prompt
and enter
help(str.<method‐name>) to receive documentation on an individual method

25
What is a text file?
- software object that stores data on permanent medium such as disk or CD
26
Advantages of a text file
- data set can be much larger - data can be input much more quickly and less chance of error - data can be used repeatedly with the same program or with different programs
27
T or F: All data output to or input from a text file must be strings
True: number must be converted to string before output
28
Data can be output to a text file using a ___ object
file * To open a file for output: >>> f = open("myfile.txt", 'w') * If file does not exist, it is created * If it already exists, Python opens it; when data are written to the file and the file is closed, any data previously existing in the file are erased
29
How do you open a file for input
method read
30
open(filename, mode)
Opens a file at the given filename and returns a file object. The mode can be ‘r’, ‘w’, ‘rw’, or ‘a’. The last two values mean read/write and append
31
f.close()
Closes an output file. Not needed for input files.
32
f.write(aString)
outputs aString to a file.
33
f.read()
Inputs the contents of a file and returns them as a single string. Returns “” if the end of file is reached.
34
f.readline()
Inputs a line of text and returns it as a string, including the newline. Returns “” if the end of file is reached.
35
Absolute pathname
when the chain starts with the root directory
36
relative pathname
when chain starts from the current working directory
37
myfile.txt
current target directory
38
child/myfile.txt
child target directory
39
../myfile.txt
parent target directory
40
../sibling/myfile.txt
sibling target directory
41
chdir(path)
Changes the current working directory to path
42
getcwd()
Returns the path of the current working directory
43
list dir(path)
Returns a list of the names in directory named path
44
mkdir(path)
Creates a new directory named path and places it in the current working directory
45
remove(path)
Removes the file named path from the current working directory.
46
rename(old, new)
Renames the file or directory named old to new.
47
rmdir(path)
Removes the directory named path from the current working directory
48
sep
A variable that holds the separator character (‘/’ or ‘\’) of the current file system
49
exists(path)
Returns True if path exists and False otherwise
50
isdir(path)
Returns True if path names a directory and False otherwise
51
isfile(path)
Returns True if path names a file and False otherwise.
52
getsize(path)
Returns the size of the object names by path in bytes.
53
normcase(path)
Converts path to a pathname appropriate for the current file system; for example, converts forward slashes to backslashes and letters to lowercase on a Windows system.