python print only even numbers in a list

Print Only Even Numbers in a List in Python

In this tutorial, we will learn how to print only the even numbers in a list using Python. We will start by understanding what lists are and how they work in Python. Then, we will move on to the concept of even numbers and how we can identify them using the modulo operator. Finally, we will combine these concepts to create a program that prints only the even numbers in a given list. By the end of this tutorial, you will have a clear understanding of how to filter out specific elements from a list in Python.

Quick Refresher on Lists in Python

Lists are one of the most commonly used data structures in Python. They are used to store an ordered collection of items, which can be of different data types such as integers, strings, or even other lists. Lists are mutable, which means that you can change their contents by adding, removing, or modifying elements.

To create a list in Python, you can use square brackets [] and separate the elements with commas. For example, to create a list of integers, you can do:

# create a list 
ls = [1, 2, 3, 4, 5]
print(ls)

Output:

[1, 2, 3, 4, 5]

You can access individual elements of a list by using their index, which starts at 0 for the first element. For example, to access the first element of the list above, you can do:

# access the first element
print(ls[0])

Output:

1

You can also use negative indexing to access elements from the end of the list. For example, to access the last element of the list above, you can do:

# access the last element
print(ls[-1])

Output:

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 Find Data Science Programs 👨‍💻 111,889 already enrolled

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

5

Lists in Python come with a number of useful built-in functions such as append(), insert(), remove(), etc. that help you perform common list operations.

That’s a brief overview of what lists are and how they work in Python. Lists are a powerful and versatile data structure that you’ll use frequently in your Python programs.

What are even numbers and how to identify them in Python?

Even numbers are integers that are divisible by 2. That is, when divided by 2, these numbers do not leave any remainder. You can also think of even numbers as numbers that are some multiple of 2. For example, 2, 4, 8, 32, 100, etc.

In Python, we can easily identify even numbers by using the modulo % operator. The modulo operator returns the remainder resulting from the division of two numbers. Let’s look at an example.

print(5 % 2)

Output:

1

Here, we use the modulo operator to get the remainder when 5 is divided by 2. You can see that we get the remainder as 1, which is the correct answer.

Using this knowledge, we can easily write a simple program to check whether a number is even or odd in Python.

num = 12
# check if the number is even or odd
if num % 2 == 0:
    print("The number is even")
else:
    print("The number is odd")

Output:

The number is even

Python program to print all the even numbers in a List

Now that we know what lists are and how to identify even numbers in Python, we can easily write a program that prints all the even numbers in a list.

The idea is that we iterate through the list and check whether each number is even or not. If it is an even number, we print the number.

# create a list 
ls = [1, 2, 3, 4, 5, 6, 7]
# print all the even numbers in the above list
for num in ls:
    if num % 2 == 0:
        print(num)

Output:

2
4
6

You can see that the above code prints the even numbers present in the list ls.

If you don’t want the numbers to be printed on separate lines, additionally pass end=' ' to the print() function. The end parameter in the print() function is used to specify what character or string should be printed at the end of the output. By default, the end parameter is set to '\n', which means that a newline character is printed at the end of the output which here we are changing to a single space character.

# create a list 
ls = [1, 2, 3, 4, 5, 6, 7]
# print all the even numbers in the above list
for num in ls:
    if num % 2 == 0:
        print(num, end=' ')

Output:

2 4 6 

The even numbers from the string are now printed on the same line with a single space between them.

Alternatively, you can use a list comprehension to get the even numbers from a list.

# get even numbers from a list
print([num for num in ls if num % 2 == 0])

Output:

[2, 4, 6]

You can see that we get the list of even numbers from the original list ls using just a single line of code which is concise with the help of a list comprehension.

Conclusion

In conclusion, printing only even numbers in a list in Python is a simple task that can be accomplished using a for loop and an if statement. By iterating through each element in the list and checking if it is divisible by 2, we can easily identify and print only the even numbers. Additionally, we can use list comprehension to achieve the same result in a more concise and efficient manner. With these techniques, we can easily manipulate lists to extract the data we need and perform various operations on them.

You might also be interested in –

Author

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

Scroll to Top