Friday, June 26, 2020

LOCAL VARIABLE PYHTON EASY


CASE1:
def local():
     l=55
     print('inside local',l)
     l+=5
    
local()


OUTPUT:
inside local 55
local var l accessed and Modified in Function  local().

CASE2:

def local():
     l=55
     print('inside local',l)
     l+=5
    


local()
print('outside access of local',l)


OUTPUT:
ERROR:
print('outside access of local',l)
NameError: name 'l' is not defined

"Local Var l is not accessing outside function ,because its life are only inside function local()


CASE-3
def local():
     global l
     l=55
     print('inside local',l)
     l+=5
    


local()
print('outside access of local',l)


OUTPUT
inside local 55
outside access of local 60

local var l inside local() function can be accessed outside function ,when declared as global .global l


GLOBAL VARIABLE PYTHON


GLOBAL VARIALBE
 CASE 1:



 CASE 1

g=90
def local():
    print('inside local',g)
local()
print('global area',g)

output :inside local 90
global area 90


Global var g can be accessed all over the program .inside any function and outside funciton.

 CASE 2:

g=90
def local():
    print('inside local',g)
    g+=10
local()
print('global area',g)

Error: UnboundLocalError: local variable 'g' referenced before assignment


CASE-3
g=90
def local():
    global g
    print('inside local',g)
    g+=10
local()
print('global area',g)

OUTPUT:
inside local 90
global area 100

ITS WORKING  SO TO MODIFY  GLOBAL VAR INSIDE FUNCTION FUNCTION YOU HAVE TO REDCLARE GLOBAL VAR IN FUNCTION .AS global g

CASE-4

g=90
def local():
    global g
    print('inside local',g)
    g+=10
def local1():
    g-=30
    print('inside local1',g)
local()
local1()
print('global area',g)

OUTPUT :
ERROR:line 7, in local1
    g-=30
UnboundLocalError: local variable 'g' referenced before assignment

Again same Error Because g is modifying in local1,but not declared as global .so all function which modifying global var have reclared as Global.







Python Anonymous/Lambda Function

What are lambda functions in Python?

In Python, an anonymous function is a function that is defined without a name.
While normal functions are defined using the def keyword in Python, anonymous functions are defined using the lambda keyword.
Hence, anonymous functions are also called lambda functions.

How to use lambda Functions in Python?

A lambda function in python has the following syntax.

Syntax of Lambda Function in python

lambda arguments: expression
Lambda functions can have any number of arguments but only one expression. The expression is evaluated and returned. Lambda functions can be used wherever function objects are required.

# Program to show the use of lambda functions
double = lambda x: x * 2

print(double(5))

In the above program, lambda x: x * 2 is the lambda function. Here x is the argument and x * 2 is the expression that gets evaluated and returned.
This function has no name. It returns a function object which is assigned to the identifier double. We can now call it as a normal function. The statement
double = lambda x: x * 2
is nearly the same as:
def double(x):
   return x * 2
def myfunc(n):
  return lambda a : a * n

mydoubler = myfunc(2)

print(mydoubler(11))