美文网首页
python 元组

python 元组

作者: 鬼会画符 | 来源:发表于2018-06-18 10:53 被阅读0次

    元组和列表格式差不多,但是元组具有不可变性,对于列表的增、删、改、查四种功能 ,元组只能使用查找,不具备其他的任何会更改元组内容的功能。

    • 元组和列表的不同:元组用( ),列表用[ ]
    • 元组在定义一个字符时需要在字符后加“ ,“
    
    In [1]: name = (18)
    
    In [2]: len(name)
    ---------------------------------------------------------------------------
    TypeError                                 Traceback (most recent call last)
    <ipython-input-2-5f1dfb2f0744> in <module>()
    ----> 1 len(name)
    
    TypeError: object of type 'int' has no len()
    
    In [3]: name = (18,)
    
    In [4]: len(name)
    Out[4]: 1
    
    
    • 元组和列表之间可以互相转换
    In [13]: b=(8,)
    
    In [14]: list(b)
    Out[14]: [8]
    
    In [15]: tuple(b)
    Out[15]: (8,)
    
    

    相关文章

      网友评论

          本文标题:python 元组

          本文链接:https://www.haomeiwen.com/subject/gitieftx.html