Python starred expression 星号*(乘法或不确定的参数的定义与输入)与**(指数运算或不确定的字典类参数的定义与输入)用法分析

https://blog.csdn.net/u010376788/article/details/49933511

https://blog.csdn.net/DawnRanger/article/details/78028171

本文实例分析了Python星号*与**用法。分享给大家供大家参考,具体如下:

1. 加了星号(*)的变量名会存放所有未命名的变量参数,不能存放dict,否则报错。

def singalStar(common, *rest):
    print("Common args: ", common)
    print("Rest args: ", rest)

带星号*的参数的正确用法:

1、传多个值(个数大于或等于函数定义时的参数个数):

singalStar("hello", "world", 000)

 输出结果:

Common args:  hello
Rest args:  ('world', 0)

多余的参数:'world', 000,传入函数后,被rest变量接收,组合成元组('world', 0),打印rest显示出元组。

2、在元组值前加上“*”,把元组值就作为星号参数的参数值(不能在加了“*”的元组后追加任何值)

singalStar("hello", *("world", 000))
#     singalStar("hello", *("world", 000), "123")    #参数"123"在元组后面,函数报错

 输出结果:

Common args:  hello
Rest args:  ('world', 0)

3.6版本可以在加了“*”的元组后再追加参数:

singalStar("hello", *("world", 000), "123")

输出结果:

Common args:  hello
Rest args:  ('world', 0, '123') 

错误写法,如果直接给带星号的参数*rest传元组类型的值,传递的元组值只能被当作星号参数的元组中的一个元素:

singalStar("hello", ("world", 000))

 输出结果:
#Common args:  hello
#Rest args:  (('world', 0),) 

2. 加了星号(**)的变量名会存放所有未命名的变量参数

1、传多个参数,必须采用“args = value”的方式。“=”前的字段成了字典的键,“=”后的字段成了字典的值。双星号参数接收的参数作为字典。

doubleStar("hello", x = "Test", y = 24)

输出结果:

Common args:  hello
Double args:  {'y': 24, 'x': 'Test'} 

2、在数据字典值前加上“**”, 把字典值就作为星号参数的参数值

doubleStar("hello", **{"name": "Test", "age": 24})

输出结果:

Common args:  hello

Double args:  {'name': 'Test', 'age': 24}

doubleStar("hello", **{"name": "Test", "age": 24}, sex='male')

输出结果:
Common args:  hello
Double args:  {'name': 'Test', 'age': 24, 'sex': 'male'} 

错误写法1,没有带“参数名=”,“args = value”,只写了value:

doubleStar("hello", "Test", 24)

 打印报错:

Traceback (most recent call last):
  File "<input>", line 1, in <module>
TypeError: doubleStar() takes 1 positional argument but 3 were given

doubleStar("hello", {"name": "Test", "age": 24})

打印报错,不接受数据字典作为参数,而且没有带“args =":

Traceback (most recent call last):
  File "<input>", line 1, in <module>
TypeError: doubleStar() takes 1 positional argument but 2 were given

doubleStar("hello", **{"name": "Test", "age": 24}, {"name": "Test2", "age": 24})

打印报错: 

  File "<input>", line 1
SyntaxError: positional argument follows keyword argument unpacking 

3. 有 *args 和 **dictargs:

def singalAndDoubleStar(common, *single, **double):
    print("Common args: ", common)
    print("Single args: ", single)
    print("Double args: ", double)

例3:赋值语句中的 * 用于列表数值取出
# example 1
>>> a, *b, c = range(5)
>>> a
0
>>> c
4
>>> b
[1, 2, 3]

# example 2
>>> ecord = ('ACME', 50, 123.45, (12, 18, 2012))
>>> name, *_1, (*_2, year) = record
>>> print(name)
'ACME'
>>> print(_1)
[50,123.45]
>>> print(_2)
[12,18]
>>> print(year)

# example 3
>>> for a, *b in [(1, 2, 3), (4, 5, 6, 7)]:
>>>     print(b)
[2, 3]
[5, 6, 7]
---------------------
作者:DawnRanger
来源:CSDN
原文:https://blog.csdn.net/DawnRanger/article/details/78028171
版权声明:本文为博主原创文章,转载请附上博文链接!

 

另外,在Python数学运算中*代表乘法,**为指数运算,示例代码如下:

?

1

2

3

4

5

>>> 2*4

8

>>> 2**4

16

>>>

 https://www.jb51.net/article/134240.htm

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值