python可变参数如何使用
python可变参数如何使用
本文教程操作环境:windows7系统、Python3.9.1,DELLG3电脑。
1、使用说明
(1)如果要使用可变参数,传递参数的时候,带有参数名字的传递会出错;
(2)可变参数有两种形式传递:
直接传入函数,如上述例子第一种形式,即print_info2('robin',20,180,'c','javascript');
先组装为列表或者元组,再传入,并且必须带有*,即类似func(*[1,2,3])或者func(*(1,2,3)),之所以必须带*,是因为如果没有带这个,传入的可变参数会多嵌套一层元组,即(1,2,3)变为((1,2,3))。
2、实例
#可变参数
defprint_info2(name,age=18,height=178,*args):
'''
打印信息函数2
:paramname:
:paramage:
:paramargs:
:return:
'''
print('name:',name)
print('age:',age)
print('height:',height)
print(args)
forlanguageinargs:
print('language:',language)
print_info2('robin',20,180,'c','javascript')
languages=('python','java','c++','go','php')
print_info2('jack',30,175,*languages)
以上就是python可变参数的使用注意,希望对大家有所帮助。更多Python学习教程请关注IT培训机构:千锋教育。

猜你喜欢LIKE
相关推荐HOT
更多>>
python里glob模块怎么用?
python里glob模块怎么用?本文教程操作环境:windows7系统、Python3.9.1,DELLG3电脑。简单介绍:匹配一定的格式文件和文件夹。包含函数:1、glo...详情>>
2023-11-14 23:59:20
python中time.gmtime是什么
python中time.gmtime是什么下面我们就time.gmtime函数的概念、语法、参数、返回值、实例分别进行讲解。1.概念gmtime是把日期和时间转换为格林威...详情>>
2023-11-14 21:58:02
python使用OpenCV画图
python中使用OpenCV画图OpenCV的强大之处的一个体现就是其可以对图片进行任意编辑,处理。下面的这个函数最后一个参数指定的就是画笔的大小1、...详情>>
2023-11-14 19:49:08
python流式读取大文件的两种方法
python流式读取大文件的两种方法1、使用read方法分块读取使用更底层的file.read()方法,与直接循环迭代文件对象不同,每次调用file.read(chunk_...详情>>
2023-11-14 16:48:08热门推荐
python里glob模块怎么用?
沸python中series转dataframe的两种方法
热python实现矩阵乘法运算的几种方法
热pandas遍历dataframe的方法有哪些
新python中time.gmtime是什么
python中RNN和LSTM的基本介绍
Python中for循环进行字典迭代
Python中哈希结构有哪几种?
python使用OpenCV画图
python绝对和相对导入的介绍
python标识符是什么意思?
python中如何查看stdin编码?
python如何访问列表元素?
python如何提取文本信息?
技术干货






