python xx002函数
转义字符:
\000 空格
\n 换行
变量+字符串组合时需要空格可以通过+‘ ’实现:
>> name = 'xiaoming' >>> fruit = 'orange' >>> print(name+'like'+fruit) xiaominglikeorange >>> print(name+' '+'like'+' '+fruit) xiaoming like orange
函数:
>> def Hello(): print('hello world') >>> Hello() hello world
>>> def HelloName(name): # \000 表示空格 print('hello\000'+name) >>> HelloName('qiao') hello qiao
>>> def addsum(num1, num2): result = num1+num2 print(result) >>> addsum(1, 2) 3
>>> def addsum(num1, num2): result = num1+num2 return(result) >>> addsum(3, 5) 8
关键字参数可以防止因参数乱传而出错,关键字参数在调用函数,传参时定义:
# 关键字参数可以防止因参数乱传而出错,关键字参数在调用函数,传参时定义
>>> def eat(name, fruit): print(name+' '+'like eat'+' '+fruit) >>> eat('xiaoming', 'orange') xiaoming like eat orange # 实参顺序错误,表达意义相反 >>> eat('apple', 'xiaohong') apple like eat xiaohong # 实参关键字对应后,顺序打乱也不影响 >>> eat(fruit='apple', name='xiaohong') xiaohong like eat apple
默认参数在创建函数时定义在函数体中:
# 默认参数定义在函数体中 >>> def eatseq(name='xiaohua', fruit='banana'): print(name+' '+'like eat'+' '+fruit) # 不传入参数时输出默认参数值 >>> eatseq() xiaohua like eat banana # 传入参数时输出传入的参数 >>> eatseq(name='xiaoming', fruit='orange') xiaoming like eat orange # 只传入1个参数时,另一个参数使用默认值 >>> eatseq(name='xiaohong') xiaohong like eat banana
可变参数:
可变参数,参数前加*号表示传入参数数量不确定,格式为def xxx(*params)
# 可变参数,参数前加*号表示传入参数数量不确定,格式为def xxx(*params) def test(*params): print(len(params)) print('second param',params[1]) def testk(*numbers): print('传入了%i个参数' %(len(numbers))) >>> testk(1, 2, 3, 4, 5, 6, 7, 8) 传入了8个参数 >>> test('a', 'b', 'c') 3 second param b >>>
#函数(function)是有返回值的,过程(procedure)是简单,特殊并没有返回值的; #有返回值叫函数,没有返回值叫过程;python只有函数没有过程;全局变量在任何地方都可以访问,但不要在函数中修改全局变量 def zhekou(old_price, rate): jiage = old_price*rate #全局变量可以在函数中引用 print("全局变量可以在函数中引用old_price:",old_price) return jiage old_price = float(input("输入原价:")) rate = float(input("输入折扣率:")) new_price = zhekou(old_price, rate) print("折扣后价格为:", new_price) 输入原价:100 输入折扣率:0.8 全局变量可以在函数中引用old_price: 100.0 折扣后价格为: 80.0 >>>
全局变量:
count = 5 def fun(): count = 10 print(count) >>> fun() 10 >>> count 5 count = 5 def globalfun(): #global 声明将要修改全局变量count的值 global count count = 10 print(count) >>> globalfun() 10 >>> count 10
内部函数:
#内部函数:函数(外部函数)中创建的函数(内部函数),内部函数整个作用域在仅外部函数之内。 >>> def fun1(): print('fun1调用中...') def fun2(): print('fun2调用中......') fun2() >>> fun1() fun1调用中... fun2调用中...... #不在外部函数中调用fun2会报错 >>> fun2() Traceback (most recent call last): File "<pyshell#9>", line 1, in <module> fun2() NameError: name 'fun2' is not defined >>>
闭包:
# 内部函数引用外部函数的变量即为闭包(funy引用funx的变量x) >>> def funx(x): def funy(y): return x*y return funy >>> funx(8)(5) 40 # 内部函数在外部函数之外使用会报错 >>> funy(5) Traceback (most recent call last): File "<pyshell#6>", line 1, in <module> funy(5) NameError: name 'funy' is not defined
匿名函数lambda,格式为:lambda 函数传入参数:return值
>>> def sum1(x): return x*2+1 >>> sum1(5) 11 # x为传入参数,:后面为return返回值 >>> g = lambda x:x*2+1 >>> g(5) 11 >>> def add1(x, y): return x+y >>> add1(3, 4) 7 >>> h = lambda x,y:x+y >>> h(3, 4) 7 >>>
filter(function or none, iterable)过滤器和map
>>> filter(None, [0, 1, False, True]) <filter object at 0x033F8ED0> >>> list(filter(None, [0, 1, False, True])) [1, True] >>> def fun1(x): return x % 2 >>> temp = range(10) >>> list(filter(fun1(x), temp)) Traceback (most recent call last): File "<pyshell#22>", line 1, in <module> list(filter(fun1(x), temp)) NameError: name 'x' is not defined >>> list(filter(fun1, temp)) [1, 3, 5, 7, 9]
# filter(function or none, iterable)过滤器:filter的参数为function时循环迭代中的元素,将元素作为function的参数传入,
# 如果函数运行结果返回True则filter返回这些元素,如果函数运行结果为False则filter过滤这些元素;filter的参数为none时,返回iterable中为true的元素。 >>> list(range(10)) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] # range(10)的元素传入lambda函数运行后结果为True的被filter返回,为0的过滤,所以没返回数字0 >>> list(filter(lambda x:x*2, range(10))) [1, 2, 3, 4, 5, 6, 7, 8, 9] # map(function, iterables) 循环迭代中的元素,将元素作为function的参数传入函数运行,返回函数运行的结果。 >>> list(map(lambda x:x*2, range(10))) [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
递归:
# 递归:1.有调用自身函数 2.有返回(终止)的条件 3.递归代码简单,但非常消耗内存 #求乘积5*4*3*2*1 >>> def dg(n): if n == 1: return 1 else: return n*dg(n-1) >>> dg(5) 120 ''' 运行过程 dg5= 5*dg(4) dg4 = 4*dg(3) dg3 = 3*dg(2) dg2 = 2*dg(1) dg1 = 1(return) '''