Knowee
Questions
Features
Study Tools

Even numbersWrite a Python program to print the even numbers from a list.Sample Test CasesTest Case 1:Expected Output:Enter·the·number·of·elements:·2Enter·element:·23Enter·element:·24[24]Test Case 2:Expected Output:Enter·the·number·of·elements:·10Enter·element:·1Enter·element:·2Enter·element:·3Enter·element:·4Enter·element:·5Enter·element:·6Enter·element:·7Enter·element:·8Enter·element:·9Enter·element:·12[2,·4,·6,·8,·12]

Question

Even numbersWrite a Python program to print the even numbers from a list.Sample Test CasesTest Case 1:Expected Output:Enter·the·number·of·elements:·2Enter·element:·23Enter·element:·24[24]Test Case 2:Expected Output:Enter·the·number·of·elements:·10Enter·element:·1Enter·element:·2Enter·element:·3Enter·element:·4Enter·element:·5Enter·element:·6Enter·element:·7Enter·element:·8Enter·element:·9Enter·element:·12[2,·4,·6,·8,·12]

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

Sure, here is a step-by-step solution to print the even numbers from a list in Python:

  1. Start by asking the user to enter the number of elements in the list.
  2. Read the input and store it in a variable, let's call it num_elements.
  3. Create an empty list to store the elements, let's call it elements.
  4. Use a loop to iterate num_elements times and ask the user to enter each element.
  5. Read each element and append it to the elements list.
  6. Create an empty list to store the even numbers, let's call it even_numbers.
  7. Use a loop to iterate over each element in the elements list.
  8. Check if the element is even by using the modulo operator % with 2. If the remainder is 0, it is even.
  9. If the element is even, append it to the even_numbers list.
  10. Finally, print the even_numbers list.

Here is the Python code that implements the above steps:

num_elements = int(input("Enter the number of elements: "))
elements = []

for i in range(num_elements):
    element = int(input("Enter element: "))
    elements.append(element)

even_numbers = []

for element in elements:
    if element % 2 == 0:
        even_numbers.append(element)

print(even_numbers)

For the given test cases, the expected outputs would be:

Test Case 1: Enter the number of elements: 2 Enter element: 23 Enter element: 24 [24]

Test Case 2: Enter the number of elements: 10 Enter element: 1 Enter element: 2 Enter element: 3 Enter element: 4 Enter element: 5 Enter element: 6 Enter element: 7 Enter element: 8 Enter element: 9 Enter element: 12 [2, 4, 6, 8, 12]

This problem has been solved

Similar Questions

Write a Python program that prompts the user to input a list of integers separated by commas. Your task is to modify the list so that all elements become odd by incrementing each even element by 1, and then print the modified list.Input format:The input consists of a list of integers separated by commas.Output format:Output the modified list where all elements are odd.Sample Test CasesTest Case 1:Expected Output:1,3,5,7[1,·3,·5,·7]Test Case 2:Expected Output:2,1,5,8[3,·1,·5,·9]

list_of_strings = input().split(',')# 🚨 Do not change the code above# TODO: Use list comprehension to convert the strings to integers 👇:# TODO: Use list comprehension to filter out the odd numbers# and store the even numbers in a list called "result"

Write a program to print only the even numbers till given NInput FormatA single integer 'N'Output FormatDisplay all the even numbers between 0 and NSample Input10Sample Output0 2 4 6 8 10

Question 3: Given a list of integers, please create a new list where each element is half of the even numbers in the original list.numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Complete the function count_odd_even() , which receives number of elements and array as input. Function should count number of even numbers and odd numbers in the array and print it.Input Format:No Need to read any input, as it is predefined.Output Format:Print the count of odd and even as: Odd = ODD_COUNTEven = EVEN_COUNTConstraints:1<=size<=10^9 1<=arr[ind]<=10^15Sample Input 1:1047 41 87 88 95 46 35 66 63 93Sample Output 1:Odd = 7Even = 3Sample Input 2:81 2 3 4 5 6 7 8Sample Output 2:Odd = 4Even = 4

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.