Important Notice:

The range() Function

The range() Function

18 views 4 min read

The range() Function :-

Python में range() function का उपयोग एक sequence of numbers (संख्याओं का क्रम) generate करने के लिए किया जाता है। इसका उपयोग विशेष रूप से for loop के साथ किसी statement को निश्चित संख्या में repeat करने के लिए किया जाता है।

range() function द्वारा बनाई गई sequence में stop value शामिल नहीं होती

Python में range() का उपयोग करते समय तीन parameters दिए जा सकते हैं: start, stop और step

English

The range() function in Python is used to generate a sequence of numbers. It is commonly used with a for loop to repeat a statement a specific number of times.

The stop value is not included in the sequence generated by range().

The range() function can use three parameters: start, stop, and step.

Syntax:-
 
range(start, stop, step)
 
Parameters:
 
start -  जहाँ से sequence शुरू होगी
stop -  जहाँ तक sequence जाएगी, लेकिन यह value शामिल नहीं होगी
step - प्रत्येक value के बीच का अंतर
 

इसमें एक, दो या तीन arguments दिए जा सकते हैं:

  • range(stop) 0 से stop-1 तक संख्याएँ generate करता है।
  • range(start, stop) start से stop-1 तक संख्याएँ generate करता है।
  • range(start, stop, step) start से stop-1 तक संख्याएँ generate करता है और प्रत्येक संख्या में step के अनुसार वृद्धि करता है।
 English
  • range(stop) generates numbers from 0 to stop-1.

  • range(start, stop) generates numbers from start to stop-1.

  • range(start, stop, step) generates numbers from start to stop-1, incrementing by step.

 
1. range(stop) :-

जब केवल एक value दी जाती है, तो उसे stop value माना जाता है। Sequence 0 से शुरू होती है और stop - 1 तक जाती है।

When only one value is given, it is treated as the stop value. The sequence starts from 0 and goes up to stop - 1.

Syntax:-
 
range(stop)
 
Example:
for i in range(5):
    print(i)
 
Output:
0
1
2
3
4
 
Because:
range(5) → 0, 1, 2, 3, 4

यहाँ 5 शामिल नहीं है।

Here, 5 is not included.

2. range(start, stop) :-

जब दो values दी जाती हैं, तो पहली value start और दूसरी value stop होती है। Sequence start से शुरू होती है और stop से पहले समाप्त होती है।

When two values are given, the first value is start and the second value is stop. The sequence starts from start and ends before stop.

Syntax:-
 
range(start, stop)
 
Example:
for i in range(2, 6):
    print(i)
 
Output:
2
3
4
5
 
Because:
range(2, 6) → 2, 3, 4, 5

यहाँ 2 included है लेकिन 6 included नहीं है।

Here, 2 is included, but 6 is not included.

3. range(start, stop, step) :-

जब तीन values दी जाती हैं, तो उनका उपयोग start, stop और step के रूप में किया जाता है।

When three values are given, they are used as start, stop, and step.

start → Sequence कहाँ से शुरू होगी। (The value from which the sequence starts.)
 
stop → Sequence कहाँ तक जाएगी; यह value शामिल नहीं होती। (The value before which the sequence ends; it is not included.)
 
step → प्रत्येक बार value कितनी बढ़ेगी या घटेगी। (The amount by which the value increases or decreases.)
 
Syntax:-
range(start, stop, step)
 
Example:
for i in range(1, 10, 2):
    print(i)
 
Output:
1
3
5
7
9
 
Because:
range(1, 10, 2)

यहाँ:

Start = 1
Stop  = 10
Step  = 2

इसलिए values इस प्रकार generate होंगी (Therefore, the values will be generated as) :

1 → 3 → 5 → 7 → 9

10 stop value है, इसलिए यह output में शामिल नहीं होगी।

10 is the stop value, so it is not included in the output.

4. Negative Step :-

range() में negative step का उपयोग numbers को reverse order (उल्टे क्रम) में generate करने के लिए किया जाता है।

A negative step is used with range() to generate numbers in reverse order.

Example:
 
for i in range(10, 0, -2):
    print(i)
 
Output:
10
8
6
4
2
 
Because:
range(10, 0, -2)

यहाँ:

Start = 10
Stop  = 0
Step  = -2

इसलिए(Therefore) :-
10 → 8 → 6 → 4 → 2

0 stop value है, इसलिए 0 output में शामिल नहीं होगा।

0 is the stop value, so 0 is not included in the output.

5. Counting in Reverse Order :-

range() function की सहायता से reverse counting (उल्टी गिनती) भी की जा सकती है।

The range() function can also be used for reverse counting.

Example:
for i in range(5, 0, -1):
    print(i)
 
Output:
5
4
3
2
1
 
Because:
range(5, 0, -1)

यहाँ:

Start = 5
Stop  = 0
Step  = -1


इसलिए:
5 → 4 → 3 → 2 → 1
यहाँ 0 stop value है, इसलिए 0 output में शामिल नहीं होगा।

Here, 0 is the stop value, so 0 is not included in the output.

6. range() with list() :-

range() से generated values को list में देखने के लिए list() function का उपयोग किया जा सकता है।

The list() function can be used to display the values generated by range() as a list.

Example:
 
print(list(range(1, 6)))
Output:
[1, 2, 3, 4, 5]
 
Because:
 
range(1, 6) → 1, 2, 3, 4, 5

यहाँ list() range की values को list में convert करता है।

Here, list() converts the range values into a list.

7. range() for Repetition :-

range() function का उपयोग किसी statement को निश्चित संख्या में repeat (दोहराने) करने के लिए किया जाता है।

The range() function can be used to repeat a statement a specific number of times.

Example:
for i in range(3):
    print("Hello")
 
Output:
Hello
Hello
Hello
 
Because:
 
range(3) → 0, 1, 2

यहाँ कुल 3 values हैं, इसलिए "Hello" 3 times print हुआ।

There are 3 values, so "Hello" is printed 3 times.

Related Notes