Try   HackMD

CS 1358 Introduction to Programming in Python SC5

tags: pythonSC

Definitions and Short Answers - functions

  1. What is the data type of (1, 2, 3)?

    tuple

  2. If s = 'ABCDE', what is the value of
    ​​​​s[0]
    ​​​​s[1]
    ​​​​s[-1] 
    ​​​​s[1:4]
    ​​​​s[-5:-2]
    ​​​​s[:2]
    ​​​​s[-3:]
    ​​​​s[:]
    ​​​​s[0:0]
    ​​​​s[1:4:2]
    ​​​​s[-1:0:-1]
    
    ​​​​'A'
    ​​​​'B'
    ​​​​'E'
    ​​​​'BCD'
    ​​​​'ABC'
    ​​​​'AB'
    ​​​​'CDE'
    ​​​​'ABCDE'
    ​​​​''
    ​​​​'BD'
    ​​​​'EDCB'
    
  3. If S = ['h', 'e', 'l', 'l', 'o'], what is the value of S after executing the statement S[1:2] = ['a']?
    ​​​​['h', 'a', 'l', 'o']
    
  4. If T = ('h', 'e', 'l', 'l', 'o'), which of the following is allowed or not allowed and why?
    ​​​​T[3] = 'z'  #not allowed, tuple is immutable
    ​​​​T = ('w', 'o', 'r', 'l', 'd')
    ​​​​T = T[2:-1] 
    
  5. What is the value of
    ​​​​list('apple')
    ​​​​tuple('apple')
    ​​​​set('apple')
    
    ​​​​['a', 'p', 'p', 'l', 'e']
    ​​​​('a', 'p', 'p', 'l', 'e')
    ​​​​{'p', 'e', 'l', 'a'}
    
  6. What is the value of
    ​​​​str(['a', 'p', 'p', 'l', 'e'])
    ​​​​str(('a', 'p', 'p', 'l', 'e'))
    ​​​​str({'a', 'p', 'p', 'l', 'e'})
    
    ​​​​"['a', 'p', 'p', 'l', 'e']"
    ​​​​"('a', 'p', 'p', 'l', 'e')"
    ​​​​"{'p', 'e', 'l', 'a'}"
    
  7. What is the value of
    ​​​​list(('a', 'p', 'p', 'l', 'e'))
    ​​​​tuple(['a', 'p', 'p', 'l', 'e'])
    ​​​​set(['a', 'p', 'p', 'l', 'e'])
    
    ​​​​['a', 'p', 'p', 'l', 'e']
    ​​​​('a', 'p', 'p', 'l', 'e')
    ​​​​{'p', 'e', 'l', 'a'}
    
  8. What is the result of
    ​​​​'Apple' < 'apple'        #True
    ​​​​'Apple' <= 'apple'       #True
    ​​​​'Apple' == 'apple'       #False
    ​​​​'Apple' >= 'apple'       #False
    ​​​​'Apple' > 'apple'        #False
    ​​​​'Apple' != 'apple'       #True
    
  9. What is the result of
    ​​​​'Apple' < 'adventure'        #True
    ​​​​'apple' < 'adventure'        #False
    ​​​​'apple' < 'Adventure'        #False
    ​​​​'apple' < 'bee'              #True
    ​​​​'apple' < 'Bee'              #False
    ​​​​'Apple' < 'bee'              #True
    ​​​​'Apple' < 'Bee'              #True
    
  10. What is the result of
    ​​​​('apple', 0) < ('apple', 2)            #True
    ​​​​('apple', 0, 3) < ('apple', 1)         #True
    ​​​​['apple', 2, 2] < ['apple', 2, 1, 5]   #False
    ​​​​['apple', 3] < ['oranges', 0]          #True
    
  11. What is the result of
    ​​​​'s' in 'school'        #True
    ​​​​'hoo' in 'school'      #True
    ​​​​'S' in 'school'        #True
    ​​​​'ol' in 'school'       #False
    ​​​​'k' not in 'school'    #True
    ​​​​'s' not in 'School'    #True
    
  12. What is the result of
    ​​​​'s' in ['s', 'c', 'h', 'o', 'o', 'l']                        #True
    ​​​​['s'] in ['s', 'c', 'h', 'o', 'o', 'l']                      #False
    ​​​​['s'] in [['s'], ['c'], ['h'], ['o'], ['o'], ['l']]          #True
    ​​​​'hoo' in ['s', 'c', 'h', 'o', 'o', 'l']                      #False
    ​​​​['h', 'o', 'o'] in ['s', 'c', 'h', 'o', 'o', 'l']            #False
    ​​​​('h', 'o', 'o') in ['s', 'c', ('h', 'o', 'o'), 'l']          #True
    ​​​​('h', 'o', 'o') not in ('s', 'c', ('h', 'o', 'o'), 'l')      #False
    ​​​​'ol' in ['s', 'c', 'h', 'o', 'ol']                           #True
    ​​​​'s' in ['S', 'c', 'h', 'o', 'o', 'l']                        #False
    
  13. What is the result of
    ​​​​'sch' + 'ool'
    ​​​​[1, 2, 3] + [4, 5, 6]
    ​​​​(1, 2, 3) + (4, 5, 6)
    
    ​​​​'school'
    ​​​​[1, 2, 3, 4, 5, 6]
    ​​​​(1, 2, 3, 4, 5, 6)
    
  14. What is the result of
    ​​​​'sch' + 'o' * 10 + 'l'
    ​​​​'do' * 5
    ​​​​['s'] + ['o'] * 5 + ['l']
    
    ​​​​'schooooooooool'
    ​​​​'dododododo'
    ​​​​['s', 'o', 'o', 'o', 'o', 'o', 'l']
    
  15. How do you express a tuple literal of a single element? For example, how do you write a tuple literal that has the same value as tuple([1])?

    (1, )

  16. Suppose you have x = 1, 2, 3
    What is the value of type(x)?

    tuple

  17. Suppose you have L = ['f', 'r', 'o', 'g']
    What is the new value of L after executing each of the following statements in order?
    ​​​​L.append('s')
    ​​​​L.extend(['p', 'o', 'n', 'd'])
    ​​​​L.insert(4, ' ')
    ​​​​L.reverse()
    ​​​​L.sort()
    ​​​​L.remove('o')
    ​​​​L.pop()
    ​​​​L.pop(0)
    ​​​​L.clear()
    ​​​​L.append('z')
    
    ​​​​['f', 'r', 'o', 'g', 's']
    ​​​​['f', 'r', 'o', 'g', 's', 'p', 'o', 'n', 'd']
    ​​​​['f', 'r', 'o', 'g', ' ', 's', 'p', 'o', 'n', 'd']
    ​​​​['d', 'n', 'o', 'p', 's', ' ', 'g', 'o', 'r', 'f']
    ​​​​[' ', 'd', 'f', 'g', 'n', 'o', 'o', 'p', 'r', 's']
    ​​​​[' ', 'd', 'f', 'g', 'n', 'o', 'p', 'r', 's']
    ​​​​'s'
    ​​​​' '
    ​​​​[]
    ​​​​['z']
    
  18. If T = (1, 3, 5, 7, 9, 11), Can you call del(T[1])? why or why not? Can you call del(T)? What is the effect?

    no, T is tuple, and tuple is immutable.
    yes, del T

  19. Suppose L = list('hello') and separately M = list('hello'). After executing
    ​​​​L.reverse() 
    ​​​​M = M[::-1]
    
    is L == M evaluate to True or False?

    True
    What is the difference between these two ways of reversing elements in a list?
    One is mutation , another is immutation.

  20. if T = tuple('hello'), are the following statements allowed in Python? Why or why not?
    ​​​​T.reverse()      #not allowed, tuple is immutable
    ​​​​T = T[::-1]
    
  21. What is a stack as a data structure? What is another name (4-letter initialism) for a stack? How can a stack be implemented using a list? Show how push and pop can be accomplished by calling list methods.

    Yes, FIFO

    ​​​​L.append()    #push
    ​​​​L.pop()       #pop
    
  22. What is a queue as a data structure? What is another name (4-letter initialism) for a queue? How can a queue be implemented using a list? Show how enqueue and dequeue can be accomplished by calling list methods.

    Yes, FILO

    ​​​​L.append()    #push
    ​​​​L.pop(0)      #pop
    
  23. Show how a tuple can be used to implement
    a stack's push and pop functionality
    ​​​​t = t[:] + element    #push
    ​​​​t = t[:-1]            #pop
    
    a queue's enqueue and dequeue functionality
    ​​​​t = t[:] + element    #push
    ​​​​t = t[1:]            #pop
    
  24. Is a tuple more or less efficient than a list for implementing the stack and queue data structures? Why?

    It's less efficient, because tuple is immutable so it need to create another new tuple every time you pop and push.

  25. What do these built-in functions do?
    ​​​​max(['h', 'e', 'l', 'l', 'o'])
    ​​​​min('hello')
    ​​​​sum([2, 3, 4, 5, 6])
    ​​​​sum(range(10))
    ​​​​any(['', 'apples', 'oranges', 'banana'])
    ​​​​any([0, '', 0.0, [], ()])
    ​​​​any(['0', '', 0.0, [], ()])
    ​​​​any([0, ' ', 0.0, [], ()])
    ​​​​all(['', 'apples', 'oranges', 'banana'])
    ​​​​all([' ', 'apples', 'oranges', 'bananas'])
    ​​​​all([0, '', 0.0, [], ()])
    
    ​​​​'o'
    ​​​​'e'
    ​​​​20
    ​​​​55
    ​​​​True
    ​​​​False
    ​​​​True
    ​​​​True
    ​​​​False
    ​​​​True
    ​​​​False
    
  26. What is the non-mutation version of the following statements? Assume L is a list
    ​​​​L.sort()
    ​​​​L.reverse()
    ​​​​L.extend([1, 2, 3])
    ​​​​del(L[1])
    ​​​​L.pop()
    
    ​​​​sorted(L)
    ​​​​reversed(L)
    ​​​​L = L[:] + [1, 2, 3]
    ​​​​L = L[0] + L[2:]
    ​​​​L = L[:-1]
    
  27. How do you use list comprehension to create a list with values
    ​​​​['*', '**', '***', '****', '*****']
    ​​​​[1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096]
    
    ​​​​['*'*i for i in range(1, 6)]
    ​​​​[2 ** i for i in range(13)]
    
  28. How do you use two-level list comprehension to create a multiplication table in the following format:
    ​​​​[(1, 1, 1), (1, 2, 2), (1, 3, 3), … (1, 9, 9), (2, 1, 2), (2, 2, 4), (2, 3, 6), (2, 4, 8), … (2, 9, 18), (3, 1, 3), (3, 2, 6), (3, 3, 9), … (3, 9, 27), (4, 1, 4) … (4, 9, 36), (5, 1, 5), … (9, 9, 81)]
    
    ​​​​[(i, j, i*j) for i in range(1, 10) for j in range(1, 10)]
    
  29. How do you use list comprehension with filter to generate the list of upper-case letters except 'A', 'E', 'I', 'O', 'U'?
