List of even integers python hackerrank solution
PythonServer Side ProgrammingProgramming Show
In this article, we will learn about the solution and approach to solve the given problem statement. Problem statementGiven a list iterable, we need to print all the even numbers in the list. Here we will be discussing three approaches for the given problem statement. Approach 1 − Using enhanced for loopExamplelist1 = [11,23,45,23,64,22,11,24] # iteration for num in list1: # check if num % 2 == 0: print(num, end = " ")Output64 22 24Approach 2 − Using filter & lambda functionExamplelist1 = [11,23,45,23,64,22,11,24] # lambda exp. even_no = list(filter(lambda x: (x % 2 == 0), list1)) print("Even numbers in the list: ", even_no)OutputEven numbers : [64, 22, 24]Approach 3 − Using list comprehensionExamplelist1 = [11,23,45,23,64,22,11,24] #list comprehension even_nos = [num for num in list1 if num % 2 == 0] print("Even numbers : ", even_nos)OutputEven numbers : [64, 22, 24]ConclusionIn this article, we learned about the approach to print even numbers in the input list.
Updated on 04-Jul-2020 12:56:14
Write a Python program to print the even numbers from a given list. Sample Solution:- Python Code: Sample Output: Flowchart: The following tool visualize what the computer is doing step-by-step as it executes the said program: Python Code Editor: Have another way to solve this solution? Contribute your code (and comments) through Disqus. Previous: Write a Python function that takes a number as a parameter and check the number is prime or not.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
Share this Tutorial / Exercise on : Facebook and Twitter
Check the Type of an Object: >>> def check_type(number): ... if type(number) == int: ... print('do something with an int') ... if isinstance(number, (int, float)): ... print('do something with an int or float') ... >>> check_type(5) do something with an int do something with an int or float >>> check_type(4.2) do something with an int or float
Given a list of numbers, write a Python program to print all even numbers in the given list. Example: Input: list1 = [2, 7, 5, 64, 14] Output: [2, 64, 14] Input: list2 = [12, 14, 95, 3] Output: [12, 14] Method 1: Using for loopIterate each element in the list using for loop and check if num % 2 == 0. If the condition satisfies, then only print the number.
Output: 10, 4, 66Method 2: Using while loop
Output: 10, 4, 66Method 3: Using list comprehension
Output: Even numbers in the list: [10, 4, 66]Method 4: Using lambda expressions
Output: Even numbers in the list: [10, 4, 66]Method 5: Using Recursion
Output Even numbers in the list: 10 4 66
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. Question: Write a program which first defines a function evens(n). The function should take an integer as the argument and return a list of n even integers starting with 2 using the range function which takes a third argument that is the "step". Rewrite the function using the third argument in the range. What I have so far: def evens(n): evensLst = [] for i in range(1,n+1): evensLst.append(2*i) return evensLst for i in range(1,n+1): evensLst.append(3*i) return evensLst n = raw_input("Enter an integer: ") print "Evens: ", evens(n) |