How to generate a sequence of numbers in Python

Generating a sequence of numbers results in a list of numbers within a range that each satisfy a certain requirement. For instance, the numbers between 1 and 10 that have a remainder of 1 or 2 when divided by 5 are [1, 2, 5, 7].

Solution for How to generate a sequence of numbers in Python : You can use range() to generate a sequence of numbers Call range(start, stop) to generate a sequence of numbers from start up to stop. Use a for-loop to iterate over each number in this sequence and use list.append(x) to append the number to an empty list if it meets a certain condition.

Use a list comprehension for a more compact implementation.

numbers = range(1, 10)


how-to-generate-a-sequence-of-numbers-in-python