Question

1. Define a function in python that returns the sum of the following 4 lists. Remember...

1. Define a function in python that returns the sum of the following 4 lists. Remember to store and re- use your code instead of summing the values for each list 4 times. Hence, define a function that takes as an argument a list and returns the sum of values.

  • Copy the following lists and paste them in a Python file after you define the sum_of_values function

  • Call the function and pass to it a different list each call as an argument

    list1 = [5, 7, 4, 1, 8, 9, 56, 34, 65, 100]
    list2 = [1, 2, 5, 6, 8, 6, 0, 19, 8, 10, 11, 12]
    list3 = [1.1, 1.6, 7.8, 9.4, 6.7, 8.9, 99.7, 65.6, 45.5, 54.5, 12.7] list4 = [10.5, 100.5, 20.5]

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is code:

def sum_of_values(list):
sum = 0
for val in list:
sum = sum + val
return round(sum,1) # round value


list1 = [5, 7, 4, 1, 8, 9, 56, 34, 65, 100]
list2 = [1, 2, 5, 6, 8, 6, 0, 19, 8, 10, 11, 12]
list3 = [1.1, 1.6, 7.8, 9.4, 6.7, 8.9, 99.7, 65.6, 45.5, 54.5, 12.7]
list4 = [10.5, 100.5, 20.5]

print("The sum of list1 is : ", sum_of_values(list1))
print("The sum of list2 is : ", sum_of_values(list2))
print("The sum of list3 is : ", sum_of_values(list3))
print("The sum of list4 is : ", sum_of_values(list4))

Output:

Add a comment
Know the answer?
Add Answer to:
1. Define a function in python that returns the sum of the following 4 lists. Remember...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Python. Write a function swap_lists that takes in two lists and swap their elements in place....

    Python. Write a function swap_lists that takes in two lists and swap their elements in place. You can not use another list. You can not return lists. Input lists have the same size. def swap_lists(alist1, alist2): """Swaps content of two lists. Does not return anything. >>> list1 = [1, 2] >>> list2 = [3, 4] >>> swap_lists(list1, list2) >>> print(list1) [3, 4] >>> print(list2) [1, 2] >>> list1 = [4, 2, 6, 8, 90, 45] >>> list2 = [30, 41,...

  • in PYTHON! Define a function count() that compute and returns the sum of the first 100...

    in PYTHON! Define a function count() that compute and returns the sum of the first 100 even numbers.

  • Define a Python function that takes a list and returns the length of the list using...

    Define a Python function that takes a list and returns the length of the list using map and sum.

  • import java.util.Arrays; import stdlib.*; public class CSC300Homework4 {    /**    * As a model for...

    import java.util.Arrays; import stdlib.*; public class CSC300Homework4 {    /**    * As a model for Problem 1, here are two functions to find the minimum value of an array of ints    * an iterative version and a recursive version    *    * precondition: list is not empty    /** iterative version */    public static double minValueIterative (int[] list) {        int result = list[0];        int i = 1;        while (i <...

  • PYTHON 3 LANGUAGE , LINKED LISTS , define function ITERATIVELY Define an iterative function named alternate_i;...

    PYTHON 3 LANGUAGE , LINKED LISTS , define function ITERATIVELY Define an iterative function named alternate_i; it is passed two linked lists (ll1 and ll2) as arguments. It returns a reference to the front of a linked list that alternates the LNs from ll1 and ll2, starting with ll1; if either linked list becomes empty, the rest of the LNs come from the other linked list. So, all LNs in ll1 and ll2 appear in the returned result (in the...

  • 1. Write a function called ordinal_sum that accepts a string argument and returns the sum of...

    1. Write a function called ordinal_sum that accepts a string argument and returns the sum of the ordinal values of each character in the string. The ordinal value of a character is its numeric Unicode code point in decimal. 2. Write code that creates a Python set containing each unique character in a string named my_string. For example, if the string is 'hello', the set would be {'h', 'e', 'l', 'o'} (in any order). Assign the set to a variable...

  • PYTHON Define a function reverseNestedList(...) which receives one possibly nested list (lst1) and returns the reverse...

    PYTHON Define a function reverseNestedList(...) which receives one possibly nested list (lst1) and returns the reverse of both the given list and any nested lists inside it. Note that the testing inputs will not include lists that are nested more than twice (a list in a list in a list ++...) [[[...],1], 1] Good luck! As an example, the following code fragment: lst1 = [[1, 2], [3, 4]] print(reverseNestedList(lst1)) should produce the output: [[4, 3], [2, 1]]

  • package homework; import java.util.Arrays; import stdlib.*; /** CSC300Homework4 version 1.0 * *    * * Find...

    package homework; import java.util.Arrays; import stdlib.*; /** CSC300Homework4 version 1.0 * *    * * Find the 3 Sections marked TODO: * * TODO #1: SumOddsRecursive * TODO #2: ReverseArray * TODO #3: MergeArrays * * You must not add static variables. You MAY add static functions. * * It is okay to add functions, such as * * <pre> * public static double sumOfOddsHelper (double[] list, int i) { * </pre> * * but it is NOT okay to...

  • Python problem. 3. (6 pts) Define the following four sorting functions: each takes an argument that...

    Python problem. 3. (6 pts) Define the following four sorting functions: each takes an argument that is a list of int or str or both values (otherwise raise an AssertionError exception with an appropriate error message) that will be sorted in a different way. Your function bodies must contain exactly one assert statement followed by one return statement. In parts a-c, create no other extra/temporary lists other than the ones returned by calling sorted. a. (2 pts) Define the mixed...

  • With using Python . Write a function that: 1. Takes two arguments and returns the product...

    With using Python . Write a function that: 1. Takes two arguments and returns the product of the arguments. The return value should be of type integer. 2. Takes two arguments and returns the quotient of the arguments. The return value should be of type float. 3. Takes one argument and squares the argument. It then uses the two values and returns the product of the two arguments.(Reuse function in 1.) 4. Takes one argument and prints the argument. Use...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT