Skip to content

Instantly share code, notes, and snippets.

@solen003
Created July 17, 2018 22:32
Show Gist options
  • Save solen003/66122b995d81bb657860605656156ba9 to your computer and use it in GitHub Desktop.
Save solen003/66122b995d81bb657860605656156ba9 to your computer and use it in GitHub Desktop.
Define a class with a generator which can iterate the numbers, which are divisible by 7, between a given range 0 and n.
n = int(input())
divBy7 = [i for i in range(0, n) if (i % 7 == 0)]
print(divBy7)
def divChecker(n):
for i in range(n):
if i % 7 == 0:
value = True
else:
value = False
print(i, value)
divChecker(n)
@gauravdh9
Copy link

thanks

@Sherin1998
Copy link

could u explain the code? Where is the generator used in this code?

@ramkatakam
Copy link

generator not used here.

@slkpddm
Copy link

slkpddm commented Jun 2, 2022

Here generator is - range(n) . Suppose if n = 5 then range(5) has to gives the values 0,1,2,3,4 . Simply if u give range(5) it won't give the result . To get the resulting values we should use loop or list

list(range(5)) - gives the values [0,1,2,3,4]

or

for i in range(5):
print(i)
gives the result as
0
1
2
3
4

So here range(4) is called the generator

Here in this example this is a generator - divBy7 = [i for i in range(0, n) if (i % 7 == 0)]
since it gives the values from 0 to n which r divisible by 7

@Gaurav0963
Copy link

you didn't use class here rather made a function.
I think you had to explicitly use generator here.
Instead of [i for i in range(0, n) if (i % 7 == 0)] you could use ((i for i in range(0, n) if (i % 7 == 0)).

@Nuke97Q
Copy link

Nuke97Q commented Nov 8, 2022

class Iter:
    def __init__(self):
        pass
        
    def divid(self,n):
        for i in range(0,n):
            if i%7==0:
                print(i)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment