• 元组定义:tuple基本上就像是一个不可变的列表。就像列表一样,元组是序列,但是它具有不可变性,和字符串类似,从语法上讲,它们编写在圆括号中而不是方括号,它们支持任意类型,任意嵌套以及常见的序列操作。
    #ipython3
    
    In [1]: T = (1,2,3,4)
    
    In [2]: len(T)
    Out[2]: 4
    
    In [3]: T + (5,6)
    Out[3]: (1, 2, 3, 4, 5, 6)
    
    In [4]: T[0]
    Out[4]: 1
    

    在Python3中,元组还有两个专用的调用方法,但它的专有方法不像列表所拥有的那么多

    In [5]: T.index(4)              //出现4的偏移量
    Out[5]: 3
    
    In [6]: T.count(4)              //出现4的次数
    Out[6]: 1
    

    元组的真正的不同之处在于一旦创建就不能再改变,也就是说,元组是不可变的序列:

    In [7]: T[0] = 2
    ---------------------------------------------------------------------------
    TypeError                                 Traceback (most recent call last)
     in ()
    ----> 1 T[0] = 2
    
    TypeError: 'tuple' object does not support item assignment
    

    与列表和字典一样,元组支持混合的类型和嵌套,但是不能增加或缩短

    In [1]: T = ('spam',3.0,[11,22,33])
    
    In [2]: T[1]
    Out[2]: 3.0
    
    In [3]: T[2][1]
    Out[3]: 22
    
    In [4]: T.append(4)
    ---------------------------------------------------------------------------
    AttributeError                            Traceback (most recent call last)
     in ()
    ----> 1 T.append(4)
    
    AttributeError: 'tuple' object has no attribute 'append'
    
  • 为什么要用元组:因为不可变性,如果在程序中以列表的形式传递一个对象的集合,它可能在任何地方改变,如果使用了元组的话,则不能,也就是说,元组提供了一种完整性的约束,这对于大型程序来说是方便的。
  • 文件定义:文件对象是Python代码对电脑上外部文件的主要接口,虽然文件是核心类型,但是它有些特殊,没有 特定的常量语法创建文件,要创建一个文件对象,需调用内置的open函数以字符串的形式传递给它的一个外部的文件名以及一个处理模式的字符串。例如,创建 一个文本输出文件,可以传递其文件名以及‘w’处理模式字符串以写数据:
    In [1]: f = open('data.txt','w')
    
    In [2]: f.wri
    f.writable    f.write       f.writelines  
    
    In [2]: f.write('hello\n')
    Out[2]: 6
    
    In [3]: f.write('world\n')
    Out[3]: 6
    
    In [4]: f.clo
    f.close   f.closed  
    
    In [4]: f.close()
    
    In [5]: exit
    MK-proser:/usr/local/kaelwork/test # ls
    data.txt
    MK-proser:/usr/local/kaelwork/test # cat data.txt
    hello
    world
    

    下面以‘r’处理模式打开文件,读取输入,之后将文件的内容读至一个字符串,并显示它。对脚本而言,文件的内容总是字符串,无论文件包含的数据是什么类型:

    In [1]: f = open('data.txt','r')
    
    In [2]: text = f.read()
    
    In [3]: text
    Out[3]: 'hello\nworld\n'
    
    In [4]: print(text)
    hello
    world
    
    In [5]: text.split()
    Out[5]: ['hello', 'world']
    

    文件对象提供了多种读和写的方法,read可以接受一个字节大小的选项,readline每次读一行等,以及其它的工具如seek移动到一个新的文 件位置。我们后面会看到,现在读取一个文件的最佳方式就是根本不读它,文件提供了一个迭代器,他在for循环或其它环境中自动地一行一行地读取。 我们可以通过dir调用查看一个完整的文件方法

    In [6]: dir(f)
    Out[6]: 
    ['_CHUNK_SIZE',
     '__class__',
     '__del__',
     '__delattr__',
     '__dict__',
     '__dir__',
     '__doc__',
     '__enter__',
     '__eq__',
     '__exit__',
     '__format__',
     '__ge__',
     '__getattribute__',
     '__getstate__',
     '__gt__',
     '__hash__',
     '__init__',
     '__iter__',
     '__le__',
     '__lt__',
     '__ne__',
     '__new__',
     '__next__',
     '__reduce__',
     '__reduce_ex__',
     '__repr__',
     '__setattr__',
     '__sizeof__',
     '__str__',
     '__subclasshook__',
     '_checkClosed',
     '_checkReadable',
     '_checkSeekable',
     '_checkWritable',
     '_finalizing',
     'buffer',
     'close',
     'closed',
     'detach',
     'encoding',
     'errors',
     'fileno',
     'flush',
     'isatty',
     'line_buffering',
     'mode',
     'name',
     'newlines',
     'read',
     'readable',
     'readline',
     'readlines',
     'seek',
     'seekable',
     'tell',
     'truncate',
     'writable',
     'write',
     'writelines']
    

    在python3中文件在文本和二进制数据之前画出了一条清晰的界限。文本文件把内容显示为字符串,并且自动执行Unicode编码和解码;而二进 制文件把内容显示为一个特定的字节字符串类型,并且允许你不修改地访问文件内容。如果你只处理ASCII文件的话,通常不需要关心这一区别,但是如果你处 理国际化的应用程序或者面向字节的数据,Python3的字符串和文件是很有用的。

  • 其它文件类工具:open函数能够完成大多数的文件处理,对于高级任务,Python还有额外的类文件工具,如 管道,先进先出队列(FIFO),套接字,通过键访问文件,对象持久,基于描述的文件,关系数据库和面向对象数据库接口等。常见的描述符文件支持文件锁定 和其它的底层工具,而套接字提供网络和进程间通讯的接口。
分类: Python3

13 条评论

https://gitlab.keysmith.bz/ · 2026-09-22 03:00

References:

Mobile casino sofortüberweisung https://gitlab.keysmith.bz/

rung90tv · 2026-09-22 06:35

[4257]rung90tv | Link vào tải app và hướng dẫn chơi casino online,Truy cập rung90tv để lấy link vào chính thức, tải app và xem cách chơi baccarat online hiệu quả. Hỗ trợ nạp tiền casino qua momo nhanh chóng. Tham gia ngay! visit: rung90tv

https://peatix.com · 2026-09-22 07:39

References:

Casino mit sofortüberweisung https://peatix.com

https://gitlab.keysmith.bz · 2026-09-22 16:46

References:

Top Sofortüberweisung Casinos https://gitlab.keysmith.bz

http://bbs.abcdv.net/ · 2026-09-23 06:10

References:

Online pokies payid http://bbs.abcdv.net/

http://oneclickcarehk.com · 2026-09-23 09:30

References:

Online casino sofort einzahlung http://oneclickcarehk.com/forum/home.php?mod=space&uid=283812

http://wargame-workshop.com · 2026-09-23 12:18

References:

Sofortüberweisung casino ohne registrierung http://wargame-workshop.com/bbs/home.php?mod=space&uid=1451958

comsenz-service.com · 2026-09-23 12:38

References:

Online casino schnelle auszahlung sofort https://comsenz-service.com/?182441

https://www.8njy.com/ · 2026-09-23 16:01

References:

Sofortüberweisung Einzahlung Casino https://www.8njy.com/home.php?mod=space&uid=162006

发表回复

Avatar placeholder

您的邮箱地址不会被公开。 必填项已用 * 标注