python学习-23 函数
函数
1.函数分为:数学定义的函数和编程语言中的函数
例如: - 数学定义的函数:y=2*x+1
- 编程语言的函数:
def test(x):
x += 1
return x
def :定义函数的关键字
test :函数名
():定义参数
return : 返回函数
def test(x):
x += 1
return x
print(test(1))
运行结果:
2 Process finished with exit code 0
2.函数的过程
没有返回值return的函数
def test():
x = 1
print(x)
print(test())
运行结果:
1
None Process finished with exit code 0
返回值数=0 返回none
返回值数=1 返回object
返回值数>1 返回tuple
3.函数的参数
-----
def test(x,y): # x,y 是形参: 不占内存空间
a = x**y
return a b=(123,321) # 123,321是实参
print(b)
---- 对应
def test(x,type='mysql'):
print(x)
print(type)
test('hello')
test('hey','sql')
运行结果:
hello
mysql
hey
sql Process finished with exit code 0
-- 参数组
第一种:
def test(x,*args):
print(x)
print(args)
print(args[1]) # 通过索引获取test元素的字符
test(1,2,4,3)
运算结果:
1
(2, 4, 3)
4 Process finished with exit code 0
第二种:
def test(x,**kwargs):
print(x)
print(kwargs) test(1,a=1,b=2)
运行结果:
1
{'a': 1, 'b': 2} Process finished with exit code 0
第三种:
def test(x,*args,**kwargs):
print(x)
print(args)
print(kwargs) test(1,'abc',333,name='john',age=18)
运行结果:
1
('abc', 333)
{'name': 'john', 'age': 18} Process finished with exit code 0
python学习-23 函数的更多相关文章
- python学习8—函数之高阶函数与内置函数
python学习8—函数之高阶函数与内置函数 1. 高阶函数 a. map()函数 对第二个输入的参数进行第一个输入的参数指定的操作.map()函数的返回值是一个迭代器,只可以迭代一次,迭代过后会被释 ...
- python学习7—函数定义、参数、递归、作用域、匿名函数以及函数式编程
python学习7—函数定义.参数.递归.作用域.匿名函数以及函数式编程 1. 函数定义 def test(x) # discription y = 2 * x return y 返回一个值,则返回原 ...
- Python学习之函数参数
上一节,我们学习了Python中是如何定义和调用函数且如何得到返回值的.在调用函数时,有的函数需要参数来启动函数,有的则无需参数.这一节我们来介绍Python中有哪些参数类型. 位置参数 在调用函数时 ...
- 从0开始的Python学习007函数&函数柯里化
简介 函数是可以重用的程序段.首先这段代码有一个名字,然后你可以在你的程序的任何地方使用这个名称来调用这个程序段.这个就是函数调用,在之前的学习中我们已经使用了很多的内置函数像type().range ...
- python学习Day10 函数的介绍(定义、组成、使用)
今日学习内容: 1.什么是函数 :函数就是一个含有特定功能的变量,一个解决某问题的工具 函数的定义:通过关键字def + 功能名字():代码体(根据需求撰写代码逻辑) 2.为什么要用函数:可以复用:函 ...
- Python学习笔记—函数
函数 我们知道圆的面积计算公式为: S = πr2 当我们知道半径r的值时,就可以根据公式计算出面积.假设我们需要计算3个不同大小的圆的面积: r1 = 12.34 r2 = 9.08 r3 = 73 ...
- Python学习笔记 - 函数参数
>>> def power(x): ... return x * x ... >>> power(5) 25 >>> def power(x, n ...
- Python学习之函数篇
python查看对象,函数帮助文档:.__doc__,例:str.split.__doc__ 查看详细对象,函数文档:help(),例:help(str.split) 函数参数可设置默认值 如果不能提 ...
- Python学习-23.Python中的函数——isinstance
在Python中可以使用isinstance函数来判断某个值或变量是否为某个类型. 例子: print(isinstance(1,int)) print(isinstance(1,float)) pr ...
随机推荐
- ID生成算法(二)
上一篇文章介绍了一种用雪花算法生成GUID的方法,下面介绍里外一种生成GUID并导出为.txt文件的方法: 话不多少 show you the code ! <!DOCTYPE html> ...
- CF786E ALT
题意 有一棵 \(n\) 个点的树和 \(m\) 个人,第 \(i\) 个人从 \(u_i\) 走到 \(v_i\) 现在要发宠物,要求一个人要么他自己发到宠物,要么他走的路径上的都有宠物. 求最小代 ...
- RK3399 4G模块移远EC20移植调试
转载请注明出处:https://www.cnblogs.com/lialong1st/p/11266330.html CPU:RK3399 系统:Android 7.1 1.通过串口打印或者adb获取 ...
- Facebook libra开发者文档- 3 -Life of a Transaction交易生命周期
Life of a Transaction交易的生命周期 https://developers.libra.org/docs/life-of-a-transaction 为了更深入地了解Libra交易 ...
- 目标检测中的选择性搜索-selective search-没弄
https://blog.csdn.net/small_munich/article/details/79595257 https://www.cnblogs.com/zyly/p/9259392.h ...
- osg::NodeVisitor osg3.4.0
x:-89.4588 y:-12.1245 z:-11.7807x:-89.4588 y:-6.44823 z:-11.7807x:-89.2164 y:-9.07239 z:-11.811x:-89 ...
- 26Flutter 日期 和时间戳/格式化日期库/flutter异步/ 官方自带日期组件showDatePicker、时间组件showTimePicker以及国际化
/* 一.Flutter日期和时间戳 日期转换成时间戳 var now=newDateTime.now(); print(now.millisecondsSinceEpoch); //单位毫秒,13位 ...
- Javescript——API连接 && json数据处理(待续)
原文链接1:How to Connect to an API with JavaScript 原文链接2:How to Use the JavaScript Fetch API to Get JSON ...
- 查看QML数据类型
assist输入: QML Types A Abstract3DSeries AbstractActionInput AbstractAnimation AbstractAxis AbstractAx ...
- 123457123456#1#-----com.threeapp.qianShuiTingYX01-----潜水艇跑酷游戏01
com.threeapp.qianShuiTingYX01-----潜水艇跑酷游戏01