-
-
Save solen003/66122b995d81bb657860605656156ba9 to your computer and use it in GitHub Desktop.
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) |
could u explain the code? Where is the generator used in this code?
generator not used here.
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
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)).
class Iter:
def __init__(self):
pass
def divid(self,n):
for i in range(0,n):
if i%7==0:
print(i)
thanks