首页 > 编程语言 > 详细

笔记-Python中逗号的作用

时间:2015-11-14 23:23:19      阅读:297      评论:0      收藏:0      [点我收藏+]

1、用,去掉额外的换行符

技术分享

>>> for i in range(0,5):
...     print i
...
0
1
2
3
4
>>> for i in range(0,5):
...     print i,
...
0 1 2 3 4

 

很明显 print语句默认的会在后面加上 换行  加了逗号之后 换行 就变成了 空格

 

赋值表达式的后面加了逗号后,会自动得到一个tuple的对象

>>> a = 1
>>> b = 2,
>>> print type(a)
<type ‘int‘>
>>> print type(b)
<type ‘tuple‘>
>>> c = []
>>> d = [],
>>> print type(c)
<type ‘list‘>
>>> print type(d)
<type ‘tuple‘>

2.逗号在类型转化中的使用 主要是元组的转换
赋值表达式的后面加了逗号后,会自动得到一个tuple的对象,在作一些与类型相关的工作或需要序列化时,是不能得到期望的结果的。工作中碰到类似灵异现象时,可以把这个放到自己的checklist中了。

>>> print c
[]
>>> print d
([],)
>>> print a
1
>>> print b
(2,)

 

3、用在 django 列表后面添加,

笔记-Python中逗号的作用

原文:http://www.cnblogs.com/wjx1/p/4965215.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!