## Programming Assignment #4 ## do not remove the extra print statements print('Question 1') ################################################################# # QUESTION 1 # # # # Here is a list containing integers, floats, and strings. # # # # Using a for-loop, traverse each element of the list. Then, # # using an if statement (with elif and else statement, so you # # could call it an if-elif-else statement) to correspond to the # # three conditions and actions: # # # # 1. Directly print integers, one number per line # # # 2. Add floats together and print the sum outside the for-loop.# # You will need to define a variable "sum" before the for-loop # # to store the cumulative sum. You should print the sum # directly after the integers in #1, which should be printed as # you go through the for-loop # # # 3. Store (append) the strings into a new list and print the # # new list outside the for-loop as a list, taking up the last # line after the sum printed for in #2 ################################################################# ##add code for question 1 here data = ['This', 1.27, 'is', 1, 'a', 0.43, 'list', 2, 2.09, 0.21, 3] ## do not remove the extra print statements print('Question 2') ################################################################# # QUESTION 2 # # Creating a range with a while loop: # # # # How would you create a list that is the same as the following,# # but with using a while loop? Please define all necessary # # variables, and show code. Do this with a while loop and basic # # python only, and without using the range function. # # # # L = list(range(5,30,5)) # # # # Your answer should be a printed list (printed directly as a # # list on one line. ################################################################# ##add code for question 2 here ## do not remove the extra print statements print('Question 3') ################################################################# # QUESTION 3 # # # # Given the following list, count how many of the numbers in # # the list are equal to 2 using an if statement, and a counting # # variable. Your code should use a for-loop to traverse the list# # and check if a given value is equal to 2. If so, it should # # add 1 to a count variable. Print the count at the end. # # Your answer should therefore only contain one number, the # count of the number of 2s. # # ################################################################# ##add code for question 3 here L = [1,2,3,4,5,4,3,2,1,2,3,4,3,2,1,2] ## do not remove the extra print statements print('Question 4') ################################################################# # QUESTION 4 # # # # Loop through the following dictionary and print out the keys # # and values side-by-side. Each key/value pair should take up # # one line of text, with a space between the key and value. Your # Answer should therefore have four lines of text # # ################################################################# ##add code for question 4 here D = {'a':'b', 'c':'d', 'e':'f', 'g':'h'} ## do not remove the extra print statements print('Question 5') ################################################################# # QUESTION 5 # # # # For the following list, write an if-else-statement that # # contains an if-statement and an else condition. It should # # check if the number 44 is in the list, and print 'yes' if it # # is in the list, and print 'no' if not. # Next if the number 139 is in the list, print that number. # Your answer should therefore have 1 or 2 lines. The first line # will be 'yes' or 'no' and the second line if it is there will # be the number 139. # # # You don't need to print out the list, and you don't need to # # loop through the list to do this. It can be done in a # # relatively simple if-else-statement. # # # ################################################################# ##add code for question 5 here L = list(range(7,377,3))