python(函数)
来源:互联网 发布:阿里云cdn审核 编辑:程序博客网 时间:2024/05/16 18:24
函数
函数_functions是指克重复使用的程序片段。
#function1.pydef say_hello(): print('Hello world')say_hello()say_hello()
输出:
Hello worldHello world
函数参数
#function_param.pydef print_max(a,b): if a > b: print(a,'is maximum') elif a==b: print(a,'is equal to',b) else: print(b,'is maximum')#直接传递字面值print_max(3,4)#以参数的形式传递变量x=5y=7print_max(x,y)
输出:
4 is maximum7 is maximum
局部变量
#function_loacl.pyx=50def func(x): print('x is',x) x=2 print('Changed local x to',x)func(x)print('x is still',x)
输出:
x is 50Changed local x to 2x is still 50
global 语句(全局)
#function_global.pyx=50def func(): global x print('x is',x) x=2 print('Change global x to',x)func()print('Value of x is',x)
输出:
x is 50Change global x to 2Value of x is 2
默认参数值
#function_default.pydef say(message,times=1): print(message*times)say('Hello')say('World',5)
HelloWorldWorldWorldWorldWorld
关键字参数
#function_keyword.pydef func(a,b=5,c=10): print('a is',a,'and b is',b,'and c is',c)func(3,7)func(25,c=24)func(c=50,a=100)
a is 3 and b is 7 and c is 10a is 25 and b is 5 and c is 24a is 100 and b is 5 and c is 50
可变参数
#function_verargs.pydef total(a=5,*numbers,**phonebook): print('a',a)'''声明一个诸如 *param 的星号参数时,从此处开始直到结束的所有 位置参数 都将被收集并汇聚成一个称为"param"的元组(tuple)。声明一个诸如 ** param 的双星号参数时,总此处开始直到结束的所有 关键字参数 都将被收集并汇聚成一个名为param的字典(Dictionary)。''' #遍历元组中的所有项目 for single_item in numbers: print('single_item',single_item) #遍历字典中的所有项目 for first_part,second_part in phonebook.items(): print(first_part,second_part)print(total(10,1,2,3,Jack=1123,John=2231,Inge=1560))
输出:
a 10single_item 1single_item 2single_item 3Jack 1123John 2231Inge 1560None
return 语句
用于函数返回
#function_return.pydef maximum(x,y): if x < y: return x elif x==y: return 'The numbers are equal' else: return yprint(maximum(2,3))print(maximum(2,2))print(maximum(3,2))
2The numbers are equal2
DocStrings
Documentation Strings文档字符串,建成docstrings。docString是一款你应当使用的重要工具,它能帮助你更好的记录程序,并让其更加易于理解。当程序实际运行时,我们甚至可以通过一个函数来获取文档。
#强烈推荐为所有重要的函数配以文档字符串。def print_max(x,y): #文档字符串,规定为遗传多行的字符串,其中第一行一某个大写字母开始,以句号结束。 #第二行为空行,后跟的第三行开始是任何详细的解释说明。 '''Prints the maximum of two numbers.打印你两个数值中的最大数。 the two values must be integers.这两个数都应该是整数''' #如果可能,将其转换至整数类型 x=int(x) y=int(y) if x > y: print(x,'is maximum') else: print(y,'is maximum')print_max(3,5)#使用函数__doc_属性来获取函数print_max的文档字符串。print('__doc__使用如下:')print(print_max.__doc__)print('help使用结果如下:')#使用help()函数,来获取__doc__属性并以一种简洁的方式将其呈现给你,help(print_max)
5 is maximum__doc__使用如下:Prints the maximum of two numbers.打印你两个数值中的最大数。 the two values must be integers.这两个数都应该是整数help使用结果如下:Help on function print_max in module __main__:print_max(x, y) Prints the maximum of two numbers.打印你两个数值中的最大数。 the two values must be integers.这两个数都应该是整数
阅读全文
0 0
- python range()函数
- python函数(一)
- python函数(二)
- 分页函数(Python)
- Python(六)函数
- python chr() 函数
- Python笔记(函数)
- Python map()函数
- Python学习(函数)
- 函数(python笔记)
- python(函数)
- Python(六) 函数
- python基础(函数)
- 函数(python)
- python---函数()
- Python:zip()函数
- Python format() 函数
- Python,函数(1)
- 【帝国CMS】点击量统计标签(内容页添加统计代码)
- 串口调试值printk.c
- 架构师之路2017半年精选40篇
- 函数对象和函数指针
- Java环境的搭建
- python(函数)
- 使用maven构建工具的好处
- mysql初始化出现:Installing MySQL system tables.../usr/local/mysql/bin/mysqld: error while loading shared
- Android数据库框架GreenDao使用详解
- 友盟崩溃日志分析
- JS nodeType返回类型
- 使用类和多文件编译
- C# .ToString() 格式化
- Linux命令大全