# 1. Is the following assignment statement valid or invalid? # If it is invalid, why? #72 = amount # 2. What will the following code display? val = 99 print('The value is', 'val') # 3. Look at the following assignment statements value1 = 99 value2 = 45.9 value3 = 7.0 value4 = 7 value5 = 'abc' value6 = value1 + value2 value7 = 'value1' + 'value2' value8 = 14 / 5 value9 = 14 // 5 value10 = 14 % 5 value11 = 10 * 3 value12 = '10' * 3 value13 = 10 ** 3 # value14 = '10' ** 3 # what is wrong? # After these statement execute, # what is the Python data type and values # referenced by each variable? print(type(value1), value1) print(type(value2), value2) print(type(value3), value3) print(type(value4), value4) print(type(value5), value5) print(type(value6), value6) print(type(value7), value7) print(type(value8), value8) print(type(value9), value9) print(type(value10), value10) print(type(value11), value11) print(type(value12), value12) print(type(value13), value13) # print(type(value14), value14) # What is wrong? # 4. What will be displayed by the following program? my_value = 99 print(type(my_value), my_value) my_value = 'my_value' print(type(my_value), my_value) my_value = 99.0 print(type(my_value), my_value) print(int(my_value)) print(str(my_value)) print(float(str(my_value))) # print(int(str(my_value))) # What is wrong? # 5. What would the following code print? def compare_things(a, z): if 'z' < 'a': print('z is less than a.') else: print('z is not less than a.') compare_things(5, 3) # 6. What would the following code print? def compare_things2(a, z): if z < a: print(z, "is less than", a) else: print(z, "is not less than", a) compare_things2(5, 3) # 7. What would the following code print? # If there are errors, what are the errors? # How should the code be fixed? # def compare_things3(a, z): # if z < a: # print(z + " is less than " + a) # else: # print(z + " is not less than " + a) # # compare_things3(5, 3) # TypeError: unsupported operand type(s) for +: 'int' and 'str' # To fix, cost a and z to string type # def compare_things3(a, z): # if z < a: # print(str(z) + " is less than " + str(a)) # else: # print(str(z) + " is not less than " + str(a)) # # compare_things3(5, 3) # 8. What would the following code print? # If there are errors, what are the errors? # How should the code be fixed? def compare_things4(a, z): if z < a: print(z, "is less than", a) else: print(z, "is not less than", a) compare_things4('5', '3') # print("a = " + a + " and z = " + z) # What is wrong? # a and z are local variables # To fix, make them of global scope, use the "global" keyword # def compare_things4(): # global a # global z # if z < a: # print(z, "is less than", a) # else: # print(z, "is not less than", a) # # a = '5' # z = '3' # compare_things4() # print("a = " + a + " and z = " + z) # 9. What would the following code print? def compare_things5(): global a global z z = 13 if z < a: print(z, "is less than", a) else: print(z, "is not less than", a) a = 5 z = 3 compare_things5() print("a = " + str(a) + " and z = " + str(z)) # 10. Write a function that return the size of a given word. # For example, if the word is 'hello', the function # should return 5 def get_length(word): return len(word) print(get_length('hello')) # 11. What does the following code print a = False if not a: print('not a') else: print('otherwise') # good problem solving, function, and local and global practice # but probably too much to ask for exam1 # 12. Write a function that return a factorial of a given number def factorial(number): global result if number > 0: result = result * number number -= 1 factorial(number) return result result = 1 # why should result be initialized to 1 print(factorial(3)) result = 1 # reset it. why? print(factorial(4)) result = 1 print(factorial(5))