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
In Python, Complete the longestWord() function to take a list as a parameter and return the...
In Python, Complete the longestWord() function to take a list as a parameter and return the length of the longest word in that list. Examples: longestWord(['Python', 'rocks']) returns 5 longestWord(['Casey', 'Riley', 'Jessie', 'Jackie', 'Jaime', 'Kerry', 'Jody']) returns 6 longestWord(['I', 'a', 'am', 'an', 'as', 'at', 'ax', 'the']) returns 3
In Python Complete the function powersOf5(). Have the function take a parameter n and return a...
In Python Complete the function powersOf5(). Have the function take a parameter n and return a list of the first n powers of 5. One way to calculate powers of 5 is by starting with 1 and then multiplying the previous number by 5. Examples: powersOf5( 1 ) returns [1] powersOf5( 2 ) returns [1, 5] powersOf5( 3 ) returns [1, 5, 25] powersOf5( 6 ) returns [1, 5, 25, 125, 625, 3125] powersOf5( 10 ) returns [1, 5, 25,...
Using Python 3, define mySum function that supposed to return the sum of a list of...
Using Python 3, define mySum function that supposed to return the sum of a list of numbers (and 0 if that list is empty), but it has one or more errors in it. Write test cases to determine what errors there are.
Write a function that removes all even numbers from an array. The function should take the...
Write a function that removes all even numbers from an array. The function should take the array, length of the array, and a pointer for number of odd numbers found as arguments and return a pointer to the new array. If there are no odd numbers found in the array, your code should print "No odds found." and return NULL. Use the function header: int *removeEvens(int *a, int length, int *oddsFound); Example: Input array a[ ] = {3, 4, 5,...
Write a Python function which finds the smallest even and odd numbers in a given list....
Write a Python function which finds the smallest even and odd numbers in a given list. (Use for loop and if conditions for this problem)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT