# Chapter 7 Exercises¶

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

Set the sum to 0 in line 1. Create a list

`[1,2,3,4,5]`

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

On line 2, the range has to be (1,11) because the second number is not inclusive. Also, the sum has to be equal to the sum plus the new number. Otherwise the sum would just be the last number in the iteration.

Change the code below into a function that returns the sum of a list of numbers. Pass the list of numbers as a parameter and print the result of calling the function.

Create a function as shown below that takes a list of numbers as input. Return the sum. Call the function with a list of numbers from 1 to 5 and print the result to test the function.

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.

Product should be initialized to equal 1 on line 1. On line 2, it should be the range function, and it should be

`range(5,26,5)`

because the first number is inclusive and the second is exclusive.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`

.Change line 3 to create a variable

`number`

that will take on the next value in the list each time through the loop. Set`product`

in line 4 to`product * number`

. Print the result when the loop has finished.Fix the errors in the code so that it prints the sum of all the odd numbers 1 through 20.

In line 1, sum should be initialized to equal 0. In line 2, the third argument of the range function should be 2 because it is the step size.

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.

Define the function and create a parameter to take a list of numbers called

`numbers`

. Print the result of calling the function with a list of numbers.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”.

On line 4, aString should be equal to itself plus the new word.

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

Initialize the sum to 0. Create a range from 0 to 11 with a step of 2. Set the sum to the current value of sum plus the value of number.

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

You need to get a list of the numbers using

`range(1,11)`

and then iterate through the list and multiply each number by itself.Define a function to calculate the sum of the even numbers from 0 to the passed number. Return the sum from the function. Call the function and print the result.

Define a function that takes the

`lastNum`

as a parameter. Get a list of the even numbers between 0 and lastNum using`range(0,lastNum+1,2)`

. Return the sum. Call the function and print the result.Create a function that returns the factorial of a passed number and call the function and print the result.

Use the range function from 1 to n + 1 where n is the parameter given to get a list of the numbers. Then get the product of the product times the number.

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

Change line 2 to initialze

`product`

to 1 instead of 0. Change line 4 to`range(10,21,2)`

. Change line 8 to`product = product * number`

.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.

Use the range function with from 1 to 20 (21 would work too) and a step size of 2. Get the sum of all those numbers and use the length of the list to find the average. Then use the range function again with a step size of the average and find the product of all the numbers in the new list.

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.

Create the procedure and be sure to call it to test it.

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

On line 1, you have to put

`letterList`

as a parameter. On line 2,`tempString`

has to equal an empty string. Line 3 should read`for letter in letterList`

. Line 5 should return the`tempString`

and Line 8 should take aList as an argument.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.

Create the procedure and be sure to call it to test it.

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

The function should use the range from parameter 1 to parameter 2 + 1 in order to make it inclusive of the given stop argument. Don’t forget to initialize the product variable as 1 and the sum as 0.

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.Create the function and be sure to call it to test it.

Create a function that takes one integer parameter and gets a list of all the odd numbers in that range and all the even numbers in that range. Find the product of all the even numbers, the sum of all the odd numbers, and then return the difference of the product by the sum and divide by the average of the two. Call the function and print the result.

You have to use the range function twice and do two different for loops.