• 元组定义: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

65 条评论

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

xbymw.com · 2026-09-24 00:51

References:

Online Casino Sofort Deutschland https://xbymw.com/space-uid-1294008.html

bbs.88moli.top · 2026-09-24 02:08

References:

Sofort Casino ohne Verifizierung http://bbs.88moli.top/home.php?mod=space&uid=128711

payid casino online pokies · 2026-09-25 07:31

References:

Trusted payid pokies australia https://md.sigma2.no/BJs9OhK6QeGzBxJcy5fF4Q

https://pad.public.cat/ · 2026-09-25 22:46

References:

Real money payid online pokies https://pad.public.cat/aTAVe7pfSsqtRBt4ZQXlMA

no verification payid pokies australia · 2026-09-29 00:52

References:

Instant withdrawal payid pokies https://shenasname.ir/ask/user/shirtsingle13

payid accepted pokies · 2026-09-29 01:55

References:

Payid pokies real money no deposit https://winnipegasians.com/home.php?mod=space&uid=185828

mobile payid pokies australia · 2026-09-29 09:53

References:

Fast payout payid pokies http://gjglm.com/home.php?mod=space&uid=202362

dahuafu.us · 2026-09-29 10:18

References:

Bonus pokies payid https://dahuafu.us/?31599

blurb.com · 2026-09-29 22:48

References:

Anonymous payid pokies https://www.blurb.com/user/italymint18

jackpot pokies payid · 2026-09-29 23:21

References:

Best online pokies payid http://www.lezcc.com/?630277

online pokies payid australia · 2026-09-30 20:48

References:

Online pokies payid no deposit https://pad.public.cat/QbGP5IZHSQaHsF1zzb9J-g

payid pokies · 2026-10-01 07:16

References:

No deposit http://volleypedia-org.50and3.com/index.php?qa=user&qa_1=viewperu8 australia payid pokies

graph.org · 2026-10-02 00:18

References:

Payid pokies welcome bonus https://graph.org/Best-Neosurf-Casinos-in-2026-Tested–Reviewed-10-01

https://atomcraft.ru/ · 2026-10-02 05:43

References:

10 dollar deposit pokies payid https://atomcraft.ru/user/batcost3/

www.1v34.com · 2026-10-04 10:09

References:

$5 minimum deposit casino neosurf australia https://www.1v34.com/space-uid-1813030.html

61.153.156.62 · 2026-10-04 10:23

References:

Online slots australia neosurf http://61.153.156.62:880/home.php?mod=space&uid=37286

md.coredump.ch · 2026-10-04 10:41

References:

Neosurf regulated casinos australia https://md.coredump.ch/J0umg752QvCL_yTQPO6gfw

pokies payid accepted australia · 2026-10-04 12:16

References:

Direct bank transfer payid pokies https://hedgedoc.uni-ak.ac.at/_7GgGXFsQtSnSVz5gFcqzQ

bbs.wuhudj.com · 2026-10-04 12:50

References:

International casinos accepting neosurf australia http://bbs.wuhudj.com/space-uid-1742594.html

http://gjglm.com/home.php?mod=space&uid=175564 · 2026-10-04 13:58

References:

Neosurf prepaid voucher casino australia http://gjglm.com/home.php?mod=space&uid=175564

neosurf e-voucher casino australia · 2026-10-04 15:01

References:

Buy neosurf voucher australia https://pad.public.cat/wvbwY976QGGIrO8Ff4ZSCA

neosurf live casino australia · 2026-10-04 17:57

References:

Neosurf casino no download australia https://bbs.ybk001.com/home.php?mod=space&uid=601054

http://www.lezcc.com · 2026-10-04 18:00

References:

Australian online casinos neosurf http://www.lezcc.com/?639629

https://vlauncher.net · 2026-10-05 20:48

References:

Neosurf accepted casinos australia https://vlauncher.net/user/minutearies69/

http://www.pshunv.com/space-uid-991837.html · 2026-10-06 00:38

References:

Neosurf casino privacy australia http://www.pshunv.com/space-uid-991837.html

neosurf video poker australia · 2026-10-06 03:45

References:

Neosurf casino minimum deposit australia https://md.interhacker.space/a7qk4itOR4q0Y_JiLe6tSA

actualites.cava.tn · 2026-10-06 05:22

References:

Australian neosurf casinos https://actualites.cava.tn/user/answersky4/

aupeopleweb.com.au · 2026-10-06 05:29

References:

Low deposit casino neosurf australia https://www.aupeopleweb.com.au/au/home.php?mod=space&uid=3138435

how neosurf works at online casinos · 2026-10-06 08:33

References:

Mobile neosurf casinos australia http://bbs.lotsmall.cn/home.php?mod=space&uid=680905

dle1.31-6kc3bfr2e.рф · 2026-10-06 11:29

References:

Neosurf casino sites for australian players http://dle1.xn--31-6kc3bfr2e.xn--p1ai/user/actionfoot2/

发表回复

Avatar placeholder

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