Home Backend Development Python Tutorial 初步讲解Python中的元组概念

初步讲解Python中的元组概念

Jun 10, 2016 pm 03:11 PM
python

 元组是不可变的Python对象序列。元组的序列就像列表。唯一的区别是,元组不能被改变,即元组是不可被修改。元组使用小括号,而列表使用方括号。

创建一个元组很简单,只要把不同的逗号分隔值,可以把括号中的这些逗号来分隔每个值。例如:

tup1 = ('physics', 'chemistry', 1997, 2000);
tup2 = (1, 2, 3, 4, 5 );
tup3 = "a", "b", "c", "d";

Copy after login

空的元组写为含有两对称括号:

tup1 = ();

Copy after login

要元组中包含一个值,必须有一个逗号,即使只有一个值的元组:

tup1 = (50,);

Copy after login

如字符串索引,元组索引从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]

Copy after login

当执行上面的代码,产生以下结果:

tup1[0]: physics
tup2[1:5]: [2, 3, 4, 5]

Copy after login

更新元组:

元组是不可变的,这意味着不能更新或更改元组元素的值。但可以利用现有的元组的部分来创建新的元组,如下例所示:

#!/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;

Copy after login

当执行上面的代码,产生以下结果:

(12, 34.56, 'abc', 'xyz')

Copy after login

删除的元组元素:

除去各个元组的元素是不可能的。当然,一个元组与丢弃不想要的元素放在一起没有错。

要明确地删除整个元组,只要使用del语句。下面是一个简单的例子:

#!/usr/bin/python

tup = ('physics', 'chemistry', 1997, 2000, hema);

print tup;
del tup;
print "After deleting tup : "
print tup;

Copy after login

这将产生以下结果。注意引发异常,这是因为经过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

Copy after login

元组的基本操作:

元组的 + 和 * 运算符回应就像字符串中一样; 他们串联和重复功能在这里也一样,不同的是,结果是一个新的记录,而不是字符串。

实际上,元组响应所有我们使用在现有章字符串的一般操作顺序:

2015521101313830.jpg (575×203)

索引,切片和矩阵:

因为元组序列,索引和切片与字符串的工作方式相同。假设下面输入:

L = ('spam', 'Spam', 'SPAM!')
Copy after login

2015521101436562.jpg (578×152)

无封闭分隔符:

任何一组多个对象,以逗号分隔,不写识别符号,即括号内的列表,括号中的元组等,默认为元组,在下面这个短短的例子说明:

#!/usr/bin/python

print 'abc', -4.24e93, 18+6.6j, 'xyz';
x, y = 1, 2;
print "Value of x , y : ", x,y;

Copy after login

当执行上面的代码,产生以下结果:

abc -4.24e+93 (18+6.6j) xyz
Value of x , y : 1 2

Copy after login

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Can the Python interpreter be deleted in Linux system? Can the Python interpreter be deleted in Linux system? Apr 02, 2025 am 07:00 AM

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...

How to solve the problem of Pylance type detection of custom decorators in Python? How to solve the problem of Pylance type detection of custom decorators in Python? Apr 02, 2025 am 06:42 AM

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

How to solve permission issues when using python --version command in Linux terminal? How to solve permission issues when using python --version command in Linux terminal? Apr 02, 2025 am 06:36 AM

Using python in Linux terminal...

Python 3.6 loading pickle file error ModuleNotFoundError: What should I do if I load pickle file '__builtin__'? Python 3.6 loading pickle file error ModuleNotFoundError: What should I do if I load pickle file '__builtin__'? Apr 02, 2025 am 06:27 AM

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

Do FastAPI and aiohttp share the same global event loop? Do FastAPI and aiohttp share the same global event loop? Apr 02, 2025 am 06:12 AM

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

What should I do if the '__builtin__' module is not found when loading the Pickle file in Python 3.6? What should I do if the '__builtin__' module is not found when loading the Pickle file in Python 3.6? Apr 02, 2025 am 07:12 AM

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

How to ensure that the child process also terminates after killing the parent process via signal in Python? How to ensure that the child process also terminates after killing the parent process via signal in Python? Apr 02, 2025 am 06:39 AM

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...

See all articles