CS 100 Common Exam 2 (Python)

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play

1/87

encourage image

There are no tags or description

Looks like no tags are added yet.

88 Terms

1
New cards

You (can/cannot) include a single quote in a double quoted string or a double quote in a single quoted string

can

2
New cards

numbers = [0,1,2]

print(numbers[0:2])

[0,1]

3
New cards

>>> fruit = "apple"

>>> fruit.index('p')

1

4
New cards

>>> 'm'.isupper()

false

5
New cards

>>> 'mA'.islower()

False

6
New cards

>>> aStr = 'my cat is catatonic'

>>> aStr.replace('cat','dog')

?

'my dog is dogatonic'

7
New cards

s = "a b\nc"

s.split()

['a','b','c']

8
New cards

s = "a b\nc"

s.split(' ')

['a', 'b\nc']

9
New cards

>>> s = '(hello!)'

>>> s.strip('()!')

>>> s.strip()

>>> s.strip(!)

>>> s.strip('!')

'hello'

'(hello!)'

Syntax Error

'(hello!)'

10
New cards

>>> import string

>>> string.punctuation

'!"#$%&\'()*+,-./:;

11
New cards

strip is a (list/string) method

string

12
New cards

why can't we just use the replace() method to get rid of punctuation

1) you would need to call the replace method a bunch times

2) replace will replace every occurrence of the parameter with its replacement parameter while strip removes all occurences of every character in the paramater only at the ends of the string

13
New cards

print(1,2,3,sep=': ')

1: 2: 3

14
New cards

>>> numbers = []

>>> for i in range(10):

if(i==5):

continue

numbers.append(i)

>>> numbers

[0, 1, 2, 3, 4, 6, 7, 8, 9]

15
New cards

>>> numbers = []

>>> while len(numbers) < 3:

numbers.append(0)

>>> numbers

[0, 0, 0]

16
New cards

>>> while True:

numbers.append(0)

if len(numbers) > 2:

break

>>> numbers

[0, 0, 0]

17
New cards

>>> girl = "sam"

>>> boy = "jake"

>>> girl, boy = "sam2","jake2"

>>> girl + boy

'sam2jake2'

18
New cards

>>> girl = "sam"

>>> boy = "jake"

>>> [girl,boy]=["sam2","jake2"]

>>> girl+boy

'sam2jake2'

19
New cards

>>> previous = 1

>>> current = 1

>>> previous,current = current,previous+current

>>> previous

1) ?

>>> current

2) ?

1

2

20
New cards

>>> p=2

>>> c=3

>>> [p,c]=[c,p+c]

>>> p

1)

>>> c

2)

3

5

21
New cards

The continue statement:

is used inside the body of a 1) _______ or 2)_______ loop

interrupts the current 3)_______ of the loop

transfers execution to the _______ of the loop

1) for

2) while

3) iteration

4) top

22
New cards

A while loop lets you specify any controlling _______ that you want

condition

23
New cards

The break statement:

is used inside the body of a 1)_______ or 2)_______ loop

_______ execution of the loop

transfers execution to the statement that _______ the loop

1) for

2) while

3) termination

4) follows

24
New cards

For both continue and break, if the current loop is nested inside another loop, only the _______ loop is affected

inntermost

25
New cards

def before(table):

for row in table:

for num in row:

if num == 0:

break

print(num, end=' ')

print()

>>> table = [ [2, 3, 0, 6],

[0, 3, 4, 5],

[4, 5, 6, 0] ]

>>> before(table)

2 3

4 5 6

26
New cards

def ignore(table):

for row in table:

for num in row:

if num == 0:

continue

print(num, end=' ')

print()

>>> table = [ [2, 3, 0, 6],

[0, 3, 4, 5],

[4, 5, 6, 0] ]

>>> ignore(table)

2 3 6

3 4 5

4 5 6

27
New cards

open a file named test (for reading) and set it to a variable x

x = open('test.txt','r')

28
New cards

the open() function returns an object of type _______. A method that is supported by this type is the _______ function

file

close()

29
New cards

default open file mode

reading

text

30
New cards

what is the difference between:

infile = open('example.txt', 'rt')

infile = open('example.txt', 'r')

infile = open('example.txt', 't')

infile = open('example.txt')

nothing

31
New cards

open a text document for reading and set it to a variable x

x = open('Test.txt','rt')

32
New cards

open a file named test (for writing) and set it to a variable x

x = open('Test.txt','w')

33
New cards

what happens if you open a file for writing and it does not exist

it gets created

34
New cards

what happens if you open a file for reading and it does not exist

FileNotFoundError

35
New cards

what does the write method print in interactive mode

The number of characters written

36
New cards

Anything you write to a text file must be a _______

The write method does not automatically append an _______

The write method returns the number of _______ written

string

endline

characters

37
New cards

file = open("Humpty.txt",'r')

for line in file:

print(line,end="")

what happens if you do not add the (end="")

it will print two newlines for each line of the file

there exists a newline at the end of each line that you read

38
New cards

