(7)函数
2015-06-07 11:03
295 查看
生成指定边界的斐波那契数列的函数:
运行结果:
1.带有默认值的参数:
调用方法:
2.关键字参数:key=value的形式调用:
示例1.
调用方法:
示例2:引入一个形如 **name 的参数时,它接收一个字典,使用一个形如 *name 的形式参数,它接收一个元组。*name 必须在 **name 之前出现。
调用:
3.可变参数列表:
调用:
lambda形式:
运行:
def fib(n): a,b=0,1 while a < n: print(a,end=',') a,b=b,a+b fib(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 argument parrot(voltage=1000) # 1 keyword argument parrot(voltage=1000000, action='VOOOOOM') # 2 keyword arguments parrot(action='VOOOOOM', voltage=1000000) # 2 keyword arguments parrot('a million', 'bereft of life', 'jump') # 3 positional arguments parrot('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 Limburger It's very runny, sir. It's really very, VERY runny, sir. ---------------------------------------- client : John Cleese shopkeeper : Michael Palin sketch : 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 >>>
相关文章推荐
- Git远程操作详解
- Java的反射机制
- mysql配置文件my.cnf参数优化和中文详解
- matlab plot符号
- 桌球游戏
- 论本事
- javsscript问题收集
- poj1323Game Prediction
- 斯坦福大学深度学习与自然语言处理第二讲:词向量
- gdb调试命令
- mysql 中的 my.ini 配置文件
- 【j2ee spring】26、巴巴运动网-整合hibernate4(1)
- JS结构图
- 卸载 Windows 8/8.1/10 无法常规卸载的内置应用
- 信庭嵌入式工作室-互联网+的交流
- C#操作XML
- 【转】arm交叉编译器gnueabi、none-eabi、arm-eabi、gnueabihf、gnueabi区别
- javascript操作ASP.NET服务器控件
- 1130-host '192.168.x.x' is not allowed to conneciton to this MySQL server
- leetcode Word Break II