Briefly introduce the concept of tuples in Python and the concept of python.

Source: Internet
Author: User

Briefly introduce the concept of tuples in Python and the concept of python.

Tuples are unchangeable Python object sequences. The sequence of tuples is like a list. The only difference is that the tuples cannot be changed, that is, the tuples cannot be modified. The tuples use parentheses, while the list uses square brackets.

It is easy to create a tuples. Just separate values with commas (,), you can separate these commas (,) from each value. For example:

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

Empty tuples are written with two symmetric parentheses:

tup1 = ();

To include a value in a tuples, a comma must be included, even if there is only one value in the tuples:

tup1 = (50,);

For example, the string index, the tuples index starts from 0, and the tuples can be sliced or joined.
Value of the access tuples:

To access the values of tuples, square brackets are used to obtain the values of available indexes along slice and indexes or indexes. The following is a simple example:

#!/usr/bin/pythontup1 = ('physics', 'chemistry', 1997, 2000);tup2 = (1, 2, 3, 4, 5, 6, 7 );print "tup1[0]: ", tup1[0]print "tup2[1:5]: ", tup2[1:5]

When the above code is executed, the following results are generated:

tup1[0]: physicstup2[1:5]: [2, 3, 4, 5]

Update tuples:

Tuples are unchangeable, which means that the values of the elements of the tuples cannot be updated or changed. However, you can use the existing tuples to create new tuples, as shown in the following example:

#!/usr/bin/pythontup1 = (12, 34.56);tup2 = ('abc', 'xyz');# Following action is not valid for tuples# tup1[0] = 100;# So let's create a new tuple as followstup3 = tup1 + tup2;print tup3;

When the above code is executed, the following results are generated:

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

Elements of the deleted tuples:

It is impossible to remove the elements of each tuples. Of course, it is not wrong to put a single tuples together with discarded unwanted elements.

To explicitly Delete the entire tuples, you only need to use the del statement. The following is a simple example:

#!/usr/bin/pythontup = ('physics', 'chemistry', 1997, 2000, hema);print tup;del tup;print "After deleting tup : "print tup;

This will produce the following results. Note that an exception is thrown because the del tup tuples do not exist:

('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

Basic operations on tuples:

The + and * operator responses of tuples are the same as those of strings. Their concatenation and repetition functions are the same here. The difference is that the results are a new record, not a string.

In fact, the tuples respond to all the general operation orders we use in the existing chapter strings:

Index, slice, and matrix:

Because of the tuples, indexes, slices, and strings work in the same way. Assume the following input:

L = ('spam', 'Spam', 'SPAM!')

No closed separator:

Multiple objects in a group are separated by commas (,). If no identification symbol is written, the list in the brackets and the tuples in the brackets are used as the tuples by default. The following is a short example:

#!/usr/bin/pythonprint 'abc', -4.24e93, 18+6.6j, 'xyz';x, y = 1, 2;print "Value of x , y : ", x,y;

When the above code is executed, the following results are generated:

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.