Untitled Document
Gnerators - yield keyword
1. Introduction Click Here
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       # Introduction 
       
       # Generators are iterable objects which are used in function which uses yield function 
       # Basically Generatrors are useful to reduce the Memory utilisation 
       # Since the vlaues are computed on each call ; rather than computing it as a whole
       # Here is the simmple example which prints square of numbers using yield keyword
       # To print the values we are using for loop ; we can use next () but in that case we don't know what is the end of lsit
       # so till what we need to write next() is unknown
       
       
2. Simple Example of generators Click Here
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       # Simple Example of generators
            
       def square_numbers(nums):
       	for i in nums:
       		yield(i * i)
       
       my_nums = square_numbers([1, 2, 3, 4, 5])
       print(my_nums) # this will print iterable object 
       
       for num in my_nums:
       	print (num)
       
       print (f'{first_name.upper()} {last_name.upper()}')
       
3. List comprehensions using generators Click Here
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       # List comprehensions using generators  

       my_nums = (x * x for x in [1, 2, 3, 4, 5])
       print(my_nums)
       
       for num in my_nums:
       	print (num)
            
4. Example to Display the Memory utlisation with and without generators Click Here
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       # Example to Display the Memory utlisation with and without generators
       
import memory_profiler as mem_profile
import random 
import time

names = ['Mangesh','Madhav','Mrunmayee','Raghav','Ketaki','Mohan','Meena']
majors = ['Math','Science','Arts','Sanskrit','Business','Engineering']

print('Memory Before : {}Mb'.format(mem_profile.memory_usage()))

def people_list(num_people):
	result = []
	for i in range(num_people):
		person = {
		           'id' : i,
		           'name' : random.choice(names),
		           'major' : random.choice(majors)
		         }
		result.append(person)
	return result


def people_generator(num_people):
	for i in range(num_people):
		person = {
		            'id' : i,
		            'name':random.choice(names),
		            'major':random.choice(majors)
		         }
		yield person

#t1 = time.time()
#people = people_list(1000000)
#t2 = time.time()

#print ('Memory After : {}Mb'.format(mem_profile.memory_usage()))
#print ('Took {} Seconds'.format(t2-t1))

t1 = time.time()
people = people_generator(1000000)
t2 = time.time()

print ('Memory After : {}Mb'.format(mem_profile.memory_usage()))
print ('Took {} Seconds'.format(t2-t1))
 
      
Untitled Document