(7)函数
来源:互联网 发布:cms推荐 编辑:程序博客网 时间:2024/06/10 20:12
生成指定边界的斐波那契数列的函数:
def fib(n): a,b=0,1 while a < n: print(a,end=',') a,b=b,a+bfib(1000)
运行结果:
>>> ================================ RESTART ================================>>> 0,1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,
1.带有默认值的参数:
def ask_ok(prompt, retries=4, complaint='Yes or no, please!'): while True: ok = input(prompt) if ok in ('y', 'ye', 'yes'): return True if ok in ('n', 'no', 'nop', 'nope'): return False retries = retries - 1 if retries < 0: raise IOError('refusenik user') print(complaint)
调用方法:
只给出必要的参数: ask_ok('Do you really want to quit?')给出一个可选的参数: ask_ok('OK to overwrite the file?', 2)或者给出所有的参数: ask_ok('OK to overwrite the file?', 2, 'Come on, only yes or no!')
2.关键字参数:key=value的形式调用:
示例1.
def parrot(voltage, state='a stiff', action='voom', type='Norwegian Blue'): print("-- This parrot wouldn't", action, end=' ') print("if you put", voltage, "volts through it.") print("-- Lovely plumage, the", type) print("-- It's", state, "!")
parrot(1000) # 1 positional argumentparrot(voltage=1000) # 1 keyword argumentparrot(voltage=1000000, action='VOOOOOM') # 2 keyword argumentsparrot(action='VOOOOOM', voltage=1000000) # 2 keyword argumentsparrot('a million', 'bereft of life', 'jump') # 3 positional argumentsparrot('a thousand', state='pushing up the daisies') # 1 positional, 1 keyword
示例2:引入一个形如 **name 的参数时,它接收一个字典,使用一个形如 *name 的形式参数,它接收一个元组。*name 必须在 **name 之前出现。
def cheeseshop(kind, *arguments, **keywords): print("-- Do you have any", kind, "?") print("-- I'm sorry, we're all out of", kind) for arg in arguments: print(arg) print("-" * 40) keys = sorted(keywords.keys()) for kw in keys: print(kw, ":", keywords[kw])
调用:
>>> ================================ RESTART ================================>>> >>> cheeseshop("Limburger", "It's very runny, sir.", "It's really very, VERY runny, sir.", shopkeeper="Michael Palin", client="John Cleese", sketch="Cheese Shop Sketch")-- Do you have any Limburger ?-- I'm sorry, we're all out of LimburgerIt's very runny, sir.It's really very, VERY runny, sir.----------------------------------------client : John Cleeseshopkeeper : Michael Palinsketch : Cheese Shop Sketch>>>
3.可变参数列表:
def write_multiple_items(file, separator, *args): file.write(separator.join(args))
调用:
>>> ================================ RESTART ================================>>> >>> def concat(*args, sep="/"): return sep.join(args)>>> concat("earth", "mars", "venus")'earth/mars/venus'>>> concat("earth", "mars", "venus", sep=".")'earth.mars.venus'>>>
lambda形式:
def make_incrementor(n): return lambda x: x + n
运行:
>>> ================================ RESTART ================================>>> >>> f=make_incrementor(42)>>> f(0)42>>> f(1)43>>>
0 0
- (7)函数
- Python(7)函数之内置函数、定义新函数、空函数
- Kotlin学习(7)高阶函数和函数类型
- 7解析函数(一)
- 组函数(聚合函数),分组函数
- 7--函数
- 函数(-)
- JavaScript之 ------ 函数(一般函数、动态函数、匿名函数)
- python学习--函数(调用函数、定义函数、递归函数)
- NET函数(字符串函数)
- 生成函数(母函数)
- select()函数、poll()函数
- 生成函数(母函数)
- 生成函数(母函数)
- 母函数(生成函数)
- 母函数(生成函数)
- select()函数、poll()函数
- 递归函数(FIBNACCI函数)
- 解决Maven离线下载中心仓库连接失败问题
- matlab plot符号
- 桌球游戏
- mysql配置文件my.cnf参数优化和中文详解
- gdb调试命令
- (7)函数
- TOR 洋葱头yutu复活,yutu利器 安装优化教程
- Java的反射机制
- 【Android开发】线程实例-多彩霓虹灯
- iOS 蓝牙使用小结 bluetooth
- 博客迁移
- 第十四周 项目三:OOP版电子词典
- 【jQuery】兼容IE6的表格前端搜索
- 编程挑战:查询