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:
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 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 –