Variable Scope

Variable Scope

Global variables: variables not defined in functions or classes are global variables

Scope: from definition to program end

#   A is a global variable
a = 10
print("Outside a",a)

def func1():
    print("In function a",a)

func1()


#Here x and b are global variables
for x in range(5):
    b = 20

Local variable: a variable defined in a function. Variables defined in a class are called properties.

Scope: from definition to function end

#  c and d are local variables and can only be used in the function that defines them. They cannot be used outside the function
def func3(c):
    d = 30
    

By default, global variables are saved in the global stack interval (released only after the program ends), and local variables are saved in the temporary stack interval corresponding to the function (the stack interval corresponding to the function is created when the function is created, and released when the function is called)

When defining variables in a function, you can add the global keyword so that variables defined in the function can be saved in the global stack interval.

a = 100  #  Global stack interval
def func3(c):
    global b 
    b =0 # global variable
    
    a =20  #  Stored in the temporary stack interval corresponding to func3
    

    print(c,a)
func3(20)
print(a)

To modify global variables in a function, you must first declare them in global

Determination of function parameters

When calling a function, what data should the actual parameters give?

#  x must be a function with one parameter and the return value must be a number
# Real parameter higher order function
def func2(x):
    print(x(10) + 20)


def temp2(m):
    # m=10
    return 18
func2(temp2)

Real parameter higher-order function - the parameter of the function is a function

If a parameter is a function, there are two methods for parameter transfer: 1) Use the function name of a common function. 2) Use anonymous functions

Anonymous function:

Syntax: function name=lambda parameter list: return value

Anonymous functions are functions in essence, but they can only realize simple functions (one line of statements can realize their functions)

# Define a function to find the sum of two numbers
sum1 = lambda  num1,num2 : num1+num2

print(sum1(1,2))
print(sum1(num1=10,num2=20))

Application of anonymous functions: When passing parameters to higher-order functions with real parameters, you can use either ordinary function names or anonymous function names

def func1(x):
    print(x(10,20) + 10)
    
# x is a function with two parameters and a return value corresponding to a number

def temp1(m,n):
    return 20

func1(temp1)   # Common function parameter transfer


func1(lambda m,n :20)  #  Anonymous function parameter transfer

Higher order functions with common real parameters

  1. max , min ,sorted

Max (sequence) - directly compare the size of sequence elements to find the maximum value

max (sequence, key=function) -- Compare the size of elements according to the comparison rules established by the function to find the maximum value

Function requirements: there is only one parameter (this parameter represents each element in the sequence); A return value is required (the return value is the comparison object)

#Case 1: finding the maximum value in num

# nums = [3,4,5,75,34,86,34,97,222,44]
# result = max(nums, key=lambda item:item)
# print(result)

#Case 1: Calculate the maximum value of single digits in num

nums = [3,4,5,75,34,89,34,97,222,44]
result = max(nums,key=lambda item:item % 10)
print(result)




# Exercise 1: Find the element with the largest ten digits in nums: 90
# nums = [9108, 23, 167, 90, 556, 149]
# 
# result = max(nums,key=lambda item : item % 100 // 10)
# print(result)


# Exercise 2: Find the element with the lowest absolute value in nums
# nums = [-992, 12, 78, -34, 99]
# result = min(nums,key=lambda i : abs(i))
# print(result)

  1. map

    Map (function, sequence 1, sequence 2, sequence 3) converts elements in the specified sequence into elements in the new sequence according to the rules specified by the function

    Function requirements: several sequences have several parameters (this parameter represents the elements in the following sequences)

    There is a return value: the return value represents the element in the new sequence

    # Case: Create a list. The elements in the list are the single digits of all elements in nums
    nums = [23,54,76,89,23,45,70,98,88]
    result = map(lambda item:item%10,nums)
    print(list(result))
    
    
    
    # Case: Add the corresponding elements in nums1 and nums2
    nums1 = [23,54,76,89,23,45,70,98,88]
    nums2 = [20,30,40,50,60,90,80,70,89]
    result = map(lambda x,y: x + y ,nums1,nums2)
    print(list(result))
    
    
    # Exercise 2: Create a list based on scores. The elements in the list are determined as
    # 'Pass' or' Fail '
    scores = [82, 67, 89, 66, 90, 23, 71]
    
    result = map(lambda x :"pass" if x >60 else "fail," ,scores)
    print(list(result))
    
    
    
    # Exercise 3: Create a student list based on names, ages, and scores:
    names = ['stu1', 'stu2', 'stu3', 'stu4']
    ages = [23, 34, 19, 22]
    scores = [82, 67, 89, 66]
    # [{'name': 'stu1', 'age': 23, 'score': 82}, .....]
    result = map(lambda i1,i2 ,i3 :{'name':i1,'age':i2,'score':i3},names,ages,scores)
    print(list(result))
    
  2. reduce -- merge all elements in the sequence into one data through the specified rules

Reduce (function, sequence, default value):

Function requirements: There are only two parameters. The first parameter points to the default value, and the second parameter points to each element in the sequence. A return value is required (describe the merging method, and clarify the final result and the operation of the initial value and the elements in the sequence

Tags: Python numpy programming language

Posted by pvtpyro on Sat, 24 Sep 2022 22:00:19 +0530