Skip to content

Scope Of Variable

Scope determines the accessibility of the variable throughout the program.

Β 

2 Scopes of Variable
1. Local
2. Global

Β 

Local Variable:
variable declared inside the body of the function. This variable can only be referred inside its declared block.

When we are trying to pass local variables as actual arguments, they just pass their values to the calling function. This is known as function callling with pass by value.

🐍
filename.py
#Example
def test(x):
    x=x+1
    print(x)
    return

#main program
x=5
test(x)
print(x)

Global Variables:
In Python, global variables are variables defined outside of any function or method, making them accessible from any part of the code. They are not limited to a specific scope and can be used throughout the program. But to avoid name conflict between local and global variable we have to use ‘global’ keyword inside function body to tell the compiler don’t create another variable instead use the program level variable.

🐍
filename.py
#Example
a=5

def test():
    print("inside test, a=",a)
    return

#main
print("inside main, a=",a)
test()

There may be a name conflict arise sometimes when a local variable has same name as the global variable. Then the function will give preference to its local one.

🐍
filename.py
#example
a=5 #global

def test():
    a=15 #local
    print("inside test, a=",a)
    return

#main
print("inside main, a=",a)
test()

But, you can refer the global variable through function using ‘global’ keyword

🐍
filename.py
#Example
a=5 #global

def test():
    global a
    a=15
    print("inside test, a=",a)
    return

#main
print("inside main, a=",a)
test()
print("inside main, a=",a)

Lambda Function:
A function with no name is known as anonymous function. The keyword β€˜lambda’ instead of β€˜def’ to be used to create an anonymous function. A lambda function usually has a single line of body but can take many arguments.

🐍
filename.py
#Example showing how lambda function works
sqr = lambda x: x * x

n=int(input("Enter any number:"))
print("Square of your number: ",sqr(n))

Recursive Function:
a function that is invoking itself in its body of the definition. This can be used as alternative to loops. Recursive function creates number of clone copies in memory until it is stopped. Developer should take care to stop recursive function through his logic.

🐍
filename.py
#EXample of recursive function
def test(x):
    if(x>10):
        return
    print(x)
    test(x+1)
    return

#main program
test(1)

Practice Questions:

1. Use a lambda function to add 5 to a number.
2. Create a lambda function to return the square of a number and call it for 6.
3. Write a lambda to filter even numbers from the list [1,2,3,4,5,6,7,8,9].
4. Use map() with a lambda to convert all strings in a list to uppercase.
5. Write a recursive function factorial(n) that returns the factorial of a number.
6. Create a recursive function fibonacci(n) that returns the nth Fibonacci number.
7. Define a recursive function to count down from a number to zero.
8. Create a program that:
–Accepts a list of integers from the user,
–Uses a lambda to filter odd numbers,
–Uses a function to return their average.
9. Write a program that defines a function

–calculate(operation, a, b) where:
–operation is a lambda for add, subtract, multiply, etc.
–Demonstrate it with different lambda expressions.