range() is a built-in function of the Python language. We use it when we need to perform an action any specific number of times. range() in Python(3.x) is just a retitled version of a function known as xrange in Python(2.x). The range() function is useful for generating a sequence of some numbers. We will study range function in detail.

range function () has a common use for looping hence, knowledge of the same is the key aspect when dealing with any type of Python code. The most common and popular use of the range() function in Python is to iterate sequence type (List, string, etc.) with â€˜forâ€™ and â€˜whileâ€™ loop.

**Python range() Basics:**

In simple words, range() permits the user to create a series of numbers within a set range. Depending on the number of the arguments user is passing to that function, the user can decide where that series of numbers will start and end as well as how much the difference will be there between one number and the next one. range() takes mostly 3 arguments, which are as follows:

- start: integer beginning from which the sequence of integers is to be reverted
- stop: integer before which the arrangement of integers is to be reverted.
- The range of integers closes at stop â€“ 1.
- step: integer value which defines the increment between each integer in the order

# Python Program to # show range() basics # printing a number for i in range(20): print(i, end =" ") print() # using range for iteration l = [20, 30, 40, 50] for i in range(len(l)): print(l[i], end =" ") print() # performing sum of natural # number sum = 0 for i in range(1, 11): sum = sum + i print("Sum of first 10 natural number :", sum)

**Output :**

0 1 2 3 4 5 6 7 8 9 10 20 30 40 Sum of first 10 natural number : 55 There are three ways you can call range(), these are as follows: range(stop) takes 1 argument. range(start, stop) takes 2 arguments. range(start, stop, step) takes 3 arguments.

**range(stop)**

When we call range() with 1 argument, we will get a sequence of numbers that starts at â€˜0â€™ and comprises every whole number up to but not containing, the number that we have delivered as the stop.

**range(start, stop)**

When we call range() with 2 arguments, we get to choose not only where the sequence of the numbers stops but also where it will start from, so we donâ€™t have to start at â€˜0â€™ every single time. We can use range() to make a series of numbers from â€˜Xâ€™ to â€˜Yâ€™ with the use of a range(X, Y).

**range(start, stop, step)**

When we call range() with 3 arguments, we can select not only where the sequence of the numbers will start from and stop at but also how vast the difference will be there between one number and the next one.

**Using the**

**float Numbers**

**in the Python range():**

Python range() function doesnâ€™t support the float numbers. i.e. we cannot use the floating-point or the non-integer number in any of its arguments. We can only use integer numbers.

**Browse more Topics UnderÂ Conditional Constructs and Looping**

- If Else Statement While
- Break
- Continue
- Else
- Pass
- Nested if
- Nested Loops
- Use of Compound Expression in Conditional and Looping Construct

**FAQ on For Range Function**

**Question 1:** What actions do a range () function performs?

**Answer 1:** The range() function reverts a series of numbers, beginning from â€˜0â€™ by default, and increases by â€˜1â€™, and stops before a stated number.

**Question 2:** What is the range actually for?

**Answer 2:** The Range is actually the difference between the lowermost and uppermost values. For example: In {4, 5, 9, 3, 8} the lowest value is â€˜3â€™, and the highest value is â€˜9â€™. So the range will be 9 âˆ’ 3 = â€˜6â€™.

## Leave a Reply