4.28 Notes and Jobs

Keywords: Programming Lambda

note

address

a='123'
b='123'
print(a==b)
print(a is b)##True, the id of the immutable type is the same, the address is the same
print(id(a))#identical
print(id(b))#identical

a=3
b=a
b=6
print(a)

a=[1,2,3]
b=[1,2,3]
print(a==b)#true
print(a is b)##Different IDs and addresses for false variable types
print(id(a))#Different ID s
print(id(b))#Different id

x=[1,2,3]
y=x.copy()
print(y)
print(id(y))#Different IDs and addresses for false variable types
print(id(x))#Different IDs and addresses for false variable types
print(x is y)


def hanshu(a,b):
    print(id(a))
    a=10
    print(id(a))
    c=a+b
    return c
x=5
print(id(x))
y=6
print(id(y))
z=hanshu(x,y)#16
print(z)
print(x)#5



a=[1,2,3,4]
y=[4,5,6]
def modify(b,c):
    print(id(b))
    c=[5,6,7]
    b.append(5)
    print(id(b))

print(id(a))#id
modify(a,y)#id,id
print(a)#[1,2,3,4,5]
print(y)#[4,5,6]

Variable types are not recommended as default parameters because they can cause ambiguity

def hanshu(a=[]):
    a.append('h')
    print(a)
hanshu()
hanshu()

Namespace access, local variables, global variables

def hanshu(a,b):
    xy=5
    hah=9
    print(locals())
x=0
y=1
hanshu(x,y)
asfa=123445
print(globals())

Local variables can only be used within functions, global variables scoped throughout the program

a=[1,2,3]
def hanshu():
    x=3
    y=45
    print(a)
hanshu()#[1,2,3]
print(a)#[1,2,3]
y=12223
print(y)#12223
#print(x)#Cannot promise because it is a local variable

Definitions are placed before function calls, otherwise errors will occur

a=3
def hanshu():
    print(a)
    print(b)
    print(c)
b=4
hanshu()
c=5##Place in front of function calls, otherwise an error will be reported

Global variables can have the same name as local variables without interruption

a=3
def hanshu():
    a=5
    print(a)
hanshu()#5
print(a)#3

Error eg1:

a=3
def hanshu():
    print(a)#Error, undefined, output value
    a=5
    print(a)
hanshu()
print(a)



eg2.
       a=3
        def hanshu():
            a+=5##Error, undefined
            print(a)
        hanshu()
        print(a)

#Global variables are immutable types and functions cannot modify the values of global variables

a=3
def hanshu():
    a=5
    print(a)
hanshu()
print(a)

Global variables are variable types, and functions can modify global variables

> a=[1,2,3,4] def hanshu():
>     a.append(10)
 hanshu()
  print(a)##a is [1,2,3,4,10]

In a function, an equal sign is a local variable and you can no longer modify the value of a global variable

a=[1,2,3,4]
def hanshu():
    a=[7,8,9]#An equal sign is a local variable and you can no longer modify the value of a global variable
hanshu()
print(a)

Internal function

def hanshu1():#2
    a=2##3
    def hanshu2(a,b):#6
        z=a+b#7
        return z#8
    b=3#4
    d=hanshu2(a,b)#5
    return d#9
print(hanshu1())#1

Global function, which changes a local variable into a global variable

b=3
def hanshu2():
    global b
    b+=4
    print(b)
print(b)#b=3
hanshu2()#b=7

nonlocal, modify outer function, non-global function

b=3
def hanshuw():
    a=1
    b=6
    def hanshu_nei():
        nonlocal a
        nonlocal b
        a=2
        b=5
    print(a)#a=1
    hanshu_nei()
    print(b)#b=5
hanshuw()
print(b)#3

Scopes can be classified into four categories, LEGB, according to the location of the variable definition
Local (Internal Function) Local Scope
Eclosing (inside the outer function of a nested function) nested scope (closure)
Global (module global), global scope
Built-in built-in scope.Refers to (builtins.py module)

a=1
b=4
c=6
def hanshuw():
    a=2
    b=5
    def hanshu_n():
        a=3
        print(a)#3
        print(b)#5
        print(c)#6
        print(max,__name__)
    hanshu_n()
hanshuw()

Anonymous function

d=lambda x,y:x*2+y
z=d(5,6)
print(z)

abs() function, take absolute value
eg1:

  a=-1
    print(abs(a))

eg2:

  a=[1,2,3,4,-5]
    b=max(a,key=abs)#max calls abs function internally, taking the maximum absolute value
    print(b)#Output 5

def hanshu(a):
    return a*-1
a=[10,20,30,-50]
b=max(a,key=hanshu)##Output-50
print(b)

Usage of sort function

a=[1,2,-3,-5,4]
a.sort(key=abs)
print(a)#1,2,-3,4,-5

The map function is a map

def hanshu(a):
    return a*a
x=[1,2,3]
y=map(hanshu,x)
print(y)
for d in y:
    print(d)

Filter function to filter out unqualified

def aaa(a):
    if a%3==0:
        return True
x=[1,2,3,4,5,6,7,8,9]
d=filter(aaa,x)
print(d)
for i in d:
    print(i)
print(x)

zip function, pairing, inconsistent length, shortest output, return type is meta-ancestor

a=[1,2,3,4]
b=['a','b','c']
d=zip(a,b)
print(d)
for i in d:
    print(i)

Output a, B as [{'a':'c'}, {'b':'d'}]

a=('a','b')
b=('c','d')
x=list(map((lambda a:{a[0]:a[1]}),zip(a,b)))
print(x)

eg1:

a = [1, 10, 2, 20, 3, 30, 4, 40]
f = []
for i in range(0, len(a), 2):
    f.append(a[i]+a[i+1])
print(f)
b = f[::2]
c = f[1::2]
d = map(lambda x: ({x[0]: x[1]}), zip(b, c))
print(list(d))

Output the oldest, and by age

a=[{"name":"Zhang San",'age':18},
{"name":"Li Si",'age':28},
{"name":"wangwu",'age':48},
{"name":"xiaoming",'age': 8},
   ]
def hanshu(a):
    return a['age']
m=max(a,key=hanshu)
print(m)
a.sort(key=hanshu)
print(a)

m=max(a,key=lambda a:a['age'])
print(m)

task
Differences between variable data types and variable data types

 def demo(num, num_list):
        num += num
        num_list += num_list
        print(num)
        print(num_list)
        print("Function Completion")
    gl_num = 9
    gl_list = [1, 2, 3]
    demo(gl_num, gl_list)
    print(gl_num)
    print(gl_list)

2. #Value and address issues
Say what the values of list1,list2,list3 are and why

def extendlist(val, lis=[]):
    lis.append(val)
    return lis


list1 = extendlist(10)#[10]


list2 = extendlist(123, [])#[123]

list3 = extendlist('a')#[a]

print(list1)  # [10,'a']

print(list2)  # [123]

print(list3)  # [10,'a']

3. #Anonymous function
Please say acts 0 And explain why

def makeActions():
    acts = []
    for i in range(5):
        acts.append(lambda x: i ** x)
    return acts


acts = makeActions()
print(acts[0](2))
print(acts[1](2))
print(acts[2](2))
print(acts[3](2))
print(acts[4](2))

Posted by steveangelis on Sun, 28 Apr 2019 16:50:36 -0700