Python少儿编程提高篇(3)元组、推导式

x33g5p2x  于2022-07-26 转载在 Python  
字(0.6k)|赞(0)|评价(0)|浏览(348)

Python少儿编程小课堂(十三)

提高篇(3)元组、推导式

元组

内置不可变序列。与列表的区别就是“不可变”。

如果没有给定参数,则构造函数返回一个空元组。

如果指定了iterable,则元组将从iterable的项初始化。

如果参数是元组,则返回值是同一个对象。

>>> print(().__doc__)
Built-in immutable sequence.

If no argument is given, the constructor returns an empty tuple.
If iterable is specified the tuple is initialized from iterable's items.

If the argument is a tuple, the return value is the same object.
>>> type(())
<class 'tuple'>
>>> tuple([1,2,3])
(1, 2, 3)
>>> tuple((1,2,3))
(1, 2, 3)

元组方法

dir(tuple) 列出元组tuple所有内置方法:

>>> dir(tuple)
['__add__', '__class__', '__contains__', &#

相关文章