Ch. 5 Flashcards

strings, lists, files (34 cards)

1
Q

accessing a single character out of a string is called

A

indexing

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

which of the following is same as [0:-1]

A

s[:len(s)-1 ]

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

what function gives the unicode value of a character?

A

ord

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

which of the following can not be used to convert a string of digits into a number

A

eval

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

a successor to ASCII that includes characters from (nearly) all written languages is

A

unicode

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

Which string method converts all the characters of a string to upper case?

A

upper

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

The string “slots” that are filled in by the format method are marked by:

A

{}

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

which of the following is not a file-reading method in python

A

readall

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

the term for a program that does its input and output with files is

A

batch

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

Before reading or writing to a file, a file object must be created via:

A

open

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

s1 = “spam”
s2 = “ni!”

“The Knights who say, “ + s2

A

the knights who say ni!

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

s1 = “spam”
s2 = “ni!”

3 * s1 + 2 * s2

A

spamspamspamni!ni!

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

s1 = “spam”
s2 = “ni!”

s1[1]

A

p

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

s1 = “spam”
s2 = “ni!”

s1[1:3]

A

pa

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

s1 = “spam”
s2 = “ni!”

s1[2] + s2[:2]

A

ani

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

s1 = “spam”
s2 = “ni!”

s1 + s2[-1]

17
Q

s1 = “spam”
s2 = “ni!”

s1.upper()

18
Q

s1 = “spam”
s2 = “ni!”

s2.upper().ljust(4) * 3

19
Q

s1 = “spam”
s2 = “ni!”

spam

20
Q

s1 = “spam”
s2 = “ni!”

NI!

A

s2[0:2].upper()

21
Q

s1 = “spam”
s2 = “ni!”

ni!spamni!

22
Q

s1 = “spam”
s2 = “ni!”

spm

A

s1[0:2] + s1[-1]

23
Q

“Looks like (1) and (0) for breakfast”.format(“eggs”, “spam”)

A

not legal bc curly braces are required around 1 and 0

24
Q

“There is {0} {1} {2} {3}”.format(1, “spam”, 4 “you”)

A

‘There is 1 spam 4 you.’

25
"Hello {0}".format("Susan", "Computewell")
'Hello Susan'
26
"{0:0.2f} {0:0.2f}".format(2.3, 2.3468)
'2.30 2.30'
27
"{7.5f} {7.5f}".format(2.3, 2.3468)
not legal bc no index followed
28
"Time left {0:02}:{1:05.2f}".format(1, 37.374)
'Time left 1:37.37'
29
"{1:3}".format("14")
'14'
30
Explain why public key encryption is more useful for securing communications on the Internet then private (shared) key encryption.
private keys use the same key for locking and unlocking, while public keys use one to lock and a different key to unlock
31
a certain CS professor gives 5 point quizzes that are graded on the scale 5-A, 4-B, 3-C, 2-D, 1-F, 0-F. Write a program that accepts a quiz score as an input and prints out the corresponding grade
lettergradescale = "FFDCBA" quizgrade = int(input("Please enter the quiz grade on a scale from 0 to 5: ")) lettergrade = lettergradescale[quizgrade] print("Your quiz grade of {0} corrsesponds to the letter grade of {1}.".format(quizgrade, lettergrade))
32
write a program that allows the user to type in a phrase and then outputs the acronym for that phrase. Note: the acronym should be all uppercase, even if the words in the phrase are not capitalized
def get_acronym(phrase): words = phrase.split() acronym = ''.join(word[0].upper() for word in words) return acronym input_phrase = input("Enter a phrase to generate an acronym: ") acronym = get_acronym(input_phrase) print("The acronym for '{0}' is '{1}'.".format(input_phrase, acronym))
33
write a program that calculates the numeric value of a single name provided as input
def calculate_numeric_value(name): name = name.upper() numeric_value = 0 for letter in name: if letter.isalpha(): numeric_value += ord(letter) - ord('A') + 1 return numeric_value input_name = input("Enter a name to calculate its numeric value: ") result = calculate_numeric_value(input_name) print("The numeric value of '{0}' is {1}.".format(input_name, result))
34