Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

difference between generator and iterator in python

* difference between generator and iterator in python
------------------------------------------
Iterator uses iter() and next() functions 	
Generator uses yield keyword
------------------------------------------
* Iterator
iter_list = iter(['Geeks', 'For', 'Geeks'])
print(next(iter_list))
print(next(iter_list))
print(next(iter_list))

* Generator
def sq_numbers(n):
	for i in range(1, n+1):
		yield i*i


a = sq_numbers(3)

print("The square of numbers 1,2,3 are : ")
print(next(a))
print(next(a))
print(next(a))
Source by www.geeksforgeeks.org #
 
PREVIOUS NEXT
Tagged: #difference #generator #iterator #python
ADD COMMENT
Topic
Name
3+6 =