[chr(i) for i in range(65, 65+26) if chr(i) not in ['A', 'E', 'I', 'O', 'U']]
  1. After executing the following sequence of statements:

    ​​​​x = 3
    ​​​​y = x
    ​​​​x = 4
    

    what is the value of y?

    ​​​​3
    
  2. After executing the following sequence of statements

    ​​​​x = [1, 2, 3]
    ​​​​y = x
    ​​​​x = [4, 5, 6]
    

    what is the value of y?

    ​​​​[4, 5, 6]
    
  3. After executing the following sequence of statements

    ​​​​x = [1, 2, 3]
    ​​​​y = x
    ​​​​x[1] = 4
    

    what is the value of y?

    ​​​​[1, 4, 3]
    
  4. After executing the following sequence of statements

    ​​​​x = [1, 2, 3]
    ​​​​y = x[:]
    ​​​​x[1] = 4
    

    what is the value of y?

    ​​​​[1, 2, 3]
    
  5. After executing the following sequence of statements

    ​​​​x = [1, 2, 3]
    ​​​​y = x
    ​​​​y[:] = [4, 5, 6]
    

    what is the value of x?

    ​​​​[4, 5, 6]
    
  6. After executing the following sequence of statements

    ​​​​z = ['a', 'b']
    ​​​​x = [1, z, 3]
    ​​​​z.append('c')
    

    what is the value of x?

    ​​​​[1, ['a', 'b', 'c'], 3]
    
  7. After executing the following sequence of statements

    ​​​​z = ['a', 'b']
    ​​​​x = [1, z, 3]
    ​​​​y = x
    ​​​​z.append('c')
    

    what is the value of y?

    ​​​​[1, ['a', 'b', 'c'], 3]
    
  8. After executing the following sequence of statements

    ​​​​z = ['a', 'b']
    ​​​​x = [1, z, 3]
    ​​​​y = x[:]
    ​​​​z.append('c')
    

    what is the value of y?

    ​​​​[1, ['a', 'b', 'c'], 3]
    
  9. After executing the following sequence of statements

    ​​​​z = ['a', 'b']
    ​​​​x = [1, z, 3]
    ​​​​y = x[:]
    ​​​​x[0] = 4
    ​​​​z.append('c')
    

    what is the value of y?

    ​​​​[1, ['a', 'b', 'c'], 3]
    
  10. After executing the following sequence of statements

    ​​​​import copy
    ​​​​z = ['a', 'b']
    ​​​​x = [1, z, 3]
    ​​​​y = copy.copy(x)
    ​​​​x[0] = 4
    ​​​​z.append('c')
    

    what is the value of y?

    ​​​​[1, ['a', 'b', 'c'], 3]
    
  11. After executing the following sequence of statements

    ​​​​import copy
    ​​​​z = ['a', 'b']
    ​​​​x = [1, z, 3]
    ​​​​y = copy.deepcopy(x)
    ​​​​x[0] = 4
    ​​​​z.append('c')
    

    what is the value of y?

    ​​​​[1, ['a', 'b'], 3]
    
  12. What is the type of {}?

    set

  13. What is the expression for an empty set?

    set()

  14. Which of the following can or cannot be a member of a set? Why?

    ​​​​'hello'
    ​​​​23
    ​​​​44.27
    ​​​​5e-3
    ​​​​2+4j
    ​​​​['Mary', 'had', 'a', 'little', 'lamb']        #not allowed, list is mutable.
    ​​​​('Mary', 'had', 'a', 'little', 'lamb')
    ​​​​{'Mary', 'had', 'a', 'little', 'lamb'}        #not allowed, set is mutable.
    ​​​​{'Sun': 0, 'Mon': 1, 'Tue': 2, 'Wed': 3}
    ​​​​True
    ​​​​False
    ​​​​()
    ​​​​[]                                            #not allowed, list is mutable.
    ​​​​{}                                            #not allowed, set is mutable.
    
  15. What is the value of len(set('hello'))?

    ​​​​4
    
  16. What is the value of each of the following expressions?

    ​​​​{1, 2} - {2, 3}
    ​​​​{1, 2} | {2, 3}
    ​​​​{1, 2} & {2, 3}
    ​​​​{1, 2} ^ {2, 3}
    
    ​​​​{1}
    ​​​​{1, 2, 3}
    ​​​​{2}
    ​​​​{1, 3}
    
  17. What is the result of the following comparisons?

    ​​​​{1, 2, 3} > {2, 3}
    ​​​​{1, 2, 3} < {1, 2, 4}
    ​​​​{1, 2, 2, 3} == {1, 2, 3}
    ​​​​{1, 2, 4} != {4, 2, 1}
    
    ​​​​True
    ​​​​False
    ​​​​True
    ​​​​False
    
  18. Assume S = 1, 2, 3}, what is the difference between
    S = S {3, 4
    and S |= {3, 4}?

    one is mutation another is non-mutation

  19. Assume D = {'Sun': 0, 'Mon': 1, 'Tue': 2, 'Wed': 3}
    What is the value of D['Mon']?

    ​​​​1
    

    What is the value of D after D['Thu'] = 4?

    ​​​​{'Sun': 0, 'Mon': 1, 'Tue': 2, 'Wed': 3, 'Thu':4}
    

    Continuing with the previous statement, what is the value of D after D['Sun'] = 7?

    ​​​​{'Sun': 7, 'Mon': 1, 'Tue': 2, 'Wed': 3, 'Thu':4}
    

    What happens if you attempt print(D['Fri'])?

    ​​​​KeyError: 'Fri'
    
  20. Assume D = {'Sun': 0, 'Mon': 1, 'Tue': 2, 'Wed': 3}
    What is the value of D.keys()

    ​​​​dict_keys(['Sun', 'Mon', 'Tue', 'Wed'])
    

    What is the value of D.values()

    ​​​​dict_values([0, 1, 2, 3])
    

    What is the value of D.items()

    ​​​​dict_items([('Sun', 0), ('Mon', 1), ('Tue', 2), ('Wed', 3)])
    
  21. Assuming D = {}, which of the following is legal or not legal in Python? If not legal, why not?

D[()] = 10
D[''] = {}
D[0] = ''
D[{}] = ()        #illegal, key must be immutable.(set)
D[[]] = set()     #illegal, key must be immutable.(list)
D[:] = range(10)  #illegal, key must be immutable.(slice)
D[-1] = [-1]
D[(())] = [{}]
  1. How do you use dictionary comprehension to create a reverse mapping? For example, suppose
    ​​​​D = {'Sun': 0, 'Mon': 1, 'Tue': 2, 'Wed': 3, 'Thu': 4, 'Fri': 5, 'Sat': 6}
    
    create its reverse mapping whose value should be
    ​​​​{0: 'Sun', 1: 'Mon', 2: 'Tue', 3: 'Wed', 4: 'Thu', 5: 'Fri', 6: 'Sat'}
    
    ​​​​{v : i for i, v in D.items()}