What is copy module in python

Like other programming language python does not support = operation to copy an objects,instead it creates a new variable that shares the reference of the original object.Fortunately we have copy module in python copy an object to a variable.Their are two ways to use copy module in python.They are Shallow Copy and Deep Copy .

Deep copy in python

Deep copy is one of the way copy module in python,in Deep copy process a copy of object is copied in other object.so whatever the change we make on the Deep copy object it will not reflect in the original object.Deep copy copies all values recursively from source to target object. First constructing a new collection object and then recursively populating it with copies of the child objects found in the original

Deep copy example

import copy
  
# initializing item1 
item1 = [7, 12, [9,50], 14]
  
# using deep copy to copy item1 
item2 = copy.deepcopy(item1 )
  
# original elements of item1 
print ("Orignal item1 ")
for i in range(0,len(item1)):
    print (item1[i],end=" ")
  
# adding and element to item2 
item2[2][0] = 69

print("\r")

print ("Deep copy result item2")
for i in range(0,len( item2 )):
    print (item2[i],end=" ")
  
print("\r")

print ("After copying Deep copy Orignal item1 ")
for i in range(0,len( item1)):
    print (item1[i],end=" ")


Copy-module-in-Pytho-deep-copy

shallow copy example

shallow copy is a way in which a reference of object is copied in other object. so any changes made to a shallow copy of object do reflect in the original object.shallow copy is done with “copy()” function.

import copy
  
# initializing item1 
item1 = [7, 12, [9,50], 14]
  
# using deep copy to copy item1 
item2 = copy.copy(item1 )
  
# original elements of item1 
print ("Orignal item1 ")
for i in range(0,len(item1)):
    print (item1[i],end=" ")
  
# adding and element to item2 
item2[2][0] = 69

print("\r")

print ("Shallow copy result item2")
for i in range(0,len( item2 )):
    print (item2[i],end=" ")
  
print("\r")

print ("After copying Shallow copy Orignal item1 ")
for i in range(0,len( item1)):
    print (item1[i],end=" ")

Shallow copy module in Python

What is lambda function in Python

Lambda function in Python is an anonymous function , it means simply a functions that don’t have a name.In normal function we are def keywords to define a functions, but in Lambda functions we always defined by the keyword Lambda.Lambda as any number of argument but only a single expression.It is generally using for a short term usage situations.

Syntax of Python Lambda Function

lambda arguments: expression
e.g

division = lambda x: x / 2

Normal functions that equal above lambda functions

def division(x):
   return x / 2
# Program to filter out only the odd number from a list
my_number = [1, 12, 6, 11, 7, 17, 23, 14]

new_odd = list(filter(lambda x: (x%2 == 1) , my_number))

print(new_odd )

Lambda function in Python

what is the usage of python lambda functions

Decorators in Python

Decorators are simply functions in python that add extra functionality to the current function without changing the structure of the current function.Suppose we have function that we already written to get user name and print it.But later the requirement changes the display should be always in small letter in that case we can use Decorators in Python.Decorators in Python can be used to fulfill this by writing a Decorators functions to accomplish this task.

Decorators in Python example

def display_msg():
   return 'Welcome Back '

display_msg()  

OutPut : Welcome Back 



def decorator_lowercase(function):
   def wrapper():
       func = function()
       string_lowercase = func.lower()
       return string_lowercase
   return wrapper

@decorator_lowercase
def display_msg():
   return 'Welcome Back '

display_msg()  


OutPut : welcome back 

Python Namespaces and Scope

Python Namespaces is a way that make sure that object names are unique and can be used without any conflict.Python maintain namespace in the form of dictionary with ‘name as key’ mapped to a corresponding ‘object as value’.

Realtime example for namespace is their may be multiple employee with same name John ,but if we specifically identify John emp1009 their will only one person.The same way based on the namespace Python interpreter understands what exact method is trying to point to in the code.

Types of namespaces in Python

Local Namespace : Local Names space is created inside a function and gets cleared when the function returns.so it is temporary created inside the functions when it is called and cleared when the function returns

Global Namespace : Global Namespace are the namespace from imported module or package .The namespace are created when the package is imported in the script and it will be last long until until the execution of the script.

Built-in Namespace : includes built-in functions and the built-in names for various types of exceptions.

Lifecycle of a Namespaces purely depend on the scope of the object the mapped are mapped to it.

Python Namespaces