-1

How to create a function "my range" In python which works same as built in range?

My attempt:

def myrange(end, start=1, step=1):
    if step == 1:
        print("(%s,%s)" % (start, end))
    else:
        print("(%s,%s,%s)" % (start, end, step))


b = myrange(10)
print(b)

But this does not work when I use for loop, please help how to create such user defined function.

Gustav Rasmussen
  • 3,720
  • 4
  • 23
  • 53
Satya
  • 187
  • 1
  • 10
  • 2
    Step 1 would be learning [What is the formal difference between “print” and “return”?](https://stackoverflow.com/q/7664779/3001761) Then you want it to be iterable, so look at e.g. https://stackoverflow.com/q/21665485/3001761 or read about *generator functions* in e.g. https://stackoverflow.com/q/102535/3001761. Literally all your function does is print what looks like a tuple of values, it's hard to see why you thought it *might* work in a `for` loop. – jonrsharpe Jul 27 '20 at 10:24
  • Oh also note that the 2.x version of range returned a *list* rather than an iterator (see https://docs.python.org/3/whatsnew/3.0.html#views-and-iterators-instead-of-lists) - the former may be an easier implementation for you to start with, then you can try adapting that looping logic to e.g. a generator function. – jonrsharpe Jul 27 '20 at 10:51

3 Answers3

0

I am new to Python but I think this will do the trick.

    def my_range(start, end=0, step=1):
    #Swapping the variables in case only one argument is passed to get proper start and end ranges
        if(end==0):
            temp = start
            start = end
            end = temp
            
        my_list = []
        i=start
        if(step == 0):
            return start
        if(step<0):
            while(i>end):
                my_list.append(i)
                i+=step
        else:
            while(i<end):
                my_list.append(i)
                i+=step
    #Converting the mutable list to immutable tuple
        return tuple(my_list)
    
    #This code here is just for checking the function
    start = int(input("\nEnter a number for range start (If none then enter 0): "))
    end = int(input("Enter a number for range end: "))
    step = int(input("Enter a number for range step(If none then enter 1): "))
    b = my_range(start,end, step)
    print(b)

0

#i created my own range function def my_range(min='',max='',step=''):

sonlar=[]

if min!='' and max!='' and step=='':
    while min<max:
        sonlar.append(min)
        min+=1
elif min!='' and max!='' and step!='':
    
    while min<max:
        sonlar.append(min)
        min+=step
elif min!='' and max=='' and step=='':
    x=0
    while min>x:
        sonlar.append(x)
        x+=1
return sonlar
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 13 '22 at 08:26
0
  def recreate_range_function(*args_) :
        output_list = []
        if len(args_) == 1 :
            start = 0 
            stop = args_[0]
            step = 1
        elif len(args_) == 2 :
            start = args_[0] 
            stop = args_[1]
            step = 1
        else : 
            start = args_[0]
            stop = args_[1]
            step = args_[2]
        val = start 
        while val < stop :
            output_list.append(val) 
            val+=step
        return output_list