python函数定义
来源:互联网 发布:黑客网站软件免费下载 编辑:程序博客网 时间:2024/06/02 18:35
在编程中,定义好函数,在需要时可以直接使用,大大简化我们的编程工作。调用函数时,只需要提供参数(除了无参的函数),就可以得到我们需要的结果。
1、无参的的函数
def hello() print('hello )
2、位置参数函数,还有返回值
def power(x) return x*x
def sum(x,y,z) return x+y+z
def power(x,n) return x^n
3、默认参数的函数
指定n次方的,返回的是x的n次方
不指定的话,返回的x的平方
def power(x,n=2) return x^n
def person(id,name,age=25,city='Beijing') print('个人信息已保存')
4、可变参数的函数
求输入的数字的平方和,将一个或者多个参数放入一个元祖中
def sumpower(*numbers) sum = 0 for n in numbers sum = sum + n^2 return sum
5、关键字参数的函数
传入的参数的形式必须为键值对,传入进去会自动保存为dict
def person(id,name,**info) print('用户的ID为:',id) print('用户的name为:',name) print('其他信息 保存在info里:',info)
6、命名关键字参数的函数
后面传输的键值对内容有要求的话,可以使用命名关键字参数,只允许输入后面指定的键
def person(id,name,*,age,city) print('id=',id,'name=',name,'age=',age,'city=',city)
还可以给这这些关键字参数设置默认值
def person(id,name,*,age=25,city) print('id=',id,'name=',name,'age=',age,'city=',city)
0 0
- python 条件定义函数
- Python function 函数定义
- python 定义函数
- Python函数定义
- Python定义函数
- python的函数定义
- Python -定义函数
- python 函数定义
- python函数定义
- python函数定义
- Python—定义函数
- python 定义颜色函数
- Python函数定义global
- python 函数定义
- Python 函数定义
- Python 函数定义,使用
- Python 函数定义练习
- python定义函数
- sql select when as的用法
- CentOS6.8 + cuda + caffe安装记录 (之五 cuda安装)
- 红外偏振成像
- 电子元件行业介绍及分析——ERP解决方案
- Android 隐藏标题栏
- python函数定义
- 从环境变量的配置到maven项目的编译、运行
- Android面试之----谈谈你对Java中Native关键字的认识!
- javascript接口实现之鸭式辨型法
- Jam的计数法
- iOS(CoreGraphics)画带箭头的框
- JavaScript-添加事件是与HTML里的结构相关的
- Performance of Using Keys in SELECT with FOR ALL ENTRIES
- RabbitMQ (三) 发布/订阅