PGDCA, BCA, B-Tech, MCA, M-Tech, B.Sc computer science राजस्थान सूचना सहायक व कंप्यूटर शिक्षक विशेष नोट्स: इस अध्याय में हम Python range() Function: range(stop), range(start, stop, step) (range() फंक्शन: start, stop और step पैरामीटर्स का सटीक उपयोग) की संपूर्ण थ्योरी, मुख्य तकनीकी बिंदु, तुलनात्मक तालिका और विगत परीक्षाओं के पैटर्न पर आधारित 20 अति-महत्वपूर्ण बहुविकल्पीय प्रश्न (MCQs) विस्तृत हिंदी व अंग्रेजी व्याख्या सहित पढ़ेंगे।
Overview: In Python 3, range() is a built-in immutable sequence type that generates arithmetic progressions of integers on demand using O(1) memory. It accepts one, two, or three integer arguments via range(stop) or range(start, stop, step), always including start and strictly excluding stop.
अध्याय सार: पायथन 3 में range() एक बिल्ट-इन अपरिवर्तनीय (immutable) सीक्वेंस टाइप है जो O(1) मेमोरी का उपयोग करके मांग पर पूर्णांकों की समानांतर श्रेणी उत्पन्न करता है। यह range(stop) या range(start, stop, step) के रूप में एक, दो या तीन पूर्णांक आर्गुमेंट्स स्वीकार करता है, जिसमें start शामिल होता है और stop हमेशा बाहर रहता है।
Python range() Function: range(stop), range(start, stop, step): Detailed Notes & Architecture (range() फंक्शन: start, stop और step पैरामीटर्स का सटीक उपयोग)
1. Signatures and Half-Open Interval Semantics (सिग्नेचर और हाफ-ओपन इंटरवल नियम)
[EN] The range() constructor provides two overloaded call forms: range(stop) defaults start to 0 and step to 1, while range(start, stop[, step]) lets callers specify the starting boundary and increment. Python adopts the half-open interval [start, stop), meaning start is inclusive and stop is strictly exclusive, so range(5) produces 0, 1, 2, 3, 4. This half-open design guarantees that len(range(0, n)) equals n and adjacent slices such as range(0, 5) and range(5, 10) partition cleanly without overlap or gaps. When step is negative, progression counts downward while still stopping before reaching or passing stop, as in range(10, 0, -2) yielding 10, 8, 6, 4, 2.
[HI] range() कंस्ट्रक्टर दो रूपों में कॉल किया जाता है: range(stop) में start का मान डिफ़ॉल्ट रूप से 0 और step का 1 होता है, जबकि range(start, stop[, step]) में शुरुआती सीमा और अंतर तय किया जा सकता है। पायथन [start, stop) हाफ-ओपन अंतराल अपनाता है, अर्थात् start शामिल रहता है और stop बाहर रहता है, इसलिए range(5) से 0, 1, 2, 3, 4 प्राप्त होते हैं। इस डिज़ाइन से len(range(0, n)) हमेशा n के बराबर होता है और range(0, 5) व range(5, 10) बिना किसी ओवरलैप के जुड़ जाते हैं। ऋणात्मक step देने पर गिनती उल्टी दिशा में चलती है और stop से ठीक पहले रुकती है, जैसे range(10, 0, -2) से 10, 8, 6, 4, 2 मिलता है।
2. Memory Efficiency and Sequence Protocol (मेमोरी दक्षता और सीक्वेंस प्रोटोकॉल)
[EN] Unlike Python 2 where range() materialized an eager list in memory and xrange() provided lazy iteration, Python 3 replaced both with a single lazy range object. A range instance occupies constant O(1) memory regardless of span because it stores only three attributes—start, stop, and step—and computes elements mathematically on demand. Because range implements collections.abc.Sequence rather than Iterator, it supports len(), positive and negative indexing, slicing (returning a new range), .index(), .count(), and repeated iteration. Even membership testing (x in range(10**12)) executes in O(1) arithmetic time in Python 3 instead of scanning elements linearly.
[HI] पायथन 2 के विपरीत—जहाँ range() मेमोरी में पूरी लिस्ट बनाता था और xrange() लेज़ी था—पायथन 3 ने दोनों की जगह एक ही लेज़ी range ऑब्जेक्ट लागू किया है। एक range ऑब्जेक्ट चाहे जितनी बड़ी श्रेणी हो, केवल O(1) नियत मेमोरी घेरता है क्योंकि यह केवल start, stop और step मान सहेजता है और आवश्यकता पड़ने पर तत्व की गणना करता है। चूँकि range एक इटरेटर नहीं बल्कि सीक्वेंस (Sequence) है, यह len(), धनात्मक/ऋणात्मक इंडेक्सिंग, स्लाइसिंग (जो नया range देता है), .index(), .count() और बार-बार लूप चलाने का समर्थन करता है। पायथन 3 में मेंबरशिप जाँच (x in range(10**12)) भी सभी मान जाँचे बिना O(1) समय में गणितीय सूत्र से पूरी होती है।
3. Type Constraints, Empty Ranges, and Errors (डेटा टाइप नियम, खाली रेंज और त्रुटियाँ)
[EN] All arguments to range() must be integers or objects implementing __index__(); passing a float such as range(1.5) or range(0, 5, 0.5) raises TypeError. Supplying step=0 always raises ValueError because an arithmetic progression with zero increment cannot advance toward stop. The constructor does not accept keyword arguments, so calling range(stop=5) triggers TypeError: range() takes no keyword arguments. When start already meets or surpasses stop in the direction of step (such as range(5, 1) or range(1, 5, -1)), range() succeeds without error and returns an empty sequence of length 0.
[HI] range() के सभी आर्गुमेंट्स पूर्णांक (int) या __index__() लागू करने वाले ऑब्जेक्ट होने चाहिए; range(1.5) या range(0, 5, 0.5) जैसे फ्लोट देने पर TypeError आता है। step=0 देने पर हमेशा ValueError उत्पन्न होता है क्योंकि शून्य कदम के साथ श्रेणी stop की ओर नहीं बढ़ सकती। यह कंस्ट्रक्टर कीवर्ड आर्गुमेंट्स स्वीकार नहीं करता, इसलिए range(stop=5) लिखने पर TypeError आता है। यदि step की दिशा में start पहले ही stop के बराबर या उससे आगे हो (जैसे range(5, 1) या range(1, 5, -1)), तो कोई एरर नहीं आता बल्कि 0 लंबाई की खाली रेंज बनती है।
4. Practical Usage: Indexing, Slicing, and Equality (व्यावहारिक उपयोग: इंडेक्सिंग, स्लाइसिंग और समानता)
[EN] In Python loops, range() is commonly paired with for loops for fixed-count repetition or combined with len() when explicit index access is required, though enumerate() is preferred for iterating values with indices. Slicing a range returns another range object directly, e.g., range(0, 20, 2)[2:5] evaluates to range(4, 10, 2). Two range objects compare equal with == when they yield the exact same sequence of integers, so range(0, 3, 2) == range(0, 4, 2) evaluates to True because both yield (0, 2). However, empty ranges are all equal to one another regardless of their start, stop, or step attributes, such as range(5, 2) == range(0).
[HI] पायथन लूप्स में निश्चित बार दोहराव के लिए range() का उपयोग for के साथ किया जाता है, यद्यपि इंडेक्स और मान साथ पाने के लिए enumerate() को बेहतर माना जाता है। range पर स्लाइसिंग करने से नया range ऑब्जेक्ट ही मिलता है, जैसे range(0, 20, 2)[2:5] का परिणाम range(4, 10, 2) होता है। दो range ऑब्जेक्ट == से तब बराबर माने जाते हैं जब वे समान पूर्णांक अनुक्रम उत्पन्न करें, इसलिए range(0, 3, 2) == range(0, 4, 2) का परिणाम True होता है क्योंकि दोनों (0, 2) देते हैं। इसी प्रकार सभी खाली रेंज (चाहे start, stop, step कुछ भी हों) आपस में बराबर होती हैं, जैसे range(5, 2) == range(0) True है।
Syntax, Formula & Technical Example (Python range() Function: range(stop), range(start, stop, step))
r = range(2, 11, 2)
print(list(r)) # [2, 4, 6, 8, 10]
print(r[1], r[-1]) # 4 10
print(r[1:4]) # range(4, 10, 2)
print(6 in r, len(r)) # True 5
print(list(range(5, 0, -2))) # [5, 3, 1]
print(list(range(5, 2))) # []
Python range() Function: range(stop), range(start, stop, step) Quick Reference & Comparison Table
| Call Pattern | Generated Values & Attributes | Exam Note |
|---|---|---|
| range(5) | 0, 1, 2, 3, 4 (start=0, step=1) | Single argument always represents exclusive stop |
| range(2, 6) | 2, 3, 4, 5 (start=2, step=1) | Includes start=2 and stops before 6 |
| range(10, 2, -3) | 10, 7, 4 (negative step) | Counts down; requires start > stop to be non-empty |
| range(1, 5, 0) | ValueError raised immediately | step cannot be 0; floats raise TypeError |
Python range() Function: range(stop), range(start, stop, step) Top 20 Bilingual MCQs with Answers
Q1. What output does list(range(1, 7, 2)) produce in Python 3?
(पायथन 3 में list(range(1, 7, 2)) का आउटपुट क्या होगा?)
(A) [1, 3, 5, 7]
(B) [2, 4, 6]
(C) [1, 3, 5]
(D) [0, 2, 4, 6]
Correct Answer: (C)
[EN] Starting at 1 with step 2 produces 1, 3, 5; 7 is excluded because stop is exclusive.
[HI] 1 से शुरू होकर step 2 लेने पर 1, 3, 5 मिलते हैं; stop शामिल न होने से 7 बाहर रहता है।
Q2. What type of object does range(10) return in Python 3?
(पायथन 3 में range(10) किस प्रकार का ऑब्जेक्ट लौटाता है?)
(A) A tuple of 10 integers
(B) A single-use generator object
(C) An immutable range sequence object
(D) A mutable list of 10 integers
Correct Answer: (C)
[EN] In Python 3, range() returns an immutable range sequence object rather than a list or generator.
[HI] पायथन 3 में range() लिस्ट या जनरेटर के बजाय एक अपरिवर्तनीय range सीक्वेंस ऑब्जेक्ट लौटाता है।
Q3. Which exception is raised by range(1, 10, 0)?
(range(1, 10, 0) चलाने पर कौन-सा एक्सेप्शन उत्पन्न होता है?)
(A) ZeroDivisionError
(B) TypeError
(C) IndexError
(D) ValueError
Correct Answer: (D)
[EN] Passing 0 as the step argument is invalid and raises ValueError: range() arg 3 must not be zero.
[HI] step आर्गुमेंट के रूप में 0 देना अवैध है और यह ValueError उत्पन्न करता है।
Q4. What exception occurs when executing range(0, 5, 0.5)?
(range(0, 5, 0.5) चलाने पर कौन-सा एरर आता है?)
(A) SyntaxError
(B) ValueError
(C) OverflowError
(D) TypeError
Correct Answer: (D)
[EN] range() only accepts integers (or __index__ objects), so float arguments raise TypeError.
[HI] range() केवल पूर्णांक स्वीकार करता है, इसलिए फ्लोट मान देने पर TypeError आता है।
Q5. What is the output of print(list(range(5, 1)))?
(print(list(range(5, 1))) का आउटपुट क्या होगा?)
(A) []
(B) [5, 4, 3, 2]
(C) Raises ValueError
(D) [5, 4, 3, 2, 1]
Correct Answer: (A)
[EN] With default positive step=+1 and start > stop, range(5, 1) produces an empty sequence [].
[HI] डिफ़ॉल्ट धनात्मक step=+1 और start > stop होने पर range(5, 1) खाली सीक्वेंस [] देता है।
Q6. What does range(10, 4, -2)[-1] evaluate to?
(range(10, 4, -2)[-1] का मान क्या होगा?)
(A) 4
(B) 8
(C) 10
(D) 6
Correct Answer: (D)
[EN] range(10, 4, -2) generates [10, 8, 6], so its last element at index -1 is 6.
[HI] range(10, 4, -2) के मान [10, 8, 6] होते हैं, अतः इंडेक्स -1 पर अंतिम तत्व 6 है।
Q7. What is the time complexity of checking (999999 in range(10**9)) in Python 3?
(पायथन 3 में (999999 in range(10**9)) जाँचने की टाइम कॉम्प्लेक्सिटी क्या होती है?)
(A) O(n log n)
(B) O(log n)
(C) O(1)
(D) O(n)
Correct Answer: (C)
[EN] Python 3 computes range membership directly using bounds and modulo arithmetic in O(1) constant time.
[HI] पायथन 3 में range मेंबरशिप की जाँच सीमाओं और मॉड्यूलो गणित द्वारा O(1) नियत समय में होती है।
Q8. What happens if you call next(range(5)) in Python 3?
(पायथन 3 में next(range(5)) कॉल करने पर क्या होता है?)
(A) Returns 1
(B) Raises StopIteration
(C) Returns 0
(D) Raises TypeError because range is not an iterator
Correct Answer: (D)
[EN] range is an iterable Sequence, not an Iterator (it lacks __next__), so next(range(5)) raises TypeError.
[HI] range एक इटरेबल सीक्वेंस है न कि इटरेटर (इसमें __next__ नहीं होता), इसलिए TypeError आता है।
Q9. What is the result of range(0, 5, 2) == range(0, 6, 2)?
(range(0, 5, 2) == range(0, 6, 2) का परिणाम क्या होगा?)
(A) False
(B) None
(C) True
(D) TypeError
Correct Answer: (C)
[EN] Both ranges produce the identical sequence (0, 2, 4), so range equality compares them as True.
[HI] दोनों रेंज समान अनुक्रम (0, 2, 4) उत्पन्न करती हैं, इसलिए == तुलना का परिणाम True होता है।
Q10. What does range(0, 10, 2)[1:4] return?
(range(0, 10, 2)[1:4] क्या लौटाता है?)
(A) [2, 4, 6]
(B) (2, 4, 6)
(C) range(1, 4, 2)
(D) range(2, 8, 2)
Correct Answer: (D)
[EN] Slicing a range object returns a new range object representing the sliced progression: range(2, 8, 2).
[HI] किसी range ऑब्जेक्ट की स्लाइसिंग करने पर नया range ऑब्जेक्ट range(2, 8, 2) प्राप्त होता है।
Q11. How do you pass keyword arguments like range(start=1, stop=5) in Python?
(पायथन में range(start=1, stop=5) लिखने का परिणाम क्या होता है?)
(A) It raises SyntaxError
(B) It raises TypeError because range accepts only positional arguments
(C) It generates [1, 2, 3, 4]
(D) It generates [1, 2, 3, 4, 5]
Correct Answer: (B)
[EN] range() is implemented in C without keyword argument support, so range(start=1, stop=5) raises TypeError.
[HI] range() कीवर्ड आर्गुमेंट्स स्वीकार नहीं करता, इसलिए range(start=1, stop=5) पर TypeError आता है।
Q12. What is the memory space complexity of storing range(0, 100_000_000) in Python 3?
(पायथन 3 में range(0, 100_000_000) को स्टोर करने की स्पेस कॉम्प्लेक्सिटी क्या है?)
(A) O(log n)
(B) O(n/2)
(C) O(n)
(D) O(1)
Correct Answer: (D)
[EN] Regardless of span, a range object only stores start, stop, and step integers, taking O(1) space.
[HI] रेंज कितनी भी बड़ी हो, ऑब्जेक्ट केवल start, stop और step सहेजता है, अतः स्पेस कॉम्प्लेक्सिटी O(1) है।
Q13. Which expression generates the exact list [3, 0, -3]?
(कौन-सा एक्सप्रेशन ठीक [3, 0, -3] की लिस्ट उत्पन्न करेगा?)
(A) list(range(-3, 3, -3))
(B) list(range(3, -5, 3))
(C) list(range(3, -3, -3))
(D) list(range(3, -4, -3))
Correct Answer: (D)
[EN] With start=3 and step=-3, stop must be strictly less than -3 (like -4) so -3 is included.
[HI] start=3 और step=-3 के साथ -3 को शामिल रखने के लिए stop को -3 से छोटा (जैसे -4) होना चाहिए।
Q14. What does len(range(10, 10)) evaluate to?
(len(range(10, 10)) का मान क्या होगा?)
(A) 1
(B) Raises ValueError
(C) 0
(D) -1
Correct Answer: (C)
[EN] Because start equals stop and stop is exclusive, range(10, 10) contains zero elements.
[HI] start और stop दोनों 10 हैं तथा stop बाहर रहता है, इसलिए इसमें 0 तत्व होते हैं।
Q15. Which Python 2 function behaved similarly to Python 3's range() by generating values lazily?
(पायथन 2 का कौन-सा फंक्शन पायथन 3 के range() की तरह लेज़ी तरीके से मान उत्पन्न करता था?)
(A) xrange()
(B) genrange()
(C) lrange()
(D) irange()
Correct Answer: (A)
[EN] In Python 2, range() returned a full list in memory whereas xrange() yielded integers lazily.
[HI] पायथन 2 में range() पूरी लिस्ट बनाता था जबकि xrange() लेज़ी रूप से पूर्णांक उत्पन्न करता था।
Q16. Which of the following statements about Python 3's range() is NOT true?
(पायथन 3 के range() के बारे में निम्नलिखित में से कौन-सा कथन असत्य (NOT true) है?)
(A) range(3, 3) has length 0
(B) range() supports negative indexing like r[-1]
(C) A range object is exhausted after one for-loop and cannot be reused
(D) range objects are immutable sequences
Correct Answer: (C)
[EN] Unlike a generator or iterator, a range object is a reusable Sequence and can be iterated over multiple times.
[HI] जनरेटर या इटरेटर के विपरीत, range एक पुनः उपयोग योग्य सीक्वेंस है जिस पर कई बार लूप चलाया जा सकता है।
Q17. Within Python range() Function: range(stop), range(start, stop, step), which call pattern directly maps to: "0, 1, 2, 3, 4 (start=0, step=1)"?
(Python range() Function: range(stop), range(start, stop, step) में कौन सा Call Pattern सीधे "0, 1, 2, 3, 4 (start=0, step=1)" से संबंधित है?)
(A) range(10, 2, -3)
(B) range(5)
(C) range(2, 6)
(D) range(1, 5, 0)
Correct Answer: (B)
[EN] In Python range() Function: range(stop), range(start, stop, step), 'range(5)' handles '0, 1, 2, 3, 4 (start=0, step=1)' (Single argument always represents exclusive stop).
[HI] Python range() Function: range(stop), range(start, stop, step) में 'range(5)' का कार्य/विवरण '0, 1, 2, 3, 4 (start=0, step=1)' (Single argument always represents exclusive stop) है।
Q18. Which option accurately describes 'range(2, 6)' in Python range() Function: range(stop), range(start, stop, step)?
(Python range() Function: range(stop), range(start, stop, step) के संदर्भ में 'range(2, 6)' की सही विशेषता कौन सी है?)
(A) 0, 1, 2, 3, 4 (start=0, step=1)
(B) 10, 7, 4 (negative step)
(C) 2, 3, 4, 5 (start=2, step=1)
(D) ValueError raised immediately
Correct Answer: (C)
[EN] 'range(2, 6)' provides '2, 3, 4, 5 (start=2, step=1)' — key point: Includes start=2 and stops before 6.
[HI] 'range(2, 6)' का मुख्य विवरण '2, 3, 4, 5 (start=2, step=1)' है (Includes start=2 and stops before 6)।
Q19. In Python range() Function: range(stop), range(start, stop, step), identify the correct parameter for the specification: "10, 7, 4 (negative step)".
(Python range() Function: range(stop), range(start, stop, step) में "10, 7, 4 (negative step)" विवरण किस विकल्प के लिए सही है?)
(A) range(2, 6)
(B) range(10, 2, -3)
(C) range(1, 5, 0)
(D) range(5)
Correct Answer: (B)
[EN] 'range(10, 2, -3)' corresponds to '10, 7, 4 (negative step)' (Counts down; requires start > stop to be non-empty).
[HI] 'range(10, 2, -3)' का संबंध '10, 7, 4 (negative step)' (Counts down; requires start > stop to be non-empty) से है।
Q20. Considering Python range() Function: range(stop), range(start, stop, step), what key behavior defines 'range(1, 5, 0)'?
(Python range() Function: range(stop), range(start, stop, step) में 'range(1, 5, 0)' की मुख्य भूमिका या मान क्या है?)
(A) ValueError raised immediately
(B) 2, 3, 4, 5 (start=2, step=1)
(C) 0, 1, 2, 3, 4 (start=0, step=1)
(D) 10, 7, 4 (negative step)
Correct Answer: (A)
[EN] 'range(1, 5, 0)' represents 'ValueError raised immediately' (step cannot be 0; floats raise TypeError).
[HI] 'range(1, 5, 0)' का अर्थ/मान 'ValueError raised immediately' है (step cannot be 0; floats raise TypeError)।
Python range() Function: range(stop), range(start, stop, step) — Exam FAQs
Q1: Why is stop excluded in Python's range(start, stop)?
पायथन के range(start, stop) में stop को बाहर (exclusive) क्यों रखा जाता है?
[EN] Excluding stop ensures len(range(0, n)) equals n, matches zero-based indexing from 0 to n-1, and lets consecutive intervals like range(0, 5) and range(5, 10) join cleanly without duplicate boundaries.
[HI] stop को बाहर रखने से len(range(0, n)) ठीक n होता है, 0 से n-1 इंडेक्सिंग से मेल खाता है, और range(0, 5) व range(5, 10) बिना किसी मान के दोहराव के जुड़ जाते हैं।
Q2: How can I iterate over floating-point steps if range() only supports integers?
यदि range() केवल पूर्णांक स्वीकार करता है, तो दशमलव (float) स्टेप के साथ लूप कैसे चलाएं?
[EN] You can scale integer steps (e.g., [i * 0.5 for i in range(0, 10)]), use a while loop with a tolerance check, or use numpy.arange() / numpy.linspace() for numerical computing.
[HI] आप पूर्णांक को स्केल कर सकते हैं (जैसे [i * 0.5 for i in range(0, 10)]), while लूप का उपयोग कर सकते हैं, या वैज्ञानिक गणना के लिए numpy.arange() / numpy.linspace() चुन सकते हैं।
Q3: Can I iterate over the same range object more than once?
क्या एक ही range ऑब्जेक्ट पर एक से अधिक बार लूप चलाया जा सकता है?
[EN] Yes. Because range implements the Sequence protocol rather than being a single-pass iterator, each for-loop requests a fresh range_iterator via __iter__().
[HI] हाँ। चूँकि range सिंगल-पास इटरेटर न होकर एक Sequence है, इसलिए हर for-लूप __iter__() के माध्यम से नया इटरेटर प्राप्त करता है।
Post a Comment
Post a Comment