Python 函数(笔记)

Python 函数

# def test(x):
# '''
# 2*x+1
# :param x:整形数字
# :return: 返回计算结果
# '''
# y=2*x+1
# return y
#
# def test():
# '''
# 2*x+1
# :param x:整形数字
# :return: 返回计算结果
# '''
# x=3
# y=2*x+1
# return y
# a=test()
# print(a)

#过程:就是没有返回值的函数


# def test01():
# msg = 'test01'
# print(msg)
#
#
# def test02():
# msg = 'test02'
# print(msg)
# return msg
#
# def test03():
# msg = 'test03'
# print(msg)
# return 1,2,3,4,'a',['alex'],{'name':'alex'},None
#
# def test04():
# msg = 'test03'
# print(msg)
# return {'name':'alex'}
# t1=test01()
# t2=test02()
# t3=test03()
# t4=test04()
# print(t1)
# print(t2)
# print(t3)
# print(t4)

 

 

 

# def calc(x,y): #x=2,y=3
# res=x**y
# return x
# return y
# res=calc(2,3)
# # print(x)
# # print(y)
# print(res)
# # a=10
# # b=10
# # calc(a,b)


# def test(x,y,z):#x=1,y=2,z=3
# print(x)
# print(y)
# print(z)

#位置参数,必须一一对应,缺一不行多一也不行
# test(1,2,3)

#关键字参数,无须一一对应,缺一不行多一也不行
# test(y=1,x=3,z=4)

#位置参数必须在关键字参数左边
# test(1,y=2,3)#报错
# test(1,3,y=2)#报错
# test(1,3,z=2)
# test(1,3,z=2,y=4)#报错
# test(z=2,1,3)#报错

# def handle(x,type='mysql'):
# print(x)
# print(type)
# handle('hello')
# handle('hello',type='sqlite')
# handle('hello','sqlite')

# def install(func1=False,func2=True,func3=True):
# pass

#参数组:**字典 *列表
def test(x,*args):
print(x)
print(args)


# test(1)
# test(1,2,3,4,5)
# test(1,{'name':'alex'})
# test(1,['x','y','z'])
# test(1,*['x','y','z'])
# test(1,*('x','y','z'))

# def test(x,**kwargs):
# print(x)
# print(kwargs)
# test(1,y=2,z=3)
# test(1,1,2,2,2,2,2,y=2,z=3)
# test(1,y=2,z=3,z=3)#会报错 :一个参数不能传两个值

def test(x,*args,**kwargs):
print(x)
print(args,args[-1])
print(kwargs,kwargs.get('y'))
# test(1,1,2,1,1,11,1,x=1,y=2,z=3) #报错
# test(1,1,2,1,1,11,1,y=2,z=3)

# test(1,*[1,2,3],**{'y':1})

 

函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段。

函数能提高应用的模块性,和代码的重复利用率。你已经知道Python提供了许多内建函数,比如print()。但你也可以自己创建函数,这被叫做用户自定义函数。


                                          作用域
# def test1():
# print('in the test1')
# def test():
# print('in the test')
# return test1
#
# # print(test)
# res=test()
# print(res()) #test1()

#函数的作用域只跟函数声明时定义的作用域有关,跟函数的调用位置无任何关系
# name = 'alex'
# def foo():
# name='linhaifeng'
# def bar():
# # name='wupeiqi'
# print(name)
# return bar
# a=foo()
# print(a)
# a() #bar()


name='alex'

def foo():
name='lhf'
def bar():
name='wupeiqi'
print(name)
def tt():
print(name)
return tt
return bar

# bar=foo()
# tt=bar()
# print(tt)
# tt()
r1 = foo()
r2 = r1() # tt
r3 = r2()
foo()()()


                                                  匿名函数

# lambda x:x+1

def calc(x):
  return x+1

res=calc(10)
print(res)
print(calc)

print(lambda x:x+1)
func=lambda x:x+1
print(func(10))

name='alex' #name='alex_sb'
def change_name(x):
  return name+'_sb'

res=change_name(name)
print(res)

func=lambda x:x+'_sb'
res=func(name)
print('匿名函数的运行结果',res)


# func=lambda x,y,z:x+y+z
# print(func(1,2,3))

name1='alex'
name2='sbalex'
name1='supersbalex'

# def test(x,y,z):
# return x+1,y+1 #----->(x+1,y+1)

# lambda x,y,z:(x+1,y+1,z+1)

 

posted @ 2018-04-16 10:14  夏天的麦田  阅读(118)  评论(0)    收藏  举报