python变量加逗号,的含义

逗号,用于生成一个长度为1的元组

>>> (1)
1
>>> (1,)
(1,)
>>> 1,
(1,)

因此需要将长度为1的元组中元素提取出来可以用,简化赋值操作

>>> a=(1,)
>>> b=a
>>> b
(1,)
>>> b,=a
>>> b
1

最后print打印变量加,实现连续打印不换行的操作在python3中行不通了

Python 3.7.3 (default, Nov 15 2019, 04:04:52) 
[Clang 11.0.0 (clang-1100.0.33.16)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 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

相关推荐