read(n)

return value?

Read n characters starting from cursor; if fewer than n characters remain, read until the end of file

returns n characters

39
New cards

read()

return value?

Read starting from cursor up to the end of the file

returns the entire line

40
New cards

readline()

return value?

Read starting from cursor up to and including the endline character

returns one line (including the endline character)

41
New cards

readlines()

return value?

Read starting from cursor up to the end of the file and return a list of lines

returns the entire file as a list of strings (one line = one string)

42
New cards

write()

Write string s to outfile starting from cursor

returns the number of characters written

43
New cards

close()

closes the file

44
New cards

When the file is opened, a cursor is associated with the opened file. The initial position of the cursor is:

at the _______ of the file, if file mode is r

at the _______ of the file, if file mode is a or w

beginning

end

45
New cards

The 3 lines end with the new line character.\n \n There is a blank line above this line.\n

>>> infile = open('example.txt')

>>> infile.read(1)

1)

>>> infile.read(5)

2)

>>> infile.readline()

3)

>>> infile.read()

4)

>>> infile.close()

1) 'T'

2) 'he 3 '

3) 'lines end with the new line character.\n'

4) '\nThere is a blank line above this line.\n'

46
New cards

When writing to files non string value like 5 must be _________ first.

converted

47
New cards

Writing (if file exists, content is _______)

wiped

48
New cards

create a dictionary where each key is a number and each value is the spelling of that word

d = {0:"zero",1:"one",2:"two"}

49
New cards

Create Empty Dictionary

d = {}

50
New cards

Syntax of dictionary

{key:value}

51
New cards

A key can be any 1) _______ type. A key cannot be a 2) _______ type.

1) immutable

2) mutable

52
New cards

Each key in a dictionary must be _______

unique

53
New cards

d = {"cat":"animal","cat":"pet"}

>>> d["cat"]

1)

>>> d

2)

1) 'pet'

2) {'cat': 'pet'}

54
New cards

A value can be _______

any type

55
New cards

add a key:value pair, using index and assignment operators

d["number"]=0

56
New cards

>>> d["number"]=0

>>> d["number"]

1)

>>> d["number"]=1

>>> d

2)

>>> number = d.pop('number')

>>> number

3)

>>> d

4)

1) 0

2) {'number': 1}

3) 1

4) {}

57
New cards

pop function of dictionary

removes the key:value pair associated with the parameter and returns the value

58
New cards

>>> d = {0:0,1:1}

>>> len(d)

2

59
New cards

>>> d = {0:1,"none":True}

>>> 0 in d

1)

>>> True in d

2)

1) True

2) False

60
New cards

>>> d = {"Anthony":18,"Michael":20}

>>> d2 = {"Grooms":22}

>>> d.update(d2)

>>> d

1)

2) does d2 change?

1) {'Anthony': 18, 'Michael': 20, 'Grooms': 22}

2) no

61
New cards

what type does the dictionary update function accept

dictionary

62
New cards

>>> d = {0:0}

>>> d.update({0:1})

>>> d

?

{0: 1}

63
New cards

two ways to go through keys of a dictionary

1) for key in d.keys():

2) for key in d:

64
New cards

how to go through values of a dictionary

for value in d.values():

65
New cards

Retrieving one element from among 10,000,000 in a dictionary is ________

immediate

66
New cards

is there any way to get an empty string w/ indexing

no

67
New cards

how to go from negative index to positive

-index plus length

with string "abby"

negative index (-1) maps to positive index 3 because -1 + 4 = 3

68
New cards

"abby".capitalize()

Abby

69
New cards

"apple".endswith('E')

False

70
New cards

difference between find and index

index will throw indexerror if index is out of bounds

find will return -1

71
New cards

>>> "apple".find("le")

3

72
New cards

>>> "apple".find("y")

-1

73
New cards

>>> import string

>>> string.punctuation

?

'!"#$%&\'()*+,-./:;

74
New cards

lst = ["a","b","c"]

lst.split("b")

AttributeError

75
New cards

min(s)

for string a list

lowest character according to ascii

for list it will give you smallest string

76
New cards

min(["a",0,"0"])

TypeError: '

77
New cards

>>> min(["a","A"])

'A'

78
New cards

it is faster for python to preform addition verse multiplication

true

79
New cards

python automatically closes files on your behave

true

80
New cards

don't forget to _______ the files

close

81
New cards

how to close a file

file.close()

82
New cards

a txt file contains the numbers 0 through 4 vertically on separate lines

f = open("numbers.txt",'r')

>>> f.readline()

1)

>>> f.readline()

2)

1) '0\n'

2) '1\n'

83
New cards

Difference between write and append mode?

Both utilize the _______ method.

write clears the file while append adds to it

write

84
New cards

cursor ________ as you read

moves

85
New cards

remember that newlines appear as _______ in a string

\n

86
New cards

examples of white space

tab

space

newline

87
New cards

values for dictionaries could be _______

repeated

88
New cards

dictionary (is/is not) a sequence

is not