Print Numbers in an Interval - Python Last Updated : 14 Apr, 2025 Comments Improve Suggest changes Like Article Like Report In this article, we are going to learn how to print numbers within a given interval in Python using simple loops, list comprehensions, and step-based ranges.For Example:Input : i = 2, j = 5Output : 2 3 4 5Input : i = 10, j = 20 , s = 2Output : 10 12 14 16 18 20Let’s explore different methods to print numbers between two values in Python.Print All Numbers Between Two ValuesThe simplest way to print numbers in an interval is by using a for loop with the range() function. Python i = 10 j = 20 for num in range(i, j + 1): print(num) Output10 11 12 13 14 15 16 17 18 19 20 Explanation:range(i, j + 1) includes the upper limit by adding +1.loop prints each number from i to j one by one.Print Only Even Numbers in an IntervalYou can modify the step of the range() function to skip numbers, such as printing only even numbers. Python i = 10 j = 20 if i % 2 == 0: for num in range(i, j + 1, 2): print(num) else: for num in range(i + 1, j + 1, 2): print(num) Output10 12 14 16 18 20 Explanation:code ensures that the loop starts with the first even number in the interval.range(..., ..., 2) increases the count by 2 to skip odd numbers.Generate Numbers Using List ComprehensionList comprehensions offer a short and elegant way to generate a list of numbers within a given range. Python i = 10 j = 20 a = [num for num in range(i, j + 1)] print(a) Output[10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] Explanation:[num for num in range(l, u + 1)] builds a list of numbers from l to u.final result is printed as a list.For in-depth knowledge of the methods used in this article, read: for-loops, list comprehension, range(), Python. Comment More infoAdvertise with us Next Article Print Numbers in an Interval - Python A aishwarya.27 Follow Improve Article Tags : Python Python Programs DSA Practice Tags : python Similar Reads Python end parameter in print() In Python, the print() function, commonly used for displaying output, by default ends each statement with a newline character (\n), but this behavior can be customized using the end parameter, which allows you to specify a different string (such as a space, comma, or hyphen) to be printed at the end 3 min read Python | sep parameter in print() The separator between the arguments to print() function in Python is space by default (softspace feature) , which can be modified and can be made to any character, integer or string as per our choice. The 'sep' parameter is used to achieve the same, it is found only in python 3.x or later. It is als 3 min read Iterate over a list in Python Python provides several ways to iterate over list. The simplest and the most common way to iterate over a list is to use a for loop. This method allows us to access each element in the list directly.Example: Print all elements in the list one by one using for loop.Pythona = [1, 3, 5, 7, 9] # On each 3 min read Access List Items in Python Accessing elements of a list is a common operation and can be done using different techniques. Below, we explore these methods in order of efficiency and their use cases. Indexing is the simplest and most direct way to access specific items in a list. Every item in a list has an index starting from 2 min read Python | sympy.Interval().contains() method With the help of method, we can check whether a value is present in the interval or not by using sympy.Interval().contains() method. Syntax : sympy.Interval(x, y).contains(value) Return : Return the boolean value True or False. Example #1 : In this example we can see that by using sympy.Interval().c 1 min read Like