元组的奇怪现象

 
 

奇怪的现象:

>>> type((1))
<class 'int'>
>>> type(('hello'))
<class 'str'>

原因是元组的()与运算符中的4*(3+1)这个()是有歧义。所有系统会规定如果括号中只有一个元素的话它会认为是要做数据运算

定义只有一个元组的方法:

>>> (1,)
(1,)
>>> type((1,))
<class 'tuple'>

定义一个空元组:

>>> ()
()
>>> type(())
<class 'tuple'>


猜你喜欢

转载自blog.csdn.net/weixin_41355124/article/details/80312805
今日推荐