Go to: LING 1330/2330 home page  

Exercise 1: Python Refresher Quiz

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.

  1. Upload a screenshot of your Python working environment. It should look like: Windows and Mac OS X. Make sure you've prepared your environment following this setup checklist.
    • 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)
    
  1. What goes in ① and ②?

  2. What goes in ③ that produces the shown result? Find all that do.
    1. w1+w2 + w3+w4
    2. w1, w2 + w3, w4
    3. w1 + w2, w3 + w4
    4. w1+w2+' '+w3+w4

  3. 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
    
  1. What goes in ①?
    1. the cat
    2. the
      cat
    3. an error message

  2. What goes in ② that produces the shown result? Find all that do.
    1. '' " """ are all string delimiters.'
    2. '\' " """ are all string delimiters.'
    3. "' \" """ are all string delimiters."
    4. "' \" \"\"\" are all string delimiters."
    5. """' \" \"\"\" are all string delimiters."""

  3. What goes in ③ that produces the shown result?
    1. mary
    2. print(mary)
    3. print('mary')

  4. What goes in ④?
    1. nothing
    2. an error message
    3. >>>
    4. True
    5. False

  5. 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   
    
    1. w1.endswith('t') or w2.endswith('t')
    2. w1.endswith('at') and not w2.endswith('at')
    3. w1.startswith('cat')
    4. w1 in 'scattered'
    5. not 'ca' in w2
    6. 'a' in w1 and 'o' in w2

  6. '' 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.
    1. '' == ""
    2. 'cat'.startswith('')
    3. ''.endswith('')
    4. '' in 'cat'
    5. 'cat' in ''
    6. '' in ''
    7. len('') == 0

  7. You have the following variable assignment: sign = 'Please be quiet.' Match each expression with its return value.
    1. sign.capitalize()
    2. sign.upper()
    3. sign.lower()
    4. sign.title()
    1. 'PLEASE BE QUIET.'
    2. 'please be quiet.'
    3. 'Please Be Quiet.'
    4. 'Please be quiet.'

  8. 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   
    

  9. Below, the whitespace characters at either edge of the string are being removed. What is the method used here?
     
    >>> name = '      Homer Simpson  '
    >>>           ??          
        'Homer Simpson'   
    
    1. name.replace(' ', '')
    2. strip(name)
    3. name.rstrip()
    4. name.lstrip()
    5. name.strip()

  10. What does 'Hello, world'.replace('l', 'r').replace('r', 'l') evaluate to?
    1. 'Hello, world'
    2. 'Herro, wolrd'
    3. 'Herro, worrd'
    4. 'Hello, wolld'
    5. No output. An error occurs.

  11. Below, we want to lowercase a string and then remove all spaces and punctuation. Which of the following code achieves this? Pick all.
     
    >>> phrase = "It's a GIRL!"
    >>>                      ??                     
        'itsagirl'   
    
    1. phrase.lower().remove("'",'!',' ')
    2. lower(pharase).replace("'! ", '')
    3. phrase.lower().replace("'! ", '')
    4. phrase.lower().replace("'", '').replace('!', '').replace(' ', '')
    5. phrase.lower().replace("'", '').replace('!', '').strip()

  12. .split() is a string method that splits the caller string into a list of substrings. What is the result of the .split() command below?
     
    >>> 'colorless       green ideas sleep furiously'.split()
                                ??                        
    
    1. 'colorless green ideas sleep furiously'
    2. ['colorless       green', 'ideas', 'sleep', 'furiously']
    3. ['colorless', '      ', 'green', 'ideas', 'sleep', 'furiously']
    4. ['colorless', 'green', 'ideas', 'sleep', 'furiously']

  13. What is the return value of the following .split() command?
     
    >>> 'green'.split('e')
                  ??          
    
    1. ['gr', 'n']
    2. ['gr', '', 'n']
    3. ['gr', 'ee', 'n']
    4. ['gr', 'e', 'e', 'n']

  14. What is the command that produces the shown output?
     
    >>> animals = ['cat', 'dog', 'fox', 'owl'] 
    >>>           ??          
        'cat-a-dog-a-fox-a-owl'   
    
    1. animals.join('-a-')
    2. animals.join('a', '-')
    3. join(animals, '-a-')
    4. '-a-'.join(animals)

  15. 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().)
     
    >>> rhyme = 'eeny, meeny, miny, moe'
    >>>            ??           
        ['eeny', 'meeny', 'miny', 'moe']   
    

  16. 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 
    

  17. Which comparison test for this if conditional produces the shown output? Pick all.
     
    >>> if           ??           :   
    ...     print('Success!')
    ... 
        Success!
    >>> 
    
    1. 9*11 <= 99
    2. 'alligator' > 'snake'
    3. 'dog'.upper() != 'DOG'
    4. 'x' in 'cat' or 'cat'.startswith('c')
    5. 'beekeeper'.count('e') == 5

  18. 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.
    >>> 
    
    1. acclimatization
    2. unintellectualism
    3. vicissitudinousness
    4. protransubstantiation

  19. Below, we have a list of integers. What is the output?
     
    >>> li = [75, 80, 95, 55, 80] 
    >>> li[5]
                  ??             
    
    1. 80
    2. [80]
    3. IndexError: list index out of range

  20. len() can be used on a list as well as a string. What's the output?
     
    >>> subjects = ['anthropology', 'economics', 'linguistics', 'biology', 'computer science']
    >>> len(subjects)
        5   
    >>> subjects[2]
        'linguistics'   
    >>> len(subjects[2])
                  ??          
    

  21. What goes in the two blanks?
     
    >>> thing = 'iPhone'
    >>> thing.upper() 
        'IPHONE' 
    >>> thing
        
    >>> thing = thing.capitalize()
    >>> thing
        
    >>> 
    

  22. 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
    >>> 
    

  23. What goes in the blank?
     
    >>> k = 'hello'
    >>> while len(k) <= 30:
    ...     k += '-ahoy'
    ... 
    >>> print(k)
                  ?          
    >>> 
    

  24. What is the value of total after the script is executed?
    total = 0 
    for i in [1,3,5,7]:
        total += i
    foo.py 
    

  25. 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 4 
        had 3
        a 1
        little 6
        lamb 4
    >>> 
    

  26. What gets printed? (Note: '' is an empty string.)
     
    >>> word = 'penguin'
    >>> new = ''
    >>> for x in word:
    ...     new = new + x + x
    ...	
    >>> print(new)
                  ?          
    >>>