Chapter 7 Exercises

  1. Complete lines 1 and 2 below to create the code to add up all the numbers from 1 to 5 and print the sum.

  2. Fix the errors in the code so that it prints the sum of all the numbers 1 to 10.

  3. Fill in the missing values in the function sum_list so that it returns the sum of all the numbers in the parameter num_list. Create a test list and call sum_list passing it your list.

  4. Fill in the missing code on lines 3 and 4 to loop through the list of numbers and calculate the product. Add a line at the end to print the value in product.

  5. Fix the errors in the code so that it prints the product of every 5th number between 5 and 25, inclusive. So, the product of 5, 10, 15, …, 25. It should print the value 375000 if you have done everything correctly.

  6. Fix the errors in the code so that it prints the sum of all the odd numbers 1 through 20.

  7. Modify the code below to create a function that calculates the product of a list of numbers and returns it. Have the function take a list of numbers as a parameter. Call the function to test it and print the result of calling the function.

  8. Fix the error in the code so that it takes each string in the list and prints out the sentence “I like to eat pizza”.

  9. Fill in the code below on lines 2, 5, and 10 to correctly add up and print the sum of all the even numbers from 0 to 10 (inclusive).

  10. Write code that prints the square of each number 1 through 10 in the format “1 * 1 = 1”, etc.

  11. The code below sums the even numbers from 0 to 20 inclusive. Turn it into a function sum_evens(to_num) that calculates the sum of the even numbers from 0 to the value of to_num passed to the function. Return the sum from the function. Call the function inside print to see the result.

  12. The factorial of a positive integer n is the product of all the integers from n down to 1.

    Complete the function below that returns the factorial of an integer passed to it and print the result of calling the function.

  13. Fix the code below to correctly calculate and return the product of all of the even numbers from 10 to 20.

  14. Use your solution to the previous exerice to complete the function below to calculate and return the sum of all of the even numbers from from_num to to_num inclusive. Calling sum_evens(6, 10) should return 24. Print the result of calling the function to test it.

  15. Create a list of all odd numbers from 1 to 20 and find the average. Then create a list of numbers from 1 to 100 using the average as the increment and print the product of those numbers.

  16. Create a procedure to calculate and return the sum of all of the odd numbers from 1 to a passed last number (inclusive). Call the function to test and it print the result.

  17. Complete the code for a function that takes a list of letters and combines them into a word. It should print “Hi”.

  18. Create a function to calculate and return the product of all of the even numbers from 2 to the passed end number (inclusive). Be sure to call the function to test it and print the result.

  19. Write a function that takes two inputs, a start and stop for a range (inclusive). Find the product and the sum of all the numbers and return the average between those two numbers. make a call to the function where you print the result

  20. Write a function that will take a list of numbers and return the average. Remember that the average is the sum of all of the numbers in the list divided by the number of items in the list. You can get the length of a list using the len(list) function.

  21. Create a function named who_knows_why that takes one integer parameter, num, and gets a list of all the odd numbers in range(num) and all the even numbers in range(num), finds the product of all the odd numbers, the sum of all the even numbers, and then returns the difference of the product by the sum divided by the average of the two. Call the function and print the result.

Next Section - Chapter 8 - While and For Loops