Python learning note function PartTwo

Python learning notes (15) function PartTwo

1, Scope of variable

The scope of variable refers to the effective range of variable, which can be divided into two categories: local variable and global variable.

  • local variable
    The so-called local variable is defined within the function body, that is, it only takes effect within the function body.
def testA():
    a = 100

Function of local variable: in the function body, temporarily save the data, that is, destroy the local variable after the function call.

  • global variable
    The so-called global variable refers to the variable that can take effect both inside and outside the function.

Thinking: what if a data needs to be used inside and outside the function?
Answer: set as global variable

a = 100  #Global variable a: accessible inside and outside the function


def testA():

def testB():


Screenshot of results:

Thinking: in the above example, testB function needs to modify the value of variable a to 200. How to modify the program?

So, how can testB function modify global variables?

  # Global keyword declares a as a global variable
    global a
    a = 200
a = 100  #Global variable a: accessible inside and outside the function

print(a)  #100

def testA():
    print(a)  #100

def testB():
    # Global keyword declares a as a global variable
    global a
    a = 200
    print(a)  #200


print(a)  #200 the global variable a has been modified in testB

2, Execution flow of multi function program

Generally in the actual development process, a program is often composed of multiple functions, and multiple functions share some data.

  • Common global variables
  • Return value passed as parameter
def test1():
    return 50

def test2(num):
result = test1()
test2(result)  #50

3, Return value of function

When a function has more than one return value:

In the above case, only one return value can be returned. How can multiple return values be returned?

def return_num():
    return 1,2

result = return_num()
print(result)  #(1,2), which returns tuples

Be careful:

  1. return a,b write method. When multiple data are returned, the default is tuple type.
  2. After return, you can connect lists, tuples, and dictionaries to return multiple values.
def return_num():
    return 1,2

result = return_num()
print(result)  #(1,2), which returns tuples by default

def return_num1():
    return (10, 20, 30)

result1 = return_num1()

def return_num2():
    return [10, 20, 30]

result2 = return_num2()

def return_num3():
    return {'name': 'Python', 'age': 3}

result3 = return_num3()

4, Parameters of function

4.1 location parameters

4.2 key parameters

4.3 default parameters (default parameters)

4.4 variable length parameter (variable parameter)

Both of the following are cases of parameter uncertainty:


def user_info(name,age,gender):
    print(f'Your name is{name},Age{age},Gender is{gender}')

# 1. Position parameters

# 2. Key parameters
user_info('Rose',age=15,gender='female')  # Position parameter must be written before key parameter
user_info(gender='male',name ='Xiao Ming',age=12)  # No sequence of key parameters

# 3. Default parameters (default parameters)
def user_info1(name,age,gender='male'):
    print(f'Your name is{name},Age{age},Gender is{gender}')

user_info1('Tom',12)  #No value is passed for the default parameter, which means the default value is used
user_info1('Rose',18,'female')  #Modify the default parameter value if the default parameter is passed

# 4. Indefinite length parameter (variable parameter)
def user_info2(*args):


def user_info3(**kwargs):

user_info3(name = 'Tom')
user_info3(name = 'Tom', age = 18, gender ='male' )

Screenshot of results:

5, Unpacking and exchanging the values of two variables

5.1 unpack

-Unpacking: tuples

def return_num():
    return 100,200

num1,num2 = return_num()
print(num1)  #100
print(num2)  #200

-Unpacking: Dictionary

dict1 = {'name':'Tom','age':19}
a,b = dict1

print(a)  #a is key value.
print(b)  #b is key value.

print(dict1[a])  #'Tom'
print(dict1[b])  #19

5.2 values of exchange variables

Requirement: a = 10 and b = 20, exchange the values of two variables.

  • Method 1
    The third variable is used to store data.
a = 10
b = 20
# 1. Define intermediate variables
c = 0

# 2. Store the data of a to c
c = a

# 3. Assign data of b to a
a = b

# 4. Assign data of c to b
b = c

print(a)  #20
print(b)  #10
  • Method two
a,b = 1,2
a,b = b,a
print(a)  #2
print(b)  #1

Six, citation

In Python, values are passed by reference.
We can use id() to determine whether two variables are references of the same value. We can understand the id value as the address identifier of that memory.

# 1. int type
a = 1
b = a

print(b)  #1

print(id(a))  #2045600944
print(id(b))  #2045600944

a = 2

print(b) # 1. The int type is immutable
print(id(a))  #2045600960, the memory address of data 2 is obtained
print(id(b))  #2045600944

# 2. list
list1 = [1,2]
list2 = list1

print(list2)  #[1.2]
print(id(list1))  #51070456
print(id(list2))  #51070456


print(list2)  # [1,2,3], list is variable type
print(id(list1))  #58934776
print(id(list2))  #58934776

Reference as argument:

# Requirement: whether the reference can be passed as an actual parameter
def test1(a):

    a += a

# int: id value is different before and after calculation
b = 100

# List: id value is the same before and after calculation
c = [10,20]

Screenshot of results:

7, Variable and immutable types

Eight, summary

Published 19 original articles, won praise 0, visited 1249
Private letter follow

Tags: Python

Posted on Tue, 11 Feb 2020 00:20:53 -0800 by marmite