mirror of
https://github.com/Hizenberg469/Python-tutorial.git
synced 2026-04-19 23:12:24 +03:00
generator done
This commit is contained in:
BIN
debugging/__pycache__/pdb.cpython-312.pyc
Normal file
BIN
debugging/__pycache__/pdb.cpython-312.pyc
Normal file
Binary file not shown.
7
debugging/pdb.py
Normal file
7
debugging/pdb.py
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
import pdb
|
||||||
|
|
||||||
|
def add(num1 , num2):
|
||||||
|
pdb.set_trace()
|
||||||
|
return num1 + num2
|
||||||
|
|
||||||
|
add(4, 'hfdksfsl')
|
||||||
0
debugging/wiki.txt
Normal file
0
debugging/wiki.txt
Normal file
11
generators/fibonacci.py
Normal file
11
generators/fibonacci.py
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
def fib(number):
|
||||||
|
a = 0
|
||||||
|
b = 1
|
||||||
|
for i in range(number):
|
||||||
|
yield a
|
||||||
|
temp = a
|
||||||
|
a = b
|
||||||
|
b = temp + b
|
||||||
|
|
||||||
|
for x in fib(20):
|
||||||
|
print(x)
|
||||||
22
generators/generator.py
Normal file
22
generators/generator.py
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
class MyGen():
|
||||||
|
|
||||||
|
current = 0
|
||||||
|
def __init__(self, first, last):
|
||||||
|
self.first = first
|
||||||
|
self.last = last
|
||||||
|
|
||||||
|
def __iter__(self):
|
||||||
|
return self
|
||||||
|
|
||||||
|
|
||||||
|
def __next__(self):
|
||||||
|
if MyGen.current < self.last:
|
||||||
|
num = MyGen.current
|
||||||
|
MyGen.current += 1
|
||||||
|
return num
|
||||||
|
raise StopIteration
|
||||||
|
|
||||||
|
gen = MyGen(0,100)
|
||||||
|
|
||||||
|
for i in gen:
|
||||||
|
print(i)
|
||||||
10
generators/special_for.py
Normal file
10
generators/special_for.py
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
def special_for(iterable):
|
||||||
|
iterator = iter(iterable)
|
||||||
|
|
||||||
|
while True:
|
||||||
|
try:
|
||||||
|
print(next(iterator))
|
||||||
|
except StopIteration:
|
||||||
|
break
|
||||||
|
|
||||||
|
special_for([1,2,3])
|
||||||
@@ -0,0 +1,42 @@
|
|||||||
|
-> What are generators?
|
||||||
|
|
||||||
|
Generators are used to generate range of values
|
||||||
|
without using extra space.
|
||||||
|
|
||||||
|
for example, range(1,100) creates number from 1 to
|
||||||
|
100 but in this the procedure is as follows:
|
||||||
|
|
||||||
|
-> initialize with value 1.
|
||||||
|
-> returns the value.
|
||||||
|
-> increment it by one.
|
||||||
|
-> Repeat this same process from step 2,
|
||||||
|
until all values are generated with
|
||||||
|
the given range.
|
||||||
|
|
||||||
|
|
||||||
|
* generators is a iterator but iterator
|
||||||
|
is not necessarily a generator.
|
||||||
|
|
||||||
|
-> How to create your own generators?
|
||||||
|
|
||||||
|
for ex:
|
||||||
|
|
||||||
|
def generator_function(num):
|
||||||
|
for i in range(num):
|
||||||
|
yield i
|
||||||
|
|
||||||
|
g = generator_function(10)
|
||||||
|
next(g)
|
||||||
|
next(g)
|
||||||
|
print(next(g))
|
||||||
|
|
||||||
|
Two keywords are used here:
|
||||||
|
-> yield: This is used to make functions a
|
||||||
|
generator.
|
||||||
|
|
||||||
|
-> next: This is used to get the next value from
|
||||||
|
generator function
|
||||||
|
|
||||||
|
Note that unlike creating a list and then returning
|
||||||
|
the range of values, generators don't take extra
|
||||||
|
space to have range of values.
|
||||||
Reference in New Issue
Block a user