In Python, you can use the optional ‘step’ parameter to skip numbers in a range. If you are using a range object in a loop, the ‘step’ parameter will allow you to skip iterations.

```
print("skipping all odds in range with 'step' parameter")
print(list(range(0,20,2)))
#Output:
skipping all odds in range with 'step' parameter
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
```

When working with ranges of numbers in Python, the ability to skip numbers and step over certain numbers is valuable in certain cases.

The Python range() function returns a sequence of numbers given three parameters – a starting number, an ending number and a step size.

By default, the step size is 1, and so if you want to create a range and skip numbers, then you want to pass a different number than 1 to the ‘step’ parameter.

For example, if you want to count by 2’s, then you would pass 2 to the ‘step’ parameter. If you wanted to count by 3’s or any other number, you’d pass that number to ‘step’.

Below is an example showing how you can create a list of numbers and skip certain numbers in Python with the **range()** function.

```
print("skipping all odds in range with 'step' parameter")
print(list(range(0,20,2)))
#Output:
skipping all odds in range with 'step' parameter
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
```

## How to Skip Iterations in Loop with Range in Python

You can use the ‘step’ parameter to skip iterations when using loops in your Python code.

For example, let’s say you are trying to find all of the prime numbers in a range and want to create a prime number list.

By definition, there is only one prime number which is even (2). Therefore, if you want to create a program which is efficient and fast, you wouldn’t want to check the even numbers when looking for primes.

In this case, we would want to skip the evens and only look at odd numbers in our range.

To skip iterations in a loop using a Python range variable, you pass a number to ‘step’ which is the step size of the range variable.

Below is a simple example showing you how to skip in a loop in Python.

```
for i in range(0,8,2):
print(i)
#Output:
0
2
4
6
```

Hopefully this article has been useful for you to learn how to skip using a range object in your Python code.

## Leave a Reply