Beginning Python - Chapter6 : Abstraction
程序员文章站
2022-06-07 14:18:56
#chapter5:abstraction
#1 creating your own functions
# -1.1- documenting functions
def...
#chapter5:abstraction
#1 creating your own functions
# -1.1- documenting functions
def hello(name):
'this is comments' #docstring:comment of this fuc, can be show using .__doc__
'this is another,cannot be printed'
return 'hello, ' + name + '!'
print hello('lili')
print hello.__doc__ # a attribute of fuc
print '----------'
help(hello) #help on function hello in module __main__:
#hello(name)
#this is comments
print '----------'
help(hello('niu')) #no python documentation found for 'hello, niu!'
# -1.2- functions that aren’t really functions
# return nothing (default none)
def notreal():
print 'return nothing'
return
print 'not show'
print '1----------'
notreal() # return nothing
print '2----------'
print notreal() # return nothing none
# -1.3- formal parameters & actual parameters or arguments
# -1.4- collecting parameters
def muti1(*i):
print i
muti1(1) #(1,)
muti1(1,2,3)#(1, 2, 3)
def muti2(*i,**j): #cannot have two **j
print i
print j
muti2(1,2,3,name='lili',no=2)
# -1.5- scoping
x = 100
scope = vars()
scope['x']+=11
print x #111
# -1.6- global
x = 100
def change():
x = 50
y = 60
print 'change() x = ',x
print 'change() global()[x]',globals()['x'] # 100,show global one
global y #chang to global,can show outside of this func
y = y+100
change()
print y
# -1.7- nested scopes
def out(i):
def inter(j):
def inter1(k):
print('----1')
return i*j*k
print('----2')
return inter1
print('----3')
return inter
print out(2)(4)(3) # ---- 3 2 1 24
# -1.8- recursion(递归)
# -- factorial
def factorial(n):
result = n
for i in range(1,n):
result *= i
return result
print factorial(1)
def factorial1(n):
if n==1:
return 1
else:
return n*factorial1(n-1)
print factorial1(2)
# -- power
def power(x,n):
if n == 0:
return 1
result = 1
for i in range(n): #range(3)1,2,3;range(1,3)1,2
result *=x
return result
print '---',power(2,3)
def power1(x,n):
if n==0:
return 1
else:
return x*power(x,n-1)
print power1(2,3)
# -1.9- functional programming
# -- map
# pass all the elements of a sequence through a given function
print map(str,range(10))
value={'01':'lili','02':'lucy'}
value1=[1,2,3]
print map(str,value) #['02', '01']
print map(str,value1) #['1', '2', '3']
# -- filter
# to filter out items based on a boolean function
def func(x):
if x >=0: return true
if x<0 : return false
seq=[-1,0,2,3,-2]
print filter(func,seq) #seq make func return ture;
# -- lambda
# for define simple function,primarily used with map/filter and reduce
print filter(lambda x:x>=0,seq)
# -- reduce
num = [1,2,3,4]
num1 = [1]
print reduce(lambda x,y:x+y,num)
print reduce(lambda x,y:x+y,num1)
#1 creating your own functions
# -1.1- documenting functions
def hello(name):
'this is comments' #docstring:comment of this fuc, can be show using .__doc__
'this is another,cannot be printed'
return 'hello, ' + name + '!'
print hello('lili')
print hello.__doc__ # a attribute of fuc
print '----------'
help(hello) #help on function hello in module __main__:
#hello(name)
#this is comments
print '----------'
help(hello('niu')) #no python documentation found for 'hello, niu!'
# -1.2- functions that aren’t really functions
# return nothing (default none)
def notreal():
print 'return nothing'
return
print 'not show'
print '1----------'
notreal() # return nothing
print '2----------'
print notreal() # return nothing none
# -1.3- formal parameters & actual parameters or arguments
# -1.4- collecting parameters
def muti1(*i):
print i
muti1(1) #(1,)
muti1(1,2,3)#(1, 2, 3)
def muti2(*i,**j): #cannot have two **j
print i
print j
muti2(1,2,3,name='lili',no=2)
# -1.5- scoping
x = 100
scope = vars()
scope['x']+=11
print x #111
# -1.6- global
x = 100
def change():
x = 50
y = 60
print 'change() x = ',x
print 'change() global()[x]',globals()['x'] # 100,show global one
global y #chang to global,can show outside of this func
y = y+100
change()
print y
# -1.7- nested scopes
def out(i):
def inter(j):
def inter1(k):
print('----1')
return i*j*k
print('----2')
return inter1
print('----3')
return inter
print out(2)(4)(3) # ---- 3 2 1 24
# -1.8- recursion(递归)
# -- factorial
def factorial(n):
result = n
for i in range(1,n):
result *= i
return result
print factorial(1)
def factorial1(n):
if n==1:
return 1
else:
return n*factorial1(n-1)
print factorial1(2)
# -- power
def power(x,n):
if n == 0:
return 1
result = 1
for i in range(n): #range(3)1,2,3;range(1,3)1,2
result *=x
return result
print '---',power(2,3)
def power1(x,n):
if n==0:
return 1
else:
return x*power(x,n-1)
print power1(2,3)
# -1.9- functional programming
# -- map
# pass all the elements of a sequence through a given function
print map(str,range(10))
value={'01':'lili','02':'lucy'}
value1=[1,2,3]
print map(str,value) #['02', '01']
print map(str,value1) #['1', '2', '3']
# -- filter
# to filter out items based on a boolean function
def func(x):
if x >=0: return true
if x<0 : return false
seq=[-1,0,2,3,-2]
print filter(func,seq) #seq make func return ture;
# -- lambda
# for define simple function,primarily used with map/filter and reduce
print filter(lambda x:x>=0,seq)
# -- reduce
num = [1,2,3,4]
num1 = [1]
print reduce(lambda x,y:x+y,num)
print reduce(lambda x,y:x+y,num1)