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.
Fix the errors in the code so that it prints the sum of all the numbers 1 to 10.
Fill in the missing values in the function
sum_list
so that it returns the sum of all the numbers in the parameternum_list
. Create a test list and callsum_list
passing it your list.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
.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.Fix the errors in the code so that it prints the sum of all the odd numbers 1 through 20.
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.
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”.
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).
Write code that prints the square of each number 1 through 10 in the format “1 * 1 = 1”, etc.
The code below sums the even numbers from
0
to20
inclusive. Turn it into a functionsum_evens(to_num)
that calculates the sum of the even numbers from0
to the value ofto_num
passed to the function. Return the sum from the function. Call the function insideprint
to see the result.The factorial of a positive integer
n
is the product of all the integers fromn
down to1
.Complete the function below that returns the factorial of an integer passed to it and print the result of calling the function.
Fix the code below to correctly calculate and return the product of all of the even numbers from 10 to 20.
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
toto_num
inclusive. Callingsum_evens(6, 10)
should return24
. Print the result of calling the function to test it.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.
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.
Complete the code for a function that takes a list of letters and combines them into a word. It should print “Hi”.
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.
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
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 a function named
who_knows_why
that takes one integer parameter,num
, and gets a list of all the odd numbers inrange(num)
and all the even numbers inrange(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.