Ch. 5 Flashcards

strings, lists, files

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]

A

spam!

17
Q

s1 = “spam”
s2 = “ni!”

s1.upper()

A

SPAM

18
Q

s1 = “spam”
s2 = “ni!”

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

A

NI! NI! NI!

19
Q

s1 = “spam”
s2 = “ni!”

spam

A

s1

20
Q

s1 = “spam”
s2 = “ni!”

NI!

A

s2[0:2].upper()

21
Q

s1 = “spam”
s2 = “ni!”

ni!spamni!

A

s2 + s1 + s2

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
Q

“Hello {0}”.format(“Susan”, “Computewell”)

A

‘Hello Susan’

26
Q

“{0:0.2f} {0:0.2f}”.format(2.3, 2.3468)

A

‘2.30 2.30’

27
Q

“{7.5f} {7.5f}”.format(2.3, 2.3468)

A

not legal bc no index followed

28
Q

“Time left {0:02}:{1:05.2f}”.format(1, 37.374)

A

‘Time left 1:37.37’

29
Q

“{1:3}”.format(“14”)

A

‘14’

30
Q

Explain why public key encryption is more useful for securing communications on the Internet then private (shared) key encryption.

A

private keys use the same key for locking and unlocking, while public keys use one to lock and a different key to unlock

31
Q

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

A

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
Q

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

A

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
Q

write a program that calculates the numeric value of a single name provided as input

A

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