''' 1. Using generator we can execute a block of code based on demand by using next() function. 2. using yield keyword we can create a generator. ''' def gen_fun(n): for i in range(n): print('Before yield') yield i print('After yield') gen_obj = gen_fun(10) #print(next(gen_obj)) #print(next(gen_obj)) # print(next(gen_obj)) ## Iterate based on for loop #for i in gen_obj: # print(' For Loop ', i) def gen_fun(n): print('Before return') yield n print(' \n After return') yield n gen_obj = gen_fun(10) print(next(gen_obj)) print(next(gen_obj))
1. What are the data types available in python ? they are 6 type 1) number, 2) string, 3) list, 4) tuple, 5) dictionary, 6) set, 2. Difference between list and tuple ? 3. Difference between set and tuple ? 4. Difference between Dictionary and set ? 5. Difference between Number and string ? 6. What is Dictionary ? 1. Dictionary is a collection of key value pairs. 2. Using dictionary we can represent data meningfully. 3. Based on key we can do operation on each value. 4. Using curly brackets "{}" we can create a dictionary 5. dictionary is a mutable object 7. Use of type() function ? 8. Use of dir() function ? 9. Use of id() function ? 10. Use of slicing operaions ? 11. Use of slicing opertions ? 12. How to get first 4 cherecters from string using slicing operation ? ## Number 1. What is n...
''' List comprehensions are used for creating new list from another iterables. Using list comprehensions we can do loop operations inside a square brackets. ''' l = [1, 2, 3, 4] ## Basic list comprehension #r = [ n + 10 for n in l] #print(r) ## list comprehension with if condition #r = [n + 2 for n in l if n > 2] #print(r) ## list comprehension with if and else condition r = [n + 100 if n > 2 else n + 10 for n in l] print(r)
Comments
Post a Comment