Question

In: Computer Science

complete in python The function sumEven should return the sum of only the even numbers contained...

complete in python

The function sumEven should return the sum of only the even numbers contained in the list, lst.
Example
list_of_nums = [1, 5, 4, 8, 5, 3, 2]
x = sum_evens(list_of_nums)
print(x) #prints 14

Start your code with

def evens(lst):

Solutions

Expert Solution

Program:

# Function sum_evens that takes a list of numbers and returns the sum of evens in the list
def sum_evens(lst):
    # declare a variable to hold the sum
    sum = 0
    # run a loop to get each number in the list
    for i in lst:
        # if i is even add it to sum
        if (i % 2 == 0):
            sum = sum + i
    # return the sum of evens
    return sum

# declare a list of numbers
list_of_nums = [1, 5, 4, 8, 5, 3, 2]
# call the sum_evens function passing the list as parameter
x = sum_evens(list_of_nums)
# display the sum of evens
print("Sum of evens in the list: " + str(x)) #prints 14

      

Output:

Program Screenshot:


Related Solutions

Make a function that calculates the summation of even numbers in the range. The function sum...
Make a function that calculates the summation of even numbers in the range. The function sum takes the two integer parameters and they are used as the range. The function uses default parameters for the range. When we call this function with one argument, it will be used as a starting point and the end of the range will be 100. Also, the function is called without any argument, the default range (0,100) will be used. We will use default...
use Python The assertion that every even number is the sum of two prime numbers is...
use Python The assertion that every even number is the sum of two prime numbers is called Goldbach’s conjecture. You will write a program that asks the user for an integer number, then checks if the integer is even, and finally determines two prime numbers that sum to the user’s entered integer number. Assignment Requirements Three functions are required: get_input(): This function takes no parameters, but will ask the user for an integer number. It will return a valid integer....
Write a Python program that calls a function to sum all the numbers in a list...
Write a Python program that calls a function to sum all the numbers in a list and returns the result to the caller. The main program creates a list (with hard-coded or user input) and passes the list as an argument to the function. You may not use the built-in function, sum. The program calls a second function to multiply all the numbers in a list passed to it by main and returns the product back to the caller. List...
In java, Write a recursive function to calculate the sum of the nodes only on even...
In java, Write a recursive function to calculate the sum of the nodes only on even levels of the subtree. please do not add any parameters to do this function. private int sumEvenLevels(Node current){ //you can only pass in root. //code }
Write a function that takes two integer inputs and returns the sum of all even numbers...
Write a function that takes two integer inputs and returns the sum of all even numbers between these inputs, and another function that takes two integer inputs and returns the sum of odd numbers between these inputs .In main function, the program will asks the user to enter two integer numbers and then passes them to these two functions and display the result of each of them
Input 10 integers and display the following: a. the sum of even numbers. b. the sum...
Input 10 integers and display the following: a. the sum of even numbers. b. the sum of odd numbers. c. the largest integer d. the smallest integer e. the total count of even numbers f. the total count of odd numbers. Using C++ program with for loop..
PYTHON PLS 1) Create a function search_by_pos. This function only has one return statement. This function...
PYTHON PLS 1) Create a function search_by_pos. This function only has one return statement. This function returns a set statement that finds out the same position and same or higher skill number. This function searches the dictionary and returns the same position and same or higher skill level. The function output the set statements that include the position only. For example input : dict = {'Fiora': {'Top': 1, 'Mid': 4, 'Bottom': 3},'Olaf': {'Top': 3, 'Mid': 2, 'Support': 4},'Yasuo': {'Mid': 2,...
show that every function can be expressed as the sum of an even function and an...
show that every function can be expressed as the sum of an even function and an odd function and that there is only one way to do this.
(Python) Implement a function to compute a sum that can compute sum for an arbitrary number...
(Python) Implement a function to compute a sum that can compute sum for an arbitrary number of input integers or float numbers. In other words, calls like this should all work: flexible_sum(x1, x2) # sum of two integer or float numbers or strings x1 and x2 flexible_sum(x1, x2, x3) # sum of 3 input parameters and can also handle a single input parameter, returning it unchanged and with the same type, i.e.:   flexible_sum(1) returns 1 and can accept an arbitrary...
###### THIS SHOULD BE IN PYTHON Calculate the sum of cubes. If the input is x,...
###### THIS SHOULD BE IN PYTHON Calculate the sum of cubes. If the input is x, the sum of cubes is equal to: 13 + 23 + ... + x3 Take input for the number of cubes to sum. No error checking is needed. Specifically, you may assume that the input is always a positive integer Here are some sample runs: Enter how many cubes to sum: 1    1    Enter how many cubes to sum: 3 36 Enter...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT