python 中有很多地方用到星号,有时候会想知道这个*是干嘛用的,总结如下,有不当之处,还望不吝指出,谢谢.
1.乘法:
在很多时候是用作乘法的,例如:

In [90]: 2*7
Out[90]: 14 In [91]: a=10
In [92]: a*10
Out[92]: 100

当然还包括字符串,列表等等的乘,姑且都当做乘法吧.例如:

In [93]: "a"*10
Out[93]: 'aaaaaaaaaa' In [94]: ['']*10
Out[94]: ['', '', '', '', '', '', '', '', '', '']

2.乘方:

In [98]: 2**3
Out[98]: 8

3.切片当中的使用(解压可迭代对象赋值给多个变量):

In [108]: a =[0,1,2,3,4,5,6,7,8,9]

In [109]: start,*midle,end=a

In [110]: start
Out[110]: 0 In [111]: end
Out[111]: 9 In [112]: midle
Out[112]: [1, 2, 3, 4, 5, 6, 7, 8]

4.对于可迭代对象的用法(不晓得怎么描述,姑且这样叫):

In [117]: a=[1, 2, 3, 4, 5, 6, 7, 8]

In [118]: print(a)
[1, 2, 3, 4, 5, 6, 7, 8] In [119]: print(*a)
1 2 3 4 5 6 7 8 In [120]: print(*range(10))
0 1 2 3 4 5 6 7 8 9 -------------------------2017年10月22日16:04:30更新----------------------------------------------
def foo(*args):
for v in args:
print(v) a = ['h', 'e', 'l', 'l', 'o']
foo('h', 'e', 'l', 'l', 'o')
foo(*a) def f(**kwargs):
# print(kwargs)
for k, v in kwargs.items():
print("{}>>>>{}".format(k, v)) f(name="tom", age=20)
f(**{'age': 20, 'name': 'tom'}) def func(*args, **kwargs):
for v in args:
print(v)
for k, v in kwargs.items():
print("{}>>>>{}".format(k, v)) a = ['h', 'e', 'l', 'l', 'o']
func(*a, **{'age': 20, 'name': 'tom'})
------------------------------------------------------------------------------------------------------

5.形式参数

def func(*args,**kwargs):
print(args)
print(kwargs) func(1, 2, 3, 4, 5, name="", age="") # (1, 2, 3, 4, 5)
# {'age': '10', 'name': '123'}

python中*号用法总结的更多相关文章

  1. python中*号和**号的用法

    1.乘法符号 2.可变长参数 当我们使用函数时,需要传入不定个数的位置参数时,就可以使用*号表示,即*args,以元组形式传入:需要传入不定个数的关键字参数时,使用**表示,即**kwargs,以字典 ...

  2. python中xrange用法分析

    本文实例讲述了python中xrange用法.分享给大家供大家参考.具体如下: 先来看如下示例: >>> x=xrange(0,8) >>> print x xra ...

  3. python 中@ 的用法【转】

    这只是我的个人理解: 在Python的函数中偶尔会看到函数定义的上一行有@functionName的修饰,当解释器读到@的这样的修饰符之后,会先解析@后的内容,直接就把@下一行的函数或者类作为@后边的 ...

  4. Python中flatten用法

    Python中flatten用法 原创 2014年04月16日 10:20:02 标签: Python / flatten 22667 一.用在数组 >>> a = [[1,3],[ ...

  5. 列表[‘hello’ , ‘python’ ,’!’ ] 用多种方法拼接,并输出’hello python !’ 以及join()在python中的用法简介

    列表[‘hello’ , ‘python’ ,’!’ ] 用多种方法拼接,并输出’hello python !’ 使用字符串链接的四种方法都可以创建 字符串拼接一共有四种方法,也可以应用到列表的拼接中 ...

  6. python中“end=”用法

    python中“end=”用法:例如print(“#”,end=" \n"),默认换行,print(“#”,end=" ")则在循环中不换行

  7. python中pkl用法

    原文连接:https://www.jianshu.com/p/2ecadebe6d13 python中pkl用法 经常遇到在Python程序运行得到了一些字符串.列表.字典等数据,想要长久的保存下来, ...

  8. Python中print用法里面% ,"%s 和 % d" 代表的意思

    Python 编程 里面% . "%s 和 % d" 代表的意思 %s,表示格化式一个对象为字符 %d,整数 "Hello, %s"%"zhang3& ...

  9. 详解python中@的用法

    python中@的用法 @是一个装饰器,针对函数,起调用传参的作用. 有修饰和被修饰的区别,‘@function'作为一个装饰器,用来修饰紧跟着的函数(可以是另一个装饰器,也可以是函数定义). 代码1 ...

随机推荐

  1. Thingworx新建Thing的数据库表变化

    为了在Thingworx的基础上建立统一的可视化平台,并且对软件产品具有自主控制权,不依赖于Thingworx软件(防止因Thingworx的升级.Bug导致的自主扩展功能受制),所以最近在研究Thi ...

  2. Unity 组件的增、查、禁、删 代码书写

    using UnityEngine; public class NewBehaviourScript : MonoBehaviour { // Use this for initialization ...

  3. C IO programming test code

    #include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl ...

  4. [读书笔记] Python数据分析 (三) IPython

    1. 什么是IPython IPyhton 本身没有提供任何的计算或者数据分析功能,在交互式计算和软件开发者两个方面最大化地提高生产力,execute-explore instead of edit- ...

  5. JAVA 重载方法,参数为NULL时,调用的处理 (精确性原则)

    引子:大家可以思考一下下面程序的输出结果 public class TestNull { public void show(String a){ System.out.println("St ...

  6. Virtual Box 新建一个虚拟机安装系统(补充:WIN7 64 bit 系统虚拟机无法安装 64 bit 系统问题)

    1.安装Virtual Box好后,点击新建 2.配置内存大小,这个根据自己需要配置就好 3.创建虚拟硬盘 这里选择固定分配.动态分配都可以,接下来就分配硬盘大小了 4.新建好后我们点击刚才建立的虚拟 ...

  7. js内置对象——Math

    Math()是JavaScript的内置对象(对于内置对象的理解,会单独写文章说明),包含了很多数学相关的方法: 常用方法: 1 Math.ceil(x) //取最近的最大整数返回 2 Math.fl ...

  8. LaTeX 插图片

    本系列文章由 @YhL_Leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/50240371 这里列出3种LaTeX中 ...

  9. 第九章 Servlet API

    第九章 Servlet API Servlet API 定义了Servlet和服务器之间的一个标准接口,这使得Servlet具有跨应用服务器的特性,通过使用Servlet API,开发人员不必关心服务 ...

  10. 洛谷 P1518 两只塔姆沃斯牛 The Tamworth Two

    P1518 两只塔姆沃斯牛 The Tamworth Two 题目背景 题目描述 两只牛逃跑到了森林里.农夫John开始用他的专家技术追捕这两头牛.你的任务是模拟他们的行为(牛和John). 追击在1 ...