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.

## Leave a Reply