Python Range Function in Hindi – Examples के साथ

Python Range Function in Hindi के इस अध्याय में हम range() का importance और use सीखेंगे। साथ ही Syntax, Flowchart, उदाहरण (Examples) और Programs की मदद से समझेंगे कि range() का उपयोग Python में loop के लिए कैसे किया जाता है।

range() Function क्या है?

  • range() Python का एक built-in function है, जिसका इस्तेमाल किसी तय range में integers की एक sequence बनाने के लिए किया जाता है।
  • इसे ज़्यादातर loops के साथ इस्तेमाल किया जाता है, खासकर for loop के साथ, ताकि control किया जा सके कि loop कितनी बार चले।
  • range() function किसी दिए गए number से शुरू होकर values बनाता है और stop value तक पहुँचता है, लेकिन stop value को include नहीं करता।
  • लगातार आने वाली values के बीच का अंतर step value से तय होता है।

Syntax

range(start, stop, step)

range() के Parameters

  • start → sequence की starting value बताता है। By default, यह 0 होता है।
  • stop → sequence की ending value बताता है (यह output में शामिल नहीं होती)।
  • step → लगातार आने वाली values के बीच का अंतर बताता है। By default, यह 1 होता है।
  • सभी parameters integers होने चाहिए
  • step zero नहीं हो सकता। यह positive या negative हो सकता है।

range() का इस्तेमाल क्यों और कब करें – When and Why to use range()

range() function तब काम आता है जब:

  • किसी loop को एक तय संख्या में बार चलाना हो
  • numbers को एक sequence में generate करना हो
  • किसी खास increment या decrement के साथ iteration करना हो
  • यह programs को छोटा, organized और efficient बनाने में मदद करता है।

Example 1: Default Step Value का इस्तेमाल

for number in range(1, 6):
    print(number)

Output

1
2
3
4
5

व्याख्या – Explaination

  • sequence 1 से शुरू होती है और 6 से पहले रुक जाती है।
  • चूँकि step value नहीं दी गई है, इसलिए यह अपने आप 1 से बढ़ती है।

Example 1: सिर्फ stop का इस्तेमाल

for i in range(5):
    print(i)

Output

0
1
2
3
4

Example 2: start और stop का इस्तेमाल

for i in range(2, 6):
    print(i)

Output

2
3
4
5

Example 3: start, stop, और step का इस्तेमाल

for i in range(2, 11, 2):
    print(i)

Output

2
4
6
8
10

Explaination – व्याख्या

  • sequence 2 से शुरू होती है और 11 से पहले रुक जाती है।
  • step value 2 होने की वजह से हर iteration में number 2 से बढ़ता है।

Example: Using range() in List

print(list(range(10)))
print(list(range(0, 30, 5)))
print(list(range(0, -9, -1)))

Output:

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[0, 5, 10, 15, 20, 25]
[0, -1, -2, -3, -4, -5, -6, -7, -8]

Example Program: Print Multiples of 10

for num in range(5):
    if num > 0:
        print(num * 10)

Output

10
20
30
40

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *