Python基础(7)--函数

简介:

本篇文章将介绍如何将语句组织成函数,以及参数概念以及在程序中的用途

本文地址:http://www.cnblogs.com/archimedes/p/python-function.html,转载请注明源地址。

Pythond 的函数是由一个新的语句编写,即def,def是可执行的语句--函数并不存在,直到Python运行了def后才存在。

函数是通过赋值传递的,参数通过赋值传递给函数

def语句将创建一个函数对象并将其赋值给一个变量名,def语句的一般格式如下:

def function_name(arg1,arg2[,...]):
    statement
[return value]  

返回值不是必须的,如果没有return语句,则Python默认返回值None。

函数名的命名规则:

  • 函数名必须以下划线或字母开头,可以包含任意字母、数字或下划线的组合。不能使用任何的标点符号;

  • 函数名是区分大小写的。

  • 函数名不能是保留字。

Python使用名称空间的概念存储对象,这个名称空间就是对象作用的区域, 不同对象存在于不同的作用域。下面是不同对象的作用域规则:

  • 每个模块都有自已的全局作用域。

  • 函数定义的对象属局部作用域,只在函数内有效,不会影响全局作用域中的对象。

  • 赋值对象属局部作用域,除非使用global关键字进行声明。

LGB规则是Python查找名字的规则,下面是LGB规则:

1.大多数名字引用在三个作用域中查找:先局部(Local),次之全局(Global),再次之内置(Build-in)。

>>> a=2
>>> b=2
>>> def test(b):
...     test=a*b
...     return test
>>>print test(10)
20

b在局部作用域中找到,a在全局作用域中找到。

2.如想在局部作用域中改变全局作用域的对象,必须使用global关键字。

#没用global时的情况
>>> name="Jims"
>>> def set():
...     name="ringkee"
...
>>> set()
>>> print name
Jims

#使用global后的情况
>>> name="Jims"
>>> def set1():
...     global name
...     name="ringkee"
...
>>> set1()
>>> print name
ringkee

3.'global'声明把赋值的名字映射到一个包含它的模块的作用域中。

函数的参数是函数与外部沟通的桥梁,它可接收外部传递过来的值。参数传递的规则如下:

4.在一个函数中对参数名赋值不影响调用者。

>>> a=1
>>> def test(a):
...     a=a+1
...     print a
...
>>> test(a)
2
>>> a
1             # a值不变

5.在一个函数中改变一个可变的对象参数会影响调用者。

>>> a=1
>>> b=[1,2]
>>> def test(a,b):
...     a=5
...     b[0]=4
...     print a,b
...
>>> test(a,b)
5 [4, 2]
>>> a
1
>>> b
[4, 2]    # b值已被更改

参数是对象指针,无需定义传递的对象类型。如:

>>> def test(a,b):
...     return a+b
...
>>> test(1,2)   #数值型
3
>>> test("a","b")   #字符型
'ab'
>>> test([12],[11])   #列表
[12, 11]

函数中的参数接收传递的值,参数可分默认参数,如:

def function(ARG=VALUE)

元组(Tuples)参数:

def function(*ARG)

字典(dictionary)参数:

def function(**ARG)

一些函数规则:

  • 默认值必须在非默认参数之后;

  • 在单个函数定义中,只能使用一个tuple参数(*ARG)和一个字典参数(**ARG)。

  • tuple参数必须在连接参数和默认参数之后。

  • 字典参数必须在最后定义。


1.常用函数

1.abs(x)

abs()返回一个数字的绝对值。如果给出复数,返回值就是该复数的模。

>>>print abs(-100)
100
>>>print abs(1+2j)
2.2360679775

2.callable(object)

callable()函数用于测试对象是否可调用,如果可以则返回1(真);否则返回0(假)。可调用对象包括函数、方法、代码对象、类和已经定义了“调用”方法的类实例。

>>> a="123"
>>> print callable(a)
0
>>> print callable(chr)
1

3.cmp(x,y)

cmp()函数比较x和y两个对象,并根据比较结果返回一个整数,如果x<y,则返回-1;如果x>y,则返回1,如果x==y则返回0。

>>>a=1
>>>b=2
>>>c=2
>>> print cmp(a,b)
-1
>>> print cmp(b,a)
1
>>> print cmp(b,c)
0

4.divmod(x,y)

divmod(x,y)函数完成除法运算,返回商和余数。

>>> divmod(10,3)
(3, 1)
>>> divmod(9,3)
(3, 0)

5.isinstance(object,class-or-type-or-tuple) -> bool

测试对象类型

>>> a='isinstance test'
>>> b=1234
>>> isinstance(a,str)
True
>>> isinstance(a,int)
False
>>> isinstance(b,str)
False
>>> isinstance(b,int)
True

下面的程序展示了isinstance函数的使用:

def displayNumType(num):
    print num, 'is',
    if isinstance(num, (int, long, float, complex)):
        print 'a number of type:', type(num).__name__
    else:
        print 'not a number at all!!!'
displayNumType(-69)
displayNumType(9999999999999999999999999L)
displayNumType(565.8)
displayNumType(-344.3+34.4j)
displayNumType('xxx')

代码运行结果如下:

-69 is a number of type: int
9999999999999999999999999 is a number of type: long
565.8 is a number of type: float
(-344.3+34.4j) is a number of type: complex
xxx is not a number at all!!!

6.len(object) -> integer

len()函数返回字符串和序列的长度。

>>> len("aa")
2
>>> len([1,2])
2

7.pow(x,y[,z])

pow()函数返回以x为底,y为指数的幂。如果给出z值,该函数就计算x的y次幂值被z取模的值。

>>> print pow(2,4)
16
>>> print pow(2,4,2)
0
>>> print pow(2.4,3)
13.824

8.range([lower,]stop[,step])

range()函数可按参数生成连续的有序整数列表。

>>> range(10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range(1,10)
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range(1,10,2)
[1, 3, 5, 7, 9]

9.round(x[,n])

round()函数返回浮点数x的四舍五入值,如给出n值,则代表舍入到小数点后的位数。

>>> round(3.333)
3.0
>>> round(3)
3.0
>>> round(5.9)
6.0

10.type(obj)

type()函数可返回对象的数据类型。

>>> type(a)
<type 'list'>
>>> type(copy)
<type 'module'>
>>> type(1)
<type 'int'>

11.xrange([lower,]stop[,step])

xrange()函数与range()类似,但xrnage()并不创建列表,而是返回一个xrange对象,它的行为与列表相似,但是只在需要时才计算列表值,当列表很大时,这个特性能为我们节省内存。

>>> a=xrange(10)
>>> print a[0]
0
>>> print a[1]
1
>>> print a[2]
2

2.内置类型转换函数

1.chr(i)

chr()函数返回ASCII码对应的字符串。

>>> print chr(65)
A
>>> print chr(66)
B
>>> print chr(65)+chr(66)
AB

2.complex(real[,imaginary])

complex()函数可把字符串或数字转换为复数。

>>> complex("2+1j")
(2+1j)
>>> complex("2")
(2+0j)
>>> complex(2,1)
(2+1j)
>>> complex(2L,1)
(2+1j)

3.float(x)

float()函数把一个数字或字符串转换成浮点数。

>>> float("12")
12.0
>>> float(12L)
12.0
>>> float(12.2)
12.199999999999999

4.hex(x)

hex()函数可把整数转换成十六进制数。

>>> hex(16)
'0x10'
>>> hex(123)
'0x7b'

5.long(x[,base])

long()函数把数字和字符串转换成长整数,base为可选的基数。

>>> long("123")
123L
>>> long(11)
11L

6.list(x)

list()函数可将序列对象转换成列表。如:

>>> list("hello world")
['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd']
>>> list((1,2,3,4))
[1, 2, 3, 4]

7.int(x[,base])

int()函数把数字和字符串转换成一个整数,base为可选的基数。

>>> int(3.3)
3
>>> int(3L)
3
>>> int("13")
13
>>> int("14",15)
19

8.min(x[,y,z...])

min()函数返回给定参数的最小值,参数可以为序列。

>>> min(1,2,3,4)
1
>>> min((1,2,3),(2,3,4))
(1, 2, 3)

9.max(x[,y,z...])

max()函数返回给定参数的最大值,参数可以为序列。

>>> max(1,2,3,4)
4
>>> max((1,2,3),(2,3,4))
(2, 3, 4)

10.oct(x)

oct()函数可把给出的整数转换成八进制数。

>>> oct(8)
'010'
>>> oct(123)
'0173'

11.ord(x)

ord()函数返回一个字符串参数的ASCII码或Unicode值。

>>> ord("a")
97
>>> ord(u"a")
97

12.str(obj)

str()函数把对象转换成可打印字符串。

>>> str("4")
'4'
>>> str(4)
'4'
>>> str(3+2j)
'(3+2j)'

13.tuple(x)

tuple()函数把序列对象转换成tuple。

>>> tuple("hello world")
('h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd')
>>> tuple([1,2,3,4])
(1, 2, 3, 4)

3.序列处理函数

1.常用函数中的len()、max()和min()同样可用于序列。

2.filter(function,list)

调用filter()时,它会把一个函数应用于序列中的每个项,并返回该函数返回真值时的所有项,从而过滤掉返回假值的所有项。

>>> def nobad(s):
...     return s.find("bad") == -1
... 
>>> s = ["bad","good","bade","we"]
>>> filter(nobad,s)
['good', 'we']

这个例子通过把nobad()函数应用于s序列中所有项,过滤掉所有包含“bad”的项。

3.map(function,list[,list])

map()函数把一个函数应用于序列中所有项,并返回一个列表。

>>> import string
>>> s=["python","zope","linux"]
>>> map(string.capitalize,s)
['Python', 'Zope', 'Linux']

map()还可同时应用于多个列表。如:

>>> import operator
>>> s=[1,2,3]; t=[3,2,1]
>>> map(operator.mul,s,t)   # s[i]*t[j]
[3, 4, 3]

如果传递一个None值,而不是一个函数,则map()会把每个序列中的相应元素合并起来,并返回该元组。如:

>>> a=[1,2];b=[3,4];c=[5,6]
>>> map(None,a,b,c)
[(1, 3, 5), (2, 4, 6)]

4.reduce(function,seq[,init])

reduce()函数获得序列中前两个项,并把它传递给提供的函数,获得结果后再取序列中的下一项,连同结果再传递给函数,以此类推,直到处理完所有项为止。

>>> import operator
>>> reduce(operator.mul,[2,3,4,5])  # ((2*3)*4)*5
120
>>> reduce(operator.mul,[2,3,4,5],1) # (((1*2)*3)*4)*5
120
>>> reduce(operator.mul,[2,3,4,5],2)  # (((2*2)*3)*4)*5
240

5.zip(seq[,seq,...])

zip()函数可把两个或多个序列中的相应项合并在一起,并以元组的格式返回它们,在处理完最短序列中的所有项后就停止。

>>> zip([1,2,3],[4,5],[7,8,9])
[(1, 4, 7), (2, 5, 8)]

如果参数是一个序列,则zip()会以一元组的格式返回每个项,如:

>>> zip((1,2,3,4,5))
[(1,), (2,), (3,), (4,), (5,)]
>>> zip([1,2,3,4,5])
[(1,), (2,), (3,), (4,), (5,)]

4.其他

def语句是实时执行的,当它运行的时候,它创建并将一个新的函数对象赋值给一个变量名,Python所有的语句都是实时执行的,没有像独立的编译时间这样的流程

由于是语句,def可以出现在任一语句可以出现的地方--甚至是嵌套在其他语句中:

if test:
    def fun():
        ...
else:
    def func():
        ...
...
func()

可以将函数赋值给一个不同的变量名,并通过新的变量名进行调用:

othername=func()
othername()

创建函数

内建的callable函数可以用来判断函数是否可调用:

>>> import math
>>> x=1
>>> y=math.sqrt
>>> callable(x)
False
>>> callable(y)
True

使用del语句定义函数:

>>> def hello(name):
      return 'Hello, '+name+'!'
>>> print hello('world')
Hello, world!
>>> print hello('Gumby')
Hello, Gumby!

编写一个fibnacci数列函数:

>>> def fibs(num):
      result=[0,1]
      for i in range(num-2):
          result.append(result[-2]+result[-1])
      return result
>>> fibs(10)
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
>>> fibs(15)
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377]

