# 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 ```python 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] ``` ```python 'A' 'B' 'E' 'BCD' 'ABC' 'AB' 'CDE' 'ABCDE' '' 'BD' 'EDCB' ``` 2. If S = ['h', 'e', 'l', 'l', 'o'], what is the value of S after executing the statement S[1:2] = ['a']? ```python ['h', 'a', 'l', 'o'] ``` 3. If T = ('h', 'e', 'l', 'l', 'o'), which of the following is allowed or not allowed and why? ```python T[3] = 'z' #not allowed, tuple is immutable T = ('w', 'o', 'r', 'l', 'd') T = T[2:-1] ``` 4. What is the value of ```python list('apple') tuple('apple') set('apple') ``` ```python ['a', 'p', 'p', 'l', 'e'] ('a', 'p', 'p', 'l', 'e') {'p', 'e', 'l', 'a'} ``` 5. What is the value of ```python str(['a', 'p', 'p', 'l', 'e']) str(('a', 'p', 'p', 'l', 'e')) str({'a', 'p', 'p', 'l', 'e'}) ``` ```python "['a', 'p', 'p', 'l', 'e']" "('a', 'p', 'p', 'l', 'e')" "{'p', 'e', 'l', 'a'}" ``` 6. What is the value of ```python list(('a', 'p', 'p', 'l', 'e')) tuple(['a', 'p', 'p', 'l', 'e']) set(['a', 'p', 'p', 'l', 'e']) ``` ```python ['a', 'p', 'p', 'l', 'e'] ('a', 'p', 'p', 'l', 'e') {'p', 'e', 'l', 'a'} ``` 7. What is the result of ```python 'Apple' < 'apple' #True 'Apple' <= 'apple' #True 'Apple' == 'apple' #False 'Apple' >= 'apple' #False 'Apple' > 'apple' #False 'Apple' != 'apple' #True ``` 8. What is the result of ```python '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 ```python ('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 ```python '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 ```python '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 ```python 'sch' + 'ool' [1, 2, 3] + [4, 5, 6] (1, 2, 3) + (4, 5, 6) ``` ```python 'school' [1, 2, 3, 4, 5, 6] (1, 2, 3, 4, 5, 6) ``` 14. What is the result of ```python 'sch' + 'o' * 10 + 'l' 'do' * 5 ['s'] + ['o'] * 5 + ['l'] ``` ```python '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? ```python 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') ``` ```python ['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 ```python 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? ```python 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 ```python 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 ```python L.append() #push L.pop(0) #pop ``` 23. Show how a tuple can be used to implement a stack's push and pop functionality ```python t = t[:] + element #push t = t[:-1] #pop ``` a queue's enqueue and dequeue functionality ```python 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? ```python 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, [], ()]) ``` ```python '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 ```python L.sort() L.reverse() L.extend([1, 2, 3]) del(L[1]) L.pop() ``` ```python 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 ```python ['*', '**', '***', '****', '*****'] [1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096] ``` ```python ['*'*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: ```python [(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)] ``` ```python [(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'? ```python [chr(i) for i in range(65, 65+26) if chr(i) not in ['A', 'E', 'I', 'O', 'U']] ``` 30. After executing the following sequence of statements: ```python x = 3 y = x x = 4 ``` what is the value of y? ```python 3 ``` 31. After executing the following sequence of statements ```python x = [1, 2, 3] y = x x = [4, 5, 6] ``` what is the value of y? ```python [4, 5, 6] ``` 32. After executing the following sequence of statements ```python x = [1, 2, 3] y = x x[1] = 4 ``` what is the value of y? ```python [1, 4, 3] ``` 33. After executing the following sequence of statements ```python x = [1, 2, 3] y = x[:] x[1] = 4 ``` what is the value of y? ```python [1, 2, 3] ``` 34. After executing the following sequence of statements ```python x = [1, 2, 3] y = x y[:] = [4, 5, 6] ``` what is the value of x? ```python [4, 5, 6] ``` 35. After executing the following sequence of statements ```python z = ['a', 'b'] x = [1, z, 3] z.append('c') ``` what is the value of x? ```python [1, ['a', 'b', 'c'], 3] ``` 36. After executing the following sequence of statements ```python z = ['a', 'b'] x = [1, z, 3] y = x z.append('c') ``` what is the value of y? ```python [1, ['a', 'b', 'c'], 3] ``` 37. After executing the following sequence of statements ```python z = ['a', 'b'] x = [1, z, 3] y = x[:] z.append('c') ``` what is the value of y? ```python [1, ['a', 'b', 'c'], 3] ``` 38. After executing the following sequence of statements ```python z = ['a', 'b'] x = [1, z, 3] y = x[:] x[0] = 4 z.append('c') ``` what is the value of y? ```python [1, ['a', 'b', 'c'], 3] ``` 39. After executing the following sequence of statements ```python 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? ```python [1, ['a', 'b', 'c'], 3] ``` 40. After executing the following sequence of statements ```python 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? ```python [1, ['a', 'b'], 3] ``` 41. What is the type of {}? > set 42. What is the expression for an empty set? > set() 43. Which of the following can or cannot be a member of a set? Why? ```python '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. ``` 44. What is the value of len(set('hello'))? ```python 4 ``` 45. What is the value of each of the following expressions? ```python {1, 2} - {2, 3} {1, 2} | {2, 3} {1, 2} & {2, 3} {1, 2} ^ {2, 3} ``` ```python {1} {1, 2, 3} {2} {1, 3} ``` 46. What is the result of the following comparisons? ```python {1, 2, 3} > {2, 3} {1, 2, 3} < {1, 2, 4} {1, 2, 2, 3} == {1, 2, 3} {1, 2, 4} != {4, 2, 1} ``` ```python True False True False ``` 47. 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 48. Assume D = {'Sun': 0, 'Mon': 1, 'Tue': 2, 'Wed': 3} What is the value of D['Mon']? ```python 1 ``` What is the value of D after D['Thu'] = 4? ```python {'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? ```python {'Sun': 7, 'Mon': 1, 'Tue': 2, 'Wed': 3, 'Thu':4} ``` What happens if you attempt print(D['Fri'])? ```console KeyError: 'Fri' ``` 49. Assume D = {'Sun': 0, 'Mon': 1, 'Tue': 2, 'Wed': 3} What is the value of D.keys() ```python dict_keys(['Sun', 'Mon', 'Tue', 'Wed']) ``` What is the value of D.values() ```python dict_values([0, 1, 2, 3]) ``` What is the value of D.items() ```python dict_items([('Sun', 0), ('Mon', 1), ('Tue', 2), ('Wed', 3)]) ``` 50. Assuming D = {}, which of the following is legal or not legal in Python? If not legal, why not? ```python 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[(())] = [{}] ``` 51. How do you use dictionary comprehension to create a reverse mapping? For example, suppose ```python D = {'Sun': 0, 'Mon': 1, 'Tue': 2, 'Wed': 3, 'Thu': 4, 'Fri': 5, 'Sat': 6} ``` create its reverse mapping whose value should be ```python {0: 'Sun', 1: 'Mon', 2: 'Tue', 3: 'Wed', 4: 'Thu', 5: 'Fri', 6: 'Sat'} ``` ```python {v : i for i, v in D.items()} ```