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)
[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:
Highlighted programs for you
University of Maryland Global Campus
University of Maryland Global Campus
# access the first element print(ls)
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])
Lists in Python come with a number of useful built-in functions such as
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)
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")
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)
2 4 6
You can see that the above code prints the even numbers present in the list
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=' ')
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])
[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.
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 –