How is your Python knowledge? Expert or beginner, we could all use a refresher now and then.
If you need pointers, visit my Python 3 Notes.
Work on the questions below. Then, submit your answers via Canvas. They are automatically graded, so you will see which questions you got right/wrong.
MAKE SURE YOU TRY OUT THE CODE. Do not just read the questions and make a guess. Figuring out the correct answer is not the point of these assignments. YOU MUST TRY WRITING THE CODE YOURSELF, OTHERWISE YOU WON'T LEARN TO WRITE THEM. The process is akin to learning a new foreign language: you cannot learn how to speak by just listening.
Your designated script folder should be visible in the screenshot.
Your IDLE shell window should show the output of os.getcwd(), which is your CWD (current working directory).
Your CWD should ideally show your designated script folder. Your "Documents" folder is also fine.
Don't worry if you are having trouble, just upload a screenshot of your environment as is. I'll take a look and help you later.
Q2--Q4: Answer the questions about the IDLE shell session below.
>>> w1 = 'the'>>> w2 = 'cat'>>> w3 = 'is'>>> w4 = ① >>> print(w1, w2, w3, w4)
the cat is sleeping>>> print(w1 + w2)
② >>> print( ③ )
thecat issleeping>>> len(w2)
3>>> len(w2+w3)
④ >>> print(w2*3)
⑤ >>> print(w2+'3')
⑥ >>> print(w2+3)
⑦
What goes in ① and ②?
What goes in ③ that produces the shown result? Find all that do.
w1+w2 + w3+w4
w1, w2 + w3, w4
w1 + w2, w3 + w4
w1+w2+' '+w3+w4
What goes in ④, ⑤, ⑥ and ⑦? Match them with below.
5
6
cat3
catcatcat
an error message
Q5--Q8: Answer the questions below. Restart your IDLE shell (Menu --> Shell --> Restart Shell or Ctrl+F6) before trying out the commands.
>>>
=============================== RESTART: Shell ==================
>>> print(w1, w2)
① >>> print( ② )
' " """ are string delimiters.>>> mary = 'Mary had a\nlittle lamb.'>>> ③ Mary had a
little lamb.>>> mary
'Mary had a\nlittle lamb.'>>> mary2 = """Mary had a... little lamb.""">>> mary2
'Mary had a\nlittle lamb.'>>> mary == mary2
④
What goes in ①?
the cat
the
cat
an error message
What goes in ② that produces the shown result? Find all that do.
'' " """ are all string delimiters.'
'\' " """ are all string delimiters.'
"' \" """ are all string delimiters."
"' \" \"\"\" are all string delimiters."
"""' \" \"\"\" are all string delimiters."""
What goes in ③ that produces the shown result?
mary
print(mary)
print('mary')
What goes in ④?
nothing
an error message
>>>
True
False
Below is an example of string methods. Which of the following expressions fits in the blank, that is, which evaluates to True? Pick all that do.
>>> w1 = 'cat'>>> w2 = 'dog'>>> ?? True
w1.endswith('t') or w2.endswith('t')
w1.endswith('at') and not w2.endswith('at')
w1.startswith('cat')
w1 in 'scattered'
not 'ca' in w2
'a' in w1 and 'o' in w2
'' is an empty string: note that there is no space between the two 's. Which of the following expressions returns True? Pick all that do.
'' == ""
'cat'.startswith('')
''.endswith('')
'' in 'cat'
'cat' in ''
'' in ''
len('') == 0
You have the following variable assignment: sign = 'Please be quiet.' Match each expression with its return value.
sign.capitalize()
sign.upper()
sign.lower()
sign.title()
'PLEASE BE QUIET.'
'please be quiet.'
'Please Be Quiet.'
'Please be quiet.'
We are interested in counting how many times 'sh' occurs in the following string. What is the appropriate command?
>>> twister = 'she sells seashells by the seashore'>>> ?? 3
Below, the whitespace characters at either edge of the string are being removed. What is the method used here?
>>> name = ' Homer Simpson '>>> ?? 'Homer Simpson'
name.replace(' ', '')
strip(name)
name.rstrip()
name.lstrip()
name.strip()
What does 'Hello, world'.replace('l', 'r').replace('r', 'l') evaluate to?
'Hello, world'
'Herro, wolrd'
'Herro, worrd'
'Hello, wolld'
No output. An error occurs.
Below, we want to lowercase a string and then remove all spaces and punctuation. Which of the following code achieves this? Pick all.
Below, we want to split 'eeny, meeny, miny, moe' into a list of words without the commas. What is the exact .split() command that produces the desired output? (Note: don't use any other functions such as print() and .replace().)
Note that there is a comma and a space between the words. Splitting can be done on a multi-character string, e.g., 'hello'.split('ll').
Which of the following scripts produces the shown shell output?
>>>
========================= RESTART: foo.py =================
You gotta do better!Your score is 90>>>
a.
score = 90
if score > 85:
print('You gotta do better!')
if score > 50:
print('This is pretty bad.')
else:
print('You did well!')
print('Your score is', score)
foo.py
b.
score = 90
if score > 50:
print('You gotta do better!')
if score > 85:
print('You did well!')
else:
print('That is pretty bad.')
print('Your score is', score)
foo.py
c.
score = 90
if score > 85:
print('You did well!')
elif score > 50:
print('You gotta do better!')
else:
print('That is pretty bad.')
print('Your score is', score)
foo.py
d.
score = 90
if score > 50:
print('You gotta do better!')
elif score > 85:
print('You did well!')
else:
print('That is pretty bad.')
print('Your score is', score)
foo.py
Which comparison test for this if conditional produces the shown output? Pick all.
>>> if ?? :
... print('Success!')
... Success!>>>
9*11 <= 99
'alligator' > 'snake'
'dog'.upper() != 'DOG'
'x' in 'cat' or 'cat'.startswith('c')
'beekeeper'.count('e') == 5
Below are a script using input() and its execution. Which user input will generate the shown output?
myword = 'transcendentalist'
yourword = input('Give me a long word: ')
if len(myword) > len(yourword):
print('Sorry, your word is not long enough. Try again.')
elif len(myword) < len(yourword):
print('Good job!')
print(yourword, 'is longer than', myword)
else:
print('Good job!')
print(yourword, 'is just long enough.')
longword.py
>>>
========================= RESTART: longword.py =================
Give me a long word: ?? Good job! ?? is just long enough.>>>
acclimatization
unintellectualism
vicissitudinousness
protransubstantiation
Below, we have a list of integers. What is the output?
>>> li = [75, 80, 95, 55, 80]
>>> li[5]
??
80
[80]
IndexError: list index out of range
len() can be used on a list as well as a string. What's the output?
>>> thing = 'iPhone'>>> thing.upper()
'IPHONE'>>> thing
① >>> thing = thing.capitalize()
>>> thing
② >>>
String methods such as .upper(), .lower(), .replace(), etc. do not change the value of the string per se. Instead, 'iPhone'.upper()creates and returns a NEW string. The original string, as assigned to the variable thing, remains unchanged. in the second instance, however, thing.capitalize() returns a capitalized new string, and the variable thing gets re-assigned to this new value.
What is the printed output of the following code?
num = 6
while num > 0 :
num = num - 2
print(num)
foo.py
a.
6
4
2
0>>>
b.
6
4
2>>>
c.
4
2
0>>>
d.
4
2>>>
What goes in the blank?
>>> k = 'hello'>>> while len(k) <= 30:
... k += '-ahoy'... >>> print(k)
? >>>
What is the value of total after the script is executed?
total = 0
for i in [1,3,5,7]:
total += i
foo.py
The point here is that running the script actually does not show you the value of total: because there is no print statement included, upon running the script, the IDLE shell will quietly go back to the prompt. But it's simple to find out -- you can look up total's value by executing print(total) or just total at the prompt.
What should go in the blank? (Note: mary should be split into a list of words.)
>>> mary = 'Mary had a little lamb'>>> for w in ? :
... print(w, len(w))
... Mary 4had 3a 1little 6lamb 4>>>
What gets printed? (Note: '' is an empty string.)
>>> word = 'penguin'>>> new = ''>>> for x in word:
... new = new + x + x
...
>>> print(new)
? >>>