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
Q

What is a text file?

A
  • software object that stores data on permanent medium such as disk or CD
26
Q

Advantages of a text file

A
  • 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
Q

T or F: All data output to or input from a text file must be strings

A

True: number must be converted to string before output

28
Q

Data can be output to a text file using a ___ object

A

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
Q

How do you open a file for input

A

method read

30
Q

open(filename, mode)

A

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
Q

f.close()

A

Closes an output file. Not needed for input files.

32
Q

f.write(aString)

A

outputs aString to a file.

33
Q

f.read()

A

Inputs the contents of a file and returns them as a
single string. Returns “” if the end of file is reached.

34
Q

f.readline()

A

Inputs a line of text and returns it as a string,
including the newline. Returns “” if the end of file is
reached.

35
Q

Absolute pathname

A

when the chain starts with the root directory

36
Q

relative pathname

A

when chain starts from the current working directory

37
Q

myfile.txt

A

current target directory

38
Q

child/myfile.txt

A

child target directory

39
Q

../myfile.txt

A

parent target directory

40
Q

../sibling/myfile.txt

A

sibling target directory

41
Q

chdir(path)

A

Changes the current working directory to path

42
Q

getcwd()

A

Returns the path of the current working directory

43
Q

list dir(path)

A

Returns a list of the names in directory named
path

44
Q

mkdir(path)

A

Creates a new directory named path and places it
in the current working directory

45
Q

remove(path)

A

Removes the file named path from the current
working directory.

46
Q

rename(old, new)

A

Renames the file or directory named old to new.

47
Q

rmdir(path)

A

Removes the directory named path from the
current working directory

48
Q

sep

A

A variable that holds the separator character (‘/’ or
‘\’) of the current file system

49
Q

exists(path)

A

Returns True if path exists and False otherwise

50
Q

isdir(path)

A

Returns True if path names a directory and False
otherwise

51
Q

isfile(path)

A

Returns True if path names a file and False
otherwise.

52
Q

getsize(path)

A

Returns the size of the object names by path in
bytes.

53
Q

normcase(path)

A

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.