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

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

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

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

元组

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

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

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

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

  1. >>> print(().__doc__)
  2. Built-in immutable sequence.
  3. If no argument is given, the constructor returns an empty tuple.
  4. If iterable is specified the tuple is initialized from iterable's items.
  5. If the argument is a tuple, the return value is the same object.
  6. >>> type(())
  7. <class 'tuple'>
  8. >>> tuple([1,2,3])
  9. (1, 2, 3)
  10. >>> tuple((1,2,3))
  11. (1, 2, 3)

元组方法

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

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

相关文章