Posts

Map function in Python

Image
  The  map ()  function  executes a specified  function  for each item in a iterable. The item is sent to the  function  as a parameter. Code Example >>> l1 = [1, 2, 3, 4, 5] >>> l2 = [5, 4, 3, 2, 1] >>> l3 = [5, 4, 3, 2, 1] >>> def cal(a=0, b=0, c=0):                  print(a+b+c) >>> x = map(cal, l1, l2, l3) >>> x <map object at 0x03FB6100> >>> x = list(x) Output: 11 10 9 8 7 Quiz Level 1 Quiz Level 2 Request Me

Deque in Python

Image
  Concept Deque (Doubly Ended Queue) in python is implemented using the module “ collections “. Deque is preferred over the list  in the cases where we need quicker append and pop operations from both the ends of the container, as deque provides an  O(1)  time complexity for append and pop operations as compared to list which provides O(n) time complexity. Code import collections dq = collections.deque([20, 30, 40]) print(dq) # added in right end dq.append(50) print(dq) # added in left end dq.appendleft(6) print(dq) # deleteing from right end dq.pop() # deleting from left end dq.popleft() Screenshot : Quiz Level 1 Quiz Level 2 Request Me  

Enumerate in Python

Image
  Code A lot of times when dealing with iterators, we also get a need to keep a count of iterations. Python eases the programmers’ task by providing a built-in function enumerate() for this task. The enumerate () method adds a counter to an iterable and returns it in a form of enumerate object. This enumerate object can then be used directly in for loops or be converted into a list of tuples using the list() method. >>> l = [i for i in range(1, 100, 10)] >>> l [1, 11, 21, 31, 41, 51, 61, 71, 81, 91] >>>  >>> for i, j in enumerate(l): print("i is :{}, j is :{}".format(i, j)) i is :0, j is :1 i is :1, j is :11 i is :2, j is :21 i is :3, j is :31 i is :4, j is :41 i is :5, j is :51 i is :6, j is :61 i is :7, j is :71 i is :8, j is :81 i is :9, j is :91 Quiz Level 1 Quiz Level 2 Request Me

Decorators in Python

Image
  Concept In Python, functions are the first class objects, which means that – Functions are objects; they can be referenced to, passed to a variable and returned from other functions as well. Functions can be defined inside another function and can also be passed as argument to another function. powerful and useful tool in Python since it allows programmers to modify the behavior of function or class. Decorators allow us to wrap another function in order to extend the behavior of wrapped function, without permanently modifying it. In Decorators, functions are taken as the argument into another function and then called inside the wrapper function. Code Example ''' Creating Decorator 1 ''' def uppercase_decorator(function):       def wrapper():             make_uppercase = function().upper()             return make_uppercase     re...

Lambda Function in Python

Image
   Concept & Code Example 1: >>> add = lambda x, y: x+y >>> add(2,3) 5 Example 2: >>> r = lambda a: [i for i in range(1, a, 10)] >>> r(50) [1, 11, 21, 31, 41] Quiz Level 1 Quiz Level 2 Request Me Click Here For More

Generators in Python

Image
  Concept Generators are used to create iterators, but with a different approach. Generators are simple functions that return an iterable set of items, one at a time, in a special way. When an iteration over a set of items starts using the for a statement, the generator is run. Once the generator's function code reaches a "yield" statement, the generator yields its execution back to the for loop, returning a new value from the set. The generator function can generate as many values (possibly infinite) as it wants, yielding each one in its turn. Code def gen_fun(): yield 1           yield 2           yield 3           yield 4           yield 5           for i in gen_fun(): print(i) >>>  1 2 3 4 5 Screenshot : Quiz Level 1 Quiz Level 2 Request Me

Filter in Python

Image
Code Example ages = [5, 12, 17, 18, 24, 32] def myFunc(x):   if x < 18:        return False   else:        return True adults = filter(myFunc, ages) for x in adults:        print("You are Adult : ", x) Screenshot : Quiz Level 1 Quiz Level 2 Request Me