day10__ Review and assignments

1, Yesterday's content review

1. Parameters of function

1) actual parameter angle: position parameter, keyword parameter and mixed parameter

2) parameter angle: position parameter, default parameter, keyword only parameter, universal parameter

3) parameter angle parameter order: position parameter, * args, default parameter, only key parameter, * * kwargs

2. Magic Usage of *

1) function definition: represents aggregation

2) when the function is called: represents break up

3. There are three spaces in python:

Built in namespace: stored built-in functions: print, input

Global namespace: py file, which stores the variables of Py file (excluding functions and classes), and the relationship between function name and function memory address

Local namespace: correspondence between variables and values within stored functions

4. Loading order: built in namespace ----- > global namespace -------- local namespace (when executing function)

5. Retrieval sequence: principle of proximity. Local namespace -------- global namespace ----- > built in namespace

1) the local scope can only reference global variables and cannot be modified.

6. Scope:

1) global scope: built in namespace + global namespace

2) local scope: local namespace

2, Job:

1. See the result of code writing

def func():
    return 1, 2, 3

val = func()
print(val)
print(type(val) == tuple)
print(type(val) == list)


# Output result: true false

2. See the result of code writing

def func(*args, **kwargs):
    print(args)
    print(kwargs)
# a. Please execute the function and realize that the value of args is (1,2,3,4)
func(1, 2, 3, 4)
func(*[1,2,3,4])
# b. Please execute the function and realize that the value of args is ([1,2,3,4], [11,22,33])
func([1,2,3,4],[11,22,33])
# c. Please execute the function and realize that the value of args is ([11,22], 33) and the value of kwargs is {'k1':'v1','k2':'v2'}
func([11,22],33,k1='v1',k2='v2')
# d. If you execute func(* {'Wu Peiqi', 'Jin Xin', 'Goddess'}), what are the values of args and kwargs?
func(*{'Wu Peiqi','Jinxin','goddess'})
# e. If you execute func({'Wu Peiqi', 'Jin Xin', 'Goddess'}, [11,22,33]), what are the values of args and kwargs?
func({'Wu Peiqi','Jinxin','goddess'},[11,22,33])
# f. If you execute func('wu Peiqi ',' Jin Xin ',' Goddess', '11,22,33],**{'k1 ':' stack '}), what are the values of args and kwargs?
func('Wu Peiqi','Jinxin','goddess',[11,22,33],**{'k1':'Stack'})

3. See the result of code writing

def func(name,age=19,email='123.qq.com'):
    print(name,age,email)
#a executes func('mrxiong ') to determine whether it is executable. Please access the values of name, age and email?
func('mrxiong')
#b execute func('mrxiong',20) to determine whether it is executable. Please access the values of name, age and email?
func('mrxiong',20)
#c executes func('mrxiong',20,30) to determine whether it can be executed. What are the values of name, age, and email?
func('mrxiong',20,50)
#d execute func ('mrxiong, email =) x@qq.com ', is the interrupt executable? Can you access the values of name, age and email?
func('mrxiong',email='x@qq.com')
#e perform func ('mrxiong, email =) x@qq.com , age = 99). Is the interrupt executable? Can you access the values of name, age and email?
func('mrxiong',email='x@qq.com',age=99)
#f executes func(name='mrxiong',99). Is it executable? Can you access the values of name, age and email?
func(name='mrxiong',99)
#g execute func(name='mrxiong',99,'111@qq.com ', is the interrupt executable? Can you access the values of name, age and email?
func(name='mrxiong',99,'111@qq.com')

4. See the result of code writing

def func(users,name):
    users.append(name)
    return users
result = func(['mrxiong','Zhang San'],'alex')
print(result)
#Return result:
#['mrxiong ',' Zhang San ',' alex ']

5. See the result of code writing

def func(users,name):
    users.append(name)
    return users
result = func(['mrxiong','Zhang San'],'alex')
print(result)
//Output results:
    ['mrxiong', 'Zhang San', 'alex']

6. See the result of code writing

a = 10
b = 20
def test(a,b):
    return a ,b
c = test(b,a)
print(c)
//Output results:
    (20, 10)

7. See the result of code writing

def a(*args):
    count = 0
    for i in args:
        count += i
    return count
c = a(1,2,3,4,5)
print(c)
//Output results:
15

Tags: Python

Posted on Sat, 27 Jun 2020 22:43:25 -0400 by jnoun