Program to find even numbers and appending it into a new list

This blog contain program that finds out even number from first list and stored all even elements into a new list

Create a number list

num = [1,2,3,4,5,6,7,8,9,0]

Create a empty list which will store all even elements

even_num = []

Use for loop to access each element from first list and checking whether the current element is even or odd using modulus operator. Appending even element in second list

for n in num:
if n%2 == 0:
even_num.append(n)

Printing both lists

print('Original Array: ',num)
print('Only even values: ',even_num)

Output

Original Array:  [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
Only even values:  [2, 4, 6, 8, 0]

Complete Code

num = [1,2,3,4,5,6,7,8,9,0]
even_num = []
for n in num:
    if n%2 == 0:
        even_num.append(n)

print('Original Array: ',num)
print('Only even values: ',even_num)
Advertisement

Leave a Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s