在函数内为参数赋值不会改变外部任何变量的值:

>>> def try_to_change(n):
      n='Mr.Gumby'
>>> name='Mrs.Entity'
>>> try_to_change(name)
>>> name
'Mrs.Entity'

由于字符串(以及元组和数字)是不可改变的,故做参数的时候也就不会改变,但是如果将可变的数据结构如列表用作参数的时候会发生什么:

>>> name='Mrs.Entity'
>>> try_to_change(name)
>>> name
'Mrs.Entity'
>>> def change(n):
      n[0]='Mr.Gumby'

>>> name=['Mrs.Entity','Mrs.Thing']
>>> change(name)
>>> name
['Mr.Gumby', 'Mrs.Thing']

参数发生了改变,这就是和前面例子的重要区别

以下不用函数再做一次:

>>> name=['Mrs.Entity','Mrs.Thing']
>>> n=name  #再来一次,模拟传参行为
>>> n[0]='Mr.Gumby' #改变列表
>>> name
['Mr.Gumby', 'Mrs.Thing']

当2个变量同时引用一个列表的时候,它们的确是同时引用一个列表,想避免这种情况,可以复制一个列表的副本,当在序列中做切片的时候,返回的切片总是一个副本,所以复制了整个列表的切片,将会得到一个副本:

>>> names=['Mrs.Entity','Mrs.Thing']
>>> n=names[:]
>>> n is names
False
>>> n==names
True

此时改变n不会影响到names:

>>> n[0]='Mr.Gumby'
>>> n
['Mr.Gumby', 'Mrs.Thing']
>>> names
['Mrs.Entity', 'Mrs.Thing']
>>> change(names[:])
>>> names
['Mrs.Entity', 'Mrs.Thing']

关键字参数和默认值

参数的顺序可以通过给参数提供参数的名字(但是参数名和值一定要对应):

>>> def hello(greeting, name):
        print '%s,%s!'%(greeting, name)
>>> hello(greeting='hello',name='world!')
hello,world!!

关键字参数最厉害的地方在于可以在参数中给参数提供默认值:

>>> def hello_1(greeting='hello',name='world!'):
      print '%s,%s!'%(greeting,name)

>>> hello_1()
hello,world!!
>>> hello_1('Greetings')
Greetings,world!!
>>> hello_1('Greeting','universe')
Greeting,universe!

若想让greeting使用默认值:

>>> hello_1(name='Gumby')
hello,Gumby!

可以给函数提供任意多的参数,实现起来也不难:

>>> def print_params(*params):
      print params

>>> print_params('Testing')
('Testing',)
>>> print_params(1,2,3)
(1, 2, 3)

混合普通参数:

>>> def print_params_2(title,*params):
      print title
      print params

>>> print_params_2('params:',1,2,3)
params:
(1, 2, 3)
>>> print_params_2('Nothing:')
Nothing:
()

 星号的意思就是“收集其余的位置参数”,如果不提供任何供收集的元素,params就是个空元组

但是不能处理关键字参数:

