In this tutorial, we will learn how to print only the odd 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 odd 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 odd 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:
# 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])
- 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
- 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
- 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.
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 odd numbers and how to identify them in Python?
Odd numbers are integers that are not divisible by 2. That is, when divided by 2, these numbers leave a non-zero remainder. You can also think of odd numbers as integers that are not some multiple of 2. For example, 1, 3, 5, 13, 19, 123, etc.
In Python, we can easily identify odd 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 = 13 # 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 odd
Python program to print all the odd numbers in a List
Now that we know what lists are and how to identify odd numbers in Python, we can easily write a program that prints all the odd numbers in a list.
The idea is that we iterate through the list and check whether each number is odd or not. If it is an odd number, we print the number.
# create a list ls = [1, 2, 3, 4, 5, 6, 7] # print all the odd numbers in the above list for num in ls: if num % 2 != 0: print(num)
1 3 5 7
You can see that the above code prints the odd 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 odd numbers in the above list for num in ls: if num % 2 != 0: print(num, end=' ')
1 3 5 7
The odd 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 odd numbers from a list.
# get odd numbers from a list print([num for num in ls if num % 2 != 0])
[1, 3, 5, 7]
You can see that we get the list of odd 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 the odd 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 not divisible by 2, we can easily identify and print only the odd 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 –