ch 8 practice questions Flashcards

1
Q

How many times is the letter ‘r’ printed below?

s = “strawberry”
idx = 1
while idx < len(s):
print(s[idx])
idx = idx + 2

A

1

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

What is printed?

s = “Ball”
s[0] = “C”
print(s)

A

Nothing, error

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

what does .find() do?

A

searches for the position of one string within another

it can also take a second index as to where it should start looking
ex. .find(‘na’, 3)

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

what does the following print?

line = ‘Have a nice day’
print(line.lower().startswith(‘h’))

A

True

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

What is printed?

s = “let’s go blue!”
print(s.count(“e”) + s.count(“b”))

A

3

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

What is parsing?

A

looking into a string and finding a substring

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

what types are the following used for?
1. %d
2. %g
3. %s

A

integer
floating point number
string

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

what does format operator % do?

A

allows us to construct strings, replacing parts of the strings with the data stored in variables; when operand is a string, % is the format operator

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

What is returned by the following?

str = “His shirt is red”
pos = str.find(“is”)
print(pos)

A

1; gives back 1st position of argument

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

What is printed by the following?

s1 = “heY”
s1 = s1.capitalize()

A

Hey
capitalize only uppercases the first letter

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

How do you know when ‘open’ is sucessful?

A

the system returns a file handle object, something that we can use to read data

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

What is a file handle?

A

an object used to read/ modify a file, not the actual data in the file

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

what is \n?

A

newline, breaks whatever ur typing into 2 lines
it counts as its own character

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

If you know the file is relatively small compared to the size of your main memory, you can read the whole file into one string using what?

A

read method; .read()

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

What is the difference between the 2 codes?

1)
fhand = open(‘mbox-short.txt’)
for line in fhand:
if line.startswith(‘From:’):
print(line)
fhand.close()

2)
fhand = open(‘mbox-short.txt’)
for line in fhand:
line = line.rstrip()
if line.startswith(‘From:’):
print(line)
fhand.close()

A

2) has line.strip(), which gets rid of the default newline in code 1

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

rstrip vs strip?

A

rstrip removes whitespace from the right side of the str, strip removes whitespace from left and right side of str

17
Q

when ‘find’ returns a negative value, what does this mean?

A

the value could not be found

18
Q

What parameters do you need for for the write function

A

filename and ‘w’

19
Q

what does the write method do?

A

handle object puts data into the file, returning the number of characters written

20
Q

why do you need the close functions?

A

to make sure that the last bit of data is physically written to the disk so it will not be lost if the power goes off

21
Q

what does repr do?

A

takes any object as an argument and returns a string representation of the object

ex.
s = ‘1 2\t 3\n 4’
print(repr(s))

‘1 2\t 3\n 4’

22
Q

What opening file format closes the file automatically when you leave the bock (indented area)?

A

with open(file) as name:

23
Q

What is printed by the following? Why

print(‘ “1958”‘.strip(‘”’))

A

“1985

strip() removes both leading and trailing whitespace. Since you didn’t add a space before the quotes, it won’t remove the leading “