• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Create List of Odd Numbers in Range with Python

Create List of Odd Numbers in Range with Python

February 11, 2022 Leave a Comment

To create a list with all odd numbers in a range using Python, we can use the range() function in a custom Python function.

def listOfOddNumbers(a,b):
    if a % 2 == 0:
        a = a + 1
    odds = list(range(a,b,2))
    return odds

print(listOfOddNumbers(1,13))
print(listOfOddNumbers(2,10))

#Output:
[1, 3, 5, 7, 9, 11]
[3, 5, 7, 9]

You can also define a loop to get a list of odd numbers in a range using Python.

def listOfOddNumbers(a,b):
    odds = []
    if a % 2 == 0:
        a = a + 1
    for x in range(a, b, 2):
        odds.append(x)
    return odds

print(listOfOddNumbers(1,13))
print(listOfOddNumbers(2,10))

#Output:
[1, 3, 5, 7, 9, 11]
[3, 5, 7, 9]

When working with numbers in a Python program, it’s possible you only want to work with the odd numbers in a range.

With Python, we can obtain a list of odd numbers easily with the Python range() function.

The range() function takes in 3 arguments. The first is the starting point, the second is the ending point, and the third argument is the step size.

For example, if I want all the numbers between 0 and 10, excluding 10, I’d call the range function in the following way.

numbers_between_0_and_10 = list(range(0,10))

To get just the odds, we start with an odd number, and then step by 2 until the end of the range.

To create a list with all odd numbers in a range using Python, we can use the range() function in a custom Python function.

First, we need to check if the starting point is even or odd, and then we can create the range.

def listOfOddNumbers(a,b):
    if a % 2 == 0:
        a = a + 1
    odds = list(range(a,b,2))
    return odds

print(listOfOddNumbers(1,13))
print(listOfOddNumbers(2,10))

#Output:
[1, 3, 5, 7, 9, 11]
[3, 5, 7, 9]

The Python range() function is includes of the first input, but excludes the second input. To make our function inclusive of the second input, we can add a little logic and get the desired result.

def listOfOddNumbers(a,b, include):
    if a % 2 == 0:
        a = a + 1
    if include:
        b = b + 1
    odds = list(range(a,b,2))
    return odds

print(listOfOddNumbers(1,13, True))

#Output:
[1, 3, 5, 7, 9, 11, 13]

If you prefer using a loop to see how the list is being created, below shows how to make a list of odd numbers in Python with a loop.

def listOfOddNumbers(a,b):
    odds = []
    if a % 2 == 0:
        a = a + 1
    for x in range(a, b, 2):
        odds.append(x)
    return odds

print(listOfOddNumbers(1,13))
print(listOfOddNumbers(2,10))

#Output:
[1, 3, 5, 7, 9, 11]
[3, 5, 7, 9]

Creating a List of Even Numbers in a Range Using Python

We can easily take our function for creating a list of odd numbers in a range in Python and create a list of even numbers.

The only difference in our Python function is that we will instead check if the first number is odd, then we will make it even.

Below is a Python function to create a list of even numbers in a range.

def listOfEvenNumbers(a,b):
    if a % 2 == 1:
        a = a + 1
    odds = list(range(a,b,2))
    return odds

print(listOfEvenNumbers(1,13))
print(listOfEvenNumbers(2,10))

#Output:
[2, 4, 6, 8, 10, 12]
[2, 4, 6, 8]

Hopefully this article has been useful for you to learn how to get a list of odd numbers in a range using Python.

Other Articles You'll Also Like:

  • 1.  Sign Function in Python – Get Sign of Number
  • 2.  Using Python to Add String to List
  • 3.  Calculate Sum of Dictionary Values in Python
  • 4.  Perform Reverse Dictionary Lookup in Python
  • 5.  Get Username in Python using os module
  • 6.  Convert String to List Using Python
  • 7.  Python if else do nothing – Using pass Statement to Do Nothing in Python
  • 8.  Using Python to Add Items to Set
  • 9.  How to Write Excel File to AWS S3 Bucket Using Python
  • 10.  How to Multiply Two Numbers in Python

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy