初步讲解Python中的元组概念
元组是不可变的Python对象序列。元组的序列就像列表。唯一的区别是,元组不能被改变,即元组是不可被修改。元组使用小括号,而列表使用方括号。
创建一个元组很简单,只要把不同的逗号分隔值,可以把括号中的这些逗号来分隔每个值。例如:
tup1 = ('physics', 'chemistry', 1997, 2000); tup2 = (1, 2, 3, 4, 5 ); tup3 = "a", "b", "c", "d";
空的元组写为含有两对称括号:
tup1 = ();
要元组中包含一个值,必须有一个逗号,即使只有一个值的元组:
tup1 = (50,);
如字符串索引,元组索引从0开始,元组可以切片,联接等。
访问元组的值:
要访问元组的值,使用方括号沿切片及索引或索引来获得可用的索引对应的值。下面是一个简单的例子:
#!/usr/bin/python tup1 = ('physics', 'chemistry', 1997, 2000); tup2 = (1, 2, 3, 4, 5, 6, 7 ); print "tup1[0]: ", tup1[0] print "tup2[1:5]: ", tup2[1:5]
当执行上面的代码,产生以下结果:
tup1[0]: physics tup2[1:5]: [2, 3, 4, 5]
更新元组:
元组是不可变的,这意味着不能更新或更改元组元素的值。但可以利用现有的元组的部分来创建新的元组,如下例所示:
#!/usr/bin/python tup1 = (12, 34.56); tup2 = ('abc', 'xyz'); # Following action is not valid for tuples # tup1[0] = 100; # So let's create a new tuple as follows tup3 = tup1 + tup2; print tup3;
当执行上面的代码,产生以下结果:
(12, 34.56, 'abc', 'xyz')
删除的元组元素:
除去各个元组的元素是不可能的。当然,一个元组与丢弃不想要的元素放在一起没有错。
要明确地删除整个元组,只要使用del语句。下面是一个简单的例子:
#!/usr/bin/python tup = ('physics', 'chemistry', 1997, 2000, hema); print tup; del tup; print "After deleting tup : " print tup;
这将产生以下结果。注意引发异常,这是因为经过del tup元组就不存在了:
('physics', 'chemistry', 1997, 2000) After deleting tup : Traceback (most recent call last): File "test.py", line 9, in <module> print tup; NameError: name 'tup' is not defined
元组的基本操作:
元组的 + 和 * 运算符回应就像字符串中一样; 他们串联和重复功能在这里也一样,不同的是,结果是一个新的记录,而不是字符串。
实际上,元组响应所有我们使用在现有章字符串的一般操作顺序:
索引,切片和矩阵:
因为元组序列,索引和切片与字符串的工作方式相同。假设下面输入:
L = ('spam', 'Spam', 'SPAM!')
无封闭分隔符:
任何一组多个对象,以逗号分隔,不写识别符号,即括号内的列表,括号中的元组等,默认为元组,在下面这个短短的例子说明:
#!/usr/bin/python print 'abc', -4.24e93, 18+6.6j, 'xyz'; x, y = 1, 2; print "Value of x , y : ", x,y;
当执行上面的代码,产生以下结果:
abc -4.24e+93 (18+6.6j) xyz Value of x , y : 1 2

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Regarding the problem of removing the Python interpreter that comes with Linux systems, many Linux distributions will preinstall the Python interpreter when installed, and it does not use the package manager...

Pylance type detection problem solution when using custom decorator In Python programming, decorator is a powerful tool that can be used to add rows...

About Pythonasyncio...

Using python in Linux terminal...

Loading pickle file in Python 3.6 environment error: ModuleNotFoundError:Nomodulenamed...

Compatibility issues between Python asynchronous libraries In Python, asynchronous programming has become the process of high concurrency and I/O...

Error loading Pickle file in Python 3.6 environment: ModuleNotFoundError:Nomodulenamed...

The problem and solution of the child process continuing to run when using signals to kill the parent process. In Python programming, after killing the parent process through signals, the child process still...