>>> print_params_2('Hmm...',something=42)
Traceback (most recent call last):
  File "<pyshell#112>", line 1, in <module>
    print_params_2('Hmm...',something=42)
TypeError: print_params_2() got an unexpected keyword argument 'something'

试试使用“**”:

>>> def print_params(**params):
      print params

>>> print_params(x=1,y=2,z=3)
{'y': 2, 'x': 1, 'z': 3}
>>> def parames(x,y,z=3,*pospar,**keypar):
      print x,y,z
      print pospar
      print keypar

>>> parames(1,2,3,5,6,7,foo=1,bar=2)
1 2 3
(5, 6, 7)
{'foo': 1, 'bar': 2}
>>> parames(1,2)
1 2 3
()
{}
>>> def print_params_3(**params):
      print params

>>> print_params_3(x=1,y=2,z=3)
{'y': 2, 'x': 1, 'z': 3}
>>> #返回的是字典而不是元组
>>> #组合‘#’与'##'
>>> def print_params_4(x,y,z=3,*pospar,**keypar):
      print x,y,z
      print pospar
      print keypar

>>> print_params_4(1,2,3,5,6,7,foo=1,bar=2)
1 2 3
(5, 6, 7)
{'foo': 1, 'bar': 2}
>>> print_params_4(1,2)
1 2 3
()
{}
目录
相关文章
|
12天前
|
Python
python函数的参数学习
学习Python函数参数涉及五个方面:1) 位置参数按顺序传递,如`func(1, 2, 3)`;2) 关键字参数通过名称传值,如`func(a=1, b=2, c=3)`;3) 默认参数设定默认值,如`func(a, b, c=0)`;4) 可变参数用*和**接收任意数量的位置和关键字参数,如`func(1, 2, 3, a=4, b=5, c=6)`;5) 参数组合结合不同类型的参数,如`func(1, 2, 3, a=4, b=5, c=6)`。
14 1
|
27天前
|
Python
Python函数使用(四)
Python函数使用(四)
60 0
|
5天前
|
Serverless 开发者 Python
《Python 简易速速上手小册》第3章:Python 的函数和模块(2024 最新版)
《Python 简易速速上手小册》第3章:Python 的函数和模块(2024 最新版)
37 1
|
5天前
|
索引 Python
Python高维变量选择:SCAD平滑剪切绝对偏差惩罚、Lasso惩罚函数比较
Python高维变量选择:SCAD平滑剪切绝对偏差惩罚、Lasso惩罚函数比较
|
7天前
|
Python
python学习-函数模块,数据结构,字符串和列表(下)
python学习-函数模块,数据结构,字符串和列表
47 0
|
7天前
05-python之函数-函数的定义/函数的参数/函数返回值/函数说明文档/函数的嵌套使用/函数变量的作用域
05-python之函数-函数的定义/函数的参数/函数返回值/函数说明文档/函数的嵌套使用/函数变量的作用域
|
8天前
|
Python
python学习10-函数
python学习10-函数
|
8天前
|
Python
python学习4-内置函数range()、循环结构、循环控制语句、else语句、嵌套循环
python学习4-内置函数range()、循环结构、循环控制语句、else语句、嵌套循环
|
11天前
|
测试技术 开发者 Python
Python中的装饰器:优雅而强大的函数修饰工具
在Python编程中,装饰器是一种强大的工具,用于修改函数或方法的行为。本文将深入探讨Python中装饰器的概念、用法和实际应用,以及如何利用装饰器实现代码的优雅和高效。
|
16天前
|
Python
Python函数学习应用案例详解
【4月更文挑战第7天】学习Python函数的应用,包括计算两数之和、判断偶数、计算阶乘、生成斐波那契数列及反转字符串。示例代码展示了函数接收参数和返回结果的功能,如`add(a, b)`求和,`is_even(num)`判断偶数,`factorial(n)`计算阶乘,`fibonacci(n)`生成斐波那契数,以及`reverse_string(s)`反转字符串。
14 1